Beispiel #1
0
    def test_correct_season_used(self):
        """
        Assert the logic which assigns a season FK based on the date values
        provided is correct.
        """
        today = timezone.now().date()

        old_season = SeasonFactory(
            start_date=today - timedelta(days=3),
            end_date=today - timedelta(days=2),
            active=False,
        )
        current_season = SeasonFactory(
            start_date=today - timedelta(days=1),
            end_date=today + timedelta(days=2),
            active=True,
        )
        forthcoming_season = SeasonFactory(
            start_date=today + timedelta(days=3),
            end_date=today + timedelta(days=4),
            active=False,
        )

        # match uses timezone.now for the date field default
        with self.assertRaises(ValidationError):
            match = MatchFactory(season=old_season)

        with self.assertRaises(ValidationError):
            match = MatchFactory(season=forthcoming_season)

        match = MatchFactory(season=current_season)
        self.assertEqual(match.season, current_season)
Beispiel #2
0
    def test_migration_task(self):
        """Tests for the task which generates all historic Elo History instances."""
        season_one, season_two = [SeasonFactory() for x in xrange(2)]
        player_one, player_two = [
            PlayerFactory(
                season_elo = 1000,
                season_win_count = 0,
                season_loss_count = 0,
                season_grannies_given_count = 0,
                season_grannies_taken_count = 0,
            ) for x in xrange(2)
        ]

        # game 1 in season 1
        player_one_match_one_elo, player_two_match_one_elo = calculate_elo(player_one.season_elo, player_two.season_elo)
        match_one = MatchFactory(winner=player_one, loser=player_two, season=season_one, date=season_one.start_date + timedelta(days=1))
        player_one.refresh_from_db()
        player_two.refresh_from_db()

        # game 2 in season 2
        player_one_match_two_elo, player_two_match_two_elo = calculate_elo(player_one.season_elo, player_two.season_elo)
        match_two = MatchFactory(winner=player_one, loser=player_two, season=season_one, date=season_one.start_date + timedelta(days=2))
        player_one.refresh_from_db()
        player_two.refresh_from_db()

        # game 3 in season 1
        player_one.reset_season_fields()

        player_two.reset_season_fields()

        player_one_match_three_elo, player_two_match_three_elo = calculate_elo(player_one.season_elo, player_two.season_elo)
        match_three = MatchFactory(winner=player_one, loser=player_two, season=season_two, date=season_two.start_date + timedelta(days=1))
        player_one.refresh_from_db()
        player_two.refresh_from_db()

        # delete all the elo history instances created in the post save signal
        EloHistory.objects.all().delete()

        # run the task
        elo_history_migration()

        # three games, two elo history for each player
        self.assertEqual(EloHistory.objects.count(), 6)

        match_one_history = EloHistory.objects.get(match=match_one, player=player_one)
        self.assertEqual(match_one_history.elo_score, player_one_match_one_elo)
        match_one_history = EloHistory.objects.get(match=match_one, player=player_two)
        self.assertEqual(match_one_history.elo_score, player_two_match_one_elo)

        match_two_history = EloHistory.objects.get(match=match_two, player=player_one)
        self.assertEqual(match_two_history.elo_score, player_one_match_two_elo)
        match_two_history = EloHistory.objects.get(match=match_two, player=player_two)
        self.assertEqual(match_two_history.elo_score, player_two_match_two_elo)

        match_three_history = EloHistory.objects.get(match=match_three, player=player_one)
        self.assertEqual(match_three_history.elo_score, player_one_match_three_elo)
        match_three_history = EloHistory.objects.get(match=match_three, player=player_two)
        self.assertEqual(match_three_history.elo_score, player_two_match_three_elo)
    def test_season_player_created(self):
        season = SeasonFactory(active=True)

        player_1 = PlayerFactory(active=True)
        player_2 = PlayerFactory(active=True)

        match = MatchFactory(date=season.start_date,
                             winner=player_1,
                             loser=player_2,
                             season=season,
                             granny=True)

        self.process_task_queues()

        player_1.refresh_from_db()
        player_season_1 = SeasonPlayer.objects.get(player=player_1)

        self.assertEqual(player_season_1.season, season)
        self.assertEqual(player_1.season_elo, player_season_1.elo_score)
        self.assertEqual(player_1.season_win_count, player_season_1.win_count)
        self.assertEqual(player_1.season_loss_count,
                         player_season_1.loss_count)

        player_2.refresh_from_db()
        player_season_2 = SeasonPlayer.objects.get(player=player_2)

        self.assertEqual(player_season_2.season, season)
        self.assertEqual(player_2.season_elo, player_season_2.elo_score)
        self.assertEqual(player_2.season_win_count, player_season_2.win_count)
        self.assertEqual(player_2.season_loss_count,
                         player_season_2.loss_count)
    def test_update_elo_ratings(self):
        """Tests for the `update_elo_ratings` reciever."""
        season = SeasonFactory(active=True)

        player_1 = PlayerFactory(active=True)
        player_2 = PlayerFactory(active=True)

        p1_cached_total_elo = player_1.total_elo
        p1_cached_season_elo = player_1.season_elo
        p2_cached_total_elo = player_2.total_elo
        p2_cached_season_elo = player_2.season_elo

        match = MatchFactory(date=season.start_date,
                             winner=player_1,
                             loser=player_2,
                             season=season)

        player_1.refresh_from_db()
        player_2.refresh_from_db()

        self.assertNotEqual(player_1.total_elo, p1_cached_total_elo)
        self.assertNotEqual(player_1.season_elo, p1_cached_season_elo)
        self.assertEqual(player_1.total_elo, player_1.season_elo)

        self.assertNotEqual(player_2.total_elo, p2_cached_total_elo)
        self.assertNotEqual(player_2.season_elo, p2_cached_season_elo)
        self.assertEqual(player_2.total_elo, player_2.season_elo)
    def test_grannies_recorded(self):
        season = SeasonFactory(active=True)

        player_1 = PlayerFactory(active=True)
        player_2 = PlayerFactory(active=True)

        match = MatchFactory(date=season.start_date,
                             winner=player_1,
                             loser=player_2,
                             season=season,
                             granny=True)

        player_1.refresh_from_db()
        self.assertEqual(player_1.total_grannies_given_count, 1)
        self.assertEqual(player_1.season_grannies_given_count, 1)

        player_2.refresh_from_db()
        self.assertEqual(player_2.total_grannies_taken_count, 1)
        self.assertEqual(player_2.season_grannies_taken_count, 1)
    def test_elo_history_created(self):
        season = SeasonFactory(active=True)

        player_1 = PlayerFactory(active=True)
        player_2 = PlayerFactory(active=True)

        match = MatchFactory(date=season.start_date,
                             winner=player_1,
                             loser=player_2,
                             season=season,
                             granny=True)

        player_1.refresh_from_db()
        player1_elo_history = EloHistory.objects.get(player=player_1)
        self.assertEqual(match, player1_elo_history.match)
        self.assertEqual(season, player1_elo_history.season)
        self.assertEqual(player_1.season_elo, player1_elo_history.elo_score)

        player_1.refresh_from_db()
        player2_elo_history = EloHistory.objects.get(player=player_2)
        self.assertEqual(match, player2_elo_history.match)
        self.assertEqual(season, player2_elo_history.season)
        self.assertEqual(player_2.season_elo, player2_elo_history.elo_score)
    def test_delete(self):
        """Assert that all of the denormalized fields are reset correctly."""
        season = SeasonFactory(active=True)

        player_1 = PlayerFactory(active=True)
        player_2 = PlayerFactory(active=True)

        match_one = MatchFactory(
            date=season.start_date,
            winner=player_1,
            loser=player_2,
            season=season,
            granny=False
        )

        self.process_task_queues()

        # elo points at this point were...
        player_1.refresh_from_db()
        player_2.refresh_from_db()
        winner_elo_original = player_1.total_elo
        loser_elo_original = player_2.total_elo

        # now lets record an incorrect game
        match_two = MatchFactory(
            date=season.start_date + timedelta(days=1),
            winner=player_1,
            loser=player_2,
            season=season,
            granny=False
        )

        self.process_task_queues()

        # find out how many elo points were wrongly won/lost
        player_1.refresh_from_db()
        player_2.refresh_from_db()

        winner_elo_diff = player_1.total_elo - winner_elo_original
        loser_elo_diff = loser_elo_original - player_2.total_elo

        match_two.delete()

        player_1.refresh_from_db()
        player_2.refresh_from_db()

        # check all Player denormalized fields reset
        self.assertEqual(player_1.total_elo, winner_elo_original)
        self.assertEqual(player_1.season_elo, winner_elo_original)
        self.assertEqual(player_1.total_win_count, 1)
        self.assertEqual(player_1.season_win_count, 1)

        self.assertEqual(player_2.total_elo, loser_elo_original)
        self.assertEqual(player_2.season_elo, loser_elo_original)
        self.assertEqual(player_2.total_loss_count, 1)
        self.assertEqual(player_2.season_loss_count, 1)

        # check the season player is updated
        player_1_season_player = SeasonPlayer.objects.get(player=player_1, season=season)
        self.assertEqual(player_1_season_player.elo_score, winner_elo_original)
        self.assertEqual(player_1_season_player.win_count, 1)

        player_2_season_player = SeasonPlayer.objects.get(player=player_2, season=season)
        self.assertEqual(player_2_season_player.elo_score, loser_elo_original)
        self.assertEqual(player_2_season_player.loss_count, 1)

        # the elo history should not longer exist
        with self.assertRaises(EloHistory.DoesNotExist):
            EloHistory.objects.get(match=match_two)

        self.process_task_queues()

        # and the cached form should be destroyed
        for player in (player_1, player_2):
            self.assertEquals(len(memcache.get(player_1.form_cache_key)), 1)
