]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Fixup assert_has_keys.
authorRobert Collins <rbtcollins@hp.com>
Thu, 27 Jun 2013 10:10:51 +0000 (22:10 +1200)
committerRobert Collins <rbtcollins@hp.com>
Fri, 28 Jun 2013 01:56:29 +0000 (13:56 +1200)
This test helper had awkward code that didn't support users in
debugging tests, as it didn't report enough information. Additionally
some style issues were present, which I fixed up at the same time.

Fix bug 1195239.

Change-Id: If07e54ae0f2db1152dfea2012ff39b0291955dd7

heat/tests/fakes.py

index 7e8d3154e11775dd898fb975f2cdf485e4a4eeda..154ccd0162c0b7eec83bf18dfb56e8f933d57092 100644 (file)
@@ -22,15 +22,26 @@ places where actual behavior differs from the spec.
 """
 
 
-def assert_has_keys(dict, required=[], optional=[]):
-    keys = dict.keys()
-    for k in required:
-        try:
-            assert k in keys
-        except AssertionError:
-            extra_keys = set(keys).difference(set(required + optional))
-            raise AssertionError("found unexpected keys: %s" %
-                                 list(extra_keys))
+def assert_has_keys(a_dict, required=(), optional=()):
+    """Raise an assertion if a_dict has the wrong keys.
+
+    :param a_dict: A dict to look for keys in.
+    :param required: An iterable of keys that must be present.
+    :param optional: An iterable of keys that may be present.
+
+    If any key from required is missing, an AssertionError will be raised.
+    If any key other than those from required + optional is present, an
+    AssertionError will be raised.
+    """
+    keys = set(a_dict.keys())
+    required = set(required)
+    optional = set(optional)
+    missing = required - keys
+    extra = keys - (required | optional)
+    if missing or extra:
+        raise AssertionError(
+            "Missing keys %r, with extra keys %r in %r" %
+            (missing, extra, keys))
 
 
 class FakeClient(object):