Beispiel #1
0
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()
Beispiel #2
0
 def setUpClass(self):
     super(Test, self).setUpClass()
     self.db = Db()
     self.db.create_cache()
     self.s1 = self.db.create_season(id=16)
     self.s2 = self.db.create_season(id=17)
     self.db.create_ranking()
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.db.create_season(id=27, version=Version.LOTV)
        self.t1, self.t2, self.t3 = self.db.create_teams(count=3)
Beispiel #4
0
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db_name = settings.DATABASES['default']['NAME']

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.t1, self.t2, self.t3 = self.db.create_teams(count=3)
Beispiel #5
0
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.now = utcnow()
        self.today = self.now.date()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.s15 = self.db.create_season(id=15, start_date=self.date(days=-200), end_date=self.date(days=-101))
        self.s16 = self.db.create_season(id=16, start_date=self.date(days=-100), end_date=None)
Beispiel #6
0
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.db.create_season()
        self.teams = self.db.create_teams(count=20)
        for i, team in enumerate(self.teams):
            setattr(self, 't%d' % i, team)
            setattr(self, 't%d_i' % i, team.id)
            setattr(self, 't%d_m0_n' % i, team.member0.name)
            setattr(self, 't%d_m0_i' % i, team.member0.id)
Beispiel #7
0
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db.create_season(id=16)

    def setUp(self):
        super().setUp()
        self.db.delete_all(keep=[Season])
        self.c = Client()

    def test_get_1v1(self):
        p = self.db.create_player(region=Region.EU, realm=1, bid=301)
        t = self.db.create_team(mode=Mode.TEAM_1V1)

        response = self.c.get(
            '/team/id/', {
                'mode': '1v1',
                'player': 'http://eu.battle.net/sc2/en/profile/301/1/xyz'
            })

        self.assertEqual(200, response.status_code)
        data = json.loads(response.content.decode('utf-8'))
        self.assertEqual(t.id, data['team_id'])

    def test_get_4v4(self):
        p1 = self.db.create_player(region=Region.EU, realm=1, bid=301)
        p2 = self.db.create_player(region=Region.EU, realm=1, bid=302)
        p3 = self.db.create_player(region=Region.EU, realm=1, bid=303)
        p4 = self.db.create_player(region=Region.EU, realm=1, bid=304)
        t = self.db.create_team(mode=Mode.TEAM_4V4,
                                member0=p1,
                                member1=p2,
                                member2=p3,
                                member3=p4)

        qp = MultiValueDict()
        qp.setlist('player', [
            'http://eu.battle.net/sc2/en/profile/304/1/xyz',
            'http://eu.battle.net/sc2/en/profile/303/1/xyz',
            'http://eu.battle.net/sc2/en/profile/302/1/xyz',
            'http://eu.battle.net/sc2/en/profile/301/1/xyz',
        ])
        qp['mode'] = 'team-4v4'
        response = self.c.get('/team/id/', qp)

        self.assertEqual(200, response.status_code)
        data = json.loads(response.content.decode('utf-8'))
        self.assertEqual(t.id, data['team_id'])
Beispiel #8
0
 def setUpClass(self):
     super(Test, self).setUpClass()
     self.db = Db()
     self.db.create_season(id=29)
     self.db.create_cache()  # Ranking needs it for source.
Beispiel #9
0
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db.create_season(id=29)
        self.db.create_cache()  # Ranking needs it for source.

    def setUp(self):
        super().setUp()
        self.now = utcnow()
        self.db.delete_all(keep=[Season, Cache])
        self.db.create_ranking()
        self.ladder = self.db.create_ladder(bid=1, created=self.datetime(seconds=1))  # Always using same ladder.

    def test_no_update_of_player_or_team_if_worse_with_another_race(self):
        p1 = self.db.create_player(bid=301,
                                   league=League.PLATINUM,
                                   race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.db.create_ranking()
        self.db.create_ranking_data(data=[dict(team_id=t1.id,
                                               league=League.PLATINUM,
                                               mmr=4000,
                                               race0=Race.ZERG)]),

        self.process_ladder(load=True,
                            mode=Mode.TEAM_1V1,
                            league=League.GOLD,
                            bid=301,
                            mmr=3000,
                            race=Race.TERRAN)

        p1.refresh_from_db()
        t1.refresh_from_db()

        self.assertEqual(League.PLATINUM, t1.league)
        self.assertEqual(Race.ZERG, t1.race0)
        self.assertEqual(League.PLATINUM, p1.league)
        self.assertEqual(Race.ZERG, p1.race)

    def test_update_of_player_and_team_if_better_with_another_race(self):
        p1 = self.db.create_player(bid=301,
                                   league=League.PLATINUM,
                                   race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.db.create_ranking()
        self.db.create_ranking_data(data=[dict(team_id=t1.id, mmr=3000, league=League.PLATINUM, race0=Race.ZERG)]),

        self.process_ladder(mode=Mode.TEAM_1V1,
                            league=League.MASTER,
                            mmr=5000,
                            bid=301,
                            race=Race.TERRAN)

        p1.refresh_from_db()
        t1.refresh_from_db()

        self.assertEqual(League.MASTER, t1.league)
        self.assertEqual(Race.TERRAN, t1.race0)
        self.assertEqual(League.MASTER, p1.league)
        self.assertEqual(Race.TERRAN, p1.race)

    def test_update_of_player_and_team_if_better_with_same_race(self):
        p1 = self.db.create_player(bid=301,
                                   league=League.PLATINUM,
                                   race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.db.create_ranking()
        self.db.create_ranking_data(data=[dict(team_id=t1.id, league=League.PLATINUM, mmr=3000, race0=Race.ZERG)]),

        self.process_ladder(mode=Mode.TEAM_1V1,
                            league=League.MASTER,
                            mmr=5000,
                            bid=301,
                            race=Race.ZERG)

        p1.refresh_from_db()
        t1.refresh_from_db()

        self.assertEqual(League.MASTER, t1.league)
        self.assertEqual(Race.ZERG, t1.race0)
        self.assertEqual(League.MASTER, p1.league)
        self.assertEqual(Race.ZERG, p1.race)

    def test_same_1v1_can_have_multiple_rankings_with_different_races(self):
        self.process_ladder(league=League.SILVER,
                            members=[
                                gen_member(bid=1, mmr=50, race=Race.TERRAN),
                                gen_member(bid=1, mmr=40, race=Race.ZERG),
                                gen_member(bid=1, mmr=30, race=Race.RANDOM),
                            ])

        self.process_ladder(league=League.GOLD,
                            members=[
                                gen_member(bid=1, mmr=90, race=Race.TERRAN),
                                gen_member(bid=1, mmr=80, race=Race.PROTOSS),
                                gen_member(bid=1, mmr=70, race=Race.ZERG),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(
            self.db.ranking.id,
            dict(region_count=4, region_rank=1, mmr=90, race0=Race.TERRAN,  race3=9, league=League.GOLD),
            dict(region_count=4, region_rank=2, mmr=80, race0=Race.PROTOSS, race3=8, league=League.GOLD),
            dict(region_count=4, region_rank=3, mmr=70, race0=Race.ZERG,    race3=8, league=League.GOLD),
            dict(region_count=4, region_rank=4, mmr=30, race0=Race.RANDOM,  race3=8, league=League.SILVER),
        )

    def test_same_1v1_can_have_multiple_rankings_with_different_races_among_other_players(self):
        self.process_ladder(league=League.SILVER,
                            members=[
                                gen_member(bid=2, mmr=50, race=Race.ZERG),
                                gen_member(bid=1, mmr=40, race=Race.TERRAN),
                                gen_member(bid=3, mmr=30, race=Race.RANDOM),
                            ])

        self.process_ladder(league=League.GOLD,
                            members=[
                                gen_member(bid=1, mmr=90, race=Race.ZERG),
                                gen_member(bid=4, mmr=80, race=Race.PROTOSS),
                                gen_member(bid=5, mmr=70, race=Race.TERRAN),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(
            self.db.ranking.id,
            dict(region_count=6, region_rank=1, mmr=90, race0=Race.ZERG,    race3=9, league=League.GOLD),
            dict(region_count=6, region_rank=2, mmr=80, race0=Race.PROTOSS, race3=9, league=League.GOLD),
            dict(region_count=6, region_rank=3, mmr=70, race0=Race.TERRAN,  race3=9, league=League.GOLD),
            dict(region_count=6, region_rank=4, mmr=50, race0=Race.ZERG,    race3=9, league=League.SILVER),
            dict(region_count=6, region_rank=5, mmr=40, race0=Race.TERRAN,  race3=8, league=League.SILVER),
            dict(region_count=6, region_rank=6, mmr=30, race0=Race.RANDOM,  race3=9, league=League.SILVER),
        )

    def test_same_random_2v2__can_not_have_multiple_rankings_with_different_races(self):
        
        self.process_ladder(league=League.SILVER,
                            mode=Mode.RANDOM_2V2,
                            members=[
                                gen_member(bid=1, mmr=40, race=Race.TERRAN),
                            ])

        self.process_ladder(league=League.GOLD,
                            mode=Mode.RANDOM_2V2,
                            members=[
                                gen_member(bid=1, mmr=90, race=Race.ZERG),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(
            self.db.ranking.id,
            dict(region_count=1, region_rank=1, mmr=90, race0=Race.ZERG, league=League.GOLD),
        )
Beispiel #10
0
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

    def setUp(self):
        super().setUp()
        self.db.delete_all(keep=[Cache, Ladder])
        self.db.create_season(id=26, version=Version.LOTV)
        self.c = Client()

    def tearDown(self):
        rankings_view_client.close()
        super(Test, self).tearDown()

    def test_view_team(self):
        p1 = self.db.create_player(name="arne")
        t1 = self.db.create_team()

        self.db.create_player(name="sune")
        t2 = self.db.create_team()

        r1 = self.db.create_ranking()
        self.db.create_ranking_data(
            data=[dict(team_id=t1.id, ladder_rank=10, version=Version.WOL)])

        r2 = self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=t1.id, ladder_rank=11, version=Version.HOTS),
            dict(team_id=t2.id, ladder_rank=8, version=Version.HOTS)
        ])

        r3 = self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=t1.id, ladder_rank=12, version=Version.HOTS),
            dict(team_id=t1.id, ladder_rank=6, version=Version.WOL)
        ])

        r4 = self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=t1.id, ladder_rank=4, version=Version.HOTS),
            dict(team_id=t1.id, ladder_rank=13, version=Version.LOTV)
        ])

        response = self.c.get('/team/%d/' % t1.id)
        self.assertEqual(200, response.status_code)
        self.assertEqual(t1, response.context['team'])
        self.assertEqual([p1], response.context['members'])

        response = self.c.get('/team/%d/rankings/' % t1.id)

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(4, len(data))

        data0 = data[0]

        self.assertEqual(r1.id, data0['id'])
        self.assertEqual(10, data0['ladder_rank'])
        self.assertEqual(Version.WOL, data0['version'])

        data1 = data[1]

        self.assertEqual(r2.id, data1['id'])
        self.assertEqual(11, data1['ladder_rank'])
        self.assertEqual(Version.HOTS, data1['version'])

        data2 = data[2]

        self.assertEqual(r3.id, data2['id'])
        self.assertEqual(12, data2['ladder_rank'])
        self.assertEqual(Version.HOTS, data2['version'])

        data3 = data[3]

        self.assertEqual(r4.id, data3['id'])
        self.assertEqual(13, data3['ladder_rank'])
        self.assertEqual(Version.LOTV, data3['version'])

    def test_no_mmr_is_filtered_after_mmr_season(self):
        s27 = self.db.create_season(id=27)
        s28 = self.db.create_season(id=28)

        p1 = self.db.create_player(name="arne")
        t1 = self.db.create_team()

        r1 = self.db.create_ranking(season=s27)
        self.db.create_ranking_data(data=[dict(ladder_rank=10, mmr=NO_MMR)])

        r2 = self.db.create_ranking(season=s28)
        self.db.create_ranking_data(data=[dict(ladder_rank=11, mmr=NO_MMR)])

        r3 = self.db.create_ranking(season=s28)
        self.db.create_ranking_data(data=[dict(ladder_rank=12, mmr=120)])

        response = self.c.get('/team/%d/rankings/' % t1.id)

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(2, len(data))

        data0 = data[0]

        self.assertEqual(r1.id, data0['id'])
        self.assertEqual(10, data0['ladder_rank'])

        data1 = data[1]

        self.assertEqual(r3.id, data1['id'])
        self.assertEqual(12, data1['ladder_rank'])
        self.assertEqual(120, data1['mmr'])
