diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4983640 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +gentestfiles: testdata/vector_columns.fits + +testdata/vector_columns.fits: filegen/vector_columns.py + python $< -o $@ diff --git a/filegen/vector_columns.py b/filegen/vector_columns.py new file mode 100755 index 0000000..9a14e62 --- /dev/null +++ b/filegen/vector_columns.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +""" +Generate a file with vector columns, to validate https://github.com/simonrw/rust-fitsio/pull/330 +""" + +import argparse +from pathlib import Path + +import numpy as np +from astropy.io import fits + + +def gen_file() -> fits.BinTableHDU: + # https://docs.astropy.org/en/stable/io/fits/index.html#creating-a-new-table-file + a1 = np.array(["NGC1001", "NGC1002", "NGC1003"]) + a2 = np.array([11.1, 12.3, 15.2]) + a3 = np.array([[1, 2], [3, 4], [5, 6]]) + col1 = fits.Column(name="target", format="20A", array=a1) + col2 = fits.Column(name="V_mag", format="E", array=a2) + col3 = fits.Column(name="index", format="2K", array=a3) + cols = fits.ColDefs([col1, col2, col3]) + hdu = fits.BinTableHDU.from_columns(cols, name="info") + return hdu + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("-o", "--output", required=True, type=Path) + args = parser.parse_args() + + file = gen_file() + file.writeto(str(args.output), overwrite=True) diff --git a/fitsio/tests/test_vector_datatypes.rs b/fitsio/tests/test_vector_datatypes.rs index f612575..b4e2fde 100644 --- a/fitsio/tests/test_vector_datatypes.rs +++ b/fitsio/tests/test_vector_datatypes.rs @@ -110,3 +110,16 @@ make_test!( ColumnDataType::Double, 3.1415926535879323 ); + +// integration test using file generated by `filegen/vector_columns.py` +#[test] +fn read_vector_data_from_example_file() { + let mut f = FitsFile::open("../testdata/vector_columns.fits").unwrap(); + let table = f.hdu("info").unwrap(); + + let string_data: Vec = table.read_col(&mut f, "target").unwrap(); + assert_eq!(string_data, &["NGC1001", "NGC1002", "NGC1003"]); + + let vector_data: Vec = table.read_col(&mut f, "index").unwrap(); + assert_eq!(vector_data, &[1, 2, 3, 4, 5, 6]); +} diff --git a/flake.nix b/flake.nix index caabaf3..17b4851 100644 --- a/flake.nix +++ b/flake.nix @@ -10,6 +10,12 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; + + test-python = pkgs.python3.withPackages (ps: with ps; [ + numpy + astropy + ipython + ]); in { devShells.default = pkgs.mkShell rec { @@ -23,7 +29,8 @@ pkgs.cargo-nextest pkgs.bacon # for bin/test - pkgs.python3 + # test-python + test-python ] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [ pkgs.cargo-tarpaulin ]; diff --git a/testdata/vector_columns.fits b/testdata/vector_columns.fits new file mode 100644 index 0000000..587cb46 Binary files /dev/null and b/testdata/vector_columns.fits differ