Add unit-test assert to check dict is superset of dict

Several VPN unit tests need the ability to check that an expected
dict is found in the actual dict output (and that other elements are
ignored - like auth tokens).

Since this will be of use to other components, adding this to the
base test case.

Note: There is a similar assertDictContainsSubset(), however, it is
not supported in 2.6 and has been deprecated in 3.2. A different, and
hopefully clearer, name is used for this method.

Change-Id: I8becf9cfad966874f8463a9c835331f2f17d2ead
Closes-Bug: 1366797
This commit is contained in:
Paul Michali
2014-09-08 10:41:50 -04:00
parent 1ad3df313e
commit 8ed6fdb20a

View File

@@ -194,3 +194,23 @@ class BaseTestCase(testtools.TestCase):
elif isinstance(value, dict):
dic[key] = self.sort_dict_lists(value)
return dic
def assertDictSupersetOf(self, expected_subset, actual_superset):
"""Checks that actual dict contains the expected dict.
After checking that the arguments are of the right type, this checks
that each item in expected_subset is in, and matches, what is in
actual_superset. Separate tests are done, so that detailed info can
be reported upon failure.
"""
if not isinstance(expected_subset, dict):
self.fail("expected_subset (%s) is not an instance of dict" %
type(expected_subset))
if not isinstance(actual_superset, dict):
self.fail("actual_superset (%s) is not an instance of dict" %
type(actual_superset))
for k, v in expected_subset.items():
self.assertIn(k, actual_superset)
self.assertEqual(v, actual_superset[k],
"Key %(key)s expected: %(exp)r, actual %(act)r" %
{'key': k, 'exp': v, 'act': actual_superset[k]})