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_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()
 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)
Beispiel #6
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"])
 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_update_user_specifying_all_info_should_only_update_info_that_can_be_updated(self):
        user = fixtures.user()
        with patch.object(self.manager.dao, "update_user") as update_user_mock:
            with patch.object(self.manager.dao, "get_user", return_value=user):
                returned_user = self.manager.update_user(self.manager.user.user_id, {"user_id": "bad", "name": "new name", "email": "new email", "phone_number": "new phone", "photo_url": "new url", "availability_text": "new availability"})
        self.assertEqual(user, returned_user)

        update_user_mock.assert_called_once()
        saved_user = update_user_mock.call_args.args[0]
        self.assertNotEqual("bad", saved_user.user_id)
        self.assertEqual("new name", saved_user.name)
        self.assertEqual("new email", saved_user.email)
        self.assertEqual("new phone", saved_user.phone_number)
        self.assertEqual("new url", saved_user.photo_url)
        self.assertEqual("new availability", saved_user.availability_text)
 def test_get_user_get_another_player_in_your_ladder(self):
     returned_user = fixtures.user()
     with patch.object(self.manager.dao, "in_same_ladder", return_value=True):
         with patch.object(self.manager.dao, "get_user", return_value=returned_user):
             user = self.manager.get_user("TEST1")
     self.assertEqual(user, returned_user)
 def test_get_user_get_yourself(self):
     returned_user = fixtures.user()
     with patch.object(self.manager.dao, "get_user", return_value=returned_user):
         user = self.manager.get_user(self.manager.user.user_id)
     self.assertEqual(user, returned_user)
 def test_validate_token_with_existing_user(self):
     with patch.object(self.manager.firebase_client, "get_firebase_user", return_value={"user_id": "FB_ID"}):
         existing_user = fixtures.user()
         with patch.object(self.manager.dao, "get_user", return_value=existing_user):
             self.manager.validate_token("")
     self.assertEqual(existing_user, self.manager.user)
 def test_delete_match_when_not_an_admin(self):
     self.manager.user = fixtures.user(admin=False)
     self.assert_error(lambda: self.manager.delete_match(0), 403, "Only admins can delete matches")
 def test_update_match_when_the_user_isnt_an_admin(self):
     self.manager.user = fixtures.user(admin=False)
     self.assert_error(lambda: self.manager.update_match_scores(0, {}), 403, "Only admins can update matches")
 def test_update_player_when_not_an_admin(self):
     self.manager.user = fixtures.user(admin=False)
     self.assert_error(lambda: self.manager.update_player(None, None, None), 403, "Only admins can update players")
 def setUp(self):
     self.manager = ManagerImpl(FirebaseClient(), Dao())
     self.manager.user = fixtures.user(user_id="USER1", admin=True)