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)
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)
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)
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'])
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), )
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"])
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_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])
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'))
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))