Example #1
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)
Example #2
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)
Example #3
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)
Example #4
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])

    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)