示例#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)
示例#2
0
    def test_activate(self):
        season = SeasonFactory(active=False)
        self.assertFalse(season.active)

        player = PlayerFactory(
            season_elo = 2000,
            season_win_count = 50,
            season_loss_count = 10,
            season_grannies_given_count = 5,
            season_grannies_taken_count = 2,
        )

        season.activate()

        # setting the active season will also put it into memcache
        self.assertEqual(
            season,
            memcache.get(Season.ACTIVE_SEASON_CACHE_KEY)
        )

        season.refresh_from_db()
        self.assertTrue(season.active)
        
        player.refresh_from_db()
        self.assertEqual(player.season_elo, 1000)
        self.assertFalse(player.season_win_count)
        self.assertFalse(player.season_loss_count)
        self.assertFalse(player.season_grannies_given_count)
        self.assertFalse(player.season_grannies_taken_count)
示例#3
0
    def test_new_season_activated(self):
        today = timezone.now().date()
        season = SeasonFactory(start_date=today)
        self.assertFalse(season.active)

        player = PlayerFactory(
            season_elo = 2000,
            season_win_count = 50,
            season_loss_count = 10,
            season_grannies_given_count = 5,
            season_grannies_taken_count = 2,
        )

        set_active_season()

        season.refresh_from_db()
        self.assertTrue(season.active)

        # as a bi-product of marking a new season as active
        # all season fields on the player instances are reset
        player.refresh_from_db()
        self.assertEqual(player.season_elo, 1000)
        self.assertFalse(player.season_win_count)
        self.assertFalse(player.season_loss_count)
        self.assertFalse(player.season_grannies_given_count)
        self.assertFalse(player.season_grannies_taken_count)
示例#4
0
    def test_only_one_active_season_permitted(self):
        season_one = SeasonFactory(active=True)
        self.assertTrue(season_one.active)

        # setting the active season will also put it into memcache
        self.assertEqual(
            season_one,
            memcache.get(Season.ACTIVE_SEASON_CACHE_KEY)
        )

        # trying to create a second season with active=True is not allowed
        with self.assertRaises(ValidationError):
            season_two = SeasonFactory(active=True)

        # and the original season stays in memcache
        self.assertEqual(
            season_one,
            memcache.get(Season.ACTIVE_SEASON_CACHE_KEY)
        )

        # sanity check you can modify other values when the active flag is
        # set, and the validation does not mistakenly think another active 
        # season exists
        season_one.start_date = season_one.start_date + timedelta(days=1)
        season_one.save()
示例#5
0
    def test_ongoing_season_remains_active(self):
        today = timezone.now().date()
        yesterday = today - timedelta(days=1)
        season = SeasonFactory(start_date=yesterday, active=True)

        set_active_season()

        season.refresh_from_db()
        self.assertTrue(season.active)
示例#6
0
    def test_get_active_helper(self):
        # should raise DoesNotExist if no active season
        with self.assertRaises(Season.DoesNotExist):
            Season.objects.get_active()

        # and the correct season if one is active
        active_season = SeasonFactory(active=True)
        inactive_season = SeasonFactory(active=False)

        self.assertEqual(active_season, Season.objects.get_active())
示例#7
0
    def test_expired_active_season_marked_inactive(self):
        today = timezone.now().date()
        yesterday = today - timedelta(days=1)
        earlier = yesterday - timedelta(days=10)
        season = SeasonFactory(
            start_date=earlier, end_date=yesterday, active=True
        )

        set_active_season()

        season.refresh_from_db()
        self.assertFalse(season.active)
    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)
示例#10
0
    def test_deactivate(self):
        season = SeasonFactory(active=True)
        self.assertTrue(season.active)

        # setting the active season will also put it into memcache
        self.assertEqual(
            season,
            memcache.get(Season.ACTIVE_SEASON_CACHE_KEY)
        )

        season.deactivate()

        season.refresh_from_db()
        self.assertFalse(season.active)

        # setting the deactivated season is deleted from the cache
        self.assertIsNone(memcache.get(Season.ACTIVE_SEASON_CACHE_KEY))
示例#11
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)
示例#12
0
    def test_activate(self):
        season = SeasonFactory(active=False)
        self.assertFalse(season.active)

        player = PlayerFactory(
            season_elo=2000,
            season_win_count=50,
            season_loss_count=10,
            season_grannies_given_count=5,
            season_grannies_taken_count=2,
        )

        season.activate()

        # setting the active season will also put it into memcache
        self.assertEqual(season, memcache.get(Season.ACTIVE_SEASON_CACHE_KEY))

        season.refresh_from_db()
        self.assertTrue(season.active)

        player.refresh_from_db()
        self.assertEqual(player.season_elo, 1000)
        self.assertFalse(player.season_win_count)
        self.assertFalse(player.season_loss_count)
        self.assertFalse(player.season_grannies_given_count)
        self.assertFalse(player.season_grannies_taken_count)
示例#13
0
    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)
示例#14
0
    def test_deactivate(self):
        season = SeasonFactory(active=True)
        self.assertTrue(season.active)

        # setting the active season will also put it into memcache
        self.assertEqual(season, memcache.get(Season.ACTIVE_SEASON_CACHE_KEY))

        season.deactivate()

        season.refresh_from_db()
        self.assertFalse(season.active)

        # setting the deactivated season is deleted from the cache
        self.assertIsNone(memcache.get(Season.ACTIVE_SEASON_CACHE_KEY))
示例#15
0
    def test_season_winner(self):
        season_one = SeasonFactory(active=False)

        player_1 = PlayerFactory()
        player_2 = PlayerFactory()

        season_player_one = SeasonPlayerFactory(
            season=season_one,
            player=player_1,
            elo_score=2000,
        )
        season_player_two = SeasonPlayerFactory(
            season=season_one,
            player=player_2,
            elo_score=1500,
        )

        self.assertEqual(
            season_player_one.pk,
            SeasonPlayer.objects.get_winner(season_one).pk,
        )
示例#16
0
    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)
示例#17
0
    def test_only_one_active_season_permitted(self):
        season_one = SeasonFactory(active=True)
        self.assertTrue(season_one.active)

        # setting the active season will also put it into memcache
        self.assertEqual(season_one,
                         memcache.get(Season.ACTIVE_SEASON_CACHE_KEY))

        # trying to create a second season with active=True is not allowed
        with self.assertRaises(ValidationError):
            season_two = SeasonFactory(active=True)

        # and the original season stays in memcache
        self.assertEqual(season_one,
                         memcache.get(Season.ACTIVE_SEASON_CACHE_KEY))

        # sanity check you can modify other values when the active flag is
        # set, and the validation does not mistakenly think another active
        # season exists
        season_one.start_date = season_one.start_date + timedelta(days=1)
        season_one.save()
示例#18
0
    def test_end_date_must_be_greater_than_start_date(self):
        today = timezone.now().date()

        with self.assertRaises(ValidationError):
            season = SeasonFactory(start_date=today,
                                   end_date=today - timedelta(days=1))
示例#19
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)
示例#20
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)