Skip to content

Commit

Permalink
add BaseMetadata compare test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlittle committed Jun 15, 2020
1 parent c439579 commit 5d9a7b5
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 70 deletions.
13 changes: 12 additions & 1 deletion lib/iris/common/lenient.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def enable(self, state):
raise ValueError(emsg)
self.__dict__["enable"] = state

def register_client(self, func, services):
def register_client(self, func, services, append=False):
"""
Add the provided mapping of lenient client function/method to
required lenient service function/methods.
Expand All @@ -422,6 +422,12 @@ def register_client(self, func, services):
One or more service function/methods or fully qualified string names
of the required service function/method.
Kwargs:
* append (bool):
If True, append the lenient services to any pre-registered lenient
services for the provided lenient client. Default is False.
"""
func = qualname(func)
cls = self.__class__.__name__
Expand All @@ -437,6 +443,11 @@ def register_client(self, func, services):
emsg = f"Require at least one {cls!r} lenient client service."
raise ValueError(emsg)
services = tuple([qualname(service) for service in services])
if append:
# Service order is not significant, therefore there is no
# requirement to preserve it.
existing = self.__dict__[func] if func in self else ()
services = tuple(sorted(set(existing) | set(services)))
self.__dict__[func] = services

def register_service(self, func):
Expand Down
46 changes: 26 additions & 20 deletions lib/iris/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ def func(field):
value = getattr(self, field)
return value if value == getattr(other, field) else None

# Note that, we use "_fields" not "_members".
# Note that, for strict we use "_fields" not "_members".
values = [func(field) for field in self._fields]

return values

def _combine_lenient(self, other):
"""
Perform lenient metadata member combination.
Perform lenient combination of metadata members.
Args:
Expand Down Expand Up @@ -221,7 +221,7 @@ def _combine_lenient_attributes(left, right):

def _compare_lenient(self, other):
"""
Support lenient metadata equality for coordinates.
Perform lenient equality of metadata memberss.
Args:
Expand Down Expand Up @@ -262,14 +262,14 @@ def func(field):
result = None if left == right else (left, right)
return result

# Note that, we use "_fields" not "_members".
# Note that, for strict we use "_fields" not "_members".
values = [func(field) for field in self._fields]

return values

def _difference_lenient(self, other):
"""
Perform lenient metadata member difference.
Perform lenient difference of metadata members.
Args:
Expand Down Expand Up @@ -380,9 +380,11 @@ def combine(self, other, lenient=None):
values = self._combine(other)
else:
if lenient:
args, kwargs = (self.combine,), dict()
# Use qualname to disassociate from the instance bounded method.
args, kwargs = (qualname(self.combine),), dict()
else:
# Require to qualname the method to make it a hashable key.
# Use qualname to guarantee that the instance bounded method
# is a hashable key.
args, kwargs = (), {qualname(self.combine): False}

with LENIENT.context(*args, **kwargs):
Expand Down Expand Up @@ -426,9 +428,11 @@ def difference(self, other, lenient=None):
values = self._difference(other)
else:
if lenient:
args, kwargs = (self.difference,), dict()
# Use qualname to disassociate from the instance bounded method.
args, kwargs = (qualname(self.difference),), dict()
else:
# Require to qualname the method to make it a hashable key.
# Use qualname to guarantee that the instance bounded method
# is a hashable key.
args, kwargs = (), {qualname(self.difference): False}

with LENIENT.context(*args, **kwargs):
Expand Down Expand Up @@ -460,9 +464,11 @@ def equal(self, other, lenient=None):
result = self.__eq__(other)
else:
if lenient:
args, kwargs = (self.equal,), dict()
# Use qualname to disassociate from the instance bounded method.
args, kwargs = (qualname(self.equal),), dict()
else:
# Require to qualname the method to make it a hashable key.
# Use qualname to guarantee that the instance bounded method
# is a hashable key.
args, kwargs = (), {qualname(self.equal): False}

with LENIENT.context(*args, **kwargs):
Expand Down Expand Up @@ -580,7 +586,7 @@ def __eq__(self, other):

def _combine_lenient(self, other):
"""
Perform lenient metadata member combination for cell measures.
Perform lenient combination of metadata members for cell measures.
Args:
Expand All @@ -602,7 +608,7 @@ def _combine_lenient(self, other):

def _compare_lenient(self, other):
"""
Perform lenient metadata equality for cell measures.
Perform lenient equality of metadata members for cell measures.
Args:
Expand All @@ -624,7 +630,7 @@ def _compare_lenient(self, other):

def _difference_lenient(self, other):
"""
Perform lenient metadata member difference for cell measures.
Perform lenient difference of metadata members for cell measures.
Args:
Expand Down Expand Up @@ -681,7 +687,7 @@ def __eq__(self, other):

def _combine_lenient(self, other):
"""
Perform lenient metadata member combination for coordinates.
Perform lenient combination of metadata members for coordinates.
Args:
Expand All @@ -707,7 +713,7 @@ def func(field):

def _compare_lenient(self, other):
"""
Perform lenient metadata equality for coordinates.
Perform lenient equality of metadata members for coordinates.
Args:
Expand All @@ -732,7 +738,7 @@ def _compare_lenient(self, other):

def _difference_lenient(self, other):
"""
Perform lenient metadata member difference for coordinates.
Perform lenient differnece of metadata members for coordinates.
Args:
Expand Down Expand Up @@ -790,7 +796,7 @@ def __eq__(self, other):

def _combine_lenient(self, other):
"""
Perform lenient metadata member combination for cubes.
Perform lenient combination of metadata members for cubes.
Args:
Expand All @@ -815,7 +821,7 @@ def _combine_lenient(self, other):

def _compare_lenient(self, other):
"""
Perform lenient metadata equality for cubes.
Perform lenient equality of metadata members for cubes.
Args:
Expand All @@ -835,7 +841,7 @@ def _compare_lenient(self, other):

def _difference_lenient(self, other):
"""
Perform lenient metadata member difference for cubes.
Perform lenient difference of metadata members for cubes.
Args:
Expand Down
25 changes: 22 additions & 3 deletions lib/iris/tests/unit/common/lenient/test_Lenient.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,7 @@ def client():

def test_enable(self):
enable = "enable"
self.assertEqual(
self.lenient.__dict__[enable], LENIENT_ENABLE_DEFAULT
)
self.assertEqual(self.lenient.__dict__[enable], LENIENT_ENABLE_DEFAULT)
self.lenient[enable] = True
self.assertTrue(self.lenient.__dict__[enable])
self.lenient[enable] = False
Expand Down Expand Up @@ -666,6 +664,27 @@ def test_services_empty(self):
with self.assertRaisesRegex(ValueError, emsg):
self.lenient.register_client("client", ())

def test_services_overwrite(self):
client = "client"
services = ("service1", "service2")
self.lenient.__dict__[client] = services
self.assertEqual(self.lenient[client], services)
new_services = ("service3", "service4")
self.lenient.register_client(client, services=new_services)
self.assertEqual(self.lenient[client], new_services)

def test_services_append(self):
client = "client"
services = ("service1", "service2")
self.lenient.__dict__[client] = services
self.assertEqual(self.lenient[client], services)
new_services = ("service3", "service4")
self.lenient.register_client(
client, services=new_services, append=True
)
expected = set(services + new_services)
self.assertEqual(set(self.lenient[client]), expected)


class Test_register_service(tests.IrisTest):
def setUp(self):
Expand Down
Loading

0 comments on commit 5d9a7b5

Please sign in to comment.