]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add unit-test assert to check dict is superset of dict
authorPaul Michali <pcm@cisco.com>
Mon, 8 Sep 2014 14:41:50 +0000 (10:41 -0400)
committerPaul Michali <pcm@cisco.com>
Mon, 8 Sep 2014 17:12:24 +0000 (13:12 -0400)
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

index e34dcc38b776ed6b4ba5ca9aad869ee08d88d310..adda2a0bd81fa0ecb4ade23e3990f3d2f84d0a24 100644 (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]})