From: Robert Collins Date: Thu, 27 Jun 2013 10:10:51 +0000 (+1200) Subject: Fixup assert_has_keys. X-Git-Tag: 2014.1~395^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=f4c6ef0ad659d414cad38e4cf5bf8321d7e05374;p=openstack-build%2Fheat-build.git Fixup assert_has_keys. 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 --- diff --git a/heat/tests/fakes.py b/heat/tests/fakes.py index 7e8d3154..154ccd01 100644 --- a/heat/tests/fakes.py +++ b/heat/tests/fakes.py @@ -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):