def test_run_side_effects(self, mock_func): def test_func1(): pass def test_func2(): pass registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 0) mock_func.reset_mock() registry.register_side_effect("foo", test_func1) registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 1) mock_func.reset_mock() registry.register_side_effect("foo", test_func2) registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 2) mock_func.reset_mock() with mock.patch("side_effects.settings.TEST_MODE", True): registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 0) with mock.patch("side_effects.settings.TEST_MODE", False): registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 2)
def test_run_side_effects__test_mode_fail(self): def test_func(): pass registry.register_side_effect("foo", test_func) self.assertRaises( registry.SideEffectsTestFailure, registry.run_side_effects, "foo" )
def test_disable_side_effects(self, events): # simple func that calls the side-effect 'foo' def test_func(): registry.run_side_effects("foo") registry.register_side_effect("foo", test_func) test_func() self.assertEqual(events, ["foo"]) test_func() self.assertEqual(events, ["foo", "foo"])
def test_register_side_effect(self): def test_func1(): pass def test_func2(): pass registry.register_side_effect("foo", test_func1) self.assertTrue(registry._registry.contains("foo", test_func1)) self.assertFalse(registry._registry.contains("foo", test_func2)) # try adding adding a duplicate registry.register_side_effect("foo", test_func1) self.assertTrue(registry._registry.contains("foo", test_func1))
def test_disable_side_effects(self, mock_func): """Side-effects can be temporarily disabled.""" def test_func(): pass registry._registry.clear() registry.register_side_effect("foo", test_func) registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 1) # shouldn't get another call inside the CM with registry.disable_side_effects() as events: registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 1) self.assertEqual(events, ["foo"]) # re-enabled registry.run_side_effects("foo") self.assertEqual(mock_func.call_count, 2)
def test_multiple_functions(self): def foo(): pass def bar(): pass def baz(arg1): pass registry._registry.clear() registry.register_side_effect("test", foo) registry.register_side_effect("test", bar) self.assertEqual(checks.check_function_signatures(None), []) registry.register_side_effect("test", baz) errors = checks.check_function_signatures(None) self.assertEqual(len(errors), 1) self.assertEqual(errors[0].id, checks.CHECK_ID_MULTIPLE_SIGNATURES)