Beispiel #8
0
    def test_delete(self):
        """Assert that all of the denormalized fields are reset correctly."""
        season = SeasonFactory(active=True)

        player_1 = PlayerFactory(active=True)
        player_2 = PlayerFactory(active=True)

        match_one = MatchFactory(date=season.start_date,
                                 winner=player_1,
                                 loser=player_2,
                                 season=season,
                                 granny=False)

        self.process_task_queues()

        # elo points at this point were...
        player_1.refresh_from_db()
        player_2.refresh_from_db()
        winner_elo_original = player_1.total_elo
        loser_elo_original = player_2.total_elo

        # now lets record an incorrect game
        match_two = MatchFactory(date=season.start_date + timedelta(days=1),
                                 winner=player_1,
                                 loser=player_2,
                                 season=season,
                                 granny=False)

        self.process_task_queues()

        # find out how many elo points were wrongly won/lost
        player_1.refresh_from_db()
        player_2.refresh_from_db()

        winner_elo_diff = player_1.total_elo - winner_elo_original
        loser_elo_diff = loser_elo_original - player_2.total_elo

        match_two.delete()

        player_1.refresh_from_db()
        player_2.refresh_from_db()

        # check all Player denormalized fields reset
        self.assertEqual(player_1.total_elo, winner_elo_original)
        self.assertEqual(player_1.season_elo, winner_elo_original)
        self.assertEqual(player_1.total_win_count, 1)
        self.assertEqual(player_1.season_win_count, 1)

        self.assertEqual(player_2.total_elo, loser_elo_original)
        self.assertEqual(player_2.season_elo, loser_elo_original)
        self.assertEqual(player_2.total_loss_count, 1)
        self.assertEqual(player_2.season_loss_count, 1)

        # check the season player is updated
        player_1_season_player = SeasonPlayer.objects.get(player=player_1,
                                                          season=season)
        self.assertEqual(player_1_season_player.elo_score, winner_elo_original)
        self.assertEqual(player_1_season_player.win_count, 1)

        player_2_season_player = SeasonPlayer.objects.get(player=player_2,
                                                          season=season)
        self.assertEqual(player_2_season_player.elo_score, loser_elo_original)
        self.assertEqual(player_2_season_player.loss_count, 1)

        # the elo history should not longer exist
        with self.assertRaises(EloHistory.DoesNotExist):
            EloHistory.objects.get(match=match_two)

        self.process_task_queues()

        # and the cached form should be destroyed
        for player in (player_1, player_2):
            self.assertEquals(len(memcache.get(player_1.form_cache_key)), 1)