Beispiel #11
0
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db_name = settings.DATABASES['default']['NAME']

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.t1, self.t2, self.t3 = self.db.create_teams(count=3)

    def setUp(self):
        super().setUp()
        self.db.clear_defaults()
        self.db.delete_all(keep=[Cache, Ladder, Team, Player])
        self.db.create_season(version=Version.HOTS)
        self.c = sc2.Get(self.db_name, Enums.INFO, 0)

    def tearDown(self):
        # Null and garbage collect to disconnect c++ code from the database.
        self.c = None
        gc.collect()
        super().tearDown()

    def test_create_stats_from_empty_ranking_data(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
        ])
        self.db.update_ranking_stats()

        for mode_id in Mode.stat_v1_ids:
            stats, = json.loads(self.c.ranking_stats(mode_id))
            self.assertEqual(0, sum(stats['data']))
            self.assertEqual(RankingStats.V1, stats['stat_version'])
            self.assertEqual(self.db.season.id, stats['season_id'])
            self.assertEqual(Version.HOTS, stats['season_version'])
            self.assertEqual(self.db.ranking.id, stats['id'])
            self.assertEqual(RankingStats.V1_DATA_COUNT * RankingStats.V1_DATA_SIZE, len(stats['data']))

    def test_create_stats_from_lotv_season_gets_correct_version(self):
        self.db.create_season(id=17, version=Version.LOTV)
        self.db.create_ranking()
        self.db.create_ranking_data(data=[])
        self.db.update_ranking_stats()

        stats, = json.loads(self.c.ranking_stats(Mode.TEAM_1V1))

        self.assertEqual(RankingStats.V1, stats['stat_version'])
        self.assertEqual(self.db.season.id, stats['season_id'])
        self.assertEqual(Version.LOTV, stats['season_version'])
        self.assertEqual(self.db.ranking.id, stats['id'])
        self.assertEqual(RankingStats.V1_DATA_COUNT * RankingStats.V1_DATA_SIZE, len(stats['data']))
        self.assertEqual(0, sum(stats['data']))

    def test_create_stats_from_lotv_season_with_archon_mode_is_calculated_correctly(self):
        self.db.default_ranking_data__data = dict(
            mode=Mode.TEAM_1V1,
            version=Version.LOTV,
            region=Region.EU,
            league=League.GOLD,
            race0=Race.ZERG
        )

        self.db.create_season(id=17, version=Version.LOTV)
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(wins=10, losses=10, points=10.0, race0=Race.ZERG, mode=Mode.RANDOM_2V2),
            dict(wins=20, losses=21, points=22.0, race0=Race.TERRAN, mode=Mode.ARCHON),
            dict(wins=30, losses=31, points=32.0, race0=Race.PROTOSS, mode=Mode.TEAM_1V1),
        ])
        self.db.update_ranking_stats()

        stats, = json.loads(self.c.ranking_stats(Mode.RANDOM_2V2))

        self.assertEqual(1 + 10 + 10 + 10, sum(stats['data']))
        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.LOTV_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.ZERG_INDEX)
        self.assertEqual([1, 10, 10, 10], stats['data'][index: index + 4])

        stats, = json.loads(self.c.ranking_stats(Mode.ARCHON))

        self.assertEqual(1 + 20 + 21 + 22, sum(stats['data']))
        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.LOTV_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.TERRAN_INDEX)
        self.assertEqual([1, 20, 21, 22], stats['data'][index: index + 4])

        stats, = json.loads(self.c.ranking_stats(Mode.TEAM_1V1))

        self.assertEqual(1 + 30 + 31 + 32, sum(stats['data']))
        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.LOTV_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.PROTOSS_INDEX)
        self.assertEqual([1, 30, 31, 32], stats['data'][index: index + 4])

    def test_create_stats_with_different_archon_versions_is_calculated_correctly(self):
        self.db.default_ranking_data__data = dict(
            mode=Mode.ARCHON,
            version=Version.LOTV,
            region=Region.EU,
            league=League.GOLD,
            race0=Race.ZERG
        )

        self.db.create_season(id=17, version=Version.LOTV)
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(wins=10, losses=10, points=10.0, version=Version.WOL),
            dict(wins=20, losses=21, points=22.0, version=Version.HOTS),
            dict(wins=30, losses=31, points=32.0, version=Version.LOTV),
        ])
        self.db.update_ranking_stats()

        stats, = json.loads(self.c.ranking_stats(Mode.ARCHON))

        self.assertEqual(3 + 60 + 62 + 64, sum(stats['data']))

        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.WOL_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.ZERG_INDEX)
        self.assertEqual([1, 10, 10, 10], stats['data'][index: index + 4])

        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.HOTS_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.ZERG_INDEX)
        self.assertEqual([1, 20, 21, 22], stats['data'][index: index + 4])

        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.LOTV_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.ZERG_INDEX)
        self.assertEqual([1, 30, 31, 32], stats['data'][index: index + 4])

    def test_create_stats_using_two_teams_that_sums(self):
        self.db.default_ranking_data__data = dict(
            mode=Mode.TEAM_1V1,
            version=Version.HOTS,
            region=Region.EU,
            league=League.GOLD,
            race0=Race.ZERG
        )
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(wins=10, losses=10, points=10.0),
            dict(wins=20, losses=21, points=22.0),
        ])
        self.db.update_ranking_stats()

        for mode_id in Mode.stat_v1_ids:
            if mode_id == Mode.TEAM_1V1:
                continue
            stats, = json.loads(self.c.ranking_stats(mode_id))
            self.assertEqual(0, sum(stats['data']))

        stats, = json.loads(self.c.ranking_stats(Mode.TEAM_1V1))

        self.assertEqual(RankingStats.V1, stats['stat_version'])
        self.assertEqual(self.db.season.id, stats['season_id'])
        self.assertEqual(Version.HOTS, stats['season_version'])
        self.assertEqual(self.db.ranking.id, stats['id'])
        self.assertEqual(RankingStats.V1_DATA_COUNT * RankingStats.V1_DATA_SIZE, len(stats['data']))
        self.assertEqual(2 + 30 + 31 + 32, sum(stats['data']))

        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.HOTS_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.ZERG_INDEX)

        self.assertEqual([2, 30, 31, 32], stats['data'][index: index + 4])

    def test_creating_stats_for_three_teams_with_different_parameters(self):
        self.db.default_ranking_data__data = dict(
            mode=Mode.RANDOM_4V4,
            version=Version.HOTS,
            region=Region.EU,
            league=League.GOLD,
            race0=Race.ZERG
        )
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, wins=10, losses=11, points=12.0, region=Region.CN),
            dict(team_id=self.t2.id, wins=20, losses=21, points=22.0, race0=Race.TERRAN),
            dict(team_id=self.t3.id, wins=30, losses=31, points=32.0, league=League.PLATINUM),
        ])
        self.db.update_ranking_stats()

        for mode_id in Mode.stat_v1_ids:
            if mode_id == Mode.RANDOM_4V4:
                continue
            stats, = json.loads(self.c.ranking_stats(mode_id))
            self.assertEqual(0, sum(stats['data']))

        stats, = json.loads(self.c.ranking_stats(Mode.RANDOM_4V4))

        self.assertEqual(3 + 60 + 63 + 66, sum(stats['data']))

        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.HOTS_INDEX,
                                          Region.CN_INDEX, League.GOLD_INDEX, Race.ZERG_INDEX)
        self.assertEqual([1, 10, 11, 12], stats['data'][index: index + 4])

        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.HOTS_INDEX,
                                          Region.EU_INDEX, League.GOLD_INDEX, Race.TERRAN_INDEX)
        self.assertEqual([1, 20, 21, 22], stats['data'][index: index + 4])

        index = RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.HOTS_INDEX,
                                          Region.EU_INDEX, League.PLATINUM_INDEX, Race.ZERG_INDEX)
        self.assertEqual([1, 30, 31, 32], stats['data'][index: index + 4])
