Skip to content

Commit da31df7

Browse files
committed
DAS-2368: Update tests to address PR feedback
1 parent ae7d2bf commit da31df7

3 files changed

Lines changed: 65 additions & 113 deletions

File tree

tests/conftest.py

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -362,38 +362,6 @@ def sample_varinfo_test03(
362362
)
363363

364364

365-
@fixture(scope='function')
366-
def sample_netcdf4_file_test04(temp_dir) -> str:
367-
"""Create a sample NetCDF-4 file for testing updating dimension variables."""
368-
file_name = path_join(temp_dir, 'test_input_04.nc')
369-
370-
sample_datatree = xr.DataTree(xr.Dataset())
371-
372-
sample_datatree['/sub_group'] = xr.Dataset(
373-
attrs={'short_name': 'TEST04'},
374-
data_vars={
375-
'variable_one': xr.DataArray(
376-
np.ones((3, 3)),
377-
attrs={},
378-
dims=['y', 'x'],
379-
),
380-
},
381-
)
382-
383-
sample_datatree.to_netcdf(file_name, encoding=None)
384-
return file_name
385-
386-
387-
@fixture(scope='function')
388-
def sample_varinfo_test04(
389-
sample_netcdf4_file, varinfo_config_file
390-
) -> VarInfoFromNetCDF4:
391-
"""Create sample VarInfoFromNetCDF4 instance."""
392-
return VarInfoFromNetCDF4(
393-
sample_netcdf4_file, config_file=varinfo_config_file, short_name='TEST04'
394-
)
395-
396-
397365
@fixture(scope='function')
398366
def sample_netcdf4_file_test05(temp_dir) -> str:
399367
"""Create a sample NetCDF-4 file for testing excluding variables."""
@@ -470,42 +438,6 @@ def sample_varinfo_test05(
470438
)
471439

472440

473-
@fixture(scope='function')
474-
def sample_netcdf4_file_test06(temp_dir) -> str:
475-
"""Create a sample NetCDF-4 file."""
476-
file_name = path_join(temp_dir, 'test_input.nc')
477-
478-
sample_datatree = xr.DataTree(
479-
dataset=xr.Dataset(
480-
attrs={
481-
'short_name': 'TEST06',
482-
},
483-
data_vars={
484-
'variable_one': xr.DataArray(
485-
np.array([]),
486-
attrs={},
487-
),
488-
'variable_two': xr.DataArray(
489-
np.array([]),
490-
attrs={},
491-
),
492-
},
493-
),
494-
)
495-
sample_datatree.to_netcdf(file_name, encoding=None)
496-
return file_name
497-
498-
499-
@fixture(scope='function')
500-
def sample_varinfo_test06(
501-
sample_netcdf4_file_test06, varinfo_config_file
502-
) -> VarInfoFromNetCDF4:
503-
"""Create sample VarInfoFromNetCDF4 instance."""
504-
return VarInfoFromNetCDF4(
505-
sample_netcdf4_file_test06, config_file=varinfo_config_file, short_name='TEST06'
506-
)
507-
508-
509441
@fixture(scope='function')
510442
def sample_netcdf4_file_test07(temp_dir) -> str:
511443
"""Create a sample NetCDF-4 file."""
@@ -519,7 +451,6 @@ def sample_netcdf4_file_test07(temp_dir) -> str:
519451
data_vars={
520452
'variable_one': xr.DataArray(
521453
np.ones((3, 3)),
522-
attrs={},
523454
dims=['y_root', 'x_root'],
524455
),
525456
},
@@ -536,28 +467,3 @@ def sample_netcdf4_file_test07(temp_dir) -> str:
536467
)
537468
sample_datatree.to_netcdf(file_name, encoding=None)
538469
return file_name
539-
540-
541-
@fixture(scope='function')
542-
def sample_netcdf4_file_test08(temp_dir) -> str:
543-
"""Create a sample NetCDF-4 file."""
544-
file_name = path_join(temp_dir, 'test_input.nc')
545-
546-
sample_datatree = xr.DataTree(
547-
dataset=xr.Dataset(
548-
attrs={
549-
'short_name': 'TEST08',
550-
},
551-
),
552-
)
553-
554-
sample_datatree['/sub_group'] = xr.Dataset(
555-
data_vars={
556-
'variable_one': xr.DataArray(
557-
np.ones((3, 3)),
558-
dims=['y', 'x'],
559-
),
560-
},
561-
)
562-
sample_datatree.to_netcdf(file_name, encoding=None)
563-
return file_name

