示例#1
0
 def test_report_match_where_players_are_too_far_apart_when_distance_penalty_on(self):
     with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder(distance_penalty_on=True)):
         with patch.object(self.manager.dao, "get_player", side_effect=[
             fixtures.player(ranking=1),
             fixtures.player(ranking=17),
         ]):
             self.assert_error(lambda: self.manager.report_match(2, create_match_dict("TEST1", "TEST17", 6, 0, 6, 0)), 400, "Players are too far apart in the rankings to challenge one another")
示例#2
0
 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()
示例#3
0
 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)
示例#4
0
    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)
示例#5
0
 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()
示例#6
0
 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.")
示例#7
0
 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.")
示例#8
0
 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)
示例#9
0
 def test_add_player_to_ladder_with_a_valid_code_should_create_player_and_return_all_ladder_players(self):
     with patch.object(self.manager.dao, "get_ladder", return_value=fixtures.ladder()):
         with patch.object(self.manager.dao, "get_ladder_code", return_value="good"):
             with patch.object(self.manager.dao, "create_player") as create_player_mock:
                 with patch.object(self.manager.dao, "get_players", return_value=[fixtures.player()]) as get_players_mock:
                     players = self.manager.add_player_to_ladder(1, "good")
     create_player_mock.assert_called_once_with(1, self.manager.user.user_id)
     get_players_mock.assert_called_once_with(1)
     self.assertEqual(1, len(players))
示例#10
0
 def test_match_serialization_contract(self):
     with patch.object(self.handler.manager,
                       "get_ladders",
                       return_value=[
                           Match(
                               1, 2, datetime(2020, 1, 2, 3, 4,
                                              5), "winner_id", "loser_id",
                               6, 0, 5, 7, 10, 8, 24, 12,
                               fixtures.player(
                                   user_=fixtures.user(user_id="winner_id"),
                                   ladder_id=2),
                               fixtures.player(
                                   user_=fixtures.user(user_id="loser_id"),
                                   ladder_id=2))
                       ]):
         response = self.handler.handle(create_event("/ladders"))
     self.assertEqual(
         """[{"match_id": 1, "ladder_id": 2, "match_date": "2020-01-02T03:04:05Z", "winner_id": "winner_id", "loser_id": "loser_id", "winner_set1_score": 6, "loser_set1_score": 0, "winner_set2_score": 5, "loser_set2_score": 7, "winner_set3_score": 10, "loser_set3_score": 8, "winner_points": 24, "loser_points": 12, "winner": {"user": {"user_id": "winner_id", "name": "", "email": "", "phone_number": null, "photo_url": null, "availability_text": null, "admin": false}, "ladder_id": 2, "score": 0, "earned_points": 0, "borrowed_points": 0, "ranking": 0, "wins": 0, "losses": 0}, "loser": {"user": {"user_id": "loser_id", "name": "", "email": "", "phone_number": null, "photo_url": null, "availability_text": null, "admin": false}, "ladder_id": 2, "score": 0, "earned_points": 0, "borrowed_points": 0, "ranking": 0, "wins": 0, "losses": 0}}]""",
         response["body"])
示例#11
0
 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)
示例#12
0
 def test_report_match_with_a_winner_and_loser_not_in_the_specified_ladder(self):
     with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder()):
         with patch.object(self.manager.dao, "get_player", return_value=None):
             self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST0", "TEST1", 6, 0, 6, 0)), 400, "No user with id: 'TEST0'")
         with patch.object(self.manager.dao, "get_player", side_effect=[fixtures.player(), None]):
             self.assert_error(lambda: self.manager.report_match(1, create_match_dict("TEST1", "TEST0", 6, 0, 6, 0)), 400, "No user with id: 'TEST0'")
示例#13
0
 def test_update_player_with_new_borrowed_points_should_update_and_return_players(self):
     with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder()):
         with patch.object(self.manager.dao, "get_players", return_value=[fixtures.player(borrowed_points=4), fixtures.player(borrowed_points=8)]):
             with patch.object(self.manager.dao, "update_borrowed_points") as update_borrowed_points_mock:
                 self.manager.update_player("1", "2", {"borrowed_points": 8})
     update_borrowed_points_mock.assert_called_once_with("1", "2", 8)
示例#14
0
 def test_update_player_with_a_borrowed_points_value_that_doesnt_exist_on_another_player(self):
     with patch.object(self.manager.dao, "get_ladder", return_value=open_ladder()):
         with patch.object(self.manager.dao, "get_players", return_value=[fixtures.player(borrowed_points=4), fixtures.player(borrowed_points=8)]):
             self.assert_error(lambda: self.manager.update_player("1", "2", {"borrowed_points": 5}), 400, "You must assign a value that is already assigned to another player in the ladder")