Beispiel #12
0
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.db.create_season()
        self.teams = self.db.create_teams(count=20)
        for i, team in enumerate(self.teams):
            setattr(self, 't%d' % i, team)
            setattr(self, 't%d_i' % i, team.id)
            setattr(self, 't%d_m0_n' % i, team.member0.name)
            setattr(self, 't%d_m0_i' % i, team.member0.id)

    def setUp(self):
        super().setUp()
        cache.clear()
        self.db.clear_defaults()
        self.db.delete_all(keep=[Season, Cache, Ladder, Team, Player])
        self.c = Client()

        # Change page size for easier testing.
        main.views.ladder.PAGE_SIZE = 10

    def tearDown(self):
        rankings_view_client.close()
        super(Test, self).tearDown()

    def get_page(self, url, content=["rank", "team_id"], page_size=10):
        """ Get the page and per team on page return the data indexed by content arg. """
        main.views.ladder.PAGE_SIZE = page_size
        cache.clear()
        response = self.c.get(url)
        self.assertEqual(200, response.status_code)
        if len(content) == 1:
            return [t[content[0]] for t in response.context['ladder']['teams']]
        else:
            return [
                tuple(t[i] for i in content)
                for t in response.context['ladder']['teams']
            ]

    def test_simple_ladder(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t0_i, league=League.GOLD, mmr=30, tier=0),
            dict(team_id=self.t1_i, league=League.PLATINUM, mmr=60, tier=1),
            dict(team_id=self.t2_i, league=League.PLATINUM, mmr=50, tier=2),
        ])

        response = self.c.get('/ladder/hots/1v1/mmr/')
        self.assertEqual(200, response.status_code)

        self.assertEqual(1, response.context['team_size'])
        self.assertEqual(0, response.context['highlight_team_id'])
        self.assertEqual(3, response.context['ladder']['count'])
        self.assertEqual(0, response.context['ladder']['offset'])

        page = response.context['ladder']['teams']

        self.assertEqual(self.t1_m0_n, page[0]["m0_name"])
        self.assertEqual(self.t2_m0_n, page[1]["m0_name"])
        self.assertEqual(self.t0_m0_n, page[2]["m0_name"])

    def test_two_players_with_the_same_mmr_should_get_same_rank(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t0_i, league=League.PLATINUM, mmr=50),
            dict(team_id=self.t1_i, league=League.PLATINUM, mmr=50),
            dict(team_id=self.t2_i, league=League.GOLD, mmr=20),
            dict(team_id=self.t3_i, league=League.GOLD, mmr=20),
        ])

        response = self.c.get('/ladder/hots/1v1/mmr/')
        self.assertEqual(200, response.status_code)

        page = response.context['ladder']['teams']

        self.assertEqual(self.t0_m0_n, page[0]["m0_name"])
        self.assertEqual(self.t1_m0_n, page[1]["m0_name"])
        self.assertEqual(self.t2_m0_n, page[2]["m0_name"])
        self.assertEqual(self.t3_m0_n, page[3]["m0_name"])

        self.assertEqual(1, page[0]["rank"])
        self.assertEqual(1, page[1]["rank"])
        self.assertEqual(3, page[2]["rank"])
        self.assertEqual(3, page[3]["rank"])

    def test_10_players_get_the_same_rank_works_over_pages(self):
        teams = self.teams
        data = [dict(team_id=teams[0].id, mmr=200)]
        for i in range(1, 11):
            data.append(dict(team_id=teams[i].id, mmr=199))
        for i in range(11, 20):
            data.append(dict(team_id=teams[i].id, mmr=188))
        self.db.create_ranking()
        self.db.create_ranking_data(data=data)

        response = self.c.get('/ladder/hots/1v1/mmr/')
        self.assertEqual(200, response.status_code)

        page = response.context['ladder']['teams']

        # Check that the entire first page is just 1, 2, 2, .., 2

        self.assertEqual(self.t0_m0_n, page[0]["m0_name"])
        self.assertEqual(1, page[0]["rank"])

        self.assertEqual(self.t1_m0_n, page[1]["m0_name"])
        self.assertEqual(2, page[1]["rank"])

        self.assertEqual(self.t9_m0_n, page[-1]["m0_name"])
        self.assertEqual(2, page[-1]["rank"])

        # For offset 1 team 1-10 should all have rank 2.

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=1')
        self.assertEqual(200, response.status_code)

        page = response.context['ladder']['teams']

        self.assertEqual(self.t1_m0_n, page[0]["m0_name"])
        self.assertEqual(2, page[0]["rank"])

        self.assertEqual(self.t10_m0_n, page[-1]["m0_name"])
        self.assertEqual(2, page[-1]["rank"])

        # For offset 6 team 1-5 should have rank 2 and team 6-10 should have rank 12.

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=6')
        self.assertEqual(200, response.status_code)

        page = response.context['ladder']['teams']

        self.assertEqual(self.t6_m0_n, page[0]["m0_name"])
        self.assertEqual(2, page[0]["rank"])

        self.assertEqual(2, page[4]["rank"])

        self.assertEqual(12, page[5]["rank"])

        self.assertEqual(self.t15_m0_n, page[-1]["m0_name"])
        self.assertEqual(12, page[-1]["rank"])

    def test_ladder_rank_sorting_sorts_on__ladder_rank__wins__losses__team_id__also_test_filters(
            self):
        self.db.default_ranking_data__data = dict(
            version=Version.HOTS,
            race0=Race.ZERG,
            region=Region.EU,
        )
        self.db.create_ranking()
        data = [
            # Outside all filters.
            dict(team_id=self.t11_i,
                 league=League.PLATINUM,
                 mmr=3000,
                 version=Version.WOL),
            dict(team_id=self.t12_i,
                 league=League.PLATINUM,
                 mmr=3000,
                 mode=Mode.RANDOM_2V2),
            # Inside most filters.
            dict(team_id=self.t0_i,
                 league=League.BRONZE,
                 mmr=0,
                 wins=0,
                 losses=2,
                 race0=Race.TERRAN),
            dict(team_id=self.t1_i,
                 league=League.BRONZE,
                 mmr=0,
                 wins=0,
                 losses=2,
                 region=Region.AM),
            dict(team_id=self.t2_i,
                 league=League.BRONZE,
                 mmr=0,
                 wins=0,
                 losses=1),
            dict(team_id=self.t3_i,
                 league=League.BRONZE,
                 mmr=0,
                 wins=1,
                 losses=1),
            dict(team_id=self.t4_i,
                 league=League.BRONZE,
                 mmr=1,
                 wins=1,
                 losses=1),
            dict(team_id=self.t5_i,
                 league=League.SILVER,
                 mmr=1000,
                 wins=0,
                 losses=4),
            dict(team_id=self.t6_i,
                 league=League.GOLD,
                 mmr=2000,
                 wins=0,
                 losses=4),
            dict(team_id=self.t7_i,
                 league=League.PLATINUM,
                 mmr=3000,
                 wins=0,
                 losses=4),
            dict(team_id=self.t8_i,
                 league=League.DIAMOND,
                 mmr=4000,
                 wins=0,
                 losses=4),
            dict(team_id=self.t9_i,
                 league=League.MASTER,
                 mmr=5000,
                 wins=0,
                 losses=4),
            dict(team_id=self.t10_i,
                 league=League.GRANDMASTER,
                 mmr=6000,
                 wins=0,
                 losses=4),
        ]
        shuffle(data)
        self.db.create_ranking_data(data=data)

        #
        # Test default filtering, (0 is before 1 because lower team_id is before).
        #

        self.assertEqual([(1, self.t10_i), (2, self.t9_i), (3, self.t8_i),
                          (4, self.t7_i), (5, self.t6_i), (6, self.t5_i),
                          (7, self.t4_i), (8, self.t3_i), (8, self.t2_i),
                          (8, self.t0_i), (8, self.t1_i)],
                         self.get_page('/ladder/hots/1v1/mmr/', page_size=20))

        self.assertEqual([
            (1, self.t1_i),
            (1, self.t0_i),
            (1, self.t2_i),
            (1, self.t3_i),
            (5, self.t4_i),
            (6, self.t5_i),
            (7, self.t6_i),
            (8, self.t7_i),
            (9, self.t8_i),
            (10, self.t9_i),
            (11, self.t10_i),
        ], self.get_page('/ladder/hots/1v1/-mmr/', page_size=20))

        #
        # Test another version filtering.
        #

        self.assertEqual([(1, self.t11_i)],
                         self.get_page('/ladder/wol/1v1/mmr/'))
        self.assertEqual([(1, self.t11_i)],
                         self.get_page('/ladder/wol/1v1/-mmr/'))

        #
        # Test another mode version filtering.
        #

        self.assertEqual([(1, self.t12_i)],
                         self.get_page('/ladder/hots/random-2v2/mmr/'))
        self.assertEqual([(1, self.t12_i)],
                         self.get_page('/ladder/hots/random-2v2/-mmr/'))

        #
        # Test league filtering.
        #

        expected_order = []
        self.assertEqual([
            (1, self.t4_i),
            (2, self.t3_i),
            (2, self.t2_i),
            (2, self.t0_i),
            (2, self.t1_i),
        ], self.get_page('/ladder/hots/1v1/mmr/?f=bronze'))

        self.assertEqual([
            (1, self.t1_i),
            (1, self.t0_i),
            (1, self.t2_i),
            (1, self.t3_i),
            (5, self.t4_i),
        ], self.get_page('/ladder/hots/1v1/-mmr/?f=bronze'))

        #
        # Test race filtering.
        #

        self.assertEqual([(1, self.t0_i)],
                         self.get_page('/ladder/hots/1v1/mmr/?f=terran'))
        self.assertEqual([(1, self.t0_i)],
                         self.get_page('/ladder/hots/1v1/-mmr/?f=terran'))

        #
        # Test region filtering.
        #

        self.assertEqual([(1, self.t1_i)],
                         self.get_page('/ladder/hots/1v1/mmr/?f=am'))
        self.assertEqual([(1, self.t1_i)],
                         self.get_page('/ladder/hots/1v1/-mmr/?f=am'))

        #
        # Test league, region and race filtering.
        #

        self.assertEqual([
            (1, self.t4_i),
            (2, self.t3_i),
            (2, self.t2_i),
        ], self.get_page('/ladder/hots/1v1/mmr/?f=bronze,zerg,eu'))

        self.assertEqual([
            (1, self.t2_i),
            (1, self.t3_i),
            (3, self.t4_i),
        ], self.get_page('/ladder/hots/1v1/-mmr/?f=bronze,zerg,eu'))

    def test_league_points_sorts_on__league__tier__points__wins__losses__team_id(
            self):
        self.db.create_ranking()
        data = [
            dict(team_id=self.t0_i,
                 league=League.DIAMOND,
                 tier=2,
                 wins=1,
                 losses=1,
                 points=1),
            dict(team_id=self.t1_i,
                 league=League.GOLD,
                 tier=0,
                 wins=1,
                 losses=1,
                 points=2),
            dict(team_id=self.t2_i,
                 league=League.GOLD,
                 tier=1,
                 wins=1,
                 losses=1,
                 points=7),
            dict(team_id=self.t3_i,
                 league=League.GOLD,
                 tier=1,
                 wins=9,
                 losses=1,
                 points=6),
            dict(team_id=self.t4_i,
                 league=League.GOLD,
                 tier=1,
                 wins=6,
                 losses=5,
                 points=6),
            dict(team_id=self.t5_i,
                 league=League.GOLD,
                 tier=1,
                 wins=6,
                 losses=8,
                 points=6),
            dict(team_id=self.t6_i,
                 league=League.GOLD,
                 tier=1,
                 wins=6,
                 losses=8,
                 points=6),
            dict(team_id=self.t7_i,
                 league=League.GOLD,
                 tier=2,
                 wins=1,
                 losses=1,
                 points=9),
            dict(team_id=self.t8_i,
                 league=League.BRONZE,
                 tier=0,
                 wins=1,
                 losses=1,
                 points=96),
        ]
        shuffle(data)
        self.db.create_ranking_data(data=data)

        self.assertEqual([
            (1, self.t0_i),
            (2, self.t1_i),
            (3, self.t2_i),
            (4, self.t3_i),
            (4, self.t4_i),
            (4, self.t5_i),
            (4, self.t6_i),
            (8, self.t7_i),
            (9, self.t8_i),
        ], self.get_page('/ladder/hots/1v1/league-points/'))

        self.assertEqual([
            (1, self.t8_i),
            (2, self.t7_i),
            (3, self.t6_i),
            (3, self.t5_i),
            (3, self.t4_i),
            (3, self.t3_i),
            (7, self.t2_i),
            (8, self.t1_i),
            (9, self.t0_i),
        ], self.get_page('/ladder/hots/1v1/-league-points/'))

    def test_games_played_sorts_on__played__mmr__wins__team_id(self):
        self.db.create_ranking()
        data = [
            dict(team_id=self.t0_i,
                 league=League.SILVER,
                 mmr=2,
                 wins=9,
                 losses=9,
                 points=2),
            dict(team_id=self.t1_i,
                 league=League.DIAMOND,
                 mmr=2,
                 wins=9,
                 losses=8,
                 points=1),
            dict(team_id=self.t2_i,
                 league=League.BRONZE,
                 mmr=1,
                 wins=9,
                 losses=8,
                 points=9),
            dict(team_id=self.t3_i,
                 league=League.GOLD,
                 mmr=1,
                 wins=8,
                 losses=9,
                 points=8),
            dict(team_id=self.t4_i,
                 league=League.DIAMOND,
                 mmr=1,
                 wins=8,
                 losses=9,
                 points=7),
        ]
        shuffle(data)
        self.db.create_ranking_data(data=data)

        self.assertEqual([
            (1, self.t0_i),
            (2, self.t1_i),
            (2, self.t2_i),
            (2, self.t3_i),
            (2, self.t4_i),
        ], self.get_page('/ladder/hots/1v1/played/'))

        self.assertEqual([
            (1, self.t4_i),
            (1, self.t3_i),
            (1, self.t2_i),
            (1, self.t1_i),
            (5, self.t0_i),
        ], self.get_page('/ladder/hots/1v1/-played/'))

    def test_wins_sorts_on__wins__mmr__losses__team_id(self):
        self.db.create_ranking()
        data = [
            dict(team_id=self.t0_i,
                 league=League.SILVER,
                 mmr=1,
                 wins=9,
                 losses=8,
                 points=2),
            dict(team_id=self.t1_i,
                 league=League.SILVER,
                 mmr=0,
                 wins=9,
                 losses=8,
                 points=5),
            dict(team_id=self.t2_i,
                 league=League.DIAMOND,
                 mmr=0,
                 wins=9,
                 losses=9,
                 points=6),
            dict(team_id=self.t3_i,
                 league=League.BRONZE,
                 mmr=0,
                 wins=8,
                 losses=9,
                 points=3),
            dict(team_id=self.t4_i,
                 league=League.GOLD,
                 mmr=0,
                 wins=8,
                 losses=9,
                 points=7),
        ]
        shuffle(data)
        self.db.create_ranking_data(data=data)

        self.assertEqual([
            (1, self.t0_i),
            (1, self.t1_i),
            (1, self.t2_i),
            (4, self.t3_i),
            (4, self.t4_i),
        ], self.get_page('/ladder/hots/1v1/wins/'))

        self.assertEqual([
            (1, self.t4_i),
            (1, self.t3_i),
            (3, self.t2_i),
            (3, self.t1_i),
            (3, self.t0_i),
        ], self.get_page('/ladder/hots/1v1/-wins/'))

    def test_losses_sorts_on__losses__wins__team_id(self):
        self.db.create_ranking()
        data = [
            dict(team_id=self.t0_i,
                 league=League.SILVER,
                 mmr=0,
                 wins=8,
                 losses=9),
            dict(team_id=self.t1_i,
                 league=League.SILVER,
                 mmr=0,
                 wins=9,
                 losses=9),
            dict(team_id=self.t2_i,
                 league=League.DIAMOND,
                 mmr=0,
                 wins=9,
                 losses=8),
            dict(team_id=self.t3_i,
                 league=League.BRONZE,
                 mmr=0,
                 wins=9,
                 losses=8),
        ]
        shuffle(data)
        self.db.create_ranking_data(data=data)

        self.assertEqual([
            (1, self.t0_i),
            (1, self.t1_i),
            (3, self.t2_i),
            (3, self.t3_i),
        ], self.get_page('/ladder/hots/1v1/losses/'))

        self.assertEqual([
            (1, self.t3_i),
            (1, self.t2_i),
            (3, self.t1_i),
            (3, self.t0_i),
        ], self.get_page('/ladder/hots/1v1/-losses/'))

    def test_win_rate_sorts_om__win_rate__played__mmr__team_id(self):
        self.db.create_ranking()
        data = [
            dict(team_id=self.t0_i,
                 league=League.SILVER,
                 mmr=2,
                 wins=9,
                 losses=1),
            dict(team_id=self.t1_i,
                 league=League.SILVER,
                 mmr=2,
                 wins=5,
                 losses=5),
            dict(team_id=self.t2_i,
                 league=League.SILVER,
                 mmr=4,
                 wins=4,
                 losses=4),
            dict(team_id=self.t3_i,
                 league=League.DIAMOND,
                 mmr=4,
                 wins=4,
                 losses=4),
            dict(team_id=self.t4_i,
                 league=League.BRONZE,
                 mmr=4,
                 wins=4,
                 losses=4),
            dict(team_id=self.t5_i,
                 league=League.BRONZE,
                 mmr=4,
                 wins=4,
                 losses=7),
        ]
        shuffle(data)
        self.db.create_ranking_data(data=data)

        self.assertEqual([
            (1, self.t0_i),
            (2, self.t1_i),
            (2, self.t2_i),
            (2, self.t3_i),
            (2, self.t4_i),
            (6, self.t5_i),
        ], self.get_page('/ladder/hots/1v1/win-rate/'))

        self.assertEqual([
            (1, self.t5_i),
            (2, self.t4_i),
            (2, self.t3_i),
            (2, self.t2_i),
            (2, self.t1_i),
            (6, self.t0_i),
        ], self.get_page('/ladder/hots/1v1/-win-rate/'))

    def test_pagination_with_no_gap_between_start_and_end_section(self):
        self.db.create_ranking()
        data = [dict(team_id=team.id) for team in self.teams]
        self.db.create_ranking_data(data=data)

        main.views.ladder.PAGE_SIZE = 3  # => 7 sections

        response = self.c.get('/ladder/hots/1v1/mmr/')

        pages = response.context['pages']

        self.assertEqual(0, response.context['current_page'])
        self.assertEqual(7, len(pages))
        self.assertEqual(0, len([p for p in pages if 'gap' in p]))

        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/', index=0), pages[0])
        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/?offset=18', index=6),
                         pages[6])

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=18')

        pages = response.context['pages']

        self.assertEqual(6, response.context['current_page'])
        self.assertEqual(7, len(pages))
        self.assertEqual(0, len([p for p in pages if 'gap' in p]))

        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/', index=0), pages[0])
        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/?offset=18', index=6),
                         pages[6])

    def test_pagination_with_start_middle_and_end_section(self):
        self.db.create_ranking()
        data = [dict(team_id=team.id) for team in self.teams]
        self.db.create_ranking_data(data=data)

        main.views.ladder.PAGE_SIZE = 1  # => 20 sections, index and offset are the same.

        # Test last page with connecting start and middle section.

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=7')

        pages = response.context['pages']

        self.assertEqual(7, response.context['current_page'])
        self.assertEqual(11 + 1 + 4, len(pages))
        self.assertEqual(1, len([p for p in pages if 'gap' in p]))

        self.assertEqual(0, pages[0]['index'])
        self.assertEqual(10, pages[10]['index'])
        self.assertIn('gap', pages[11])
        self.assertEqual(16, pages[12]['index'])
        self.assertEqual(19, pages[15]['index'])

        # Test first page with separate start and middle section.

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=8')

        pages = response.context['pages']

        self.assertEqual(8, response.context['current_page'])
        self.assertEqual(4 + 1 + 7 + 1 + 4, len(pages))
        self.assertEqual(2, len([p for p in pages if 'gap' in p]))

        self.assertEqual(0, pages[0]['index'])
        self.assertEqual(3, pages[3]['index'])
        self.assertIn('gap', pages[4])
        self.assertEqual(5, pages[5]['index'])
        self.assertEqual(11, pages[11]['index'])
        self.assertIn('gap', pages[12])
        self.assertEqual(16, pages[13]['index'])
        self.assertEqual(19, pages[16]['index'])

        # Test last page with separate middle and end section.

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=11')

        pages = response.context['pages']

        self.assertEqual(11, response.context['current_page'])
        self.assertEqual(4 + 1 + 7 + 1 + 4, len(pages))
        self.assertEqual(2, len([p for p in pages if 'gap' in p]))

        self.assertEqual(0, pages[0]['index'])
        self.assertEqual(3, pages[3]['index'])
        self.assertIn('gap', pages[4])
        self.assertEqual(8, pages[5]['index'])
        self.assertEqual(14, pages[11]['index'])
        self.assertIn('gap', pages[12])
        self.assertEqual(16, pages[13]['index'])
        self.assertEqual(19, pages[16]['index'])

        # Test first page with connecting middle and end section.

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=12')

        pages = response.context['pages']

        self.assertEqual(12, response.context['current_page'])
        self.assertEqual(4 + 1 + 11, len(pages))
        self.assertEqual(1, len([p for p in pages if 'gap' in p]))

        self.assertEqual(0, pages[0]['index'])
        self.assertEqual(3, pages[3]['index'])
        self.assertIn('gap', pages[4])
        self.assertEqual(9, pages[5]['index'])
        self.assertEqual(19, pages[15]['index'])

    def test_non_aligned_pagination(self):
        self.db.create_ranking()
        data = [dict(team_id=team.id) for team in self.teams]
        data.extend(
            dict(team_id=team.id, mode=Mode.TEAM_2V2) for team in self.teams)
        self.db.create_ranking_data(data=data)

        main.views.ladder.PAGE_SIZE = 5  # => 4 sections, all visible all the time.

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=7')

        pages = response.context['pages']

        self.assertEqual(2, response.context['current_page'])
        self.assertEqual(5, len(pages))
        self.assertEqual(0, len([p for p in pages if 'gap' in p]))

        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/', index=0), pages[0])
        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/?offset=2', index=1),
                         pages[1])
        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/?offset=7', index=2),
                         pages[2])
        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/?offset=12', index=3),
                         pages[3])
        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/?offset=17', index=4),
                         pages[4])

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=19')

        pages = response.context['pages']
        page = response.context['ladder']['teams']

        self.assertEqual(4, response.context['current_page'])
        self.assertEqual(5, len(pages))
        self.assertEqual(0, len([p for p in pages if 'gap' in p]))
        self.assertEqual(1, len(page))

        self.assertEqual(dict(href='/ladder/hots/1v1/mmr/?offset=19', index=4),
                         pages[4])

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=21')

        pages = response.context['pages']
        page = response.context['ladder']['teams']

        self.assertEqual(-1, response.context['current_page'])
        self.assertEqual(4, len(pages))
        self.assertEqual(0, len([p for p in pages if 'gap' in p]))
        self.assertEqual(0, len(page))

    def test_pagination_start_offset_with_team_link(self):
        self.db.create_ranking()
        data = [dict(team_id=team.id) for team in self.teams]
        self.db.create_ranking_data(data=data)

        main.views.ladder.PAGE_SIZE = 12  # team link wants to display -10 iteams.

        response = self.c.get('/ladder/hots/1v1/mmr/?team=%d' % self.t14_i)

        pages = response.context['pages']
        page = response.context['ladder']['teams']

        self.assertEqual(1, response.context['current_page'])
        self.assertEqual(3, len(pages))

        self.assertEqual(self.t14_i, page[10]["team_id"])
        self.assertEqual(12, len(page))

        self.assertEqual(
            dict(href='/ladder/hots/1v1/mmr/?team=%d&offset=0' % self.t14_i,
                 index=0), pages[0])
        self.assertEqual(
            dict(href='/ladder/hots/1v1/mmr/?team=%d&offset=4' % self.t14_i,
                 index=1), pages[1])
        self.assertEqual(
            dict(href='/ladder/hots/1v1/mmr/?team=%d&offset=16' % self.t14_i,
                 index=2), pages[2])

    def test_team_link_with_and_without_filtering(self):
        self.db.create_ranking()
        data = [
            dict(team_id=team.id,
                 race0=(Race.ZERG if team.id % 2 == 1 else Race.TERRAN))
            for team in self.teams
        ]
        self.db.create_ranking_data(data=data)

        main.views.ladder.PAGE_SIZE = 8

        response = self.c.get('/ladder/hots/1v1/mmr/?f=zerg&team=%d' %
                              self.t14_i)

        pages = response.context['pages']
        page = response.context['ladder']['teams']

        self.assertEqual(0, response.context['current_page'])
        self.assertEqual(2, len(pages))

        self.assertEqual(self.t14_i, page[7]["team_id"])
        self.assertEqual(8, len(page))

        self.assertEqual(
            dict(href='/ladder/hots/1v1/mmr/?f=zerg&team=%d&offset=0' %
                 self.t14_i,
                 index=0), pages[0])
        self.assertEqual(
            dict(href='/ladder/hots/1v1/mmr/?f=zerg&team=%d&offset=8' %
                 self.t14_i,
                 index=1), pages[1])

        response = self.c.get('/ladder/hots/1v1/mmr/?f=terran&team=%d' %
                              self.t14_i)

        pages = response.context['pages']
        page = response.context['ladder']['teams']

        self.assertEqual(0, response.context['current_page'])
        self.assertEqual(2, len(pages))

        self.assertNotIn(self.t14_i, [p["team_id"] for p in page])
        self.assertEqual(8, len(page))

        self.assertEqual(
            dict(href='/ladder/hots/1v1/mmr/?f=terran&team=%d&offset=0' %
                 self.t14_i,
                 index=0), pages[0])
        self.assertEqual(
            dict(href='/ladder/hots/1v1/mmr/?f=terran&team=%d&offset=8' %
                 self.t14_i,
                 index=1), pages[1])

    def test_various_bad_requests_for_good_code_coverage_and_error_checking(
            self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[dict(team_id=self.t1_i)])

        response = self.c.get('/ladder/bw/1v1/mmr/')
        self.assertEqual(404, response.status_code)

        response = self.c.get('/ladder/hots/team-5v5/mmr/')
        self.assertEqual(404, response.status_code)

        response = self.c.get('/ladder/hots/1v1/ladder-rank/')
        self.assertEqual(302, response.status_code)

        response = self.c.get('/ladder/hots/1v1/mmr/?offset=%d' % 3e9)
        self.assertEqual(404, response.status_code)

        response = self.c.get('/ladder/hots/1v1/mmr/?team=%d' % 3e9)
        self.assertEqual(404, response.status_code)

    def test_race_filtering_on_archon_mode_which_normally_does_not_work_on_teams(
            self):
        self.db.default_ranking_data__data = dict(
            version=Version.LOTV,
            mode=Mode.ARCHON,
        )

        t0 = self.db.create_team(
            mode=Mode.ARCHON,
            version=Version.LOTV,
            member0=self.db.create_player(),
            race0=Race.ZERG,
            member1=self.db.create_player(),
            race1=Race.ZERG,
        )

        t1 = self.db.create_team(
            mode=Mode.ARCHON,
            version=Version.LOTV,
            member0=self.db.create_player(),
            race0=Race.PROTOSS,
            member1=self.db.create_player(),
            race1=Race.PROTOSS,
        )

        t2 = self.db.create_team(
            mode=Mode.ARCHON,
            version=Version.LOTV,
            member0=self.db.create_player(),
            race0=Race.ZERG,
            member1=self.db.create_player(),
            race1=Race.ZERG,
        )

        t3 = self.db.create_team(
            mode=Mode.ARCHON,
            version=Version.LOTV,
            member0=self.db.create_player(),
            race0=Race.TERRAN,
            member1=self.db.create_player(),
            race1=Race.TERRAN,
        )

        self.db.create_ranking()
        data = [
            dict(team_id=t0.id,
                 league=League.PLATINUM,
                 mmr=50,
                 race0=Race.ZERG,
                 race1=Race.ZERG),
            dict(team_id=t1.id,
                 league=League.PLATINUM,
                 mmr=40,
                 race0=Race.PROTOSS,
                 race1=Race.PROTOSS),
            dict(team_id=t2.id,
                 league=League.PLATINUM,
                 mmr=30,
                 race0=Race.ZERG,
                 race1=Race.ZERG),
            dict(team_id=t3.id,
                 league=League.PLATINUM,
                 mmr=20,
                 race0=Race.TERRAN,
                 race1=Race.TERRAN),
        ]
        shuffle(data)
        self.db.create_ranking_data(data=data)

        #
        # Test default filtering.
        #

        self.assertEqual([
            (1, t0.id),
            (2, t1.id),
            (3, t2.id),
            (4, t3.id),
        ], self.get_page('/ladder/lotv/archon/mmr/', page_size=20))

        #
        # Test race filtering.
        #

        self.assertEqual([(1, t0.id), (2, t2.id)],
                         self.get_page('/ladder/lotv/archon/mmr/?f=zerg'))
        self.assertEqual([(1, t1.id)],
                         self.get_page('/ladder/lotv/archon/mmr/?f=protoss'))
        self.assertEqual([(1, t3.id)],
                         self.get_page('/ladder/lotv/archon/mmr/?f=terran'))
        self.assertEqual([],
                         self.get_page('/ladder/lotv/archon/mmr/?f=random'))
