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 17, 2024
1 parent ad9aae9 commit 2114d7d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 52 deletions.
Empty file added tests/__init__.py
Empty file.
34 changes: 15 additions & 19 deletions tests/test_pyglider.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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'
)

Expand Down Expand Up @@ -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(
Expand Down
22 changes: 10 additions & 12 deletions tests/test_seaexplorer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os
from pathlib import Path

import numpy as np
import polars as pl
import pytest
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():
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 11 additions & 14 deletions tests/test_slocum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from pathlib import Path

import numpy as np
import pytest
Expand All @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -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/'

0 comments on commit 2114d7d

Please sign in to comment.