Skip to content

Commit

Permalink
Better structure for test parameterisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
trexfeathers committed Oct 24, 2023
1 parent 20fca6e commit 2bcee96
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 174 deletions.
170 changes: 88 additions & 82 deletions lib/iris/tests/unit/coord_systems/test_ObliqueMercator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,89 @@
####


class GlobeWithEq(ccrs.Globe):
def __eq__(self, other):
"""Need eq to enable comparison with expected arguments."""
result = NotImplemented
if isinstance(other, ccrs.Globe):
result = other.__dict__ == self.__dict__
return result


class ParamTuple(NamedTuple):
"""Used for easy coupling of test parameters."""

id: str
class_kwargs: dict
cartopy_kwargs: dict


kwarg_permutations: List[ParamTuple] = [
ParamTuple(
"default",
dict(),
dict(),
),
ParamTuple(
"azimuth",
dict(azimuth_of_central_line=90),
dict(azimuth=90),
),
ParamTuple(
"central_longitude",
dict(longitude_of_projection_origin=90),
dict(central_longitude=90),
),
ParamTuple(
"central_latitude",
dict(latitude_of_projection_origin=45),
dict(central_latitude=45),
),
ParamTuple(
"false_easting_northing",
dict(false_easting=1000000, false_northing=-2000000),
dict(false_easting=1000000, false_northing=-2000000),
),
ParamTuple(
"scale_factor",
# Number inherited from Cartopy's test_mercator.py .
dict(scale_factor_at_projection_origin=0.939692620786),
dict(scale_factor=0.939692620786),
),
ParamTuple(
"globe",
dict(ellipsoid=GeogCS(1)),
dict(
globe=GlobeWithEq(semimajor_axis=1, semiminor_axis=1, ellipse=None)
),
),
ParamTuple(
"combo",
dict(
azimuth_of_central_line=90,
longitude_of_projection_origin=90,
latitude_of_projection_origin=45,
false_easting=1000000,
false_northing=-2000000,
scale_factor_at_projection_origin=0.939692620786,
ellipsoid=GeogCS(1),
),
dict(
azimuth=90.0,
central_longitude=90.0,
central_latitude=45.0,
false_easting=1000000,
false_northing=-2000000,
scale_factor=0.939692620786,
globe=GlobeWithEq(
semimajor_axis=1, semiminor_axis=1, ellipse=None
),
),
),
]
permutation_ids: List[str] = [p.id for p in kwarg_permutations]


class TestArgs:
GeogCS = GeogCS
class_kwargs_default = dict(
Expand All @@ -36,89 +119,12 @@ class TestArgs:
globe=None,
)

class GlobeWithEq(ccrs.Globe):
def __eq__(self, other):
"""Need eq to enable comparison with expected arguments."""
result = NotImplemented
if isinstance(other, ccrs.Globe):
result = other.__dict__ == self.__dict__
return result

class ParamTuple(NamedTuple):
id: str
class_kwargs: dict
cartopy_kwargs: dict

param_list: List[ParamTuple] = [
ParamTuple(
"default",
dict(),
dict(),
),
ParamTuple(
"azimuth",
dict(azimuth_of_central_line=90),
dict(azimuth=90),
),
ParamTuple(
"central_longitude",
dict(longitude_of_projection_origin=90),
dict(central_longitude=90),
),
ParamTuple(
"central_latitude",
dict(latitude_of_projection_origin=45),
dict(central_latitude=45),
),
ParamTuple(
"false_easting_northing",
dict(false_easting=1000000, false_northing=-2000000),
dict(false_easting=1000000, false_northing=-2000000),
),
ParamTuple(
"scale_factor",
# Number inherited from Cartopy's test_mercator.py .
dict(scale_factor_at_projection_origin=0.939692620786),
dict(scale_factor=0.939692620786),
),
ParamTuple(
"globe",
dict(ellipsoid=GeogCS(1)),
dict(
globe=GlobeWithEq(
semimajor_axis=1, semiminor_axis=1, ellipse=None
)
),
),
ParamTuple(
"combo",
dict(
azimuth_of_central_line=90,
longitude_of_projection_origin=90,
latitude_of_projection_origin=45,
false_easting=1000000,
false_northing=-2000000,
scale_factor_at_projection_origin=0.939692620786,
ellipsoid=GeogCS(1),
),
dict(
azimuth=90.0,
central_longitude=90.0,
central_latitude=45.0,
false_easting=1000000,
false_northing=-2000000,
scale_factor=0.939692620786,
globe=GlobeWithEq(
semimajor_axis=1, semiminor_axis=1, ellipse=None
),
),
),
]
param_ids: List[str] = [p.id for p in param_list]

@pytest.fixture(autouse=True, params=param_list, ids=param_ids)
@pytest.fixture(
autouse=True, params=kwarg_permutations, ids=permutation_ids
)
def make_variant_inputs(self, request) -> None:
inputs: TestArgs.ParamTuple = request.param
"""Parse a ParamTuple into usable test information."""
inputs: ParamTuple = request.param
self.class_kwargs = dict(
self.class_kwargs_default, **inputs.class_kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,100 @@
)


class ParamTuple(NamedTuple):
"""Used for easy coupling of test parameters."""

id: str
nc_attributes: dict
expected_class: Type[CoordSystem]
coord_system_kwargs: dict


