Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest migration for unit.common.mixin.test_LimitedAttributeDict #5851

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions lib/iris/tests/unit/common/mixin/test_LimitedAttributeDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,55 @@
# See LICENSE in the root of the repository for full licensing details.
"""Unit tests for the :class:`iris.common.mixin.LimitedAttributeDict`."""

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests # isort:skip

from unittest import mock

import numpy as np
import pytest

from iris.common.mixin import LimitedAttributeDict


class Test(tests.IrisTest):
def setUp(self):
class Test:
@pytest.fixture(autouse=True)
def _setup(self):
self.forbidden_keys = LimitedAttributeDict.CF_ATTRS_FORBIDDEN
self.emsg = "{!r} is not a permitted attribute"

def test__invalid_keys(self):
for key in self.forbidden_keys:
with self.assertRaisesRegex(ValueError, self.emsg.format(key)):
with pytest.raises(ValueError, match=self.emsg.format(key)):
_ = LimitedAttributeDict(**{key: None})

def test___eq__(self):
def test___eq__(self, mocker):
values = dict(
one=mock.sentinel.one,
two=mock.sentinel.two,
three=mock.sentinel.three,
one=mocker.sentinel.one,
two=mocker.sentinel.two,
three=mocker.sentinel.three,
)
left = LimitedAttributeDict(**values)
right = LimitedAttributeDict(**values)
self.assertEqual(left, right)
self.assertEqual(left, values)
assert left == right
assert left == values

def test___eq___numpy(self):
values = dict(one=np.arange(1), two=np.arange(2), three=np.arange(3))
left = LimitedAttributeDict(**values)
right = LimitedAttributeDict(**values)
self.assertEqual(left, right)
self.assertEqual(left, values)
assert left == right
assert left == values
values = dict(one=np.arange(1), two=np.arange(1), three=np.arange(1))
left = LimitedAttributeDict(dict(one=0, two=0, three=0))
right = LimitedAttributeDict(**values)
self.assertEqual(left, right)
self.assertEqual(left, values)
assert left == right
assert left == values

def test___setitem__(self):
for key in self.forbidden_keys:
item = LimitedAttributeDict()
with self.assertRaisesRegex(ValueError, self.emsg.format(key)):
with pytest.raises(ValueError, match=self.emsg.format(key)):
item[key] = None

def test_update(self):
for key in self.forbidden_keys:
item = LimitedAttributeDict()
with self.assertRaisesRegex(ValueError, self.emsg.format(key)):
other = {key: None}
other = {key: None}
with pytest.raises(ValueError, match=self.emsg.format(key)):
item.update(other)


if __name__ == "__main__":
tests.main()
Loading