diff --git a/lib/iris/tests/unit/common/mixin/test_CFVariableMixin.py b/lib/iris/tests/unit/common/mixin/test_CFVariableMixin.py index 020f18a358..e75d734284 100644 --- a/lib/iris/tests/unit/common/mixin/test_CFVariableMixin.py +++ b/lib/iris/tests/unit/common/mixin/test_CFVariableMixin.py @@ -4,14 +4,10 @@ # See LICENSE in the root of the repository for full licensing details. """Unit tests for the :class:`iris.common.mixin.CFVariableMixin`.""" -# Import iris.tests first so that some things can be initialised before -# importing anything else. -import iris.tests as tests # isort:skip - from collections import OrderedDict, namedtuple -from unittest import mock from cf_units import Unit +import pytest from iris.common.metadata import ( AncillaryVariableMetadata, @@ -24,16 +20,17 @@ from iris.experimental.ugrid.metadata import ConnectivityMetadata -class Test__getter(tests.IrisTest): - def setUp(self): - self.standard_name = mock.sentinel.standard_name - self.long_name = mock.sentinel.long_name - self.var_name = mock.sentinel.var_name - self.units = mock.sentinel.units - self.attributes = mock.sentinel.attributes - self.metadata = mock.sentinel.metadata +class Test__getter: + @pytest.fixture(autouse=True) + def _setup(self, mocker): + self.standard_name = mocker.sentinel.standard_name + self.long_name = mocker.sentinel.long_name + self.var_name = mocker.sentinel.var_name + self.units = mocker.sentinel.units + self.attributes = mocker.sentinel.attributes + self.metadata = mocker.sentinel.metadata - metadata = mock.MagicMock( + metadata = mocker.MagicMock( standard_name=self.standard_name, long_name=self.long_name, var_name=self.var_name, @@ -46,32 +43,33 @@ def setUp(self): self.item._metadata_manager = metadata def test_standard_name(self): - self.assertEqual(self.item.standard_name, self.standard_name) + assert self.item.standard_name == self.standard_name def test_long_name(self): - self.assertEqual(self.item.long_name, self.long_name) + assert self.item.long_name == self.long_name def test_var_name(self): - self.assertEqual(self.item.var_name, self.var_name) + assert self.item.var_name == self.var_name def test_units(self): - self.assertEqual(self.item.units, self.units) + assert self.item.units == self.units def test_attributes(self): - self.assertEqual(self.item.attributes, self.attributes) + assert self.item.attributes == self.attributes def test_metadata(self): - self.assertEqual(self.item.metadata, self.metadata) - - -class Test__setter(tests.IrisTest): - def setUp(self): - metadata = mock.MagicMock( - standard_name=mock.sentinel.standard_name, - long_name=mock.sentinel.long_name, - var_name=mock.sentinel.var_name, - units=mock.sentinel.units, - attributes=mock.sentinel.attributes, + assert self.item.metadata == self.metadata + + +class Test__setter: + @pytest.fixture(autouse=True) + def _setup(self, mocker): + metadata = mocker.MagicMock( + standard_name=mocker.sentinel.standard_name, + long_name=mocker.sentinel.long_name, + var_name=mocker.sentinel.var_name, + units=mocker.sentinel.units, + attributes=mocker.sentinel.attributes, token=lambda name: name, ) @@ -81,68 +79,67 @@ def setUp(self): def test_standard_name__valid(self): standard_name = "air_temperature" self.item.standard_name = standard_name - self.assertEqual(self.item._metadata_manager.standard_name, standard_name) + assert self.item._metadata_manager.standard_name == standard_name def test_standard_name__none(self): self.item.standard_name = None - self.assertIsNone(self.item._metadata_manager.standard_name) + assert self.item._metadata_manager.standard_name is None def test_standard_name__invalid(self): standard_name = "nope nope" emsg = f"{standard_name!r} is not a valid standard_name" - with self.assertRaisesRegex(ValueError, emsg): + with pytest.raises(ValueError, match=emsg): self.item.standard_name = standard_name def test_long_name(self): long_name = "long_name" self.item.long_name = long_name - self.assertEqual(self.item._metadata_manager.long_name, long_name) + assert self.item._metadata_manager.long_name == long_name def test_long_name__none(self): self.item.long_name = None - self.assertIsNone(self.item._metadata_manager.long_name) + assert self.item._metadata_manager.long_name is None def test_var_name(self): var_name = "var_name" self.item.var_name = var_name - self.assertEqual(self.item._metadata_manager.var_name, var_name) + assert self.item._metadata_manager.var_name == var_name def test_var_name__none(self): self.item.var_name = None - self.assertIsNone(self.item._metadata_manager.var_name) + assert self.item._metadata_manager.var_name is None def test_var_name__invalid_token(self): var_name = "nope nope" self.item._metadata_manager.token = lambda name: None emsg = f"{var_name!r} is not a valid NetCDF variable name." - with self.assertRaisesRegex(ValueError, emsg): + with pytest.raises(ValueError, match=emsg): self.item.var_name = var_name def test_attributes(self): attributes = dict(hello="world") self.item.attributes = attributes - self.assertEqual(self.item._metadata_manager.attributes, attributes) - self.assertIsNot(self.item._metadata_manager.attributes, attributes) - self.assertIsInstance( - self.item._metadata_manager.attributes, LimitedAttributeDict - ) + assert self.item._metadata_manager.attributes == attributes + assert self.item._metadata_manager.attributes is not attributes + assert isinstance(self.item._metadata_manager.attributes, LimitedAttributeDict) def test_attributes__none(self): self.item.attributes = None - self.assertEqual(self.item._metadata_manager.attributes, {}) + assert self.item._metadata_manager.attributes == {} -class Test__metadata_setter(tests.IrisTest): - def setUp(self): +class Test__metadata_setter: + @pytest.fixture(autouse=True) + def _setup(self, mocker): class Metadata: def __init__(self): self.cls = BaseMetadata self.fields = BaseMetadata._fields - self.standard_name = mock.sentinel.standard_name - self.long_name = mock.sentinel.long_name - self.var_name = mock.sentinel.var_name - self.units = mock.sentinel.units - self.attributes = mock.sentinel.attributes + self.standard_name = mocker.sentinel.standard_name + self.long_name = mocker.sentinel.long_name + self.var_name = mocker.sentinel.var_name + self.units = mocker.sentinel.units + self.attributes = mocker.sentinel.attributes self.token = lambda name: name @property @@ -170,31 +167,31 @@ def values(self): def test_dict(self): metadata = dict(**self.args) self.item.metadata = metadata - self.assertEqual(self.item._metadata_manager.values, metadata) - self.assertIsNot(self.item._metadata_manager.attributes, self.attributes) + assert self.item._metadata_manager.values == metadata + assert self.item._metadata_manager.attributes is not self.attributes - def test_dict__partial(self): + def test_dict__partial(self, mocker): metadata = dict(**self.args) del metadata["standard_name"] self.item.metadata = metadata - metadata["standard_name"] = mock.sentinel.standard_name - self.assertEqual(self.item._metadata_manager.values, metadata) - self.assertIsNot(self.item._metadata_manager.attributes, self.attributes) + metadata["standard_name"] = mocker.sentinel.standard_name + assert self.item._metadata_manager.values == metadata + assert self.item._metadata_manager.attributes is not self.attributes def test_ordereddict(self): metadata = self.args self.item.metadata = metadata - self.assertEqual(self.item._metadata_manager.values, metadata) - self.assertIsNot(self.item._metadata_manager.attributes, self.attributes) + assert self.item._metadata_manager.values == metadata + assert self.item._metadata_manager.attributes is not self.attributes - def test_ordereddict__partial(self): + def test_ordereddict__partial(self, mocker): metadata = self.args del metadata["long_name"] del metadata["units"] self.item.metadata = metadata - metadata["long_name"] = mock.sentinel.long_name - metadata["units"] = mock.sentinel.units - self.assertEqual(self.item._metadata_manager.values, metadata) + metadata["long_name"] = mocker.sentinel.long_name + metadata["units"] = mocker.sentinel.units + assert self.item._metadata_manager.values == metadata def test_tuple(self): metadata = tuple(self.args.values()) @@ -205,14 +202,14 @@ def test_tuple(self): for field in self.item._metadata_manager.fields ] ) - self.assertEqual(result, metadata) - self.assertIsNot(self.item._metadata_manager.attributes, self.attributes) + assert result == metadata + assert self.item._metadata_manager.attributes is not self.attributes def test_tuple__missing(self): metadata = list(self.args.values()) del metadata[2] emsg = "Invalid .* metadata, require .* to be specified." - with self.assertRaisesRegex(TypeError, emsg): + with pytest.raises(TypeError, match=emsg): self.item.metadata = tuple(metadata) def test_namedtuple(self): @@ -222,10 +219,10 @@ def test_namedtuple(self): ) metadata = Metadata(**self.args) self.item.metadata = metadata - self.assertEqual(self.item._metadata_manager.values, metadata._asdict()) - self.assertIsNot(self.item._metadata_manager.attributes, metadata.attributes) + assert self.item._metadata_manager.values == metadata._asdict() + assert self.item._metadata_manager.attributes is not metadata.attributes - def test_namedtuple__partial(self): + def test_namedtuple__partial(self, mocker): Metadata = namedtuple( "Metadata", ("standard_name", "long_name", "var_name", "units") ) @@ -233,20 +230,20 @@ def test_namedtuple__partial(self): metadata = Metadata(**self.args) self.item.metadata = metadata expected = metadata._asdict() - expected.update(dict(attributes=mock.sentinel.attributes)) - self.assertEqual(self.item._metadata_manager.values, expected) + expected.update(dict(attributes=mocker.sentinel.attributes)) + assert self.item._metadata_manager.values == expected def test_class_ancillaryvariablemetadata(self): metadata = AncillaryVariableMetadata(**self.args) self.item.metadata = metadata - self.assertEqual(self.item._metadata_manager.values, metadata._asdict()) - self.assertIsNot(self.item._metadata_manager.attributes, metadata.attributes) + assert self.item._metadata_manager.values == metadata._asdict() + assert self.item._metadata_manager.attributes is not metadata.attributes def test_class_basemetadata(self): metadata = BaseMetadata(**self.args) self.item.metadata = metadata - self.assertEqual(self.item._metadata_manager.values, metadata._asdict()) - self.assertIsNot(self.item._metadata_manager.attributes, metadata.attributes) + assert self.item._metadata_manager.values == metadata._asdict() + assert self.item._metadata_manager.attributes is not metadata.attributes def test_class_cellmeasuremetadata(self): self.args["measure"] = None @@ -254,8 +251,8 @@ def test_class_cellmeasuremetadata(self): self.item.metadata = metadata expected = metadata._asdict() del expected["measure"] - self.assertEqual(self.item._metadata_manager.values, expected) - self.assertIsNot(self.item._metadata_manager.attributes, metadata.attributes) + assert self.item._metadata_manager.values == expected + assert self.item._metadata_manager.attributes is not metadata.attributes def test_class_connectivitymetadata(self): self.args.update(dict(cf_role=None, start_index=None, location_axis=None)) @@ -265,8 +262,8 @@ def test_class_connectivitymetadata(self): del expected["cf_role"] del expected["start_index"] del expected["location_axis"] - self.assertEqual(self.item._metadata_manager.values, expected) - self.assertIsNot(self.item._metadata_manager.attributes, metadata.attributes) + assert self.item._metadata_manager.values == expected + assert self.item._metadata_manager.attributes is not metadata.attributes def test_class_coordmetadata(self): self.args.update(dict(coord_system=None, climatological=False)) @@ -275,8 +272,8 @@ def test_class_coordmetadata(self): expected = metadata._asdict() del expected["coord_system"] del expected["climatological"] - self.assertEqual(self.item._metadata_manager.values, expected) - self.assertIsNot(self.item._metadata_manager.attributes, metadata.attributes) + assert self.item._metadata_manager.values == expected + assert self.item._metadata_manager.attributes is not metadata.attributes def test_class_cubemetadata(self): self.args["cell_methods"] = None @@ -284,19 +281,20 @@ def test_class_cubemetadata(self): self.item.metadata = metadata expected = metadata._asdict() del expected["cell_methods"] - self.assertEqual(self.item._metadata_manager.values, expected) - self.assertIsNot(self.item._metadata_manager.attributes, metadata.attributes) - - -class Test_rename(tests.IrisTest): - def setUp(self): - metadata = mock.MagicMock( - standard_name=mock.sentinel.standard_name, - long_name=mock.sentinel.long_name, - var_name=mock.sentinel.var_name, - units=mock.sentinel.units, - attributes=mock.sentinel.attributes, - values=mock.sentinel.metadata, + assert self.item._metadata_manager.values == expected + assert self.item._metadata_manager.attributes is not metadata.attributes + + +class Test_rename: + @pytest.fixture(autouse=True) + def _setup(self, mocker): + metadata = mocker.MagicMock( + standard_name=mocker.sentinel.standard_name, + long_name=mocker.sentinel.long_name, + var_name=mocker.sentinel.var_name, + units=mocker.sentinel.units, + attributes=mocker.sentinel.attributes, + values=mocker.sentinel.metadata, token=lambda name: name, ) @@ -306,39 +304,36 @@ def setUp(self): def test__valid_standard_name(self): name = "air_temperature" self.item.rename(name) - self.assertEqual(self.item._metadata_manager.standard_name, name) - self.assertIsNone(self.item._metadata_manager.long_name) - self.assertIsNone(self.item._metadata_manager.var_name) + assert self.item._metadata_manager.standard_name == name + assert self.item._metadata_manager.long_name is None + assert self.item._metadata_manager.var_name is None def test__invalid_standard_name(self): name = "nope nope" self.item.rename(name) - self.assertIsNone(self.item._metadata_manager.standard_name) - self.assertEqual(self.item._metadata_manager.long_name, name) - self.assertIsNone(self.item._metadata_manager.var_name) + assert self.item._metadata_manager.standard_name is None + assert self.item._metadata_manager.long_name == name + assert self.item._metadata_manager.var_name is None -class Test_name(tests.IrisTest): - def setUp(self): +class Test_name: + @pytest.fixture(autouse=True) + def _setup(self, mocker): class Metadata: def __init__(self, name): - self.name = mock.MagicMock(return_value=name) + self.name = mocker.MagicMock(return_value=name) - self.name = mock.sentinel.name + self.name = mocker.sentinel.name metadata = Metadata(self.name) self.item = CFVariableMixin() self.item._metadata_manager = metadata - def test(self): - default = mock.sentinel.default - token = mock.sentinel.token + def test(self, mocker): + default = mocker.sentinel.default + token = mocker.sentinel.token result = self.item.name(default=default, token=token) - self.assertEqual(result, self.name) + assert result == self.name self.item._metadata_manager.name.assert_called_with( default=default, token=token ) - - -if __name__ == "__main__": - tests.main()