summaryrefslogtreecommitdiff
path: root/nova/test.py
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2015-06-10 12:48:15 +0100
committerDaniel P. Berrange <berrange@redhat.com>2015-06-17 12:16:43 +0100
commit1debf0b57203d631bec17cf70c31875076533b2d (patch)
treeef06a6192e23515f16ab4ee910cc59e776505f0f /nova/test.py
parenta96dc64afb52e070cef03f2b91f2bcb63560f095 (diff)
downloadnova-1debf0b57203d631bec17cf70c31875076533b2d.tar.gz
test: add MatchType helper class as equivalent of mox.IsA
The mox test library had a helper method mox.IsA(SomeType) which allowed unit tests to assert that a method call argument had a particular type. The mock test library has a mock.ANY helper which is often used, but nothing that allows a stricter checker for a specific type. This patch introduces a MatchType class which is similar to mock.ANY, but restricted to a single type. So instead of using lots of mock.ANY parameters mock_some_method.assert_called_once_with( "hello", mock.ANY, mock.ANY, "world", mock.ANY) It becomes possible to be stricter mock_some_method.assert_called_once_with( "hello", MatchType(objects.Instance), mock.ANY, "world", MatchType(objects.KeyPair)) Change-Id: I3a1ca33500ef8007b6c496e4e0917d7de07ac40a
Diffstat (limited to 'nova/test.py')
-rw-r--r--nova/test.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/nova/test.py b/nova/test.py
index 4399299eab..39f61728e6 100644
--- a/nova/test.py
+++ b/nova/test.py
@@ -389,3 +389,35 @@ class BaseHookTestCase(NoDBTestCase):
def assert_has_hook(self, expected_name, func):
self.assertTrue(hasattr(func, '__hook_name__'))
self.assertEqual(expected_name, func.__hook_name__)
+
+
+class MatchType(object):
+ """Matches any instance of a specified type
+
+ The MatchType class is a helper for use with the
+ mock.assert_called_with() method that lets you
+ assert that a particular parameter has a specific
+ data type. It enables strict check than the built
+ in mock.ANY helper, and is the equivalent of the
+ mox.IsA() function from the legacy mox library
+
+ Example usage could be:
+
+ mock_some_method.assert_called_once_with(
+ "hello",
+ MatchType(objects.Instance),
+ mock.ANY,
+ "world",
+ MatchType(objects.KeyPair))
+ """
+ def __init__(self, wanttype):
+ self.wanttype = wanttype
+
+ def __eq__(self, other):
+ return type(other) == self.wanttype
+
+ def __ne__(self, other):
+ return type(other) != self.wanttype
+
+ def __repr__(self):
+ return "<MatchType:" + str(self.wanttype) + ">"