def test_report_match_when_the_two_have_already_played_the_max_number_of_times(self): test_match = fixtures.match(match_date=datetime.today() - timedelta(days=1), winner_id="TEST1", loser_id="TEST2") with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder()): with patch.object(self.manager.dao, "get_player", return_value=fixtures.player()): with patch.object(self.manager.dao, "get_matches", return_value=[test_match] * 5): self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST1", "TEST2", 6, 0, 6, 0)), 400, "Players have already played 5 times.") self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST2", "TEST1", 6, 0, 6, 0)), 400, "Players have already played 5 times.")
def test_report_match_when_each_player_has_already_played_a_match_that_day(self): with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder()): with patch.object(self.manager.dao, "get_player", return_value=fixtures.player()): with patch.object(self.manager.dao, "get_matches", return_value=[fixtures.match(match_date=datetime.now(tz=timezone("US/Mountain")), winner_id="TEST1", loser_id="TEST2")]): self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST1", "TEST3", 6, 0, 6, 0)), 400, "Reported winner has already played a match today. Only one match can be played each day.") self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST2", "TEST3", 6, 0, 6, 0)), 400, "Reported winner has already played a match today. Only one match can be played each day.") self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST3", "TEST1", 6, 0, 6, 0)), 400, "Reported loser has already played a match today. Only one match can be played each day.") self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST3", "TEST2", 6, 0, 6, 0)), 400, "Reported loser has already played a match today. Only one match can be played each day.")
def create_match(winner_set1_score, loser_set1_score, winner_set2_score, loser_set2_score, winner_set3_score=None, loser_set3_score=None): return fixtures.match( winner_set1_score=winner_set1_score, loser_set1_score=loser_set1_score, winner_set2_score=winner_set2_score, loser_set2_score=loser_set2_score, winner_set3_score=winner_set3_score, loser_set3_score=loser_set3_score )
def test_delete_match_valid_should_delete_match_and_reverse_players_earned_points(self): with patch.object(self.manager.dao, "get_match", return_value=fixtures.match(match_id=123, ladder_id=1, winner_id="TEST1", loser_id="TEST2", winner_points=33, loser_points=6)): with patch.object(self.manager.dao, "update_earned_points") as update_earned_points_mock: with patch.object(self.manager.dao, "delete_match") as delete_match_mock: self.manager.delete_match(1) # Test that match was deleted delete_match_mock.assert_called_once_with(1) # Test earned points updated self.assertEqual(2, update_earned_points_mock.call_count) update_earned_points_mock.assert_any_call(1, "TEST1", -33) update_earned_points_mock.assert_any_call(1, "TEST2", -6)
def test_report_match_when_you_have_played_your_opponent_one_less_than_the_max_number_of_times_should_create_match(self): test_match = fixtures.match(match_date=datetime.now() - timedelta(days=1), winner_id="TEST1", loser_id="TEST2") with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder()): with patch.object(self.manager.dao, "get_player", return_value=fixtures.player()): with patch.object(self.manager.dao, "get_matches", return_value=[test_match] * 4): with patch.object(self.manager.dao, "update_earned_points"): with patch.object(self.manager.dao, "create_match", return_value=fixtures.match(winner_id="TEST1", loser_id="TEST2")) as create_match_mock: with patch.object(self.manager.dao, "get_players", return_value=[ fixtures.player(user_=fixtures.user(user_id="TEST1", name="Player 1")), fixtures.player(user_=fixtures.user(user_id="TEST2", name="Player 2")), ]): self.manager.report_match(1, create_match_dict("TEST1", "TEST2", 6, 0, 6, 0)) create_match_mock.assert_called_once()
def test_get_matches_adds_players_to_all_matches(self): with patch.object(self.manager.dao, "get_matches", return_value=[fixtures.match(match_id=1, winner_id="TEST1", loser_id="TEST2")]): with patch.object(self.manager.dao, "get_players", return_value=[ fixtures.player(user_=fixtures.user(user_id="TEST1", name="Player 1")), fixtures.player(user_=fixtures.user(user_id="TEST2", name="Player 2")), ]): matches = self.manager.get_matches(1, "TEST1") self.assertIsNotNone(matches) self.assertEqual(1, len(matches)) self.assertEqual(1, matches[0].match_id) self.assertEqual("TEST1", matches[0].winner.user.user_id) self.assertEqual("Player 1", matches[0].winner.user.name) self.assertEqual("TEST2", matches[0].loser.user.user_id) self.assertEqual("Player 2", matches[0].loser.user.name)
def test_update_match_with_valid_match_should_update_match_with_new_points_as_well_as_players_points_and_return_updated_match(self): existing_match = fixtures.match( ladder_id=1, match_date=datetime(2020, 1, 2, 3, 4, 5), winner_id='TEST1', loser_id='TEST2', winner_set1_score=6, loser_set1_score=0, winner_set2_score=0, loser_set2_score=6, winner_set3_score=6, loser_set3_score=0, winner_points=33, loser_points=6 ) with patch.object(self.manager.dao, "get_match", return_value=existing_match): with patch.object(self.manager.dao, "update_match") as update_match_mock: with patch.object(self.manager.dao, "update_earned_points") as update_earned_points_mock: with patch.object(self.manager.dao, "get_players", return_value=[ fixtures.player(user_=fixtures.user(user_id="TEST1", name="Player 1")), fixtures.player(user_=fixtures.user(user_id="TEST2", name="Player 2")), ]): returned_match = self.manager.update_match_scores(1, create_match_dict('BAD1', 'BAD2', 6, 0, 0, 6, 6, 1, ladder_id=2)) # Test that returned value has winner/loser info self.assertEqual("Player 1", returned_match.winner.user.name) self.assertEqual("Player 2", returned_match.loser.user.name) # Test that the update was made update_match_mock.assert_called_once() updated_match = update_match_mock.call_args.args[0] # Test values that should have updated self.assertEqual(6, updated_match.winner_set1_score) self.assertEqual(0, updated_match.loser_set1_score) self.assertEqual(0, updated_match.winner_set2_score) self.assertEqual(6, updated_match.loser_set2_score) self.assertEqual(6, updated_match.winner_set3_score) self.assertEqual(1, updated_match.loser_set3_score) self.assertEqual(32, updated_match.winner_points) self.assertEqual(7, updated_match.loser_points) # Test values that shouldn't update self.assertEqual(1, updated_match.ladder_id) self.assertEqual(datetime(2020, 1, 2, 3, 4, 5), updated_match.match_date) self.assertEqual("TEST1", updated_match.winner_id) self.assertEqual("TEST2", updated_match.loser_id) # Test earned points updated self.assertEqual(2, update_earned_points_mock.call_count) update_earned_points_mock.assert_any_call(1, "TEST1", -1) update_earned_points_mock.assert_any_call(1, "TEST2", 1)
def test_update_match_when_the_winner_would_go_below_the_min_amount_should_stay_at_min_amount(self): existing_match = fixtures.match(ladder_id=1, winner_id='TEST1', loser_id='TEST2', winner_set1_score=6, loser_set1_score=0, winner_set2_score=6, loser_set2_score=0, winner_points=Match.MIN_WINNER_POINTS, loser_points=0) with patch.object(self.manager.dao, "get_match", return_value=existing_match): with patch.object(self.manager.dao, "update_match") as update_match_mock: with patch.object(self.manager.dao, "update_earned_points") as update_earned_points_mock: with patch.object(self.manager.dao, "get_players", return_value=[ fixtures.player(user_=fixtures.user(user_id="TEST1", name="Player 1")), fixtures.player(user_=fixtures.user(user_id="TEST2", name="Player 2")), ]): self.manager.update_match_scores(1, create_match_dict('BAD1', 'BAD2', 6, 0, 6, 1)) update_match_mock.assert_called_once() updated_match = update_match_mock.call_args.args[0] self.assertIsNotNone(updated_match) self.assertEqual(Match.MIN_WINNER_POINTS, updated_match.winner_points) # Can't go below the min amount, even though you're losing points self.assertEqual(1, updated_match.loser_points) self.assertEqual(2, update_earned_points_mock.call_count) update_earned_points_mock.assert_any_call(1, "TEST1", 0) update_earned_points_mock.assert_any_call(1, "TEST2", 1)
def test_report_match_valid_match_should_update_player_scores_and_create_match_with_new_date(self, _): with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder()): with patch.object(self.manager.dao, "get_match", return_value=fixtures.match()): with patch.object(self.manager.dao, "get_player", return_value=fixtures.player()): with patch.object(self.manager.dao, "get_matches", return_value=[]): with patch.object(self.manager.dao, "update_earned_points") as update_earned_points_mock: with patch.object(self.manager.dao, "create_match", return_value=fixtures.match(winner_id="TEST1", loser_id="TEST2")) as create_match_mock: with patch.object(self.manager.dao, "get_players", return_value=[ fixtures.player(user_=fixtures.user(user_id="TEST1")), fixtures.player(user_=fixtures.user(user_id="TEST2")) ]): match = self.manager.report_match(1, create_match_dict("TEST1", "TEST2", 6, 0, 6, 0)) self.assertIsNotNone(match) self.assertEqual(2, update_earned_points_mock.call_count) update_earned_points_mock.assert_any_call(1, "TEST1", 10) update_earned_points_mock.assert_any_call(1, "TEST2", 5) create_match_mock.assert_called_once() saved_match = create_match_mock.call_args.args[0] self.assertIsNotNone(saved_match) self.assertIsNotNone(saved_match.match_date)
def test_report_match_where_players_are_too_far_apart_when_distance_penalty_off_should_create_match(self): with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder(distance_penalty_on=False)): with patch.object(self.manager.dao, "get_player", side_effect=[ fixtures.player(ranking=1), fixtures.player(ranking=17), ]): with patch.object(self.manager.dao, "get_matches", return_value=[]): with patch.object(self.manager.dao, "update_earned_points"): with patch.object(self.manager.dao, "create_match", return_value=fixtures.match(winner_id="TEST1", loser_id="TEST2")) as create_match_mock: with patch.object(self.manager.dao, "get_players", return_value=[ fixtures.player(user_=fixtures.user(user_id="TEST1", name="Player 1")), fixtures.player(user_=fixtures.user(user_id="TEST2", name="Player 2")), ]): self.manager.report_match(1, create_match_dict("TEST1", "TEST17", 6, 0, 6, 0)) create_match_mock.assert_called_once()