Beispiel #13
0
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super().setUpClass()
        self.db = Db()

    def setUp(self):
        super().setUp()
        self.now = utcnow()
        self.db.delete_all()
        self.db.create_season(id=1)
        self.db.create_ranking()

    def test_same_team_in_multiple_ladders_select_later_added_instance_and_disregards_times(self):
        # Given this, the ranker will have to rank ladders in fetch order to prevent overwriting data when teams
        # change ladder due to promotion/demotion.

        self.process_ladder(league=League.PLATINUM,
                            fetch_time=self.datetime(days=-200),
                            members=[
                                gen_member(bid=301,
                                           race=Race.ZERG,
                                           join_time=int(self.unix_time(days=-410))),
                                gen_member(bid=302,
                                           race=Race.TERRAN,
                                           join_time=int(self.unix_time(days=-400))),
                                gen_member(bid=303,
                                           race=Race.PROTOSS,
                                           join_time=int(self.unix_time(days=-400)),
                                           points=20,
                                           wins=50),
                                gen_member(bid=304,
                                           race=Race.RANDOM,
                                           join_time=int(self.unix_time(days=-400)),
                                           points=10,
                                           wins=40,
                                           losses=10),
                            ])
        
        self.process_ladder(league=League.GOLD,
                            fetch_time=self.datetime(days=-330),
                            members=[
                                gen_member(bid=302,
                                           race=Race.TERRAN,
                                           join_time=int(self.unix_time(days=-410))),
                                gen_member(bid=303,
                                           race=Race.PROTOSS,
                                           join_time=int(self.unix_time(days=-410)),
                                           points=20,
                                           wins=60),
                                gen_member(bid=304,
                                           race=Race.RANDOM,
                                           join_time=int(self.unix_time(days=-410)),
                                           points=10,
                                           wins=40,
                                           losses=20),
                            ])

        self.save_to_ranking()

        t1, t2, t3, t4 = self.db.get_teams_by_member0_bids(301, 302, 303, 304)

        self.assert_team_ranks(self.db.ranking.id,
                               dict(team_id=t1, region_count=4, region_rank=1, league=League.PLATINUM),
                               dict(team_id=t2, region_count=4, region_rank=2, league=League.GOLD),
                               dict(team_id=t3, region_count=4, region_rank=3, league=League.GOLD, wins=60),
                               dict(team_id=t4, region_count=4, region_rank=4, league=League.GOLD, wins=40, losses=20),
                               )

    def test_same_team_in_multiple_ladders_but_different_versions_and_all_are_in_the_ranking(self):
        self.process_ladder(version=Version.HOTS,
                            members=[
                                gen_member(bid=301, points=20),
                                gen_member(bid=302, points=10),
                            ])

        self.process_ladder(version=Version.WOL,
                            members=[
                                gen_member(bid=301, points=10),
                                gen_member(bid=302, points=20),
                            ])

        self.process_ladder(version=Version.LOTV,
                            members=[
                                gen_member(bid=301, points=40),
                                gen_member(bid=302, points=30),
                            ])

        self.save_to_ranking()

        t1, t2 = self.db.get_teams_by_member0_bids(301, 302)

        self.assert_team_ranks(self.db.ranking.id,
                               dict(team_id=t2, region_count=2, region_rank=1, points=20, version=Version.WOL),
                               dict(team_id=t1, region_count=2, region_rank=2, points=10, version=Version.WOL),
                               dict(team_id=t1, region_count=2, region_rank=1, points=20, version=Version.HOTS),
                               dict(team_id=t2, region_count=2, region_rank=2, points=10, version=Version.HOTS),
                               dict(team_id=t1, region_count=2, region_rank=1, points=40, version=Version.LOTV),
                               dict(team_id=t2, region_count=2, region_rank=2, points=30, version=Version.LOTV),
                               )

    def test_same_player_in_two_teams_multiple_ladders_but_different_modes_and_both_are_in_ranking(self):
        self.process_ladder(mode=Mode.RANDOM_4V4,
                            members=[
                                gen_member(bid=301, points=20),
                                gen_member(bid=302, points=10),
                            ])

        self.process_ladder(mode=Mode.RANDOM_3V3,
                            members=[
                                gen_member(bid=301, points=10),
                                gen_member(bid=302, points=20),
                            ])

        self.save_to_ranking()

        t1, t2 = self.db.get_teams_by_member0_bids(301, 302, mode=Mode.RANDOM_3V3)
        t3, t4 = self.db.get_teams_by_member0_bids(301, 302, mode=Mode.RANDOM_4V4)

        self.assert_team_ranks(self.db.ranking.id,
                               dict(team_id=t2, region_count=2, region_rank=1, points=20, mode=Mode.RANDOM_3V3),
                               dict(team_id=t1, region_count=2, region_rank=2, points=10, mode=Mode.RANDOM_3V3),
                               dict(team_id=t3, region_count=2, region_rank=1, points=20, mode=Mode.RANDOM_4V4),
                               dict(team_id=t4, region_count=2, region_rank=2, points=10, mode=Mode.RANDOM_4V4),
                               )

    def test_different_versions_are_ranked_separately(self):
        self.process_ladder(version=Version.HOTS,
                            members=[
                                gen_member(points=20),
                                gen_member(points=10),
                            ])

        self.process_ladder(version=Version.WOL,
                            members=[
                                gen_member(points=21),
                                gen_member(points=11),
                            ])

        self.process_ladder(version=Version.LOTV,
                            members=[
                                gen_member(points=22),
                                gen_member(points=12),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(region_count=2, region_rank=1, world_rank=1, points=21, version=Version.WOL),
                               dict(region_count=2, region_rank=2, world_rank=2, points=11, version=Version.WOL),
                               dict(region_count=2, region_rank=1, world_rank=1, points=20, version=Version.HOTS),
                               dict(region_count=2, region_rank=2, world_rank=2, points=10, version=Version.HOTS),
                               dict(region_count=2, region_rank=1, world_rank=1, points=22, version=Version.LOTV),
                               dict(region_count=2, region_rank=2, world_rank=2, points=12, version=Version.LOTV),
                               )

    def test_different_modes_are_ranked_separatly(self):
        self.process_ladder(mode=Mode.TEAM_1V1,
                            members=[
                                gen_member(points=110),
                                gen_member(points=111),
                            ])

        self.process_ladder(mode=Mode.ARCHON,
                            members=[
                                gen_member(points=120),
                                gen_member(points=120),
                                gen_member(points=121),
                                gen_member(points=121),
                            ])

        self.process_ladder(mode=Mode.TEAM_2V2,
                            members=[
                                gen_member(points=220),
                                gen_member(points=220),
                                gen_member(points=221),
                                gen_member(points=221),
                            ])

        self.process_ladder(mode=Mode.RANDOM_2V2,
                            members=[
                                gen_member(points=210),
                                gen_member(points=211),
                            ])

        self.process_ladder(mode=Mode.TEAM_3V3,
                            members=[
                                gen_member(points=330),
                                gen_member(points=330),
                                gen_member(points=330),
                                gen_member(points=331),
                                gen_member(points=331),
                                gen_member(points=331),
                            ])

        self.process_ladder(mode=Mode.RANDOM_3V3,
                            members=[
                                gen_member(points=310),
                                gen_member(points=311),
                            ])

        self.process_ladder(mode=Mode.TEAM_4V4,
                            members=[
                                gen_member(points=440),
                                gen_member(points=440),
                                gen_member(points=440),
                                gen_member(points=440),
                                gen_member(points=441),
                                gen_member(points=441),
                                gen_member(points=441),
                                gen_member(points=441),
                            ])

        self.process_ladder(mode=Mode.RANDOM_4V4,
                            members=[
                                gen_member(points=410),
                                gen_member(points=411),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(region_count=2, region_rank=1, world_rank=1, points=111, mode=Mode.TEAM_1V1),
                               dict(region_count=2, region_rank=2, world_rank=2, points=110, mode=Mode.TEAM_1V1),
                               dict(region_count=2, region_rank=1, world_rank=1, points=121, mode=Mode.ARCHON),
                               dict(region_count=2, region_rank=2, world_rank=2, points=120, mode=Mode.ARCHON),
                               dict(region_count=2, region_rank=1, world_rank=1, points=221, mode=Mode.TEAM_2V2),
                               dict(region_count=2, region_rank=2, world_rank=2, points=220, mode=Mode.TEAM_2V2),
                               dict(region_count=2, region_rank=1, world_rank=1, points=211, mode=Mode.RANDOM_2V2),
                               dict(region_count=2, region_rank=2, world_rank=2, points=210, mode=Mode.RANDOM_2V2),
                               dict(region_count=2, region_rank=1, world_rank=1, points=331, mode=Mode.TEAM_3V3),
                               dict(region_count=2, region_rank=2, world_rank=2, points=330, mode=Mode.TEAM_3V3),
                               dict(region_count=2, region_rank=1, world_rank=1, points=311, mode=Mode.RANDOM_3V3),
                               dict(region_count=2, region_rank=2, world_rank=2, points=310, mode=Mode.RANDOM_3V3),
                               dict(region_count=2, region_rank=1, world_rank=1, points=441, mode=Mode.TEAM_4V4),
                               dict(region_count=2, region_rank=2, world_rank=2, points=440, mode=Mode.TEAM_4V4),
                               dict(region_count=2, region_rank=1, world_rank=1, points=411, mode=Mode.RANDOM_4V4),
                               dict(region_count=2, region_rank=2, world_rank=2, points=410, mode=Mode.RANDOM_4V4),
                               )

    def test_ranking_world_region_league_tier_and_ladder_ranking_at_once_works_as_expected(self):
        self.process_ladder(region=Region.EU, league=League.SILVER,
                            members=[
                                gen_member(points=90),
                                gen_member(points=80),
                            ])

        self.process_ladder(region=Region.EU, league=League.GOLD,
                            members=[
                                gen_member(points=77),
                                gen_member(points=33),
                            ])

        self.process_ladder(region=Region.AM, league=League.GOLD,
                            members=[
                                gen_member(points=95),
                                gen_member(points=10),
                            ])

        self.process_ladder(region=Region.KR, league=League.GRANDMASTER,
                            members=[
                                gen_member(points=1),
                            ])

        self.process_ladder(region=Region.EU, league=League.SILVER, tier=1,
                            members=[
                                gen_member(points=90),
                                gen_member(points=80),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(world_rank=1,  region_rank=1,  league_rank=1,  ladder_rank=1,
                                    world_count=9, region_count=1, league_count=1, ladder_count=1,
                                    points=1, region=Region.KR, league=League.GRANDMASTER),
                               dict(world_rank=2,  region_rank=1,  league_rank=1,  ladder_rank=1,
                                    world_count=9, region_count=2, league_count=2, ladder_count=2,
                                    points=95, region=Region.AM, league=League.GOLD),
                               dict(world_rank=3,  region_rank=1,  league_rank=1,  ladder_rank=1,
                                    world_count=9, region_count=6, league_count=2, ladder_count=2,
                                    points=77, region=Region.EU, league=League.GOLD),
                               dict(world_rank=4,  region_rank=2,  league_rank=2,  ladder_rank=2,
                                    world_count=9, region_count=6, league_count=2, ladder_count=2,
                                    points=33, region=Region.EU, league=League.GOLD),
                               dict(world_rank=5,  region_rank=2,  league_rank=2,  ladder_rank=2,
                                    world_count=9, region_count=2, league_count=2, ladder_count=2,
                                    points=10, region=Region.AM, league=League.GOLD),
                               dict(world_rank=6,  region_rank=3,  league_rank=1,  ladder_rank=1, tier=0,
                                    world_count=9, region_count=6, league_count=4, ladder_count=2,
                                    points=90, region=Region.EU, league=League.SILVER),
                               dict(world_rank=7,  region_rank=4,  league_rank=2,  ladder_rank=2, tier=0,
                                    world_count=9, region_count=6, league_count=4, ladder_count=2,
                                    points=80, region=Region.EU, league=League.SILVER),
                               dict(world_rank=8,  region_rank=5,  league_rank=3,  ladder_rank=1, tier=1,
                                    world_count=9, region_count=6, league_count=4, ladder_count=2,
                                    points=90, region=Region.EU, league=League.SILVER),
                               dict(world_rank=9,  region_rank=6,  league_rank=4,  ladder_rank=2, tier=1,
                                    world_count=9, region_count=6, league_count=4, ladder_count=2,
                                    points=80, region=Region.EU, league=League.SILVER))

    def test_region_ranking_does_rank_calculation_correctly_when_teams_have_same_points(self):
        self.process_ladder(members=[
            gen_member(points=90),
            gen_member(points=90),
        ])

        self.process_ladder(members=[
            gen_member(points=90),
            gen_member(points=10),
        ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(region_rank=1, world_rank=1, points=90),
                               dict(region_rank=1, world_rank=1, points=90),
                               dict(region_rank=1, world_rank=1, points=90),
                               dict(region_rank=4, world_rank=4, points=10),
                               )

    def test_simple_world_ranking(self):
        self.process_ladder(region=Region.EU, league=League.GOLD,
                            members=[
                                gen_member(points=90),
                                gen_member(points=80),
                            ])

        self.process_ladder(region=Region.AM, league=League.GOLD,
                            members=[
                                gen_member(points=95),
                                gen_member(points=10),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(world_count=4, world_rank=1, points=95),
                               dict(world_count=4, world_rank=2, points=90),
                               dict(world_count=4, world_rank=3, points=80),
                               dict(world_count=4, world_rank=4, points=10),
                               )

    def test_simple_region_ranking(self):
        self.process_ladder(league=League.GOLD,
                            members=[
                                gen_member(points=90),
                                gen_member(points=80),
                            ])

        self.process_ladder(league=League.SILVER,
                            members=[
                                gen_member(points=95),
                                gen_member(points=10),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(region_count=4, region_rank=1, points=90, league=League.GOLD),
                               dict(region_count=4, region_rank=2, points=80, league=League.GOLD),
                               dict(region_count=4, region_rank=3, points=95, league=League.SILVER),
                               dict(region_count=4, region_rank=4, points=10, league=League.SILVER),
                               )

    def test_simple_league_ranking(self):
        self.process_ladder(members=[
            gen_member(points=90),
            gen_member(points=80),
        ])

        self.process_ladder(members=[
            gen_member(points=95),
            gen_member(points=10),
        ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(league_count=4, league_rank=1, points=95),
                               dict(league_count=4, league_rank=2, points=90),
                               dict(league_count=4, league_rank=3, points=80),
                               dict(league_count=4, league_rank=4, points=10),
                               )

    def test_simple_ladder_ranking(self):
        self.process_ladder(members=[
            gen_member(points=90),
            gen_member(points=80),
        ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(ladder_count=2, ladder_rank=1, points=90),
                               dict(ladder_count=2, ladder_rank=2, points=80),
                               )

    def test_updating_with_members_at_end_or_beginning_or_mid_works(self):
        self.process_ladder(members=[
            gen_member(bid=302, points=40),
            gen_member(bid=304, points=20),
        ])

        self.process_ladder(members=[
            gen_member(bid=301, points=50),
            gen_member(bid=303, points=30),
            gen_member(bid=305, points=10),
        ])

        self.save_to_ranking()

        t1, t2, t3, t4, t5 = self.db.get_teams_by_member0_bids(301, 302, 303, 304, 305)

        self.assert_team_ranks(self.db.ranking.id,
                               dict(team_id=t1, points=50),
                               dict(team_id=t2, points=40),
                               dict(team_id=t3, points=30),
                               dict(team_id=t4, points=20),
                               dict(team_id=t5, points=10),
                               )

    def test_updating_with_replacing_members_in_different_order_works(self):
        self.process_ladder(members=[
            gen_member(bid=301, points=50),
            gen_member(bid=302, points=30),
            gen_member(bid=303, points=10),
        ])

        self.process_ladder(members=[
            gen_member(bid=303, points=60),
            gen_member(bid=302, points=40),
            gen_member(bid=301, points=20),
        ])

        self.save_to_ranking()

        t1, t2, t3 = self.db.get_teams_by_member0_bids(301, 302, 303)

        self.assert_team_ranks(self.db.ranking.id,
                               dict(team_id=t3, points=60),
                               dict(team_id=t2, points=40),
                               dict(team_id=t1, points=20),
                               )

    def test_trigger_bug_insert_before_replace_will_make_double_insert(self):

        p1 = self.db.create_player(bid=301)
        t1 = self.db.create_team(member0=p1)

        p2 = self.db.create_player(bid=302)
        t2 = self.db.create_team(member0=p2)

        p3 = self.db.create_player(bid=303)
        t3 = self.db.create_team(member0=p3)

        self.process_ladder(members=[
            gen_member(bid=301),
            gen_member(bid=303),
        ])

        self.process_ladder(members=[
            gen_member(bid=302),
            gen_member(bid=303),
        ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(team_id=t1.id),
                               dict(team_id=t2.id),
                               dict(team_id=t3.id),
                               sort=False
                               )

    def test_trigger_insert_replace_and_add_at_the_same_time(self):

        p1 = self.db.create_player(bid=301)
        t1 = self.db.create_team(member0=p1)

        p2 = self.db.create_player(bid=302)
        t2 = self.db.create_team(member0=p2)

        p3 = self.db.create_player(bid=303)
        t3 = self.db.create_team(member0=p3)

        p4 = self.db.create_player(bid=304)
        t4 = self.db.create_team(member0=p4)

        p5 = self.db.create_player(bid=305)
        t5 = self.db.create_team(member0=p5)

        p6 = self.db.create_player(bid=306)
        t6 = self.db.create_team(member0=p6)

        self.process_ladder(members=[
            gen_member(bid=302, points=10),
            gen_member(bid=305),
        ])

        self.process_ladder(members=[
            gen_member(bid=301),
            gen_member(bid=302, points=20),
            gen_member(bid=303),
            gen_member(bid=304),
            gen_member(bid=306),
        ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(team_id=t1.id),
                               dict(team_id=t2.id, points=20),
                               dict(team_id=t3.id),
                               dict(team_id=t4.id),
                               dict(team_id=t5.id),
                               dict(team_id=t6.id),
                               sort=False
                               )

    def test_gm_is_using_points_before_season_28(self):
        self.db.create_season(id=27)
        self.db.create_ranking()

        self.process_ladder(league=League.GRANDMASTER, region=Region.EU,
                            members=[
                                gen_member(points=80),
                                gen_member(points=10),
                            ])

        self.process_ladder(league=League.GRANDMASTER, region=Region.KR,
                            members=[
                                gen_member(points=99),
                                gen_member(points=70),
                                gen_member(points=70),
                            ])

        self.process_ladder(league=League.GRANDMASTER, region=Region.AM,
                            members=[
                                gen_member(points=200),
                                gen_member(points=98),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(world_rank=1,  region_rank=1,  league_rank=1,  ladder_rank=1,
                                    points=200, region=Region.AM),
                               dict(world_rank=2,  region_rank=1,  league_rank=1,  ladder_rank=1,
                                    points=99, region=Region.KR),
                               dict(world_rank=3,  region_rank=2,  league_rank=2,  ladder_rank=2,
                                    points=98, region=Region.AM),
                               dict(world_rank=4,  region_rank=1,  league_rank=1,  ladder_rank=1,
                                    points=80, region=Region.EU),
                               dict(world_rank=5,  region_rank=2,  league_rank=2,  ladder_rank=2,
                                    points=70, region=Region.KR),
                               dict(world_rank=5,  region_rank=2,  league_rank=2,  ladder_rank=2,
                                    points=70, region=Region.KR),
                               dict(world_rank=7,  region_rank=2,  league_rank=2,  ladder_rank=2,
                                    points=10, region=Region.EU))

    def test_race3_is_not_overwritten__this_was_a_bug(self):
        self.db.create_season(id=29)
        self.db.create_ranking()

        self.process_ladder(mode=Mode.TEAM_4V4,
                            members=[
                                gen_member(race=Race.ZERG),
                                gen_member(race=Race.ZERG),
                                gen_member(race=Race.ZERG),
                                gen_member(race=Race.ZERG),
                            ])

        self.save_to_ranking()

        self.assert_team_ranks(self.db.ranking.id,
                               dict(race3=Race.ZERG))
Beispiel #14
0
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.s1 = self.db.create_season(id=1)
        self.s2 = self.db.create_season(id=2)

    def setUp(self):
        super().setUp()
        self.now = utcnow()
        self.db.delete_all(keep=[Season])
        self.db.create_ranking()

    def test_player_season_is_updated_if_greater_than_current(self):
        self.db.create_player(bid=301,
                              name='arne0',
                              clan='arne0',
                              tag='arne0',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.TEAM_1V1)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            league=League.GOLD,
                            season=self.s2,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.TERRAN)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.TEAM_1V1, p.mode)
        self.assertEqual(self.s2.id, p.season_id)

    def test_player_name_is_not_updated_but_other_info_is_if_new_name_is_empty__this_handles_bug_in_blizzard_api(
            self):
        self.db.create_player(bid=301,
                              name='arne0',
                              clan='arne0',
                              tag='arne0',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.TEAM_1V1)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            league=League.GRANDMASTER,
                            season=self.s2,
                            bid=301,
                            name="",
                            tag="",
                            clan="",
                            race=Race.TERRAN)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne0", p.name)
        self.assertEqual("arne0", p.clan)
        self.assertEqual("arne0", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GRANDMASTER, p.league)
        self.assertEqual(Mode.TEAM_1V1, p.mode)
        self.assertEqual(self.s2.id, p.season_id)

    def test_race_mode_and_league_within_season_is_not_updated_if_mode_11_is_already_set(
            self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.TEAM_1V1)

        self.process_ladder(mode=Mode.RANDOM_3V3,
                            league=League.PLATINUM,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.ZERG)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.TEAM_1V1, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_race_is_updated_if_everything_else_is_the_same(self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.RANDOM_2V2)

        self.process_ladder(mode=Mode.RANDOM_2V2,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.ZERG)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.ZERG, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.RANDOM_2V2, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_race_is_updated_if_everything_else_is_the_same__but_not_if_1v1(
            self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.TEAM_1V1)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.ZERG)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.TEAM_1V1, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_three_ladders_to_see_that_correct_player_version_sticks_in_cache(
            self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.RANDOM_3V3)

        # Loaded to cache, not updated.
        self.process_ladder(mode=Mode.RANDOM_3V3,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.TERRAN)

        p = self.db.get(Player, bid=301)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.RANDOM_3V3, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

        # Updated in db (and hopefully in cache).
        self.process_ladder(mode=Mode.RANDOM_2V2,
                            league=League.DIAMOND,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.ZERG)

        p = self.db.get(Player, bid=301)
        self.assertEqual(Race.ZERG, p.race)
        self.assertEqual(League.DIAMOND, p.league)
        self.assertEqual(Mode.RANDOM_2V2, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

        # Not updated if compared to cached version as it should.
        self.process_ladder(mode=Mode.RANDOM_4V4,
                            league=League.PLATINUM,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.PROTOSS)

        p = self.db.get(Player, bid=301)
        self.assertEqual(Race.ZERG, p.race)
        self.assertEqual(League.DIAMOND, p.league)
        self.assertEqual(Mode.RANDOM_2V2, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_race_is_updated_if_everything_else_is_the_same_even_if_mode_is_not_1v1(
            self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.RANDOM_3V3)

        self.process_ladder(mode=Mode.RANDOM_3V3,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.ZERG)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.ZERG, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.RANDOM_3V3, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_league_is_updated_if_better_league_and_another_mode_but_not_11_and_same_season(
            self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.RANDOM_3V3)

        self.process_ladder(mode=Mode.RANDOM_4V4,
                            league=League.PLATINUM,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.TERRAN)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.PLATINUM, p.league)
        self.assertEqual(Mode.RANDOM_4V4, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_league_is_not_updated_if_worse_league_and_another_mode_but_not_11_and_same_season(
            self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.PLATINUM,
                              mode=Mode.RANDOM_3V3)

        self.process_ladder(mode=Mode.RANDOM_4V4,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.TERRAN)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.PLATINUM, p.league)
        self.assertEqual(Mode.RANDOM_3V3, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_league_is_updated_to_lower_within_mode_if_nothing_else_changes(
            self):
        self.db.create_player(bid=301,
                              name='arne1',
                              clan='arne1',
                              tag='arne1',
                              season=self.s1,
                              race=Race.TERRAN,
                              league=League.PLATINUM,
                              mode=Mode.RANDOM_3V3)

        self.process_ladder(mode=Mode.RANDOM_3V3,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.TERRAN)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.RANDOM_3V3, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_update_from_null_works(self):
        self.db.create_player(bid=301,
                              name="arne1",
                              tag="",
                              clan="",
                              mode=None,
                              league=None,
                              race=None,
                              season=None)

        self.process_ladder(mode=Mode.RANDOM_3V3,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.TERRAN)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne1", p.name)
        self.assertEqual("arne1", p.clan)
        self.assertEqual("arne1", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.RANDOM_3V3, p.mode)
        self.assertEqual(self.s1.id, p.season_id)

    def test_season_is_not_updated_if_season_if_after_player_season(self):

        self.db.create_player(bid=301,
                              name='arne0',
                              clan='arne0',
                              tag='arne0',
                              season=self.s2,
                              race=Race.TERRAN,
                              league=League.GOLD,
                              mode=Mode.TEAM_1V1)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            league=League.GOLD,
                            season=self.s1,
                            bid=301,
                            name="arne1",
                            tag="arne1",
                            clan="arne1",
                            race=Race.TERRAN)

        self.assertEqual(1, len(Player.objects.all()))

        p = self.db.get(Player, bid=301)
        self.assertEqual("arne0", p.name)
        self.assertEqual("arne0", p.clan)
        self.assertEqual("arne0", p.tag)
        self.assertEqual(Race.TERRAN, p.race)
        self.assertEqual(League.GOLD, p.league)
        self.assertEqual(Mode.TEAM_1V1, p.mode)
        self.assertEqual(self.s2.id, p.season_id)
Beispiel #15
0
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db.create_season(id=1)

    def setUp(self):
        super().setUp()
        self.now = utcnow()
        self.db.delete_all(keep=[Season])
        self.ladder = self.db.create_ladder(bid=1, created=self.datetime(seconds=1))  # Always using same ladder.

    def test_non_existent_4v4_team_is_created(self):
        self.process_ladder(mode=Mode.TEAM_4V4,
                            members=[gen_member(bid=301, race=Race.ZERG),
                                     gen_member(bid=302, race=Race.PROTOSS),
                                     gen_member(bid=303, race=Race.TERRAN),
                                     gen_member(bid=304, race=Race.TERRAN)])
        
        p1 = self.db.get(Player, bid=301)
        p2 = self.db.get(Player, bid=302)
        p3 = self.db.get(Player, bid=303)
        p4 = self.db.get(Player, bid=304)
        t1 = self.db.all(Team)[0]
        self.assertEqual(1, len(self.db.all(Team)))
        self.assertEqual(Mode.TEAM_4V4, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(p2.id, t1.member1_id)
        self.assertEqual(p3.id, t1.member2_id)
        self.assertEqual(p4.id, t1.member3_id)
        self.assertEqual(Race.ZERG,    t1.race0)
        self.assertEqual(Race.PROTOSS, t1.race1)
        self.assertEqual(Race.TERRAN,  t1.race2)
        self.assertEqual(Race.TERRAN,  t1.race3)

    def test_process_two_4v4_ladders_with_the_same_team_but_different_order_just_creates_one_team(self):
        self.process_ladder(mode=Mode.TEAM_4V4,
                            members=[gen_member(bid=301, race=Race.ZERG),
                                     gen_member(bid=302, race=Race.PROTOSS),
                                     gen_member(bid=303, race=Race.TERRAN),
                                     gen_member(bid=304, race=Race.RANDOM)])

        self.process_ladder(mode=Mode.TEAM_4V4,
                            members=[gen_member(bid=304, race=Race.RANDOM),
                                     gen_member(bid=303, race=Race.TERRAN),
                                     gen_member(bid=302, race=Race.PROTOSS),
                                     gen_member(bid=301, race=Race.ZERG)])

        self.assertEqual(1, len(self.db.all(Team)))

        p1 = self.db.get(Player, bid=301)
        p2 = self.db.get(Player, bid=302)
        p3 = self.db.get(Player, bid=303)
        p4 = self.db.get(Player, bid=304)
        t1 = self.db.all(Team)[0]

        self.assertEqual(1, len(self.db.all(Team)))
        self.assertEqual(Mode.TEAM_4V4, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(p2.id, t1.member1_id)
        self.assertEqual(p3.id, t1.member2_id)
        self.assertEqual(p4.id, t1.member3_id)
        self.assertEqual(Race.ZERG,    t1.race0)
        self.assertEqual(Race.PROTOSS, t1.race1)
        self.assertEqual(Race.TERRAN,  t1.race2)
        self.assertEqual(Race.RANDOM,  t1.race3)

    def test_process_two_3v3_ladders_with_the_same_team_but_different_order_just_creates_one_team(self):
        self.process_ladder(mode=Mode.TEAM_3V3,
                            members=[gen_member(bid=301, race=Race.ZERG),
                                     gen_member(bid=302, race=Race.PROTOSS),
                                     gen_member(bid=303, race=Race.TERRAN)])

        self.process_ladder(mode=Mode.TEAM_3V3,
                            members=[gen_member(bid=303, race=Race.TERRAN),
                                     gen_member(bid=302, race=Race.PROTOSS),
                                     gen_member(bid=301, race=Race.ZERG)])

        self.assertEqual(1, len(self.db.all(Team)))

        p1 = self.db.get(Player, bid=301)
        p2 = self.db.get(Player, bid=302)
        p3 = self.db.get(Player, bid=303)
        t1 = self.db.all(Team)[0]

        self.assertEqual(Mode.TEAM_3V3, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(p2.id, t1.member1_id)
        self.assertEqual(p3.id, t1.member2_id)
        self.assertEqual(None,  t1.member3_id)
        self.assertEqual(Race.ZERG,     t1.race0)
        self.assertEqual(Race.PROTOSS,  t1.race1)
        self.assertEqual(Race.TERRAN,   t1.race2)
        self.assertEqual(Race.UNKNOWN,  t1.race3)

    def test_process_two_2v2_ladders_with_the_same_team_but_different_order_just_creates_one_team(self):
        self.process_ladder(mode=Mode.TEAM_2V2, members=[gen_member(bid=301, race=Race.ZERG),
                                                         gen_member(bid=302, race=Race.PROTOSS)])

        self.assertEqual(1, len(self.db.all(Team)))

        p1 = self.db.get(Player, bid=301)
        p2 = self.db.get(Player, bid=302)
        t1 = self.db.all(Team)[0]

        self.assertEqual(Mode.TEAM_2V2, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(p2.id, t1.member1_id)
        self.assertEqual(None,  t1.member2_id)
        self.assertEqual(None,  t1.member3_id)
        self.assertEqual(Race.ZERG,     t1.race0)
        self.assertEqual(Race.PROTOSS,  t1.race1)
        self.assertEqual(Race.UNKNOWN,  t1.race2)
        self.assertEqual(Race.UNKNOWN,  t1.race3)

    def test_existent_random_4v4_is_updated_with_changed_race(self):
        p1 = self.db.create_player(bid=301)
        t1 = self.db.create_team(mode=Mode.RANDOM_4V4,
                                 member0=p1,
                                 season=self.db.season,
                                 race0=Race.TERRAN)

        self.process_ladder(mode=Mode.RANDOM_4V4, bid=301, race=Race.ZERG)

        self.assertEqual(1, len(self.db.all(Team)))

        p1 = self.db.get(Player, bid=301)
        t1 = self.db.get(Team, id=t1.pk)
        self.assertEqual(1, len(Team.objects.all()))
        self.assertEqual(Mode.RANDOM_4V4, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(None,  t1.member1_id)
        self.assertEqual(None,  t1.member2_id)
        self.assertEqual(None,  t1.member3_id)
        self.assertEqual(Race.ZERG,    t1.race0)
        self.assertEqual(Race.UNKNOWN, t1.race1)
        self.assertEqual(Race.UNKNOWN, t1.race2)
        self.assertEqual(Race.UNKNOWN, t1.race3)

    def test_existent_2v2_random_team_is_updated_even_if_there_is_a_3v3_random_team_with_the_same_member(self):
        p1 = self.db.create_player(bid=301)

        t1 = self.db.create_team(mode=Mode.RANDOM_2V2,
                                 season=self.db.season,
                                 member0=p1,
                                 race0=Race.TERRAN)

        t2 = self.db.create_team(mode=Mode.RANDOM_3V3,
                                 season=self.db.season,
                                 member0=p1,
                                 race0=Race.RANDOM)

        self.process_ladder(mode=Mode.RANDOM_2V2, bid=301, race=Race.ZERG)

        self.assertEqual(2, len(Team.objects.all()))

        p1 = self.db.get(Player, bid=301)

        t1 = self.db.get(Team, id=t1.pk)
        self.assertEqual(Mode.RANDOM_2V2, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(None,  t1.member1_id)
        self.assertEqual(None,  t1.member2_id)
        self.assertEqual(None,  t1.member3_id)
        self.assertEqual(Race.ZERG,    t1.race0)
        self.assertEqual(Race.UNKNOWN, t1.race1)
        self.assertEqual(Race.UNKNOWN, t1.race2)
        self.assertEqual(Race.UNKNOWN, t1.race3)

        t2 = self.db.get(Team, id=t2.pk)
        self.assertEqual(Mode.RANDOM_3V3, t2.mode)
        self.assertEqual(p1.id, t2.member0_id)
        self.assertEqual(None,  t2.member1_id)
        self.assertEqual(None,  t2.member2_id)
        self.assertEqual(None,  t2.member3_id)
        self.assertEqual(Race.RANDOM,  t2.race0)
        self.assertEqual(Race.UNKNOWN, t2.race1)
        self.assertEqual(Race.UNKNOWN, t2.race2)
        self.assertEqual(Race.UNKNOWN, t2.race3)

    def test_existent_1v1_team_is_updated_on_league_change_to_better_league(self):
        p1 = self.db.create_player(bid=301)
        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.db.season,
                                 member0=p1,
                                 league=League.GOLD)

        self.process_ladder(mode=Mode.TEAM_1V1, league=League.PLATINUM, bid=301, race=Race.ZERG)

        self.assertEqual(1, len(Team.objects.all()))

        p1 = self.db.get(Player, bid=301)
        t1 = self.db.get(Team, id=t1.pk)
        self.assertEqual(1, len(Team.objects.all()))
        self.assertEqual(Mode.TEAM_1V1, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(None,  t1.member1_id)
        self.assertEqual(None,  t1.member2_id)
        self.assertEqual(None,  t1.member3_id)
        self.assertEqual(League.PLATINUM, t1.league)
        self.assertEqual(Race.ZERG, t1.race0)
        self.assertEqual(Race.UNKNOWN, t1.race1)
        self.assertEqual(Race.UNKNOWN, t1.race2)
        self.assertEqual(Race.UNKNOWN, t1.race3)

    def test_existent_1v1_team_is_not_updated_on_league_change_to_worse(self):
        p1 = self.db.create_player(bid=301)
        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.db.season,
                                 member0=p1,
                                 league=League.GOLD)

        self.process_ladder(mode=Mode.TEAM_1V1, league=League.SILVER, bid=301, race=Race.ZERG)

        self.assertEqual(1, len(Team.objects.all()))

        p1 = self.db.get(Player, bid=301)
        t1 = self.db.get(Team, id=t1.pk)
        self.assertEqual(1, len(Team.objects.all()))
        self.assertEqual(Mode.TEAM_1V1, t1.mode)
        self.assertEqual(p1.id, t1.member0_id)
        self.assertEqual(None,  t1.member1_id)
        self.assertEqual(None,  t1.member2_id)
        self.assertEqual(None,  t1.member3_id)
        self.assertEqual(League.GOLD, t1.league)
        self.assertEqual(Race.ZERG, t1.race0)
        self.assertEqual(Race.UNKNOWN, t1.race1)
        self.assertEqual(Race.UNKNOWN, t1.race2)
        self.assertEqual(Race.UNKNOWN, t1.race3)
Beispiel #16
0
 def setUpClass(self):
     super().setUpClass()
     self.runner = django.test.runner.DiscoverRunner(interactive=False)
     django.test.utils.setup_test_environment()
     self.old_config = self.runner.setup_databases()
     self.db = Db()
Beispiel #17
0
class DjangoTestCase(TestCase):
    # This is really ugly hack of django test framework. This was made a long time ago maybe possible to make it
    # better now, since djanog test framwork have been changed a lot. The c++ code needs to access the database so
    # the django postgresql test rollback scheme does not really work. Since using postgresql like this makes the
    # tests so slow sqlite is used for tests that doew not require postgresql. This makes it impossible to run
    # them in the same process.
    #
    # Sometimes it is useful to debug the db. Set the KEEP_DATA environment variable to prevent deletion of the
    # database.

    maxDiff = 1e4

    def __str__(self):
        """ Return a string that can be used as a command line argument to nose. """
        return "%s:%s.%s" % (inspect.getfile(
            self.__class__), self.__class__.__name__, self._testMethodName)

    @classmethod
    def _enter_atomics(cls):
        # Prevent rollbacks.
        pass

    @classmethod
    def _rollback_atomics(cls, atomics):
        # Prevent rollbacks.
        pass

    def _fixture_teardown(self):
        # Prevent clearing of test data.
        pass

    @classmethod
    def setUpClass(self):
        super().setUpClass()
        self.runner = django.test.runner.DiscoverRunner(interactive=False)
        django.test.utils.setup_test_environment()
        self.old_config = self.runner.setup_databases()
        self.db = Db()

    @classmethod
    def tearDownClass(self):
        if 'KEEP_DATA' in os.environ:
            print("\nkeeping test database: %r." % self.db.db_name,
                  file=sys.stderr)
        else:
            self.db.delete_all()
            self.runner.teardown_databases(self.old_config)
            django.test.utils.teardown_test_environment()
        super().tearDownClass()

    def tearDown(self):
        if hasattr(self, 'cpp') and self.cpp is not None:
            self.cpp.release()
            self.cpp = None
        super().tearDown()

    def load(self):
        self.cpp = sc2.RankingData(self.db.db_name, Enums.INFO)
        self.cpp.load(self.db.ranking.id)

    def process_ladder(self,
                       load=False,
                       save=False,
                       region=Region.EU,
                       fetch_time=None,
                       mode=Mode.TEAM_1V1,
                       version=Version.HOTS,
                       league=League.GOLD,
                       season=None,
                       tier=0,
                       members=None,
                       **kwargs):
        """ Update a ranking building single member with kwargs or use members if set. """

        season = season or self.db.season
        fetch_time = fetch_time or utcnow()
        members = members or [gen_member(**kwargs)]

        if not getattr(self, 'cpp', None):
            self.cpp = sc2.RankingData(self.db.db_name, Enums.INFO)

        if load:
            self.load()

        self.cpp.update_with_ladder(
            0,  # bid
            0,  # source_id
            region,
            mode,
            league,
            tier,
            version,
            season.id,
            to_unix(fetch_time),
            Mode.team_size(mode),
            members)
        if save:
            self.save_to_ranking()

    def save_to_ranking(self):
        self.cpp.save_data(self.db.ranking.id, self.db.ranking.season_id,
                           to_unix(utcnow()))

    @classinstancemethod
    def date(self, **kwargs):
        return self.today + timedelta(**kwargs)

    @classinstancemethod
    def datetime(self, **kwargs):
        return self.now + timedelta(**kwargs)

    @classinstancemethod
    def unix_time(self, **kwargs):
        return to_unix(self.now + timedelta(**kwargs))

    def assert_team_ranks(self, ranking_id, *ranks, skip_len=False, sort=True):
        """ Get all team ranks using the current ranking id and assert that all ranks corresponds to team ranks in
        db. All keys in ranks will be verified against team ranks values. """

        team_ranks = sc2.get_team_ranks(self.db.db_name, ranking_id, sort)

        all_keys = {key for rank in ranks for key in rank}
        try:
            if not skip_len:
                self.assertEqual(len(team_ranks), len(ranks))
            for i, (team_rank, r) in enumerate(zip(team_ranks, ranks),
                                               start=1):
                for key, value in r.items():
                    self.assertEqual(
                        value,
                        team_rank[key],
                        msg="%s wrong @ rank %d, expected %r, was %r" %
                        (key, i, r,
                         {key: team_rank.get(key, None)
                          for key in r.keys()}))
        except AssertionError:
            print("Expected:\n%s" % "\n".join([repr(tr) for tr in ranks]))
            print("Actual:\n%s" % "\n".join([
                repr({k: v
                      for k, v in tr.items() if k in all_keys})
                for tr in team_ranks
            ]))
            raise
Beispiel #18
0
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db.create_cache()
        self.db.create_season()
        self.db.create_ranking()

    def setUp(self):
        super().setUp()
        self.db.delete_all(keep=[Cache, Season, Ranking])
        self.c = Client()

    def test_search_by_name(self):
        p = self.db.create_player(name="arne")

        response = self.c.get('/search/', {'name': 'arne'})

        self.assertEqual(200, response.status_code)
        self.assertEqual([p], response.context['items'])
        self.assertIsNone(response.context['prev'])
        self.assertIsNone(response.context['next'])

    def test_search_by_name_prefix(self):
        p = self.db.create_player(name="sunexxxyz")

        response = self.c.get('/search/', {'name': 'sune'})

        self.assertEqual(200, response.status_code)
        self.assertEqual([p], response.context['items'])
        self.assertIsNone(response.context['prev'])
        self.assertIsNone(response.context['next'])

    def test_search_not_long_enough(self):
        response = self.c.get('/search/', {'name': 'n'})

        self.assertEqual(200, response.status_code)
        self.assertEqual(True, response.context['no_search'])

    def test_search_by_url(self):
        p = self.db.create_player(name="Kuno", bid=927, region=Region.EU, realm=1)
        p = self.db.create_player(name="Kuno", bid=927, region=Region.AM, realm=1)
        p = self.db.create_player(name="Kuno", bid=927, region=Region.KR, realm=1)
        p = self.db.create_player(name="Kuno", bid=927, region=Region.CN, realm=1)
        p = self.db.create_player(name="Kuno", bid=927, region=Region.SEA, realm=1)

        response = self.c.get('/search/', {'name': 'http://eu.battle.net/sc2/en/profile/927/1/Kuno/'})

        self.assertEqual(302, response.status_code)
        self.assertTrue('/player/' in response.url)

        response = self.c.get('/search/', {'name': 'http://us.battle.net/sc2/en/profile/927/1/Kuno/'})

        self.assertEqual(302, response.status_code)
        self.assertTrue('/player/' in response.url)

        response = self.c.get('/search/', {'name': 'http://kr.battle.net/sc2/en/profile/927/1/Kuno/'})

        self.assertEqual(302, response.status_code)
        self.assertTrue('/player/' in response.url)

        response = self.c.get('/search/', {'name': 'http://sea.battle.net/sc2/en/profile/927/1/Kuno/'})

        self.assertEqual(302, response.status_code)
        self.assertTrue('/player/' in response.url)

        response = self.c.get('/search/', {'name': 'http://www.battlenet.com.cn/sc2/en/profile/927/1/Kuno/'})

        self.assertEqual(302, response.status_code)
        self.assertTrue('/player/' in response.url)

    def test_pagination(self):
        for i in range(40):
            self.db.create_player(bid=300 + i, name='sune')

        response = self.c.get('/search/', {'name': 'sune'})

        self.assertEqual(200, response.status_code)
        self.assertEqual(32, len(response.context['items']))
        self.assertIsNone(response.context['prev'])
        self.assertEqual(32, response.context['next'])

        response = self.c.get('/search/', {'name': 'sune', 'offset': response.context['next']})

        self.assertEqual(200, response.status_code)
        self.assertEqual(8, len(response.context['items']))
        self.assertEqual(0, response.context['prev'])
        self.assertIsNone(response.context['next'])

    def test_json_api_search_by_name(self):
        p = self.db.create_player(bid=300, name='sune', race=Race.ZERG, league=League.GOLD, mode=Mode.TEAM_1V1)

        response = self.c.get('/search/', {'name': 'sune', 'json': ''})

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(1, data['count'])
        self.assertEqual(0, data['offset'])
        self.assertEqual([
            {
                'name': 'sune',
                'tag': p.tag,
                'clan': p.clan,
                'race': 'zerg',
                'mode': '1v1',
                'bnet_url': 'http://eu.battle.net/sc2/en/profile/300/0/sune/',
                'region': 'eu',
                'league': 'gold',
                'season': self.db.season.id,
            }
        ], data['items'])

    def test_json_api_search_by_name_prefix_several_hits(self):
        p1 = self.db.create_player(bid=300, name='sune',
                                   race=Race.ZERG, league=League.GOLD, mode=Mode.TEAM_1V1)
        p2 = self.db.create_player(bid=301, name='sunebune',
                                   race=Race.TERRAN, league=League.PLATINUM, mode=Mode.ARCHON)
        p3 = self.db.create_player(bid=302, name='sunerune',
                                   race=Race.RANDOM, league=League.PLATINUM, mode=Mode.TEAM_1V1)

        response = self.c.get('/search/', {'name': 'sune', 'json': ''})

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(3, data['count'])
        self.assertEqual(0, data['offset'])
        self.assertEqual([
            {
                'name': 'sune',
                'tag': p1.tag,
                'clan': p1.clan,
                'race': 'zerg',
                'mode': '1v1',
                'bnet_url': 'http://eu.battle.net/sc2/en/profile/300/0/sune/',
                'region': 'eu',
                'league': 'gold',
                'season': self.db.season.id,
            },
            {
                'name': 'sunerune',
                'tag': p3.tag,
                'clan': p3.clan,
                'race': 'random',
                'mode': '1v1',
                'bnet_url': 'http://eu.battle.net/sc2/en/profile/302/0/sunerune/',
                'region': 'eu',
                'league': 'platinum',
                'season': self.db.season.id,
            },
            {
                'name': 'sunebune',
                'tag': p2.tag,
                'clan': p2.clan,
                'race': 'terran',
                'mode': 'archon',
                'bnet_url': 'http://eu.battle.net/sc2/en/profile/301/0/sunebune/',
                'region': 'eu',
                'league': 'platinum',
                'season': self.db.season.id,
            },
        ], data['items'])

    def test_json_api_search_with_no_hits(self):
        p = self.db.create_player(bid=300, name='sune')

        response = self.c.get('/search/', {'name': 'kuno', 'json': ''})

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(0, data['count'])
        self.assertEqual(0, data['offset'])
        self.assertEqual([], data['items'])

    def test_json_api_search_with_no_search(self):
        p = self.db.create_player(bid=300, name='sune')

        response = self.c.get('/search/', {'name': 'k', 'json': ''})

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(-1, data['count'])

    def test_json_api_search_by_prefix_and_offset(self):
        p1 = self.db.create_player(bid=300, name='sune',
                                   race=Race.ZERG, league=League.GOLD, mode=Mode.TEAM_1V1)
        p2 = self.db.create_player(bid=301, name='sunebune',
                                   race=Race.TERRAN, league=League.PLATINUM, mode=Mode.ARCHON)
        p3 = self.db.create_player(bid=302, name='sunerune',
                                   race=Race.RANDOM, league=League.PLATINUM, mode=Mode.TEAM_1V1)

        response = self.c.get('/search/', {'name': 'sune', 'json': '', 'offset': '2'})

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(3, data['count'])
        self.assertEqual(2, data['offset'])
        self.assertEqual([
            {
                'name': 'sunebune',
                'tag': p2.tag,
                'clan': p2.clan,
                'race': 'terran',
                'mode': 'archon',
                'bnet_url': 'http://eu.battle.net/sc2/en/profile/301/0/sunebune/',
                'region': 'eu',
                'league': 'platinum',
                'season': self.db.season.id,
            },
        ], data['items'])

    def test_json_api_search_by_prefix_and_offset_out_of_range(self):
        p = self.db.create_player(bid=300, name='sune', race=Race.ZERG, league=League.GOLD, mode=Mode.TEAM_1V1)

        response = self.c.get('/search/', {'name': 'sune', 'json': '', 'offset': '200'})

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(1, data['count'])
        self.assertEqual(200, data['offset'])
        self.assertEqual([], data['items'])

        response = self.c.get('/search/', {'name': 'sune', 'json': '', 'offset': '-1'})

        self.assertEqual(200, response.status_code)

        data = json.loads(response.content.decode('utf-8'))

        self.assertEqual(1, data['count'])
        self.assertEqual(0, data['offset'])
        self.assertEqual([{
            'name': 'sune',
            'tag': p.tag,
            'clan': p.clan,
            'race': 'zerg',
            'mode': '1v1',
            'bnet_url': 'http://eu.battle.net/sc2/en/profile/300/0/sune/',
            'region': 'eu',
            'league': 'gold',
            'season': self.db.season.id,
        }], data['items'])
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.db.create_season(id=29, version=Version.LOTV)
        self.t1, self.t2, self.t3, self.t4, self.t5 = self.db.create_teams(count=5)

    def setUp(self):
        super().setUp()
        self.db.clear_defaults()
        self.db.delete_all(keep=[Season, Ladder, Cache])
        self.c = sc2.Get(settings.DATABASES['default']['NAME'], Enums.INFO, 0)

    def tearDown(self):
        # Null and garbage collect to disconnect c++ code from the database (RAII).
        self.c = None
        gc.collect()
        super().tearDown()

    def test_all_races_are_found_and_returned(self):
        self.db.create_ranking()
        self.db.create_ranking_data(raw=False, data=[
            dict(team_id=self.t1.id, race0=Race.UNKNOWN, mmr=1),
            dict(team_id=self.t1.id, race0=Race.ZERG,    mmr=2),
            dict(team_id=self.t1.id, race0=Race.PROTOSS, mmr=6),
            dict(team_id=self.t1.id, race0=Race.TERRAN,  mmr=5),
            dict(team_id=self.t1.id, race0=Race.RANDOM,  mmr=4),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)
        self.assertEqual(5, len(rankings))

        self.assertEqual(Race.UNKNOWN, rankings[0]["race0"])
        self.assertEqual(Race.ZERG,    rankings[1]["race0"])
        self.assertEqual(Race.PROTOSS, rankings[2]["race0"])
        self.assertEqual(Race.TERRAN,  rankings[3]["race0"])
        self.assertEqual(Race.RANDOM,  rankings[4]["race0"])

        self.assertEqual(False, rankings[0]["best_race"])
        self.assertEqual(False, rankings[1]["best_race"])
        self.assertEqual(True,  rankings[2]["best_race"])
        self.assertEqual(False, rankings[3]["best_race"])
        self.assertEqual(False, rankings[4]["best_race"])

    def test_all_races_and_all_versions_are_present_at_end_of_ranking(self):
        teams = [self.t2, self.t3, self.t4, self.t5, self.t1]
        self.db.create_ranking()
        data = []
        mmr = 0
        for team in teams:
            mmr += 100
            for version in Version.ids:
                for race in Race.ranking_ids:
                    if race != Race.UNKNOWN:
                        data.append(dict(team_id=team.id, race0=race, mmr=mmr + race, version=version))
        self.db.create_ranking_data(raw=False, data=data)

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(4, len(rankings))

        self.assertEqual(Race.ZERG,    rankings[0]["race0"])
        self.assertEqual(Race.PROTOSS, rankings[1]["race0"])
        self.assertEqual(Race.TERRAN,  rankings[2]["race0"])
        self.assertEqual(Race.RANDOM,  rankings[3]["race0"])

        self.assertEqual(500, rankings[0]["mmr"])
        self.assertEqual(501, rankings[1]["mmr"])
        self.assertEqual(502, rankings[2]["mmr"])
        self.assertEqual(503, rankings[3]["mmr"])

        self.assertEqual(Version.LOTV, rankings[0]["version"])
        self.assertEqual(Version.LOTV, rankings[1]["version"])
        self.assertEqual(Version.LOTV, rankings[2]["version"])
        self.assertEqual(Version.LOTV, rankings[3]["version"])

        self.assertEqual(False, rankings[0]["best_race"])
        self.assertEqual(False, rankings[1]["best_race"])
        self.assertEqual(False, rankings[2]["best_race"])
        self.assertEqual(True,  rankings[3]["best_race"])

    def test_all_races_and_all_versions_are_present_at_beginning_of_ranking(self):
        teams = [self.t1, self.t2, self.t3, self.t4, self.t5]
        self.db.create_ranking()
        data = []
        mmr = 0
        for team in teams:
            mmr += 100
            for version in Version.ids:
                for race in Race.ranking_ids:
                    if race != Race.UNKNOWN:
                        data.append(dict(team_id=team.id, race0=race, mmr=mmr + race, version=version))
        self.db.create_ranking_data(raw=False, data=data)

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(4, len(rankings))

        self.assertEqual(Race.ZERG,    rankings[0]["race0"])
        self.assertEqual(Race.PROTOSS, rankings[1]["race0"])
        self.assertEqual(Race.TERRAN,  rankings[2]["race0"])
        self.assertEqual(Race.RANDOM,  rankings[3]["race0"])

        self.assertEqual(100, rankings[0]["mmr"])
        self.assertEqual(101, rankings[1]["mmr"])
        self.assertEqual(102, rankings[2]["mmr"])
        self.assertEqual(103, rankings[3]["mmr"])

        self.assertEqual(Version.LOTV, rankings[0]["version"])
        self.assertEqual(Version.LOTV, rankings[1]["version"])
        self.assertEqual(Version.LOTV, rankings[2]["version"])
        self.assertEqual(Version.LOTV, rankings[3]["version"])

        self.assertEqual(False, rankings[0]["best_race"])
        self.assertEqual(False, rankings[1]["best_race"])
        self.assertEqual(False, rankings[2]["best_race"])
        self.assertEqual(True,  rankings[3]["best_race"])

    def test_finds_one_wol_race_among_many(self):
        teams = [self.t2, self.t3, self.t4, self.t5]
        self.db.create_ranking()
        data = []
        mmr = 0
        for team in teams:
            mmr += 100
            for version in Version.ids:
                for race in Race.ranking_ids:
                    if race != Race.UNKNOWN:
                        data.append(dict(team_id=team.id, race0=race, mmr=mmr + race, version=version))
        data.append(dict(team_id=self.t1.id, race0=Race.ZERG, mmr=120, version=Version.WOL))

        self.db.create_ranking_data(raw=False, data=data)

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))

        self.assertEqual(Race.ZERG, rankings[0]["race0"])
        self.assertEqual(120, rankings[0]["mmr"])
        self.assertEqual(Version.WOL, rankings[0]["version"])
        self.assertEqual(True, rankings[0]["best_race"])

    def test_finds_one_among_many_of_same_version__this_was_a_bug__returned_teams_after(self):
        self.db.create_ranking()
        self.db.create_ranking_data(raw=False, data=[
            dict(team_id=self.t1.id, race0=Race.UNKNOWN),
            dict(team_id=self.t1.id, race0=Race.ZERG),
            dict(team_id=self.t1.id, race0=Race.PROTOSS),
            dict(team_id=self.t1.id, race0=Race.RANDOM),
            dict(team_id=self.t1.id, race0=Race.TERRAN),
            dict(team_id=self.t2.id, race0=Race.UNKNOWN),
            dict(team_id=self.t2.id, race0=Race.ZERG),
            dict(team_id=self.t2.id, race0=Race.PROTOSS),
            dict(team_id=self.t2.id, race0=Race.RANDOM),
            dict(team_id=self.t2.id, race0=Race.TERRAN),
            dict(team_id=self.t3.id, race0=Race.UNKNOWN),
            dict(team_id=self.t4.id, race0=Race.UNKNOWN),
            dict(team_id=self.t4.id, race0=Race.ZERG),
            dict(team_id=self.t4.id, race0=Race.PROTOSS),
            dict(team_id=self.t4.id, race0=Race.RANDOM),
        ])

        rankings = self.c.rankings_for_team(self.t3.id)

        self.assertEqual(1, len(rankings))

    def test_find_latest_version_in_same_window__this_was_a_bug_returned_both_versions(self):
        self.db.create_ranking()
        self.db.create_ranking_data(raw=False, data=[
            dict(team_id=self.t1.id, mode=Mode.RANDOM_2V2, version=Version.WOL),
            dict(team_id=self.t2.id, mode=Mode.RANDOM_2V2, version=Version.WOL),
            dict(team_id=self.t3.id, mode=Mode.RANDOM_2V2, version=Version.WOL),
            dict(team_id=self.t3.id, mode=Mode.RANDOM_2V2, version=Version.LOTV),
            dict(team_id=self.t4.id, mode=Mode.RANDOM_2V2, version=Version.WOL),
        ])

        rankings = self.c.rankings_for_team(self.t3.id)

        self.assertEqual(1, len(rankings))
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db.create_season(id=1)

    def setUp(self):
        super().setUp()
        self.now = utcnow()
        self.db.delete_all(keep=[Season])

    def test_non_existent_player_is_created(self):
        self.process_ladder(bid=301,
                            realm=0,
                            name="player_name",
                            tag="EE",
                            clan="EE is our name")

        p = self.db.get(Player, bid=301)

        self.assertEqual(1, len(Player.objects.all()))
        self.assertEqual(0, p.realm)
        self.assertEqual(Region.EU, p.region)
        self.assertEqual("player_name", p.name)
        self.assertEqual("EE", p.tag)
        self.assertEqual("EE is our name", p.clan)

    def test_existing_player_is_updated_with_new_name_tag_clan_and_existing_other_player_is_not_touched(
            self):
        # See also whole file with player update tests.

        p1 = self.db.create_player(region=Region.EU,
                                   bid=301,
                                   realm=0,
                                   race=Race.ZERG,
                                   name="arne1",
                                   tag="arne1",
                                   clan="arne1")
        p2 = self.db.create_player(region=Region.EU,
                                   bid=302,
                                   realm=0,
                                   race=Race.ZERG,
                                   name="arne2",
                                   tag="arne2",
                                   clan="arne2")

        self.process_ladder(bid=301,
                            realm=0,
                            race=Race.ZERG,
                            name="player_name",
                            tag="EE",
                            clan="EE is our name")

        self.assertEqual(2, len(Player.objects.all()))

        p1 = self.db.get(Player, bid=301)
        self.assertEqual(0, p1.realm)
        self.assertEqual(Region.EU, p1.region)
        self.assertEqual("player_name", p1.name)
        self.assertEqual("EE", p1.tag)
        self.assertEqual("EE is our name", p1.clan)

        p2 = self.db.get(Player, bid=302)
        self.assertEqual(0, p2.realm)
        self.assertEqual(Region.EU, p2.region)
        self.assertEqual("arne2", p2.name)
        self.assertEqual("arne2", p2.tag)
        self.assertEqual("arne2", p2.clan)

    def test_player_occurs_in_several_teams_in_the_same_ladder_creates_one_player(
            self):
        # See our friend drake (bid 5305519, realm 1, region 1) in ladder 164930.

        self.db.create_ranking()
        self.process_ladder(mode=Mode.TEAM_2V2,
                            members=[
                                gen_member(bid=301,
                                           realm=0,
                                           name="arne1",
                                           race=Race.TERRAN),
                                gen_member(bid=302,
                                           realm=0,
                                           name="arne2",
                                           race=Race.TERRAN),
                                gen_member(bid=303,
                                           realm=0,
                                           name="arne3",
                                           race=Race.TERRAN),
                                gen_member(bid=301,
                                           realm=0,
                                           name="arne1",
                                           race=Race.TERRAN),
                            ])

        self.assertEqual(3, len(Player.objects.all()))

        p1 = self.db.get(Player, bid=301)
        p2 = self.db.get(Player, bid=302)
        p3 = self.db.get(Player, bid=303)

        self.assertEqual(2, len(Team.objects.all()))

        t1, t2 = self.db.filter(Team,
                                member1_id__in=(p2.id, p3.id)).order_by('id')

        self.assertEqual(t1.member0_id, p1.id)
        self.assertEqual(t1.member1_id, p2.id)
        self.assertEqual(t2.member0_id, p1.id)
        self.assertEqual(t2.member1_id, p3.id)

        self.assertEqual('arne1', p1.name)
        self.assertEqual('arne2', p2.name)
        self.assertEqual('arne3', p3.name)
Beispiel #21
0
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.s1 = self.db.create_season(id=1)
        self.s2 = self.db.create_season(id=2)
        self.s3 = self.db.create_season(id=3)

    def setUp(self):
        super().setUp()
        self.now = utcnow()
        self.db.delete_all(keep=[Season])
        self.ladder = self.db.create_ladder(
            bid=1,
            created=self.datetime(seconds=1))  # Always using same ladder.

    def test_team_is_updated_with_both_version_and_league_with_new_season(
            self):
        p1 = self.db.create_player(bid=301, race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.s1,
                                 version=Version.HOTS,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.db.create_ranking(season=self.s2)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            season=self.s2,
                            version=Version.WOL,
                            league=League.GOLD,
                            members=[gen_member(bid=301, race=Race.ZERG)])

        self.assertEqual(1, len(Team.objects.all()))

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s2, t1.season)
        self.assertEqual(Version.WOL, t1.version)
        self.assertEqual(League.GOLD, t1.league)

    def test_team_is_updated_with_both_version_and_league_with_new_season_test_2(
            self):
        p1 = self.db.create_player(bid=301, race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.s1,
                                 version=Version.WOL,
                                 league=League.GOLD,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.db.create_ranking(season=self.s2)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            season=self.s2,
                            version=Version.HOTS,
                            league=League.PLATINUM,
                            members=[gen_member(bid=301, race=Race.ZERG)])

        self.assertEqual(1, len(Team.objects.all()))

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s2, t1.season)
        self.assertEqual(Version.HOTS, t1.version)
        self.assertEqual(League.PLATINUM, t1.league)

    def test_update_of_version_to_later_version_is_updated_within_same_season(
            self):
        p1 = self.db.create_player(bid=301, race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.s1,
                                 version=Version.WOL,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            season=self.s1,
                            version=Version.HOTS,
                            league=League.GOLD,
                            members=[gen_member(bid=301, race=Race.ZERG)])

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s1, t1.season)
        self.assertEqual(Version.HOTS, t1.version)
        self.assertEqual(League.GOLD, t1.league)

    def test_update_of_version_to_earlier_version_is_not_updated_when_in_same_season(
            self):
        p1 = self.db.create_player(bid=301, race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.s1,
                                 version=Version.HOTS,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.process_ladder(mode=Mode.TEAM_1V1,
                            season=self.s1,
                            version=Version.WOL,
                            league=League.GRANDMASTER,
                            bid=301,
                            race=Race.ZERG)

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s1, t1.season)
        self.assertEqual(Version.HOTS, t1.version)
        self.assertEqual(League.PLATINUM, t1.league)

    def test_update_of_league_is_updated_when_version_and_season_is_then_same_1(
            self):
        p1 = self.db.create_player(bid=301, race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.RANDOM_2V2,
                                 season=self.s1,
                                 version=Version.HOTS,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.process_ladder(mode=Mode.RANDOM_2V2,
                            season=self.s1,
                            version=Version.HOTS,
                            league=League.GRANDMASTER,
                            bid=301,
                            race=Race.ZERG)

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s1, t1.season)
        self.assertEqual(Version.HOTS, t1.version)
        self.assertEqual(League.GRANDMASTER, t1.league)

    def test_update_of_league_is_updated_when_version_and_season_is_then_same_2(
            self):

        p1 = self.db.create_player(bid=301, race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.RANDOM_2V2,
                                 season=self.s1,
                                 version=Version.HOTS,
                                 league=League.GRANDMASTER,
                                 member0=p1,
                                 race0=Race.ZERG)

        self.process_ladder(mode=Mode.RANDOM_2V2,
                            season=self.s1,
                            version=Version.HOTS,
                            league=League.PLATINUM,
                            bid=301,
                            race=Race.ZERG)

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s1, t1.season)
        self.assertEqual(Version.HOTS, t1.version)
        self.assertEqual(League.PLATINUM, t1.league)

    def test_three_ladders_to_see_that_correct_team_version_sticks_in_cache(
            self):

        p1 = self.db.create_player(bid=301, race=Race.ZERG)

        t1 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.s1,
                                 version=Version.HOTS,
                                 league=League.PLATINUM,
                                 member0=p1,
                                 race0=Race.ZERG)

        # Loaded to cache, not updated.

        self.process_ladder(mode=Mode.TEAM_1V1,
                            season=self.s1,
                            version=Version.HOTS,
                            league=League.PLATINUM,
                            bid=301,
                            race=Race.ZERG)

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s1, t1.season)
        self.assertEqual(Version.HOTS, t1.version)
        self.assertEqual(League.PLATINUM, t1.league)

        # Updated in db (and hopefully in cache).

        self.process_ladder(mode=Mode.TEAM_1V1,
                            season=self.s3,
                            version=Version.WOL,
                            league=League.GOLD,
                            bid=301,
                            race=Race.ZERG)

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s3, t1.season)
        self.assertEqual(Version.WOL, t1.version)
        self.assertEqual(League.GOLD, t1.league)

        # Not updated if compared to cached version as it should.

        self.process_ladder(mode=Mode.TEAM_1V1,
                            season=self.s2,
                            version=Version.LOTV,
                            league=League.GRANDMASTER,
                            bid=301,
                            race=Race.ZERG)

        t1 = self.db.get(Team, member0=self.db.get(Player, bid=301))

        self.assertEqual(self.s3, t1.season)
        self.assertEqual(Version.WOL, t1.version)
        self.assertEqual(League.GOLD, t1.league)
Beispiel #22
0
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.db.create_cache()
        self.s1 = self.db.create_season(id=16)
        self.s2 = self.db.create_season(id=17)
        self.db.create_ranking()

    def setUp(self):
        super().setUp()
        cache.clear()
        self.db.delete_all(keep=[Season, Cache, Ranking])
        self.c = Client()

    def test_view_player(self):
        p = self.db.create_player(name="arne")
        t = self.db.create_team(season=self.s2)

        response = self.c.get('/player/%d/' % p.id)

        self.assertEqual(200, response.status_code)
        self.assertEqual(p, response.context['player'])
        teams = response.context['teams']
        self.assertEqual([t.id], [t.id for t in teams])
        self.assertEqual('/ladder/hots/1v1/ladder-rank/?team=%d' % t.id,
                         teams[0].ladder_url)

    def test_view_player_many_teams(self):
        p0 = self.db.create_player(name="arne0", bid=301)
        p1 = self.db.create_player(name="arne1", bid=302)
        p1 = self.db.create_player(name="arne2", bid=303)
        p2 = self.db.create_player(name="arne3", bid=304)
        p3 = self.db.create_player(name="arne4", bid=305)
        p4 = self.db.create_player(name="arne5", bid=306)
        t0 = self.db.create_team(mode=Mode.TEAM_1V1,
                                 season=self.s1,
                                 member0=p0)
        t1 = self.db.create_team(mode=Mode.TEAM_4V4,
                                 season=self.s1,
                                 member0=p3,
                                 member1=p2,
                                 member2=p1,
                                 member3=p0)
        t2 = self.db.create_team(mode=Mode.TEAM_2V2,
                                 season=self.s1,
                                 member0=p0,
                                 member1=p4)
        t3 = self.db.create_team(mode=Mode.ARCHON,
                                 season=self.s1,
                                 member0=p0,
                                 member1=p1)

        response = self.c.get('/player/%d/' % p0.id)

        self.assertEqual(200, response.status_code)
        self.assertEqual(p0, response.context['player'])
        teams = response.context['teams']
        self.assertEqual([t0.id, t3.id, t2.id, t1.id], [t.id for t in teams])
        self.assertEqual([False, False, False, False],
                         [hasattr(t, 'ladder_url') for t in teams])
Beispiel #23
0
 def setUpClass(self):
     super(Test, self).setUpClass()
     self.db = Db()
     self.s1 = self.db.create_season(id=1)
     self.s2 = self.db.create_season(id=2)
Beispiel #24
0
 def setUpClass(self):
     super(Test, self).setUpClass()
     self.db = Db()
Beispiel #25
0
 def setUpClass(self):
     super().setUpClass()
     self.db = Db()
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.db.create_season(id=27, version=Version.LOTV)
        self.t1, self.t2, self.t3 = self.db.create_teams(count=3)

    def setUp(self):
        super().setUp()
        self.db.clear_defaults()
        self.db.delete_all(keep=[Season, Ladder, Cache])
        self.c = sc2.Get(settings.DATABASES['default']['NAME'], Enums.INFO, 0)

    def tearDown(self):
        # Null and garbage collect to disconnect c++ code from the database (RAII).
        self.c = None
        gc.collect()
        super().tearDown()

    def test_get_team_from_ranking_with_only_one_team(self):

        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, points=8912),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(8912, rankings[0]["points"])

        rankings = self.c.rankings_for_team(self.t1.id + 1)

        self.assertEqual(0, len(rankings))

    def test_get_team_from_tanking_with_two_different_teams(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, points=8912),
            dict(team_id=self.t2.id, points=1234),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(8912, rankings[0]["points"])

        rankings = self.c.rankings_for_team(self.t2.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(1234, rankings[0]["points"])

    def test_get_team_from_ranking_with_two_rankings_from_same_team_but_different_versions_1(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, points=8912, version=Version.WOL),
            dict(team_id=self.t1.id, points=1234, version=Version.HOTS),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(1234, rankings[0]["points"])
        self.assertEqual(Version.HOTS, rankings[0]["version"])

    def test_get_team_from_ranking_with_two_rankings_from_same_team_but_different_versions_2(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, points=8912, version=Version.WOL),
            dict(team_id=self.t1.id, points=1111, version=Version.LOTV),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(1111, rankings[0]["points"])
        self.assertEqual(Version.LOTV, rankings[0]["version"])

    def test_get_team_from_ranking_with_two_rankings_from_same_team_but_different_versions_3(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, points=1234, version=Version.HOTS),
            dict(team_id=self.t1.id, points=1111, version=Version.LOTV),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(1111, rankings[0]["points"])
        self.assertEqual(Version.LOTV, rankings[0]["version"])

    def test_get_team_from_ranking_with_tree_rankings_from_same_team_but_different_versions(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, points=8912, version=Version.WOL),
            dict(team_id=self.t1.id, points=1234, version=Version.HOTS),
            dict(team_id=self.t1.id, points=2222, version=Version.LOTV),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(2222, rankings[0]["points"])
        self.assertEqual(Version.LOTV, rankings[0]["version"])

    def test_get_from_ranking_with_tree_teams_of_which_one_has_all_versions_one_has_two_versions_and_one_only_wol(self):
        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=self.t1.id, points=1, version=Version.WOL),
            dict(team_id=self.t2.id, points=2, version=Version.WOL),
            dict(team_id=self.t3.id, points=3, version=Version.WOL),
            dict(team_id=self.t2.id, points=4, version=Version.HOTS),
            dict(team_id=self.t3.id, points=5, version=Version.HOTS),
            dict(team_id=self.t3.id, points=6, version=Version.LOTV),
        ])

        rankings = self.c.rankings_for_team(self.t1.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(Version.WOL, rankings[0]["version"])
        self.assertEqual(1, rankings[0]["points"])

        rankings = self.c.rankings_for_team(self.t2.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(Version.HOTS, rankings[0]["version"])
        self.assertEqual(4, rankings[0]["points"])

        rankings = self.c.rankings_for_team(self.t3.id)

        self.assertEqual(1, len(rankings))
        self.assertEqual(Version.LOTV, rankings[0]["version"])
        self.assertEqual(6, rankings[0]["points"])
Beispiel #27
0
class Test(DjangoTestCase):
    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()
        self.now = utcnow()
        self.today = self.now.date()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.s15 = self.db.create_season(id=15,
                                         start_date=self.date(days=-200),
                                         end_date=self.date(days=-101))
        self.s16 = self.db.create_season(id=16,
                                         start_date=self.date(days=-100),
                                         end_date=None)

    def setUp(self):
        super().setUp()
        cache.clear()
        self.db.clear_defaults()
        self.db.delete_all(keep=[Season, Cache, Ladder])
        self.c = Client()

    def tearDown(self):
        rankings_view_client.close()
        super(Test, self).tearDown()

    def test_simple_gets_with_various_filtering_and_sorting(self):

        p1 = self.db.create_player(tag='TL', clan='Team Liquid')
        p2 = self.db.create_player(tag='TL', clan='Team Liquid')

        t1 = self.db.create_team(member0=p1)
        t2 = self.db.create_team(member0=p2)

        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=t1.id,
                 league=League.GOLD,
                 region=Region.EU,
                 race0=Race.TERRAN,
                 points=20,
                 mmr=3,
                 version=Version.LOTV,
                 wins=100,
                 losses=100),
            dict(team_id=t2.id,
                 league=League.PLATINUM,
                 region=Region.AM,
                 race0=Race.ZERG,
                 points=40,
                 mmr=4,
                 version=Version.LOTV,
                 wins=200,
                 losses=200),
        ])

        def get_and_check(url, *players):
            response = self.c.get(url)
            self.assertEqual(200, response.status_code)
            teams = response.context['ladder']['teams']
            self.assertEqual(len(players), len(teams))
            for i, p in enumerate(players):
                self.assertEqual(p.name, teams[i]['m0_name'])

        get_and_check('/clan/TL/mmr/', p2, p1)
        get_and_check('/clan/TL/-played/', p1, p2)
        get_and_check('/clan/TL/wins/', p2, p1)
        get_and_check('/clan/TL/league-points/', p2, p1)

        get_and_check('/clan/TL/mmr/?f=terran', p1)
        get_and_check('/clan/TL/mmr/?f=am', p2)
        get_and_check('/clan/TL/mmr/?f=eu,zerg')

    def test_other_clan_season_mode_version_are_excluded(self):

        trs = []

        p = self.db.create_player(tag='TL',
                                  clan='Team Liquid',
                                  season=self.s16,
                                  mode=Mode.TEAM_1V1)
        t = self.db.create_team(member0=p,
                                mode=p.mode,
                                season=p.season,
                                version=Version.LOTV)
        trs.append(dict(team_id=t.id, mode=t.mode, version=t.version))
        p1 = p

        p = self.db.create_player(tag='XX',
                                  clan='Team Liquid',
                                  season=self.s16,
                                  mode=Mode.TEAM_1V1)
        t = self.db.create_team(member0=p,
                                mode=p.mode,
                                season=p.season,
                                version=Version.LOTV)
        trs.append(dict(team_id=t.id, mode=t.mode, version=t.version))

        p = self.db.create_player(tag='TL',
                                  clan='Team Liquid',
                                  season=self.s15,
                                  mode=Mode.TEAM_1V1)
        t = self.db.create_team(member0=p,
                                mode=p.mode,
                                season=p.season,
                                version=Version.LOTV)
        trs.append(dict(team_id=t.id, mode=t.mode, version=t.version))

        p = self.db.create_player(tag='TL',
                                  clan='Team Liquid',
                                  season=self.s16,
                                  mode=Mode.RANDOM_2V2)
        t = self.db.create_team(member0=p,
                                mode=p.mode,
                                season=p.season,
                                version=Version.LOTV)
        trs.append(dict(team_id=t.id, mode=t.mode, version=t.version))

        p = self.db.create_player(tag='TL',
                                  clan='Team Liquid',
                                  season=self.s16,
                                  mode=Mode.TEAM_1V1)
        t = self.db.create_team(member0=p,
                                mode=p.mode,
                                season=p.season,
                                version=Version.HOTS)
        trs.append(dict(team_id=t.id, mode=t.mode, version=t.version))

        self.db.create_ranking()
        self.db.create_ranking_data(data=trs)

        response = self.c.get('/clan/TL/mmr/')
        self.assertEqual(200, response.status_code)
        teams = response.context['ladder']['teams']
        self.assertEqual(1, len(teams))
        self.assertEqual(p1.name, teams[0]['m0_name'])

    def test_json_get(self):

        self.maxDiff = 1e9

        p1 = self.db.create_player(tag='TL')
        p2 = self.db.create_player(tag='TL')

        t1 = self.db.create_team(member0=p1)
        t2 = self.db.create_team(member0=p2)

        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=t1.id,
                 league=League.PLATINUM,
                 region=Region.EU,
                 race0=Race.TERRAN,
                 points=20,
                 version=Version.LOTV,
                 wins=100,
                 losses=100,
                 mmr=3000),
            dict(team_id=t2.id,
                 league=League.GOLD,
                 region=Region.AM,
                 race0=Race.ZERG,
                 points=40,
                 version=Version.LOTV,
                 wins=200,
                 losses=200,
                 mmr=2000),
        ])

        response = self.c.get('/clan/TL/mmr/?json')
        self.assertEqual(200, response.status_code)
        teams = json.loads(response.content.decode('utf-8'))
        self.assertTrue(teams[0].pop("age"))
        self.assertTrue(teams[0].pop("data_time"))
        self.assertTrue(teams[1].pop("age"))
        self.assertTrue(teams[1].pop("data_time"))
        self.assertEqual([
            dict(
                rank=1,
                team_id=t1.id,
                league="platinum",
                tier=0,
                region="eu",
                m0_race="terran",
                m0_name=p1.name,
                m0_bnet_url=
                f'https://starcraft2.com/en-gb/profile/2/1/{p1.bid}',
                mmr=3000,
                win_rate=50.0,
                wins=100,
                losses=100,
                points=20.0,
            ),
            dict(
                rank=2,
                team_id=t2.id,
                league="gold",
                tier=0,
                region="am",
                m0_race="zerg",
                m0_name=p2.name,
                m0_bnet_url=
                f'https://starcraft2.com/en-gb/profile/2/1/{p2.bid}',
                mmr=2000,
                win_rate=50.0,
                wins=200,
                losses=200,
                points=40.0,
            ),
        ], teams)
