diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_pyglider.py b/tests/test_pyglider.py index f552d12..c072ca2 100644 --- a/tests/test_pyglider.py +++ b/tests/test_pyglider.py @@ -1,28 +1,24 @@ -from pathlib import Path - import numpy as np import pytest import xarray as xr import yaml import pyglider.seaexplorer as seaexplorer - -library_dir = Path(__file__).parent.parent.absolute() -example_dir = library_dir / 'tests/example-data/' +from tests.utils import EXAMPLE_DIR, LIBRARY_DIR # Create an L0 timeseries from seaexplorer data and test that the resulting netcdf # is identical to the test data -rawdir = str(example_dir / 'example-seaexplorer/realtime_raw/') + '/' -rawncdir = str(example_dir / 'example-seaexplorer/realtime_rawnc/') + '/' -deploymentyaml = str(example_dir / 'example-seaexplorer/deploymentRealtime.yml') -l0tsdir = str(example_dir / 'example-seaexplorer/L0-timeseries-test/') + '/' +rawdir = str(EXAMPLE_DIR / 'example-seaexplorer/realtime_raw/') + '/' +rawncdir = str(EXAMPLE_DIR / 'example-seaexplorer/realtime_rawnc/') + '/' +deploymentyaml = str(EXAMPLE_DIR / 'example-seaexplorer/deploymentRealtime.yml') +l0tsdir = str(EXAMPLE_DIR / 'example-seaexplorer/L0-timeseries-test/') + '/' seaexplorer.raw_to_rawnc(rawdir, rawncdir, deploymentyaml) seaexplorer.merge_parquet(rawncdir, rawncdir, deploymentyaml, kind='sub') outname = seaexplorer.raw_to_L0timeseries(rawncdir, l0tsdir, deploymentyaml, kind='sub') output = xr.open_dataset(outname) # Open test data file test_data = xr.open_dataset( - library_dir + LIBRARY_DIR / 'tests/expected/example-seaexplorer/L0-timeseries/dfo-eva035-20190718.nc' ) variables = list(output.variables) @@ -61,12 +57,12 @@ def test_example_seaexplorer_metadata(): # Test that interpolation over nans does not change the output with nrt data with open(deploymentyaml) as fin: deployment = yaml.safe_load(fin) -interp_yaml = str(example_dir / 'example-seaexplorer/deploymentRealtimeInterp.yml') +interp_yaml = str(EXAMPLE_DIR / 'example-seaexplorer/deploymentRealtimeInterp.yml') deployment['netcdf_variables']['interpolate'] = True with open(interp_yaml, 'w') as fout: yaml.dump(deployment, fout) l0tsdir_interp = ( - str(example_dir / 'example-seaexplorer/L0-timeseries-test-interp/') + '/' + str(EXAMPLE_DIR / 'example-seaexplorer/L0-timeseries-test-interp/') + '/' ) outname_interp = seaexplorer.raw_to_L0timeseries( @@ -91,10 +87,10 @@ def test_example_seaexplorer_interp_nrt(var): # Test raw (full resolution) seaexplorer data. -rawdir = str(example_dir / 'example-seaexplorer-raw/delayed_raw/') + '/' -rawncdir = str(example_dir / 'example-seaexplorer-raw/delayed_rawnc/') + '/' -deploymentyaml_raw = str(example_dir / 'example-seaexplorer-raw/deployment.yml') -l0tsdir = str(example_dir / 'example-seaexplorer-raw/L0-timeseries-test/') + '/' +rawdir = str(EXAMPLE_DIR / 'example-seaexplorer-raw/delayed_raw/') + '/' +rawncdir = str(EXAMPLE_DIR / 'example-seaexplorer-raw/delayed_rawnc/') + '/' +deploymentyaml_raw = str(EXAMPLE_DIR / 'example-seaexplorer-raw/deployment.yml') +l0tsdir = str(EXAMPLE_DIR / 'example-seaexplorer-raw/L0-timeseries-test/') + '/' seaexplorer.raw_to_rawnc(rawdir, rawncdir, deploymentyaml_raw) seaexplorer.merge_parquet(rawncdir, rawncdir, deploymentyaml_raw, kind='raw') outname_raw = seaexplorer.raw_to_L0timeseries( @@ -103,7 +99,7 @@ def test_example_seaexplorer_interp_nrt(var): output_raw = xr.open_dataset(outname_raw) # Open test data file test_data_raw = xr.open_dataset( - library_dir + LIBRARY_DIR / 'tests/expected/example-seaexplorer-raw/L0-timeseries/dfo-bb046-20200908.nc' ) @@ -138,12 +134,12 @@ def test_example_seaexplorer_metadata_raw(): # Test that interpolation over nans in raw data results in a greater or equal number of non-nan values with open(deploymentyaml_raw) as fin: deployment_raw = yaml.safe_load(fin) -interp_yaml = str(example_dir / 'example-seaexplorer-raw/deploymentDelayedInterp.yml') +interp_yaml = str(EXAMPLE_DIR / 'example-seaexplorer-raw/deploymentDelayedInterp.yml') deployment_raw['netcdf_variables']['interpolate'] = True with open(interp_yaml, 'w') as fout: yaml.dump(deployment_raw, fout) l0tsdir_interp_raw = ( - str(example_dir / 'example-seaexplorer-raw/L0-timeseries-test-interp/') + '/' + str(EXAMPLE_DIR / 'example-seaexplorer-raw/L0-timeseries-test-interp/') + '/' ) outname_interp_raw = seaexplorer.raw_to_L0timeseries( diff --git a/tests/test_seaexplorer.py b/tests/test_seaexplorer.py index c4b09f5..64c5a5d 100644 --- a/tests/test_seaexplorer.py +++ b/tests/test_seaexplorer.py @@ -1,5 +1,4 @@ import os -from pathlib import Path import numpy as np import polars as pl @@ -7,10 +6,9 @@ import yaml os.system('rm tests/data/realtime_rawnc/*') -library_dir = Path(__file__).parent.parent.absolute() -example_dir = library_dir / 'tests/example-data/' import pyglider.seaexplorer as seaexplorer +from tests.utils import EXAMPLE_DIR def test__outputname(): @@ -67,13 +65,13 @@ def test_merge_rawnc(): result_default = seaexplorer.merge_parquet( 'tests/data/realtime_rawnc/', 'tests/data/realtime_rawnc/', - str(example_dir / 'example-seaexplorer/deploymentRealtime.yml'), + str(EXAMPLE_DIR / 'example-seaexplorer/deploymentRealtime.yml'), ) result_sub = seaexplorer.merge_parquet( 'tests/data/realtime_rawnc/', 'tests/data/realtime_rawnc/', - str(example_dir / 'example-seaexplorer/deploymentRealtime.yml'), + str(EXAMPLE_DIR / 'example-seaexplorer/deploymentRealtime.yml'), kind='sub', ) assert result_default is False @@ -107,12 +105,12 @@ def test_raw_to_timeseries(): result_default = seaexplorer.raw_to_timeseries( 'tests/data/realtime_rawnc/', 'tests/data/l0-profiles/', - str(example_dir / 'example-seaexplorer/deploymentRealtime.yml'), + str(EXAMPLE_DIR / 'example-seaexplorer/deploymentRealtime.yml'), ) result_sub = seaexplorer.raw_to_timeseries( 'tests/data/realtime_rawnc/', 'tests/data/l0-profiles/', - str(example_dir / 'example-seaexplorer/deploymentRealtime.yml'), + str(EXAMPLE_DIR / 'example-seaexplorer/deploymentRealtime.yml'), kind='sub', ) assert 'No such file or directory' in str(missing_file_exc) @@ -121,26 +119,26 @@ def test_raw_to_timeseries(): def test_missing_bad_timebase(): # Prepare yaml files with bad timebase and no timebase - with open(example_dir / 'example-seaexplorer/deploymentRealtime.yml') as fin: + with open(EXAMPLE_DIR / 'example-seaexplorer/deploymentRealtime.yml') as fin: deployment = yaml.safe_load(fin) deployment['netcdf_variables']['timebase']['source'] = 'non existing sensor' - with open(example_dir / 'example-seaexplorer/bad_timebase.yml', 'w') as fin: + with open(EXAMPLE_DIR / 'example-seaexplorer/bad_timebase.yml', 'w') as fin: yaml.dump(deployment, fin) deployment['netcdf_variables'].pop('timebase') - with open(example_dir / 'example-seaexplorer/no_timebase.yml', 'w') as fin: + with open(EXAMPLE_DIR / 'example-seaexplorer/no_timebase.yml', 'w') as fin: yaml.dump(deployment, fin) with pytest.raises(ValueError) as bad_timebase_exc: result_bad_timebase = seaexplorer.raw_to_timeseries( 'tests/data/realtime_rawnc/', 'tests/data/l0-profiles/', - str(example_dir / 'example-seaexplorer/bad_timebase.yml'), + str(EXAMPLE_DIR / 'example-seaexplorer/bad_timebase.yml'), kind='sub', ) with pytest.raises(ValueError) as no_timebase_exc: result_no_timebase = seaexplorer.raw_to_timeseries( 'tests/data/realtime_rawnc/', 'tests/data/l0-profiles/', - str(example_dir / 'example-seaexplorer/no_timebase.yml'), + str(EXAMPLE_DIR / 'example-seaexplorer/no_timebase.yml'), kind='sub', ) assert 'sensor not found in pld1 columns' in str(bad_timebase_exc) diff --git a/tests/test_slocum.py b/tests/test_slocum.py index 565667a..c07b031 100644 --- a/tests/test_slocum.py +++ b/tests/test_slocum.py @@ -1,5 +1,4 @@ import json -from pathlib import Path import numpy as np import pytest @@ -8,21 +7,19 @@ import pyglider.ncprocess as ncprocess import pyglider.slocum as slocum - -library_dir = Path(__file__).parent.parent.absolute() -example_dir = library_dir / 'tests/example-data/' +from tests.utils import EXAMPLE_DIR, LIBRARY_DIR # Create an L0 timeseries from slocum data and test that the resulting netcdf is # identical to the test data -cacdir = example_dir / 'example-slocum/cac/' -sensorlist = str(example_dir / 'example-slocum/dfo-rosie713_sensors.txt') -binarydir = str(example_dir / 'example-slocum/realtime_raw/') + '/' -rawdir_slocum = str(example_dir / 'example-slocum/realtime_rawnc/') + '/' -deploymentyaml_slocum = str(example_dir / 'example-slocum/deploymentRealtime.yml') -tsdir = str(example_dir / 'example-slocum/L0-timeseries/') + '/' +cacdir = EXAMPLE_DIR / 'example-slocum/cac/' +sensorlist = str(EXAMPLE_DIR / 'example-slocum/dfo-rosie713_sensors.txt') +binarydir = str(EXAMPLE_DIR / 'example-slocum/realtime_raw/') + '/' +rawdir_slocum = str(EXAMPLE_DIR / 'example-slocum/realtime_rawnc/') + '/' +deploymentyaml_slocum = str(EXAMPLE_DIR / 'example-slocum/deploymentRealtime.yml') +tsdir = str(EXAMPLE_DIR / 'example-slocum/L0-timeseries/') + '/' scisuffix = 'tbd' glidersuffix = 'sbd' -profiledir = str(example_dir / 'example-slocum/L0-profiles/') +profiledir = str(EXAMPLE_DIR / 'example-slocum/L0-profiles/') do_direct = True # This needs to get run every time the tests are run, so do at top level: @@ -45,7 +42,7 @@ output_slocum = xr.open_dataset(outname_slocum) # Open test data file test_data_slocum = xr.open_dataset( - library_dir / 'tests/expected/example-slocum/L0-timeseries/dfo-rosie713-20190615.nc' + LIBRARY_DIR / 'tests/expected/example-slocum/L0-timeseries/dfo-rosie713-20190615.nc' ) variables_slocum = list(output_slocum.variables) @@ -95,7 +92,7 @@ def test_profiles_compliant(): checker_names = ['gliderdac', 'cf:1.8'] verbose = 0 criteria = 'normal' - output_filename = example_dir / 'report.json' + output_filename = EXAMPLE_DIR / 'report.json' output_format = 'json' """ Inputs to ComplianceChecker.run_checker @@ -139,7 +136,7 @@ def test_timeseries_compliant(): checker_names = ['cf:1.8'] verbose = 0 criteria = 'normal' - output_filename = example_dir / 'report.json' + output_filename = EXAMPLE_DIR / 'report.json' output_format = 'json' """ Inputs to ComplianceChecker.run_checker diff --git a/tests/test_utils.py b/tests/test_utils.py index 0d3e935..ed93de4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,21 +1,17 @@ -from pathlib import Path - import numpy as np import pytest import xarray as xr import yaml import pyglider.utils as utils - -library_dir = Path(__file__).parent.parent.absolute() -example_dir = library_dir / 'tests/example-data/' +from tests.utils import EXAMPLE_DIR, LIBRARY_DIR test_data = xr.open_dataset( - library_dir + LIBRARY_DIR / 'tests/expected/example-seaexplorer/L0-timeseries/dfo-eva035-20190718.nc' ) deploymentyaml = ( - example_dir / 'example-seaexplorer-legato-flntu-arod-ad2cp/deploymentRealtime.yml' + EXAMPLE_DIR / 'example-seaexplorer-legato-flntu-arod-ad2cp/deploymentRealtime.yml' ) with open(deploymentyaml) as fin: deployment = yaml.safe_load(fin) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..aac3421 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,6 @@ +"""Utilities specific to the test suite.""" + +from pathlib import Path + +LIBRARY_DIR = Path(__file__).parent.parent.absolute() +EXAMPLE_DIR = LIBRARY_DIR / 'tests/example-data/'