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
This commit is contained in:
parent
a96dc64afb
commit
1debf0b572
32
nova/test.py
32
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) + ">"
|
||||
|
@ -94,3 +94,31 @@ class BadLogTestCase(test.TestCase):
|
||||
def test_bad_debug_log(self):
|
||||
self.assertRaises(KeyError,
|
||||
LOG.debug, "this is a misformated %(log)s", {'nothing': 'nothing'})
|
||||
|
||||
|
||||
class MatchTypeTestCase(test.TestCase):
|
||||
|
||||
def test_match_type_simple(self):
|
||||
matcher = test.MatchType(dict)
|
||||
|
||||
self.assertEqual(matcher, {})
|
||||
self.assertEqual(matcher, {"hello": "world"})
|
||||
self.assertEqual(matcher, {"hello": ["world"]})
|
||||
self.assertNotEqual(matcher, [])
|
||||
self.assertNotEqual(matcher, [{"hello": "world"}])
|
||||
self.assertNotEqual(matcher, 123)
|
||||
self.assertNotEqual(matcher, "foo")
|
||||
|
||||
def test_match_type_object(self):
|
||||
class Hello(object):
|
||||
pass
|
||||
|
||||
class World(object):
|
||||
pass
|
||||
|
||||
matcher = test.MatchType(Hello)
|
||||
|
||||
self.assertEqual(matcher, Hello())
|
||||
self.assertNotEqual(matcher, World())
|
||||
self.assertNotEqual(matcher, 123)
|
||||
self.assertNotEqual(matcher, "foo")
|
||||
|
Loading…
x
Reference in New Issue
Block a user