Beispiel #9
0
    def test_migration_task(self):
        """Tests for the task which generates all historic Season Player instances."""
        season_one, season_two = [SeasonFactory() for x in xrange(2)]
        player_one, player_two = [
            PlayerFactory(
                season_elo = 1000,
                season_win_count = 0,
                season_loss_count = 0,
                season_grannies_given_count = 0,
                season_grannies_taken_count = 0,
            ) for x in xrange(2)
        ]

        # record two games in season one
        match_one = MatchFactory(winner=player_one, loser=player_two, season=season_one, date=season_one.start_date + timedelta(days=1))
        match_two = MatchFactory(winner=player_one, loser=player_two, season=season_one, date=season_one.start_date + timedelta(days=2))

        # cache the season elo and win/loss count
        player_one.refresh_from_db()
        player_one_season_one_elo = player_one.season_elo
        player_one_season_one_win = player_one.season_win_count
        player_one_season_one_loss = player_one.season_loss_count

        player_two.refresh_from_db()
        player_two_season_one_elo = player_two.season_elo
        player_two_season_one_win = player_two.season_win_count
        player_two_season_one_loss = player_two.season_loss_count

        # reset denormalized values before the new season
        player_one.reset_season_fields()
        player_two.reset_season_fields()

        # record a game in season two
        match_three = MatchFactory(winner=player_one, loser=player_two, season=season_two, date=season_two.start_date + timedelta(days=1))

        # cache the season elo and win/loss count
        player_one.refresh_from_db()
        player_one_season_two_elo = player_one.season_elo
        player_one_season_two_win = player_one.season_win_count
        player_one_season_two_loss = player_one.season_loss_count

        player_two.refresh_from_db()
        player_two_season_two_elo = player_two.season_elo
        player_two_season_two_win = player_two.season_win_count
        player_two_season_two_loss = player_two.season_loss_count

        # run the migration task
        season_player_migration()

        season_one_player_one = SeasonPlayer.objects.get(season=season_one, player=player_one)
        season_one_player_two = SeasonPlayer.objects.get(season=season_one, player=player_two)
        season_two_player_one = SeasonPlayer.objects.get(season=season_two, player=player_one)
        season_two_player_two =  SeasonPlayer.objects.get(season=season_two, player=player_two)

        self.assertEqual(season_one_player_one.elo_score, player_one_season_one_elo)
        self.assertEqual(season_one_player_one.win_count, player_one_season_one_win)
        self.assertEqual(season_one_player_one.loss_count, player_one_season_one_loss)

        self.assertEqual(season_one_player_two.elo_score, player_two_season_one_elo)
        self.assertEqual(season_one_player_two.win_count, player_two_season_one_win)
        self.assertEqual(season_one_player_two.loss_count, player_two_season_one_loss)

        self.assertEqual(season_two_player_one.elo_score, player_one_season_two_elo)
        self.assertEqual(season_two_player_one.win_count, player_one_season_two_win)
        self.assertEqual(season_two_player_one.loss_count, player_one_season_two_loss)

        self.assertEqual(season_two_player_two.elo_score, player_two_season_two_elo)
        self.assertEqual(season_two_player_two.win_count, player_two_season_two_win)
        self.assertEqual(season_two_player_two.loss_count, player_two_season_two_loss)