tests/data/earthdata_varinfo_test_config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,10 @@
550550
"Attributes": [
551551
{
552552
"Name": "dimensions",
553-
"Value": "y X"
553+
"Value": "y x"
554554
}
555555
],
556-
"_Description": ""
556+
"_Description": "Dimensions override used to rename dimensions"
557557
},
558558
{
559559
"Applicability": {

tests/unit/test_annotate.py

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for metadata_annotator.annotate.py."""
22

3-
import os
3+
from os.path import join as path_join
4+
from os.path import split as path_split
45
from unittest.mock import patch
56

67
import numpy as np
@@ -630,8 +631,37 @@ def test_get_referenced_variables(sample_varinfo_test02):
630631
)
631632

632633

633-
def test_update_dimension_variables(sample_netcdf4_file_test04, sample_varinfo_test04):
634+
def test_update_dimension_variables(
635+
varinfo_config_file,
636+
temp_dir,
637+
):
634638
"""Ensure the dimension variables are updated correctly."""
639+
file_name = path_join(temp_dir, 'test_input_04.nc')
640+
sample_datatree = xr.DataTree(xr.Dataset())
641+
sample_datatree['/sub_group'] = xr.Dataset(
642+
attrs={'short_name': 'TEST04'},
643+
data_vars={
644+
'variable_one': xr.DataArray(
645+
np.ones((3, 3)),
646+
attrs={'dimensions': 'y x'},
647+
dims=['y_old', 'x_old'],
648+
),
649+
'x_old': xr.DataArray(
650+
np.array([0, 1, 2]),
651+
dims=['x_old'],
652+
),
653+
'y_old': xr.DataArray(
654+
np.array([0, 1, 2]),
655+
dims=['y_old'],
656+
),
657+
},
658+
)
659+
sample_datatree.to_netcdf(file_name, encoding=None)
660+
661+
varinfo = VarInfoFromNetCDF4(
662+
file_name, config_file=varinfo_config_file, short_name='TEST04'
663+
)
664+
635665
expected_x_attributes = {
636666
'standard_name': 'projection_x_coordinate',
637667
'long_name': 'x coordinate of projection',
@@ -656,16 +686,14 @@ def test_update_dimension_variables(sample_netcdf4_file_test04, sample_varinfo_t
656686
'metadata_annotator.annotate.get_dimension_index_map',
657687
return_value={'/sub_group/y': 0, '/sub_group/x': 0},
658688
):
659-
with xr.open_datatree(
660-
sample_netcdf4_file_test04, decode_times=False
661-
) as test_datatree:
689+
with xr.open_datatree(file_name, decode_times=False) as test_datatree:
662690
variables_to_create = {'/sub_group/x', '/sub_group/y'}
663691
items_to_update = {'/sub_group/variable_one'}
664692
update_dimension_variables(
665693
test_datatree,
666694
items_to_update,
667695
variables_to_create,
668-
sample_varinfo_test04,
696+
varinfo,
669697
)
670698
assert (
671699
test_datatree['sub_group'].dataset['x'].attrs == expected_x_attributes
@@ -755,20 +783,32 @@ def test_construct_dim_path(ancestor_path, dim_name, expected):
755783
@pytest.mark.parametrize(
756784
'variable_path, expected', [('/variable_one', True), ('/variable_two', False)]
757785
)
758-
def test_has_dimension_override(sample_varinfo_test06, variable_path, expected):
786+
def test_has_dimension_override(varinfo_config_file, temp_dir, variable_path, expected):
759787
"""Ensure variables correctly evaluated to contain non-null dimensions override."""
760-
assert has_dimension_override(sample_varinfo_test06, variable_path) == expected
788+
file_name = path_join(temp_dir, 'test_input.nc')
789+
sample_datatree = xr.DataTree(
790+
dataset=xr.Dataset(
791+
attrs={'short_name': 'TEST06'},
792+
data_vars={
793+
'variable_one': xr.DataArray([]),
794+
'variable_two': xr.DataArray([]),
795+
},
796+
),
797+
)
798+
sample_datatree.to_netcdf(file_name, encoding=None)
799+
varinfo = VarInfoFromNetCDF4(
800+
file_name, config_file=varinfo_config_file, short_name='TEST06'
801+
)
802+
assert has_dimension_override(varinfo, variable_path) == expected
761803

762804

763805
@pytest.mark.parametrize(
764806
'variables_to_create, expected',
765807
[
766808
(
767-
{'/x_root', '/y_root', '/sub_group/x', '/sub_group/y'},
768-
{'/x_root', '/y_root', '/sub_group/x', '/sub_group/y'},
809+
{'/y_root', '/sub_group/y', '/missing/dimension/z'},
810+
{'/y_root', '/sub_group/y'},
769811
),
770-
({'/x_root', '/y_root'}, {'/x_root', '/y_root'}),
771-
({'/sub_group/x', '/sub_group/y'}, {'/sub_group/x', '/sub_group/y'}),
772812
(set(), set()),
773813
],
774814
)
@@ -803,23 +843,29 @@ def test_ensure_dimension_node_exists(sample_netcdf4_file_test07, variable_path)
803843
creates it successfully.
804844
"""
805845
with xr.open_datatree(sample_netcdf4_file_test07) as datatree:
806-
group_path, variable_name = os.path.split(variable_path)
846+
group_path, variable_name = path_split(variable_path)
807847
assert variable_name not in datatree[group_path]
808848
ensure_dimension_node_exists(datatree, variable_path)
809849
assert variable_name in datatree[group_path]
810850

811851

812-
def test_copy_shared_dimensions_to_parent(sample_netcdf4_file_test08):
852+
def test_copy_shared_dimensions_to_parent(temp_dir):
813853
"""Ensure dimension variables are copied to their configured parent group."""
814-
with xr.open_datatree(sample_netcdf4_file_test08) as test_datatree:
854+
file_name = path_join(temp_dir, 'test_input.nc')
855+
sample_datatree = xr.DataTree(dataset=xr.Dataset())
856+
sample_datatree['/sub_group'] = xr.Dataset(
857+
data_vars={'variable_one': xr.DataArray(np.ones((3, 3)), dims=['y', 'x'])}
858+
)
859+
sample_datatree.to_netcdf(file_name, encoding=None)
860+
with xr.open_datatree(file_name) as test_datatree:
815861
variables_to_create = set(['/x', '/y'])
816862
# This inital check confirms the dimension variables are not present in the
817863
# configured parent group before being processed.
818864
for variable in variables_to_create:
819-
group_path, variable_name = os.path.split(variable)
865+
group_path, variable_name = path_split(variable)
820866
assert variable_name not in test_datatree[group_path]
821867

822868
copy_shared_dimensions_to_parent(test_datatree, variables_to_create)
823869
for variable in variables_to_create:
824-
group_path, variable_name = os.path.split(variable)
870+
group_path, variable_name = path_split(variable)
825871
assert variable_name in test_datatree[group_path]

0 commit comments

Comments
 (0)