Skip to content

Commit

Permalink
refactor: keep the unittest.mock names in the 'mock' namespace (#1379)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tonyandrewmeyer authored Sep 18, 2024
1 parent 483579a commit 84ad4b0
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'}
Expand All @@ -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):
Expand Down Expand Up @@ -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'}
Expand Down Expand Up @@ -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'}

Expand Down

0 comments on commit 84ad4b0

Please sign in to comment.