diff --git a/nova/test.py b/nova/test.py index 529cce5cb..8ef301682 100644 --- a/nova/test.py +++ b/nova/test.py @@ -454,3 +454,42 @@ class MatchType(object): def __repr__(self): return "" + + +class ContainKeyValue(object): + """Checks whether a key/value pair is in a dict parameter. + + The ContainKeyValue class is a helper for use with the + mock.assert_*() method that lets you assert that a particular + dict contain a key/value paire. It enables strict check than + the built in mock.ANY helper, and is the equivalent of the + mox.ContainsKeyValue() function from the legacy mox library + + Example usage could be: + + mock_some_method.assert_called_once_with( + "hello", + ContainKeyValue('foo', bar), + mock.ANY, + "world", + ContainKeyValue('hello', world)) + """ + def __init__(self, wantkey, wantvalue): + self.wantkey = wantkey + self.wantvalue = wantvalue + + def __eq__(self, other): + try: + return other[self.wantkey] == self.wantvalue + except (KeyError, TypeError): + return False + + def __ne__(self, other): + try: + return other[self.wantkey] != self.wantvalue + except (KeyError, TypeError): + return True + + def __repr__(self): + return ""