def testReturnValueForFollowingCallsIsCached(self): result = object() mock_fn = mock.Mock(side_effect=[result]) mock_fn.__name__ = compatibility.NativeStr("MockFunction") fn = utils.RunOnce(mock_fn) self.assertIs(fn(), result) self.assertIs(fn(), result)
def testDecoratedFunctionIsCalledAtLeastOnce(self): mock_fn = mock.Mock() mock_fn.__name__ = compatibility.NativeStr("MockFunction") fn = utils.RunOnce(mock_fn) mock_fn.assert_not_called() fn() mock_fn.assert_called_once()
def testExceptionsArePassedThrough(self): mock_fn = mock.Mock(side_effect=ValueError()) mock_fn.__name__ = compatibility.NativeStr("MockFunction") fn = utils.RunOnce(mock_fn) with self.assertRaises(ValueError): fn() with self.assertRaises(ValueError): fn()
def testDecoratedFunctionIsCalledAtMostOnce(self): mock_fn = mock.Mock(side_effect=[None, AssertionError()]) mock_fn.__name__ = compatibility.NativeStr("MockFunction") fn = utils.RunOnce(mock_fn) fn() fn() fn() mock_fn.assert_called_once()
def testWrapsFunctionProperly(self): mock_fn = mock.Mock() mock_fn.__name__ = compatibility.NativeStr("MockFunction") fn = utils.RunOnce(mock_fn) self.assertEqual(fn.__name__, compatibility.NativeStr("MockFunction"))
def testReturnValueIsPassedThrough(self): mock_fn = mock.Mock(return_value="bar") mock_fn.__name__ = compatibility.NativeStr("MockFunction") fn = utils.RunOnce(mock_fn) self.assertEqual("bar", fn())
def testArgumentsArePassedThrough(self): mock_fn = mock.Mock() mock_fn.__name__ = compatibility.NativeStr("MockFunction") fn = utils.RunOnce(mock_fn) fn(1, 2, foo="bar") mock_fn.assert_called_once_with(1, 2, foo="bar")