def test_when_call_then_raise_multiple(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then(TestException(sentinel.exception1))\ .then(TestException)\ .then(TestException(sentinel.exception3)) try: mock_fn(sentinel.arg1) except TestException as e: assert e.message == sentinel.exception1 else: assert False try: mock_fn(sentinel.arg1) except TestException as e: assert e.message == "" else: assert False try: mock_fn(sentinel.arg1) except TestException as e: assert e.message == sentinel.exception3 else: assert False with pytest.raises(TestException): mock_fn(sentinel.arg1)
def test_duplicate_called_with_statements_second_ignored(): mock = Mock() when(mock).called_with(100, 200).then("monkey") when(mock).called_with(100, 200).then("hello") assert 'monkey' == mock(100, 200) assert 'hello' != mock(100, 200)
def test_when_missing_unicode_case_py2(): mock_fn = Mock() when(mock_fn).called_with(u"hello \u2698").then(sentinel.result1) with pytest.raises(UnexpectedStubCall) as err: mock_fn(u"goodbye \u2698") assert str(err.value) == """Unexpected stub call:
def test_most_general_last(): mock = Mock() when(mock).called_with(100, 200).then("monkey") when(mock).called_with(100, Any()).then("hello") assert 'monkey' == mock(100, 200) assert 'hello' == mock(100, 300) assert 'hello' == mock(100, "monkey") assert 'hello' == mock(100, { "key" : 1000 })
def test_most_general_last(): mock = Mock() when(mock).called_with(100, 200).then("monkey") when(mock).called_with(100, Any()).then("hello") assert 'monkey' == mock(100, 200) assert 'hello' == mock(100, 300) assert 'hello' == mock(100, "monkey") assert 'hello' == mock(100, {"key": 1000})
def test_when_missing_case(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then(sentinel.result1) when(mock_fn).called_with(sentinel.arg2, sentinel.arg3).then(sentinel.result2) with pytest.raises(UnexpectedStubCall) as err: mock_fn(sentinel.arg4) assert str(err.value) == """Unexpected stub call:
def test_when_call_then_raise_single(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then(TestException(sentinel.exception1)) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1) assert str(err.value) == str(sentinel.exception1) with pytest.raises(TestException): mock_fn(sentinel.arg1)
def test_when_call_then_return_multiple(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then(sentinel.result1)\ .then(sentinel.result2)\ .then(sentinel.result3) assert mock_fn(sentinel.arg1) == sentinel.result1 assert mock_fn(sentinel.arg1) == sentinel.result2 assert mock_fn(sentinel.arg1) == sentinel.result3 assert mock_fn(sentinel.arg1) == sentinel.result3
def test_unsuccessful_conversation(): stranger = Mock(name="stranger") when(stranger.speakto).called_with("Hello.").then("go away") delegate = ConferenceDelegate() with patch("mocking.conferencedelegate4.tweet") as mock_tweet: delegate.smalltalk(stranger) assert stranger.speakto.called_once_with("hello") assert not mock_tweet.called
def test_when_call_then_raise_single(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then( TestException(sentinel.exception1)) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1) assert str(err.value) == str(sentinel.exception1) with pytest.raises(TestException): mock_fn(sentinel.arg1)
def test_successful_conversation(): stranger = Mock(name="stranger") when(stranger.speakto).called_with("Hello.").then("hi") when(stranger.speakto).called_with("Good conference?").then("not bad") when(stranger.speakto).called_with("What has been your favourite part?").then("a brilliant talk on mocks") when(stranger.speakto).called_with("Really, I didn't go to that.").then("shame, it was amazing") when(stranger.speakto).called_with("Nice chatting with you, gotta go.").then("laters") delegate = ConferenceDelegate() with patch("mocking.conferencedelegate4.tweet") as mock_tweet: delegate.smalltalk(stranger) mock_tweet.assert_called_once_with("Absolutely loved a brilliant talk on mocks")
def test_write_pandas_data_to_right_libraries(): self = create_autospec(TopLevelTickStore, _arctic_lib=MagicMock(), _collection=MagicMock()) self._collection.find.return_value = [{ 'library_name': sentinel.libname1, 'start': sentinel.st1, 'end': sentinel.end1 }, { 'library_name': sentinel.libname2, 'start': sentinel.st2, 'end': sentinel.end2 }] slice1 = range(2) slice2 = range(4) when(self._slice).called_with(sentinel.data, sentinel.st1, sentinel.end1).then(slice1) when(self._slice).called_with(sentinel.data, sentinel.st2, sentinel.end2).then(slice2) mock_lib1 = Mock() mock_lib2 = Mock() when(self._arctic_lib.arctic.__getitem__).called_with( sentinel.libname1).then(mock_lib1) when(self._arctic_lib.arctic.__getitem__).called_with( sentinel.libname2).then(mock_lib2) with patch("arctic.tickstore.toplevel.to_dt") as patch_to_dt: patch_to_dt.side_effect = [ sentinel.st1, sentinel.end1, sentinel.st2, sentinel.end2 ] TopLevelTickStore.write(self, 'blah', sentinel.data) mock_lib1.write.assert_called_once_with('blah', slice1) mock_lib2.write.assert_called_once_with('blah', slice2)
def test_when_call_then_mixed(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then(sentinel.result1)\ .then(TestException(sentinel.exception2))\ .then(TestException)\ .then(sentinel.result3) assert mock_fn(sentinel.arg1) == sentinel.result1 with pytest.raises(TestException): mock_fn(sentinel.arg1) with pytest.raises(TestException): mock_fn(sentinel.arg1) assert mock_fn(sentinel.arg1) == sentinel.result3 assert mock_fn(sentinel.arg1) == sentinel.result3
def test_successful_conversation(): stranger = Mock(name="stranger") when(stranger.speakto).called_with("Hello.").then("hi") when(stranger.speakto).called_with("Good conference?").then("not bad") when(stranger.speakto).called_with( "What has been your favourite part?").then("a brilliant talk on mocks") when(stranger.speakto).called_with("Really, I didn't go to that.").then( "shame, it was amazing") when(stranger.speakto).called_with( "Nice chatting with you, gotta go.").then("laters") delegate = ConferenceDelegate() with patch("mocking.conferencedelegate4.tweet") as mock_tweet: delegate.smalltalk(stranger) mock_tweet.assert_called_once_with( "Absolutely loved a brilliant talk on mocks")
def test_write_pandas_data_to_right_libraries(): self = create_autospec(TopLevelTickStore, _arctic_lib=MagicMock(), _collection=MagicMock()) self._collection.find.return_value = [{'library_name': sentinel.libname1, 'start': sentinel.st1, 'end': sentinel.end1}, {'library_name': sentinel.libname2, 'start': sentinel.st2, 'end': sentinel.end2}] slice1 = range(2) slice2 = range(4) when(self._slice).called_with(sentinel.data, sentinel.st1, sentinel.end1).then(slice1) when(self._slice).called_with(sentinel.data, sentinel.st2, sentinel.end2).then(slice2) mock_lib1 = Mock() mock_lib2 = Mock() when(self._arctic_lib.arctic.__getitem__).called_with(sentinel.libname1).then(mock_lib1) when(self._arctic_lib.arctic.__getitem__).called_with(sentinel.libname2).then(mock_lib2) TopLevelTickStore.write(self, 'blah', sentinel.data) mock_lib1.write.assert_called_once_with('blah', slice1) mock_lib2.write.assert_called_once_with('blah', slice2)
def test_when_with_any(): mock_fn = Mock() when(mock_fn).called_with(Any()).then(sentinel.result1) when(mock_fn).called_with(sentinel.arg1, Any()).then(sentinel.result2) when(mock_fn).called_with(sentinel.arg2, Any(list)).then(sentinel.result3) when(mock_fn).called_with(sentinel.arg2, Any(str)).then(sentinel.result4) assert mock_fn(sentinel.arg1) == sentinel.result1 assert mock_fn(sentinel.arg2) == sentinel.result1 assert mock_fn("hello") == sentinel.result1 assert mock_fn(100) == sentinel.result1 assert mock_fn(sentinel.arg1, "hello") == sentinel.result2 assert mock_fn(sentinel.arg1, "world") == sentinel.result2 assert mock_fn(sentinel.arg2, []) == sentinel.result3 assert mock_fn(sentinel.arg2, [1, 2, 3]) == sentinel.result3 assert mock_fn(sentinel.arg2, ["hello", "world"]) == sentinel.result3 assert mock_fn(sentinel.arg2, "world") == sentinel.result4
def test_can_not_use_when_with_mock_that_has_already_got_a_side_effect(): with pytest.raises(RuntimeError): when(Mock(side_effect=lambda _: 10)) with pytest.raises(RuntimeError): when(Mock(side_effect=[1, 2, 3, 4]))
def test_can_not_use_when_with_non_mock(): mock_fn = lambda _: 10 with pytest.raises(RuntimeError): when(mock_fn)
def test_when_with_mock(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg).then(sentinel.result) assert mock_fn(sentinel.arg) == sentinel.result
def test_called_with_object_has_empty_string_representation(): mock_fn = MagicMock() assert repr(when(mock_fn).called_with(sentinel.arg)) == ""
def test_when_missing_case(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then(sentinel.result1) with pytest.raises(KeyError): mock_fn(sentinel.arg2)
def test_when_call_then_raise(): mock_fn = Mock() when(mock_fn).called_with().then(TestException(sentinel.exception0)) when(mock_fn).called_with(sentinel.arg1).then(TestException(sentinel.exception1)) when(mock_fn).called_with(sentinel.arg2).then(TestException(sentinel.exception2)) when(mock_fn).called_with(sentinel.arg1, sentinel.arg2).then(TestException(sentinel.exception3)) when(mock_fn).called_with(sentinel.arg1, sentinel.arg1).then(TestException(sentinel.exception4)) when(mock_fn).called_with(sentinel.arg1, other=sentinel.other).then(TestException(sentinel.exception5)) when(mock_fn).called_with(x=sentinel.x, y=sentinel.y).then(TestException(sentinel.exception6)) with pytest.raises(TestException) as err: mock_fn() assert str(err.value) == str(sentinel.exception0) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1) assert str(err.value) == str(sentinel.exception1) with pytest.raises(TestException) as err: mock_fn(sentinel.arg2) assert str(err.value) == str(sentinel.exception2) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1, sentinel.arg2) assert str(err.value) == str(sentinel.exception3) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1, sentinel.arg1) assert str(err.value) == str(sentinel.exception4) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1, other=sentinel.other) assert str(err.value) == str(sentinel.exception5) with pytest.raises(TestException) as err: mock_fn(x=sentinel.x, y=sentinel.y) assert str(err.value) == str(sentinel.exception6)
def test_max_limit_bmi(self): player = PLAYER_170_76 when(self.height_entity.get_height_in_meters).called_with(player).then(ONE_POINT_SEVENTY_FOUR) when(self.weight_entity.get_weight).called_with(player).then(SEVENTY_EIGHT_POINT_SEVENTY_ONE_KILOS) when(self.bmi_entity.calculate_bmi).called_with(player).then(BMI_TWENTY_SIX) self.assertEqual("Player was successfully selected", self.select_player.select_player(player))
def test_low_bmi(self): player = PLAYER_170_57 when(self.height_entity.get_height_in_meters).called_with(player).then(ONE_POINT_SEVEN_METERS) when(self.weight_entity.get_weight).called_with(player).then(FIFTY_SEVEN_KILOS) when(self.bmi_entity.calculate_bmi).called_with(player).then(BMI_NINETEEN_POINT_SEVENTY_TWO) self.assertEqual("Player's BMI is too low (19.72)", self.select_player.select_player(player))
def test_player_too_heavy(self): player = PLAYER_170_131 when(self.height_entity.get_height_in_meters).called_with(player).then(ONE_POINT_SEVEN_METERS) when(self.weight_entity.get_weight).called_with(player).then(ONE_HUNDRED_THIRTY_ONE) when(self.bmi_entity.calculate_bmi).called_with(player).then(45.33) self.assertEqual("Player is too heavy (131)", self.select_player.select_player(player))
def test_calculates_bmi(self): player = PLAYER_170_75 when(self.height_entity.get_height_in_meters).called_with(player).then(ONE_POINT_SEVEN_METERS) when(self.weight_entity.get_weight).called_with(player).then(SEVENTY_FIVE_KILOS) self.assertEqual(25.95, self.bmi_entity.calculate_bmi(player))
def test_can_not_use_when_with_non_mock(): mock_fn = lambda _ : 10 with pytest.raises(RuntimeError): when(mock_fn)
def test_when_call_then_return(): mock_fn = Mock() when(mock_fn).called_with().then(sentinel.result0) when(mock_fn).called_with(sentinel.arg1).then(sentinel.result1) when(mock_fn).called_with(sentinel.arg2).then(sentinel.result2) when(mock_fn).called_with(sentinel.arg1, sentinel.arg2).then(sentinel.result3) when(mock_fn).called_with(sentinel.arg1, sentinel.arg1).then(sentinel.result4) when(mock_fn).called_with(sentinel.arg1, other=sentinel.other).then(sentinel.result5) when(mock_fn).called_with(x=sentinel.x, y=sentinel.y).then(sentinel.result6) assert mock_fn() == sentinel.result0 assert mock_fn(sentinel.arg1) == sentinel.result1 assert mock_fn(sentinel.arg2) == sentinel.result2 assert mock_fn(sentinel.arg1, sentinel.arg2) == sentinel.result3 assert mock_fn(sentinel.arg1, sentinel.arg1) == sentinel.result4 assert mock_fn(sentinel.arg1, other=sentinel.other) == sentinel.result5 assert mock_fn(x=sentinel.x, y=sentinel.y) == sentinel.result6
def test_when_call_then__return_single(): mock_fn = Mock() when(mock_fn).called_with(sentinel.arg1).then(sentinel.result1) assert mock_fn(sentinel.arg1) == sentinel.result1 assert mock_fn(sentinel.arg1) == sentinel.result1
def test_player_too_short(self): player = PLAYER_160_75 when(self.height_entity.get_height_in_meters).called_with(player).then(ONE_POINT_SIX_METERS) self.assertEqual("Player is too short (1.6)", self.select_player.select_player(player))
def test_player_too_heavy_limit(self): player = PLAYER_170_130 when(self.height_entity.get_height_in_meters).called_with(player).then(TWO_POINT_TWENTY_FOUR) when(self.weight_entity.get_weight).called_with(player).then(ONE_HUNDRED_THIRTY_KILOS) when(self.bmi_entity.calculate_bmi).called_with(player).then(BMI_TWENTY_FIVE_POINT_NINETY_ONE) self.assertEqual("Player was successfully selected", self.select_player.select_player(player))
def test_can_not_use_when_with_mock_that_has_already_got_a_side_effect(): with pytest.raises(RuntimeError): when(Mock(side_effect=lambda _ : 10)) with pytest.raises(RuntimeError): when(Mock(side_effect=[1, 2, 3, 4]))
def test_high_bmi(self): player = PLAYER_170_76 when(self.height_entity.get_height_in_meters).called_with(player).then(ONE_POINT_SEVEN_METERS) when(self.weight_entity.get_weight).called_with(player).then(SEVENTY_SIX_KILOS) when(self.bmi_entity.calculate_bmi).called_with(player).then(BMI_TWENTY_SIX_POINT_THREE) self.assertEqual("Player's BMI is too high (26.3)", self.select_player.select_player(player))
def test_selected_player(self): player = PLAYER_170_72 when(self.height_entity.get_height_in_meters).called_with(player).then(ONE_POINT_SEVEN_METERS) when(self.weight_entity.get_weight).called_with(player).then(SEVENTY_TWO_KILOS) when(self.bmi_entity.calculate_bmi).called_with(player).then(BMI_TWENTY_FOUR_POINT_NINETY_ONE) self.assertEqual("Player was successfully selected", self.select_player.select_player(player))
def test_when_call_then_raise(): mock_fn = Mock() when(mock_fn).called_with().then(TestException(sentinel.exception0)) when(mock_fn).called_with(sentinel.arg1).then( TestException(sentinel.exception1)) when(mock_fn).called_with(sentinel.arg2).then( TestException(sentinel.exception2)) when(mock_fn).called_with(sentinel.arg1, sentinel.arg2).then( TestException(sentinel.exception3)) when(mock_fn).called_with(sentinel.arg1, sentinel.arg1).then( TestException(sentinel.exception4)) when(mock_fn).called_with(sentinel.arg1, other=sentinel.other).then( TestException(sentinel.exception5)) when(mock_fn).called_with(x=sentinel.x, y=sentinel.y).then( TestException(sentinel.exception6)) with pytest.raises(TestException) as err: mock_fn() assert str(err.value) == str(sentinel.exception0) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1) assert str(err.value) == str(sentinel.exception1) with pytest.raises(TestException) as err: mock_fn(sentinel.arg2) assert str(err.value) == str(sentinel.exception2) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1, sentinel.arg2) assert str(err.value) == str(sentinel.exception3) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1, sentinel.arg1) assert str(err.value) == str(sentinel.exception4) with pytest.raises(TestException) as err: mock_fn(sentinel.arg1, other=sentinel.other) assert str(err.value) == str(sentinel.exception5) with pytest.raises(TestException) as err: mock_fn(x=sentinel.x, y=sentinel.y) assert str(err.value) == str(sentinel.exception6)
def test_when_call_then_raise(): mock_fn = Mock() when(mock_fn).called_with().then(TestException(sentinel.exception0)) when(mock_fn).called_with(sentinel.arg1).then(TestException(sentinel.exception1)) when(mock_fn).called_with(sentinel.arg2).then(TestException(sentinel.exception2)) when(mock_fn).called_with(sentinel.arg1, sentinel.arg2).then(TestException(sentinel.exception3)) when(mock_fn).called_with(sentinel.arg1, sentinel.arg1).then(TestException(sentinel.exception4)) when(mock_fn).called_with(sentinel.arg1, other=sentinel.other).then(TestException(sentinel.exception5)) when(mock_fn).called_with(x=sentinel.x, y=sentinel.y).then(TestException(sentinel.exception6)) try: mock_fn() except TestException as e: assert e.message == sentinel.exception0 else: assert False try: mock_fn(sentinel.arg1) except TestException as e: assert e.message == sentinel.exception1 else: assert False try: mock_fn(sentinel.arg2) except TestException as e: assert e.message == sentinel.exception2 else: assert False try: mock_fn(sentinel.arg1, sentinel.arg2) except TestException as e: assert e.message == sentinel.exception3 else: assert False try: mock_fn(sentinel.arg1, sentinel.arg1) except TestException as e: assert e.message == sentinel.exception4 else: assert False try: mock_fn(sentinel.arg1, other=sentinel.other) except TestException as e: assert e.message == sentinel.exception5 else: assert False try: mock_fn(x=sentinel.x, y=sentinel.y) except TestException as e: assert e.message == sentinel.exception6 else: assert False