From 1debf0b57203d631bec17cf70c31875076533b2d Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 10 Jun 2015 12:48:15 +0100 Subject: [PATCH] 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 --- nova/test.py | 32 ++++++++++++++++++++++++++++++++ nova/tests/unit/test_test.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/nova/test.py b/nova/test.py index 4399299eabe7..39f61728e6aa 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 "" diff --git a/nova/tests/unit/test_test.py b/nova/tests/unit/test_test.py index d7f3ff99689f..dd235b165202 100644 --- a/nova/tests/unit/test_test.py +++ b/nova/tests/unit/test_test.py @@ -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")