From 9f4e21b724e632159d484219b3ead9c3d8ab0198 Mon Sep 17 00:00:00 2001 From: Hieu LE Date: Fri, 17 Jun 2016 16:10:51 +0700 Subject: [PATCH] Remove mox from unit/compute/test_compute.py (8) Introduce test.ContainKeyValue for replacing mox.ContainKeyValue Stop using mox in test_compute.py in below test cases: - test_lifecycle_events - test_allow_confirm_resize_on_instance_in_deleting_task_state - test_default_block_device_names_empty_instance_root_dev - test_default_block_device_names_empty_root_device - test_default_block_device_names_no_root_device - test_vnc_console Partially implements: blueprint remove-mox-newton Change-Id: I22b97e05a130ac65bd4b7412aac1b5e95f41b614 --- nova/test.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) 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 ""