def test(self): index1 = unittest.mock.Mock() index1.find = unittest.mock.Mock() index2 = TypeIgnoringIndex(index1, {EntityType.GPE, EntityType.LOC}) with unittest.mock.patch.object(index1, 'find') as mock: index2.find('test', EntityType.GPE, 1) self.assertEqual(mock.call_count, 2) mock.assert_any_call('test', EntityType.GPE, 1) mock.assert_any_call('test', EntityType.LOC, 1) mock.reset_mock() index2.find('test', EntityType.PER, 1) mock.assert_called_once_with('test', EntityType.PER, 1)
def test_assert_any_call_with_function_spec(self): def f(a, b, c, d=None): pass mock = Mock(spec=f) mock(1, b=2, c=3) mock(4, 5, c=6, d=7) mock.assert_any_call(1, 2, 3) mock.assert_any_call(a=1, b=2, c=3) mock.assert_any_call(4, 5, 6, 7) mock.assert_any_call(a=4, b=5, c=6, d=7) self.assertRaises(AssertionError, mock.assert_any_call, 1, b=3, c=2) with self.assertRaises(AssertionError) as cm: mock.assert_any_call(e=8) self.assertIsInstance(cm.exception.__cause__, TypeError)
def test_assert_any_call_with_function_spec(self): def f(a, b, c, d=None): pass mock = Mock(spec=f) mock(1, b=2, c=3) mock(4, 5, c=6, d=7) mock.assert_any_call(1, 2, 3) mock.assert_any_call(a=1, b=2, c=3) mock.assert_any_call(4, 5, 6, 7) mock.assert_any_call(a=4, b=5, c=6, d=7) self.assertRaises(AssertionError, mock.assert_any_call, 1, b=3, c=2) # Expected call doesn't match the spec's signature with self.assertRaises(AssertionError) as cm: mock.assert_any_call(e=8) self.assertIsInstance(cm.exception.__cause__, TypeError)
def test_assert_any_call(self): mock = Mock() mock(1, 2) mock(a=3) mock(1, b=6) mock.assert_any_call(1, 2) mock.assert_any_call(a=3) mock.assert_any_call(1, b=6) self.assertRaises(AssertionError, mock.assert_any_call) self.assertRaises(AssertionError, mock.assert_any_call, 1, 3) self.assertRaises(AssertionError, mock.assert_any_call, a=4)
def assert_called(mock, *args, **kwds): mock.assert_any_call(*args, encoding='utf8', **kwds)
def __inject_test_user(self, callback): with unittest.mock.patch('prog_code.util.user_util.get_user') as mock: mock.return_value = TEST_USER self.__callback_called = True callback() mock.assert_any_call(TEST_EMAIL)