Beispiel #28
0
class Test(DjangoTestCase):

    @classmethod
    def setUpClass(self):
        super(Test, self).setUpClass()
        self.db = Db()

        # Required objects, not actually used in test cases.
        self.db.create_cache()
        self.db.create_ladder()

        self.db.create_season()

    def setUp(self):
        super().setUp()
        self.db.delete_all(keep=[Cache, Ladder, Season])
        self.c = Client()

    def tearDown(self):
        rankings_view_client.close()
        super(Test, self).tearDown()

    def test_view_league_stats(self):
        self.db.create_player()
        t1 = self.db.create_team()

        self.db.create_player()
        t2 = self.db.create_team()

        self.db.create_player()
        t3 = self.db.create_team()

        self.db.create_ranking()
        self.db.create_ranking_data(data=[
            dict(team_id=t1.id, league=League.GOLD,     wins=11, losses=10, points=20),
            dict(team_id=t2.id, league=League.PLATINUM, wins=21, losses=20, points=40),
            dict(team_id=t3.id, league=League.PLATINUM, wins=31, losses=30, points=50),
        ])
        self.db.update_ranking_stats()

        response = self.c.get('/stats/leagues/1v1/')
        self.assertEqual(200, response.status_code)
        self.assertIn('1v1 League Distribution', response.content.decode('utf-8'))

        response = self.c.get('/stats/raw/11/')
        self.assertEqual(200, response.status_code)
        content = json.loads(response.content.decode('utf-8'))

        self.assertEqual(1, len(content))

        data = content[0]['data']

        def get_stat(league_index, type_index):
            return data[RankingStats.raw_v1_index(RankingStats.V1_DATA_SIZE, Version.HOTS_INDEX, Region.EU_INDEX,
                                                  league_index, Race.ZERG_INDEX)
                        + type_index]

        self.assertEqual(1,  get_stat(League.GOLD_INDEX,     RankingStats.V1_COUNT_INDEX))
        self.assertEqual(11, get_stat(League.GOLD_INDEX,     RankingStats.V1_WINS_INDEX))
        self.assertEqual(10, get_stat(League.GOLD_INDEX,     RankingStats.V1_LOSSES_INDEX))
        self.assertEqual(20, get_stat(League.GOLD_INDEX,     RankingStats.V1_POINT_INDEX))
        self.assertEqual(2,  get_stat(League.PLATINUM_INDEX, RankingStats.V1_COUNT_INDEX))
        self.assertEqual(52, get_stat(League.PLATINUM_INDEX, RankingStats.V1_WINS_INDEX))
        self.assertEqual(50, get_stat(League.PLATINUM_INDEX, RankingStats.V1_LOSSES_INDEX))
        self.assertEqual(90, get_stat(League.PLATINUM_INDEX, RankingStats.V1_POINT_INDEX))