From 2df2ee027ccfb7c7ed400bd575530468f2a54366 Mon Sep 17 00:00:00 2001 From: Bill Little Date: Tue, 12 Mar 2024 13:24:16 +0000 Subject: [PATCH 1/3] pytest migration for unit.common.lenient.test_Lenient --- .../tests/unit/common/lenient/test_Lenient.py | 221 +++++++++--------- 1 file changed, 104 insertions(+), 117 deletions(-) diff --git a/lib/iris/tests/unit/common/lenient/test_Lenient.py b/lib/iris/tests/unit/common/lenient/test_Lenient.py index 375a745ce8..57c1ecfaa0 100644 --- a/lib/iris/tests/unit/common/lenient/test_Lenient.py +++ b/lib/iris/tests/unit/common/lenient/test_Lenient.py @@ -4,179 +4,166 @@ # See LICENSE in the root of the repository for full licensing details. """Unit tests for the :class:`iris.common.lenient.Lenient`.""" -# Import iris.tests first so that some things can be initialised before -# importing anything else. -import iris.tests as tests # isort:skip +import pytest -from unittest.mock import sentinel +from iris.common.lenient import _LENIENT, _LENIENT_PROTECTED, Lenient -from iris.common.lenient import _LENIENT, Lenient +@pytest.fixture() +def lenient(): + # setup + state = {key: _LENIENT.__dict__[key] for key in _LENIENT_PROTECTED} + # call + yield Lenient() + # teardown + for key, value in state.items(): + _LENIENT.__dict__[key] = value -class Test___init__(tests.IrisTest): - def test_default(self): - lenient = Lenient() + +class Test___init__: + def test_default(self, lenient): expected = dict(maths=True) - self.assertEqual(expected, lenient.__dict__) + assert expected == lenient.__dict__ - def test_kwargs(self): - lenient = Lenient(maths=False) + def test_kwargs(self, lenient): + actual = Lenient(maths=False) expected = dict(maths=False) - self.assertEqual(expected, lenient.__dict__) + assert expected, actual.__dict__ - def test_kwargs_invalid(self): + def test_kwargs_invalid(self, lenient): emsg = "Invalid .* option, got 'merge'." - with self.assertRaisesRegex(KeyError, emsg): + with pytest.raises(KeyError, match=emsg): _ = Lenient(merge=True) -class Test___contains__(tests.IrisTest): - def setUp(self): - self.lenient = Lenient() - - def test_in(self): - self.assertIn("maths", self.lenient) +class Test___contains__: + def test_in(self, lenient): + assert "maths" in lenient - def test_not_in(self): - self.assertNotIn("concatenate", self.lenient) + def test_not_in(self, lenient): + assert "concatenate" not in lenient -class Test___getitem__(tests.IrisTest): - def setUp(self): - self.lenient = Lenient() +class Test___getitem__: + def test_in(self, lenient): + assert lenient["maths"] is True - def test_in(self): - self.assertTrue(self.lenient["maths"]) - - def test_not_in(self): + def test_not_in(self, lenient): emsg = "Invalid .* option, got 'MATHS'." - with self.assertRaisesRegex(KeyError, emsg): - _ = self.lenient["MATHS"] - + with pytest.raises(KeyError, match=emsg): + _ = lenient["MATHS"] -class Test___repr__(tests.IrisTest): - def setUp(self): - self.lenient = Lenient() - def test(self): +class Test___repr__: + def test(self, lenient): expected = "Lenient(maths=True)" - self.assertEqual(expected, repr(self.lenient)) + assert expected == repr(lenient) -class Test___setitem__(tests.IrisTest): - def setUp(self): - self.lenient = Lenient() - - def test_key_invalid(self): +class Test___setitem__: + def test_key_invalid(self, lenient): emsg = "Invalid .* option, got 'MATHS." - with self.assertRaisesRegex(KeyError, emsg): - self.lenient["MATHS"] = False + with pytest.raises(KeyError, match=emsg): + lenient["MATHS"] = False - def test_maths_value_invalid(self): - value = sentinel.value + def test_maths_value_invalid(self, mocker, lenient): + value = mocker.sentinel.value emsg = f"Invalid .* option 'maths' value, got {value!r}." - with self.assertRaisesRegex(ValueError, emsg): - self.lenient["maths"] = value + with pytest.raises(ValueError, match=emsg): + lenient["maths"] = value - def test_maths_disable__lenient_enable_true(self): - self.assertTrue(_LENIENT.enable) - self.lenient["maths"] = False - self.assertFalse(self.lenient.__dict__["maths"]) - self.assertFalse(_LENIENT.enable) + def test_maths_disable__lenient_enable_true(self, lenient): + assert _LENIENT.enable is True + lenient["maths"] = False + assert lenient.__dict__["maths"] is False + assert _LENIENT.enable is False - def test_maths_disable__lenient_enable_false(self): + def test_maths_disable__lenient_enable_false(self, lenient): _LENIENT.__dict__["enable"] = False - self.assertFalse(_LENIENT.enable) - self.lenient["maths"] = False - self.assertFalse(self.lenient.__dict__["maths"]) - self.assertFalse(_LENIENT.enable) - - def test_maths_enable__lenient_enable_true(self): - self.assertTrue(_LENIENT.enable) - self.lenient["maths"] = True - self.assertTrue(self.lenient.__dict__["maths"]) - self.assertTrue(_LENIENT.enable) - - def test_maths_enable__lenient_enable_false(self): + assert _LENIENT.enable is False + lenient["maths"] = False + assert lenient.__dict__["maths"] is False + assert _LENIENT.enable is False + + def test_maths_enable__lenient_enable_true(self, lenient): + assert _LENIENT.enable is True + lenient["maths"] = True + assert lenient.__dict__["maths"] is True + assert _LENIENT.enable is True + + def test_maths_enable__lenient_enable_false(self, lenient): _LENIENT.__dict__["enable"] = False - self.assertFalse(_LENIENT.enable) - self.lenient["maths"] = True - self.assertTrue(self.lenient.__dict__["maths"]) - self.assertTrue(_LENIENT.enable) - + assert _LENIENT.enable is False + lenient["maths"] = True + assert lenient.__dict__["maths"] is True + assert _LENIENT.enable is True -class Test_context(tests.IrisTest): - def setUp(self): - self.lenient = Lenient() - def test_nop(self): - self.assertTrue(self.lenient["maths"]) +class Test_context: + def test_nop(self, lenient): + assert lenient["maths"] is True - with self.lenient.context(): - self.assertTrue(self.lenient["maths"]) + with lenient.context(): + assert lenient["maths"] is True - self.assertTrue(self.lenient["maths"]) + assert lenient["maths"] is True - def test_maths_disable__lenient_true(self): + def test_maths_disable__lenient_true(self, lenient): # synchronised - self.assertTrue(_LENIENT.enable) - self.assertTrue(self.lenient["maths"]) + assert _LENIENT.enable is True + assert lenient["maths"] is True - with self.lenient.context(maths=False): + with lenient.context(maths=False): # still synchronised - self.assertFalse(_LENIENT.enable) - self.assertFalse(self.lenient["maths"]) + assert _LENIENT.enable is False + assert lenient["maths"] is False # still synchronised - self.assertTrue(_LENIENT.enable) - self.assertTrue(self.lenient["maths"]) + assert _LENIENT.enable is True + assert lenient["maths"] is True - def test_maths_disable__lenient_false(self): + def test_maths_disable__lenient_false(self, lenient): # not synchronised _LENIENT.__dict__["enable"] = False - self.assertFalse(_LENIENT.enable) - self.assertTrue(self.lenient["maths"]) + assert _LENIENT.enable is False + assert lenient["maths"] is True - with self.lenient.context(maths=False): + with lenient.context(maths=False): # now synchronised - self.assertFalse(_LENIENT.enable) - self.assertFalse(self.lenient["maths"]) + assert _LENIENT.enable is False + assert lenient["maths"] is False # still synchronised - self.assertTrue(_LENIENT.enable) - self.assertTrue(self.lenient["maths"]) + assert _LENIENT.enable is True + assert lenient["maths"] is True - def test_maths_enable__lenient_true(self): + def test_maths_enable__lenient_true(self, lenient): # not synchronised - self.assertTrue(_LENIENT.enable) - self.lenient.__dict__["maths"] = False - self.assertFalse(self.lenient["maths"]) + assert _LENIENT.enable is True + lenient.__dict__["maths"] = False + assert lenient["maths"] is False - with self.lenient.context(maths=True): + with lenient.context(maths=True): # now synchronised - self.assertTrue(_LENIENT.enable) - self.assertTrue(self.lenient["maths"]) + assert _LENIENT.enable is True + assert lenient["maths"] is True # still synchronised - self.assertFalse(_LENIENT.enable) - self.assertFalse(self.lenient["maths"]) + assert _LENIENT.enable is False + assert lenient["maths"] is False - def test_maths_enable__lenient_false(self): + def test_maths_enable__lenient_false(self, lenient): # synchronised _LENIENT.__dict__["enable"] = False - self.assertFalse(_LENIENT.enable) - self.lenient.__dict__["maths"] = False - self.assertFalse(self.lenient["maths"]) + assert _LENIENT.enable is False + lenient.__dict__["maths"] = False + assert lenient["maths"] is False - with self.lenient.context(maths=True): + with lenient.context(maths=True): # still synchronised - self.assertTrue(_LENIENT.enable) - self.assertTrue(self.lenient["maths"]) + assert _LENIENT.enable is True + assert lenient["maths"] is True # still synchronised - self.assertFalse(_LENIENT.enable) - self.assertFalse(self.lenient["maths"]) - - -if __name__ == "__main__": - tests.main() + assert _LENIENT.enable is False + assert lenient["maths"] is False From fb6b96bfa934ff979181bbc00c0bbb449910cfc9 Mon Sep 17 00:00:00 2001 From: Bill Little Date: Wed, 13 Mar 2024 13:18:28 +0000 Subject: [PATCH 2/3] assert order --- lib/iris/tests/unit/common/lenient/test_Lenient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/iris/tests/unit/common/lenient/test_Lenient.py b/lib/iris/tests/unit/common/lenient/test_Lenient.py index 57c1ecfaa0..6bf0aad2b7 100644 --- a/lib/iris/tests/unit/common/lenient/test_Lenient.py +++ b/lib/iris/tests/unit/common/lenient/test_Lenient.py @@ -23,12 +23,12 @@ def lenient(): class Test___init__: def test_default(self, lenient): expected = dict(maths=True) - assert expected == lenient.__dict__ + assert lenient.__dict__ == expected def test_kwargs(self, lenient): actual = Lenient(maths=False) expected = dict(maths=False) - assert expected, actual.__dict__ + assert actual.__dict__, expected def test_kwargs_invalid(self, lenient): emsg = "Invalid .* option, got 'merge'." @@ -57,7 +57,7 @@ def test_not_in(self, lenient): class Test___repr__: def test(self, lenient): expected = "Lenient(maths=True)" - assert expected == repr(lenient) + assert repr(lenient) == expected class Test___setitem__: From 8fc47eee21398602f3dc472a68bacc88ea23724a Mon Sep 17 00:00:00 2001 From: Bill Little Date: Wed, 13 Mar 2024 13:59:23 +0000 Subject: [PATCH 3/3] review actions --- .../tests/unit/common/lenient/test_Lenient.py | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/lib/iris/tests/unit/common/lenient/test_Lenient.py b/lib/iris/tests/unit/common/lenient/test_Lenient.py index 6bf0aad2b7..cbc1c8fe1f 100644 --- a/lib/iris/tests/unit/common/lenient/test_Lenient.py +++ b/lib/iris/tests/unit/common/lenient/test_Lenient.py @@ -28,7 +28,7 @@ def test_default(self, lenient): def test_kwargs(self, lenient): actual = Lenient(maths=False) expected = dict(maths=False) - assert actual.__dict__, expected + assert actual.__dict__ == expected def test_kwargs_invalid(self, lenient): emsg = "Invalid .* option, got 'merge'." @@ -46,7 +46,7 @@ def test_not_in(self, lenient): class Test___getitem__: def test_in(self, lenient): - assert lenient["maths"] is True + assert bool(lenient["maths"]) is True def test_not_in(self, lenient): emsg = "Invalid .* option, got 'MATHS'." @@ -73,97 +73,97 @@ def test_maths_value_invalid(self, mocker, lenient): lenient["maths"] = value def test_maths_disable__lenient_enable_true(self, lenient): - assert _LENIENT.enable is True + assert bool(_LENIENT.enable) is True lenient["maths"] = False - assert lenient.__dict__["maths"] is False - assert _LENIENT.enable is False + assert bool(lenient.__dict__["maths"]) is False + assert bool(_LENIENT.enable) is False def test_maths_disable__lenient_enable_false(self, lenient): _LENIENT.__dict__["enable"] = False - assert _LENIENT.enable is False + assert bool(_LENIENT.enable) is False lenient["maths"] = False - assert lenient.__dict__["maths"] is False - assert _LENIENT.enable is False + assert bool(lenient.__dict__["maths"]) is False + assert bool(_LENIENT.enable) is False def test_maths_enable__lenient_enable_true(self, lenient): - assert _LENIENT.enable is True + assert bool(_LENIENT.enable) is True lenient["maths"] = True - assert lenient.__dict__["maths"] is True - assert _LENIENT.enable is True + assert bool(lenient.__dict__["maths"]) is True + assert bool(_LENIENT.enable) is True def test_maths_enable__lenient_enable_false(self, lenient): _LENIENT.__dict__["enable"] = False - assert _LENIENT.enable is False + assert bool(_LENIENT.enable) is False lenient["maths"] = True - assert lenient.__dict__["maths"] is True - assert _LENIENT.enable is True + assert bool(lenient.__dict__["maths"]) is True + assert bool(_LENIENT.enable) is True class Test_context: def test_nop(self, lenient): - assert lenient["maths"] is True + assert bool(lenient["maths"]) is True with lenient.context(): - assert lenient["maths"] is True + assert bool(lenient["maths"]) is True - assert lenient["maths"] is True + assert bool(lenient["maths"]) is True def test_maths_disable__lenient_true(self, lenient): # synchronised - assert _LENIENT.enable is True - assert lenient["maths"] is True + assert bool(_LENIENT.enable) is True + assert bool(lenient["maths"]) is True with lenient.context(maths=False): # still synchronised - assert _LENIENT.enable is False - assert lenient["maths"] is False + assert bool(_LENIENT.enable) is False + assert bool(lenient["maths"]) is False # still synchronised - assert _LENIENT.enable is True - assert lenient["maths"] is True + assert bool(_LENIENT.enable) is True + assert bool(lenient["maths"]) is True def test_maths_disable__lenient_false(self, lenient): # not synchronised _LENIENT.__dict__["enable"] = False - assert _LENIENT.enable is False - assert lenient["maths"] is True + assert bool(_LENIENT.enable) is False + assert bool(lenient["maths"]) is True with lenient.context(maths=False): # now synchronised - assert _LENIENT.enable is False - assert lenient["maths"] is False + assert bool(_LENIENT.enable) is False + assert bool(lenient["maths"]) is False # still synchronised - assert _LENIENT.enable is True - assert lenient["maths"] is True + assert bool(_LENIENT.enable) is True + assert bool(lenient["maths"]) is True def test_maths_enable__lenient_true(self, lenient): # not synchronised - assert _LENIENT.enable is True + assert bool(_LENIENT.enable) is True lenient.__dict__["maths"] = False - assert lenient["maths"] is False + assert bool(lenient["maths"]) is False with lenient.context(maths=True): # now synchronised - assert _LENIENT.enable is True - assert lenient["maths"] is True + assert bool(_LENIENT.enable) is True + assert bool(lenient["maths"]) is True # still synchronised - assert _LENIENT.enable is False - assert lenient["maths"] is False + assert bool(_LENIENT.enable) is False + assert bool(lenient["maths"]) is False def test_maths_enable__lenient_false(self, lenient): # synchronised _LENIENT.__dict__["enable"] = False - assert _LENIENT.enable is False + assert bool(_LENIENT.enable) is False lenient.__dict__["maths"] = False - assert lenient["maths"] is False + assert bool(lenient["maths"]) is False with lenient.context(maths=True): # still synchronised - assert _LENIENT.enable is True - assert lenient["maths"] is True + assert bool(_LENIENT.enable) is True + assert bool(lenient["maths"]) is True # still synchronised - assert _LENIENT.enable is False - assert lenient["maths"] is False + assert bool(_LENIENT.enable) is False + assert bool(lenient["maths"]) is False