From c6c881160ce9502e0b77235022e7726688c45be7 Mon Sep 17 00:00:00 2001 From: Paul Michali Date: Mon, 8 Sep 2014 10:41:50 -0400 Subject: [PATCH] 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 --- neutron/tests/base.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index e34dcc38b..adda2a0bd 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -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]}) -- 2.45.2