kwarg_permutations: List[ParamTuple] = [
ParamTuple(
"default",
dict(),
ObliqueMercator,
dict(),
),
ParamTuple(
"azimuth",
dict(azimuth_of_central_line=90),
ObliqueMercator,
dict(azimuth_of_central_line=90),
),
ParamTuple(
"central_longitude",
dict(longitude_of_projection_origin=90),
ObliqueMercator,
dict(longitude_of_projection_origin=90),
),
ParamTuple(
"central_latitude",
dict(latitude_of_projection_origin=45),
ObliqueMercator,
dict(latitude_of_projection_origin=45),
),
ParamTuple(
"false_easting_northing",
dict(false_easting=1000000, false_northing=-2000000),
ObliqueMercator,
dict(false_easting=1000000, false_northing=-2000000),
),
ParamTuple(
"scale_factor",
# Number inherited from Cartopy's test_mercator.py .
dict(scale_factor_at_projection_origin=0.939692620786),
ObliqueMercator,
dict(scale_factor_at_projection_origin=0.939692620786),
),
ParamTuple(
"globe",
dict(semi_major_axis=1),
ObliqueMercator,
dict(ellipsoid=GeogCS(semi_major_axis=1, semi_minor_axis=1)),
),
ParamTuple(
"combo",
dict(
azimuth_of_central_line=90,
longitude_of_projection_origin=90,
latitude_of_projection_origin=45,
false_easting=1000000,
false_northing=-2000000,
scale_factor_at_projection_origin=0.939692620786,
semi_major_axis=1,
),
ObliqueMercator,
dict(
azimuth_of_central_line=90.0,
longitude_of_projection_origin=90.0,
latitude_of_projection_origin=45.0,
false_easting=1000000,
false_northing=-2000000,
scale_factor_at_projection_origin=0.939692620786,
ellipsoid=GeogCS(semi_major_axis=1, semi_minor_axis=1),
),
),
ParamTuple(
"rotated",
dict(grid_mapping_name="rotated_mercator"),
RotatedMercator,
dict(),
),
ParamTuple(
"rotated_azimuth_ignored",
dict(
grid_mapping_name="rotated_mercator",
azimuth_of_central_line=45,
),
RotatedMercator,
dict(),
),
]
permutation_ids: List[str] = [p.id for p in kwarg_permutations]


class TestAttributes:
"""Test that NetCDF attributes are correctly converted to class arguments."""

Expand All @@ -46,99 +140,12 @@ class TestAttributes:
ellipsoid=None,
)

class ParamTuple(NamedTuple):
id: str
nc_attributes: dict
expected_class: Type[CoordSystem]
coord_system_kwargs: dict

param_list: List[ParamTuple] = [
ParamTuple(
"default",
dict(),
ObliqueMercator,
dict(),
),
ParamTuple(
"azimuth",
dict(azimuth_of_central_line=90),
ObliqueMercator,
dict(azimuth_of_central_line=90),
),
ParamTuple(
"central_longitude",
dict(longitude_of_projection_origin=90),
ObliqueMercator,
dict(longitude_of_projection_origin=90),
),
ParamTuple(
"central_latitude",
dict(latitude_of_projection_origin=45),
ObliqueMercator,
dict(latitude_of_projection_origin=45),
),
ParamTuple(
"false_easting_northing",
dict(false_easting=1000000, false_northing=-2000000),
ObliqueMercator,
dict(false_easting=1000000, false_northing=-2000000),
),
ParamTuple(
"scale_factor",
# Number inherited from Cartopy's test_mercator.py .
dict(scale_factor_at_projection_origin=0.939692620786),
ObliqueMercator,
dict(scale_factor_at_projection_origin=0.939692620786),
),
ParamTuple(
"globe",
dict(semi_major_axis=1),
ObliqueMercator,
dict(ellipsoid=GeogCS(semi_major_axis=1, semi_minor_axis=1)),
),
ParamTuple(
"combo",
dict(
azimuth_of_central_line=90,
longitude_of_projection_origin=90,
latitude_of_projection_origin=45,
false_easting=1000000,
false_northing=-2000000,
scale_factor_at_projection_origin=0.939692620786,
semi_major_axis=1,
),
ObliqueMercator,
dict(
azimuth_of_central_line=90.0,
longitude_of_projection_origin=90.0,
latitude_of_projection_origin=45.0,
false_easting=1000000,
false_northing=-2000000,
scale_factor_at_projection_origin=0.939692620786,
ellipsoid=GeogCS(semi_major_axis=1, semi_minor_axis=1),
),
),
ParamTuple(
"rotated",
dict(grid_mapping_name="rotated_mercator"),
RotatedMercator,
dict(),
),
ParamTuple(
"rotated_azimuth_ignored",
dict(
grid_mapping_name="rotated_mercator",
azimuth_of_central_line=45,
),
RotatedMercator,
dict(),
),
]
param_ids: List[str] = [p.id for p in param_list]

@pytest.fixture(autouse=True, params=param_list, ids=param_ids)
@pytest.fixture(
autouse=True, params=kwarg_permutations, ids=permutation_ids
)
def make_variant_inputs(self, request) -> None:
inputs: TestAttributes.ParamTuple = request.param
"""Parse a ParamTuple into usable test information."""
inputs: ParamTuple = request.param

self.nc_attributes = dict(
self.nc_attributes_default, **inputs.nc_attributes
Expand Down

0 comments on commit 2bcee96

Please sign in to comment.