Skip to content

Commit

Permalink
Refactor test paths
Browse files Browse the repository at this point in the history
  • Loading branch information
VeckoTheGecko committed Dec 12, 2024
1 parent 88f1398 commit 69c9225
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
Empty file added tests/__init__.py
Empty file.
31 changes: 15 additions & 16 deletions tests/test_pyglider.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
import pyglider.seaexplorer as seaexplorer
import pyglider.slocum as slocum

library_dir = Path(__file__).parent.parent.absolute()
example_dir = library_dir / 'tests/example-data/'
from tests.utils import LIBRARY_DIR, EXAMPLE_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)

Expand Down Expand Up @@ -62,11 +61,11 @@ 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/') + '/'
l0tsdir_interp = str(EXAMPLE_DIR / 'example-seaexplorer/L0-timeseries-test-interp/') + '/'

outname_interp = seaexplorer.raw_to_L0timeseries(rawncdir, l0tsdir_interp, interp_yaml, kind='sub')
output_interp = xr.open_dataset(outname_interp)
Expand All @@ -85,18 +84,18 @@ 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(rawncdir, l0tsdir,
deploymentyaml_raw, kind='raw')
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')

@pytest.mark.parametrize("var", variables)
Expand Down Expand Up @@ -127,11 +126,11 @@ 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/') + '/'
l0tsdir_interp_raw = str(EXAMPLE_DIR / 'example-seaexplorer-raw/L0-timeseries-test-interp/') + '/'

outname_interp_raw = seaexplorer.raw_to_L0timeseries(rawncdir, l0tsdir_interp_raw, interp_yaml, kind='raw')
output_interp_raw = xr.open_dataset(outname_interp_raw)
Expand Down
21 changes: 10 additions & 11 deletions tests/test_seaexplorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import os
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 LIBRARY_DIR, EXAMPLE_DIR

def test__outputname():
fnout, filenum = seaexplorer._outputname('tests/data/realtime_raw/sea035.12.pld1.sub.36',
Expand Down Expand Up @@ -58,12 +57,12 @@ 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
assert result_sub is True
Expand Down Expand Up @@ -91,35 +90,35 @@ def test_raw_to_timeseries():
with pytest.raises(FileNotFoundError) as missing_file_exc:
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)
assert result_sub == 'tests/data/l0-profiles/dfo-eva035-20190718.nc'


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)
assert "Must specify timebase" in str(no_timebase_exc)
24 changes: 11 additions & 13 deletions tests/test_slocum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@
import pyglider.seaexplorer as seaexplorer
import pyglider.slocum as slocum

from tests.utils import LIBRARY_DIR, EXAMPLE_DIR


library_dir = Path(__file__).parent.parent.absolute()
example_dir = library_dir / 'tests/example-data/'

# 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:
Expand All @@ -42,7 +40,7 @@
output_slocum = xr.open_dataset(outname_slocum)
# Open test data file
test_data_slocum = xr.open_dataset(
library_dir /
LIBRARY_DIR /
'tests/expected/example-slocum/L0-timeseries/dfo-rosie713-20190615.nc')
variables_slocum = list(output_slocum.variables)

Expand Down Expand Up @@ -90,7 +88,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
Expand Down Expand Up @@ -132,7 +130,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
Expand Down
7 changes: 3 additions & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

import pyglider.utils as utils

library_dir = Path(__file__).parent.parent.absolute()
example_dir = library_dir / 'tests/example-data/'
from tests.utils import LIBRARY_DIR, EXAMPLE_DIR

test_data = xr.open_dataset(library_dir / 'tests/expected/example-seaexplorer/L0-timeseries/dfo-eva035-20190718.nc')
deploymentyaml = example_dir / 'example-seaexplorer-legato-flntu-arod-ad2cp/deploymentRealtime.yml'
test_data = xr.open_dataset(LIBRARY_DIR / 'tests/expected/example-seaexplorer/L0-timeseries/dfo-eva035-20190718.nc')
deploymentyaml = EXAMPLE_DIR / 'example-seaexplorer-legato-flntu-arod-ad2cp/deploymentRealtime.yml'
with open(deploymentyaml) as fin:
deployment = yaml.safe_load(fin)
metadata = deployment['metadata']
Expand Down
5 changes: 5 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Utilities specific to the test suite."""
from pathlib import Path

LIBRARY_DIR = Path(__file__).parent.parent.absolute()
EXAMPLE_DIR = LIBRARY_DIR / 'tests/example-data/'

0 comments on commit 69c9225

Please sign in to comment.