From 84ad4b06c3cc09a76bd5e25ed83f7a35e7c4ff01 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Wed, 18 Sep 2024 15:39:05 +1200 Subject: [PATCH] refactor: keep the unittest.mock names in the 'mock' namespace (#1379) No functional changes (including to the tests) - this just adjusts the imports so that the names from `unittest.mock` are imported into a `mock` namespace, rather than all into the module namespace. Primarily, this ensures that `typing.Any` and `unittest.mock.ANY` are clearly distinguished by more than case, but it's also generally tidier ("Namespaces are one honking great idea"). See discussion in #1373. --- test/test_model.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/test/test_model.py b/test/test_model.py index d9e8ac02c..0eadd4d2c 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -24,7 +24,7 @@ import unittest from collections import OrderedDict from textwrap import dedent -from unittest.mock import ANY, MagicMock, patch +from unittest import mock import pytest @@ -1114,9 +1114,9 @@ def test_run_error(self, fake_script: FakeScript): assert str(excinfo.value) == 'ERROR cannot get status\n' assert excinfo.value.args[0] == 'ERROR cannot get status\n' - @patch('grp.getgrgid') - @patch('pwd.getpwuid') - def test_push_path_unnamed(self, getpwuid: MagicMock, getgrgid: MagicMock): + @mock.patch('grp.getgrgid') + @mock.patch('pwd.getpwuid') + def test_push_path_unnamed(self, getpwuid: mock.MagicMock, getgrgid: mock.MagicMock): getpwuid.side_effect = KeyError getgrgid.side_effect = KeyError harness = ops.testing.Harness( @@ -3369,7 +3369,9 @@ def test_app_add_secret_simple(self, fake_script: FakeScript, model: ops.Model): assert secret.id == 'secret:123' assert secret.label is None - assert fake_script.calls(clear=True) == [['secret-add', '--owner', 'application', ANY]] + assert fake_script.calls(clear=True) == [ + ['secret-add', '--owner', 'application', mock.ANY] + ] assert fake_script.secrets() == {'foo': 'x'} def test_app_add_secret_args(self, fake_script: FakeScript, model: ops.Model): @@ -3400,8 +3402,8 @@ def test_app_add_secret_args(self, fake_script: FakeScript, model: ops.Model): 'hourly', '--owner', 'application', - ANY, - ANY, + mock.ANY, + mock.ANY, ] ] assert fake_script.secrets() == {'foo': 'x', 'bar': 'y'} @@ -3414,7 +3416,7 @@ def test_unit_add_secret_simple(self, fake_script: FakeScript, model: ops.Model) assert secret.id == 'secret:345' assert secret.label is None - assert fake_script.calls(clear=True) == [['secret-add', '--owner', 'unit', ANY]] + assert fake_script.calls(clear=True) == [['secret-add', '--owner', 'unit', mock.ANY]] assert fake_script.secrets() == {'foo': 'x'} def test_unit_add_secret_args(self, fake_script: FakeScript, model: ops.Model): @@ -3445,8 +3447,8 @@ def test_unit_add_secret_args(self, fake_script: FakeScript, model: ops.Model): 'yearly', '--owner', 'unit', - ANY, - ANY, + mock.ANY, + mock.ANY, ] ] assert fake_script.secrets() == {'foo': 'w', 'bar': 'z'} @@ -3776,9 +3778,9 @@ def test_set_content(self, model: ops.Model, fake_script: FakeScript): secret.set_content({'s': 't'}) # ensure it validates content (key too short) assert fake_script.calls(clear=True) == [ - ['secret-set', f'secret://{model._backend.model_uuid}/x', ANY], + ['secret-set', f'secret://{model._backend.model_uuid}/x', mock.ANY], ['secret-info-get', '--label', 'y', '--format=json'], - ['secret-set', f'secret://{model._backend.model_uuid}/z', ANY], + ['secret-set', f'secret://{model._backend.model_uuid}/z', mock.ANY], ] assert fake_script.secrets() == {'foo': 'bar', 'bar': 'foo'}