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.metadata.test_NamedTupleMeta #5852

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
58 changes: 26 additions & 32 deletions lib/iris/tests/unit/common/metadata/test__NamedTupleMeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
# See LICENSE in the root of the repository for full licensing details.
"""Unit tests for the :class:`iris.common.metadata._NamedTupleMeta`."""

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

from abc import abstractmethod

import pytest

from iris.common.metadata import _NamedTupleMeta


class Test(tests.IrisTest):
class Test:
@staticmethod
def names(classes):
return [cls.__name__ for cls in classes]
Expand Down Expand Up @@ -42,11 +40,11 @@ def _members(self):
pass

expected = ["object"]
self.assertEqual(self.names(Metadata.__bases__), expected)
assert self.names(Metadata.__bases__) == expected
expected = ["Metadata", "object"]
self.assertEqual(self.names(Metadata.__mro__), expected)
assert self.names(Metadata.__mro__) == expected
emsg = "Can't instantiate abstract class .* with abstract method.* _members"
with self.assertRaisesRegex(TypeError, emsg):
with pytest.raises(TypeError, match=emsg):
_ = Metadata()

def test__no_bases_single_member(self):
Expand All @@ -56,15 +54,15 @@ class Metadata(metaclass=_NamedTupleMeta):
_members = member

expected = ["MetadataNamedtuple"]
self.assertEqual(self.names(Metadata.__bases__), expected)
assert self.names(Metadata.__bases__) == expected
expected = ["Metadata", "MetadataNamedtuple", "tuple", "object"]
self.assertEqual(self.names(Metadata.__mro__), expected)
assert self.names(Metadata.__mro__) == expected
emsg = self.emsg_generate(member)
with self.assertRaisesRegex(TypeError, emsg):
with pytest.raises(TypeError, match=emsg):
_ = Metadata()
metadata = Metadata(1)
self.assertEqual(metadata._fields, (member,))
self.assertEqual(metadata.arg_one, 1)
assert metadata._fields == (member,)
assert metadata.arg_one == 1

def test__no_bases_multiple_members(self):
members = ("arg_one", "arg_two")
Expand All @@ -73,17 +71,17 @@ class Metadata(metaclass=_NamedTupleMeta):
_members = members

expected = ["MetadataNamedtuple"]
self.assertEqual(self.names(Metadata.__bases__), expected)
assert self.names(Metadata.__bases__) == expected
expected = ["Metadata", "MetadataNamedtuple", "tuple", "object"]
self.assertEqual(self.names(Metadata.__mro__), expected)
assert self.names(Metadata.__mro__) == expected
emsg = self.emsg_generate(members)
with self.assertRaisesRegex(TypeError, emsg):
with pytest.raises(TypeError, match=emsg):
_ = Metadata()
values = range(len(members))
metadata = Metadata(*values)
self.assertEqual(metadata._fields, members)
assert metadata._fields == members
expected = dict(zip(members, values))
self.assertEqual(metadata._asdict(), expected)
assert metadata._asdict() == expected

def test__multiple_bases_multiple_members(self):
members_parent = ("arg_one", "arg_two")
Expand All @@ -97,26 +95,26 @@ class MetadataChild(MetadataParent):

# Check the parent class...
expected = ["MetadataParentNamedtuple"]
self.assertEqual(self.names(MetadataParent.__bases__), expected)
assert self.names(MetadataParent.__bases__) == expected
expected = [
"MetadataParent",
"MetadataParentNamedtuple",
"tuple",
"object",
]
self.assertEqual(self.names(MetadataParent.__mro__), expected)
assert self.names(MetadataParent.__mro__) == expected
emsg = self.emsg_generate(members_parent)
with self.assertRaisesRegex(TypeError, emsg):
with pytest.raises(TypeError, match=emsg):
_ = MetadataParent()
values_parent = range(len(members_parent))
metadata_parent = MetadataParent(*values_parent)
self.assertEqual(metadata_parent._fields, members_parent)
assert metadata_parent._fields == members_parent
expected = dict(zip(members_parent, values_parent))
self.assertEqual(metadata_parent._asdict(), expected)
assert metadata_parent._asdict() == expected

# Check the dependent child class...
expected = ["MetadataChildNamedtuple", "MetadataParent"]
self.assertEqual(self.names(MetadataChild.__bases__), expected)
assert self.names(MetadataChild.__bases__) == expected
expected = [
"MetadataChild",
"MetadataChildNamedtuple",
Expand All @@ -125,17 +123,13 @@ class MetadataChild(MetadataParent):
"tuple",
"object",
]
self.assertEqual(self.names(MetadataChild.__mro__), expected)
assert self.names(MetadataChild.__mro__) == expected
emsg = self.emsg_generate((*members_parent, *members_child))
with self.assertRaisesRegex(TypeError, emsg):
with pytest.raises(TypeError, match=emsg):
_ = MetadataChild()
fields_child = (*members_parent, *members_child)
values_child = range(len(fields_child))
metadata_child = MetadataChild(*values_child)
self.assertEqual(metadata_child._fields, fields_child)
assert metadata_child._fields == fields_child
expected = dict(zip(fields_child, values_child))
self.assertEqual(metadata_child._asdict(), expected)


if __name__ == "__main__":
tests.main()
assert metadata_child._asdict() == expected
Loading