当前位置: 首页>>代码示例>>Python>>正文


Python Dao.insert_tournament方法代码示例

本文整理汇总了Python中dao.Dao.insert_tournament方法的典型用法代码示例。如果您正苦于以下问题:Python Dao.insert_tournament方法的具体用法?Python Dao.insert_tournament怎么用?Python Dao.insert_tournament使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dao.Dao的用法示例。


在下文中一共展示了Dao.insert_tournament方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: import_tournament

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
def import_tournament(type, path, bracket, region, name):
    config = Config()
    mongo_client = MongoClient(host=config.get_mongo_url())

    if type == 'tio':
        scraper = TioScraper(path, bracket)
    elif type =='challonge':
        scraper = ChallongeScraper(path)
    else:
        click.echo("Illegal type")

    dao = Dao(region, mongo_client=mongo_client)

    player_map = get_player_alias_to_id_map(scraper, dao)

    # TODO pass in a map of overrides for specific players
    tournament = Tournament.from_scraper(type, scraper, player_map, region)
    if name:
        tournament.name = name

    dao.insert_tournament(tournament)

    click.echo("Generating new ranking...")
    rankings.generate_ranking(dao)

    click.echo("Done!")
开发者ID:AJAnderson,项目名称:garpr,代码行数:28,代码来源:import_tournament.py

示例2: import_tournament

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
def import_tournament(type, path, bracket, region, name):
    config = parse_config()
    username = config.get('database', 'user')
    host = config.get('database', 'host')
    auth_db = config.get('database', 'auth_db')
    password = getpass.getpass()

    mongo_client = MongoClient(host='mongodb://%s:%[email protected]%s/%s' % (username, password, host, auth_db))

    if type == 'tio':
        scraper = TioScraper(path, bracket)
    elif type =='challonge':
        scraper = ChallongeScraper(path)
    else:
        click.echo("Illegal type")

    dao = Dao(region, mongo_client=mongo_client)

    import_players(scraper, dao)

    tournament = Tournament.from_scraper(type, scraper, dao)
    if name:
        tournament.name = name

    dao.insert_tournament(tournament)

    click.echo("Generating new ranking...")
    rankings.generate_ranking(dao)

    click.echo("Done!")
开发者ID:novacourtois,项目名称:garpr,代码行数:32,代码来源:import_tournament.py

示例3: post

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
    def post(self, region, id):
        dao = Dao(region, mongo_client=mongo_client)

        pending_tournament = dao.get_pending_tournament_by_id(ObjectId(id))
        if not pending_tournament:
            return 'No pending tournament found with that id.', 400
        user = get_user_from_request(request, dao)
        if not user:
            return 'Permission denied', 403
        if not is_user_admin_for_region(user, pending_tournament.regions):
            return 'Permission denied', 403

        new_player_names = []
        for mapping in pending_tournament.alias_to_id_map:
            if mapping["player_id"] == None:
                new_player_names.append(mapping["player_alias"])

        for player_name in new_player_names:
            player = Player.create_with_default_values(player_name, region)
            player_id = dao.insert_player(player)
            pending_tournament.set_alias_id_mapping(player_name, player_id)

        dao.update_pending_tournament(pending_tournament)
        
        try:
            tournament = Tournament.from_pending_tournament(pending_tournament)
            tournament_id = dao.insert_tournament(tournament)
            dao.delete_pending_tournament(pending_tournament)
            return {"success": True, "tournament_id": str(tournament_id)}
        except ValueError:
            return 'Not all player aliases in this pending tournament have been mapped to player ids.', 400
开发者ID:acoulon99,项目名称:garpr,代码行数:33,代码来源:server.py

示例4: post

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
    def post(self, region, id):
        print "finalize tournament post"
        dao = Dao(region, mongo_client=mongo_client)
        if not dao:
            return 'Dao not found', 404
        pending_tournament = None
        try:
            pending_tournament = dao.get_pending_tournament_by_id(ObjectId(id))
        except:
            return 'Invalid ObjectID', 400
        if not pending_tournament:
            return 'No pending tournament found with that id.', 400
        user = get_user_from_request(request, dao)
        if not user:
            return 'Permission denied', 403
        if not is_user_admin_for_regions(user, pending_tournament.regions):
            return 'Permission denied', 403

        # Make sure multiple player aliases don't map to a single player ID.
        player_ids = set()
        for mapping in pending_tournament.alias_to_id_map:
            if mapping.player_id is not None:
                if mapping.player_id in player_ids:
                    return "Player id %s is already mapped" % mapping.player_id, 400
                else:
                    player_ids.add(mapping.player_id)

        new_player_names = []
        for mapping in pending_tournament.alias_to_id_map:
            if mapping.player_id is None:
                new_player_names.append(mapping.player_alias)

        for player_name in new_player_names:
            player = M.Player.create_with_default_values(player_name, region)
            player_id = dao.insert_player(player)
            pending_tournament.set_alias_id_mapping(player_name, player_id)

        # validate players in this tournament
        for mapping in pending_tournament.alias_to_id_map:
            try:
                player_id = mapping.player_id
                # TODO: reduce queries to DB by batching
                player = dao.get_player_by_id(player_id)
                if player.merged:
                    return "Player {} has already been merged".format(player.name), 400
            except:
                return "Not all player ids are valid", 400

        try:
            dao.update_pending_tournament(pending_tournament)
            tournament = M.Tournament.from_pending_tournament(
                pending_tournament)
            tournament_id = dao.insert_tournament(tournament)
            dao.delete_pending_tournament(pending_tournament)
            return {"success": True, "tournament_id": str(tournament_id)}
        except ValueError as e:
            print e
            return 'Not all player aliases in this pending tournament have been mapped to player ids.', 400
        except:
            return 'Dao threw an error somewhere', 400
开发者ID:garsh0p,项目名称:garpr,代码行数:62,代码来源:server.py

示例5: TestRankings

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]

#.........这里部分代码省略.........
        self.tournament_matches_2 = [
                Match(winner=self.player_5_id, loser=self.player_2_id),
                Match(winner=self.player_3_id, loser=self.player_4_id)
        ]
        self.tournament_regions_2 = ['norcal', 'texas']

        self.tournament_1 = Tournament(
                    type=self.tournament_type_1,
                    raw=self.tournament_raw_1,
                    date=self.tournament_date_1,
                    name=self.tournament_name_1,
                    players=self.tournament_players_1,
                    matches=self.tournament_matches_1,
                    regions=self.tournament_regions_1,
                    id=self.tournament_id_1)

        self.tournament_2 = Tournament(
                    type=self.tournament_type_2,
                    raw=self.tournament_raw_2,
                    date=self.tournament_date_2,
                    name=self.tournament_name_2,
                    players=self.tournament_players_2,
                    matches=self.tournament_matches_2,
                    regions=self.tournament_regions_2,
                    id=self.tournament_id_2)

        self.tournament_ids = [self.tournament_id_1, self.tournament_id_2]
        self.tournaments = [self.tournament_1, self.tournament_2]

        for player in self.players:
            self.dao.insert_player(player)

        for tournament in self.tournaments:
            self.dao.insert_tournament(tournament)

    # all tournaments are within the active range and will be included
    def test_generate_rankings(self):
        now = datetime(2013, 10, 17)

        # assert rankings before they get reset
        self.assertEquals(self.dao.get_player_by_id(self.player_1_id).ratings, self.player_1.ratings)
        self.assertEquals(self.dao.get_player_by_id(self.player_2_id).ratings, self.player_2.ratings)
        self.assertEquals(self.dao.get_player_by_id(self.player_3_id).ratings, self.player_3.ratings)
        self.assertEquals(self.dao.get_player_by_id(self.player_4_id).ratings, self.player_4.ratings)
        self.assertEquals(self.dao.get_player_by_id(self.player_5_id).ratings, self.player_5.ratings)

        rankings.generate_ranking(self.dao, now=now, day_limit=30, num_tourneys=1)

        # assert rankings after ranking calculation
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_1_id).ratings['norcal'].mu,
                                28.458, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_1_id).ratings['norcal'].sigma,
                                7.201, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_2_id).ratings['norcal'].mu,
                                18.043, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_2_id).ratings['norcal'].sigma,
                                6.464, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_3_id).ratings['norcal'].mu,
                                2, delta=delta) #changing this b/c of new in regionon only stuff, lol
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_3_id).ratings['norcal'].sigma,
                                3, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_4_id).ratings['norcal'].mu,
                                25, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_4_id).ratings['norcal'].sigma,
                                8.333, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_5_id).ratings['norcal'].mu,
开发者ID:BrandonCookeDev,项目名称:garpr,代码行数:70,代码来源:test_rankings.py

示例6: TestDAO

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]

#.........这里部分代码省略.........
                                 [self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_3])
        self.ranking_2 = Ranking('norcal', self.ranking_time_2, self.tournament_ids, 
                                 [self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_4])
        self.ranking_3 = Ranking('texas', self.ranking_time_2, self.tournament_ids, 
                                 [self.ranking_entry_1, self.ranking_entry_2])

        self.rankings = [self.ranking_1, self.ranking_2, self.ranking_3]

        self.user_id_1 = 'abc123'
        self.user_admin_regions_1 = ['norcal', 'texas']
        self.user_1 = User(self.user_id_1, self.user_admin_regions_1)

        self.user_id_2 = 'asdfasdf'
        self.user_full_name_2 = 'Full Name'
        self.user_admin_regions_2 = []
        self.user_2 = User(self.user_id_2, self.user_admin_regions_2, full_name=self.user_full_name_2)

        self.users = [self.user_1, self.user_2]

        self.region_1 = Region('norcal', 'Norcal')
        self.region_2 = Region('texas', 'Texas')

        self.regions = [self.region_1, self.region_2]

        for region in self.regions:
            Dao.insert_region(region, self.mongo_client, database_name=DATABASE_NAME)

        self.norcal_dao = Dao('norcal', self.mongo_client, database_name=DATABASE_NAME)

        for player in self.players:
            self.norcal_dao.insert_player(player)

        for tournament in self.tournaments:
            self.norcal_dao.insert_tournament(tournament)

        for pending_tournament in self.pending_tournaments:
            self.norcal_dao.insert_pending_tournament(pending_tournament)

        for ranking in self.rankings:
            self.norcal_dao.insert_ranking(ranking)

        for user in self.users:
            self.norcal_dao.insert_user(user)

    def tearDown(self):
        self.mongo_client.drop_database(DATABASE_NAME)

    def test_init_with_invalid_region(self):
        # create a dao with an existing region
        Dao('norcal', self.mongo_client, database_name=DATABASE_NAME)

        # create a dao with a new region
        with self.assertRaises(RegionNotFoundException):
            Dao('newregion', self.mongo_client, database_name=DATABASE_NAME)

    def test_get_all_regions(self):
        # add another region
        region = Region('newregion', 'New Region')
        Dao.insert_region(region, self.mongo_client, database_name=DATABASE_NAME)

        regions = Dao.get_all_regions(self.mongo_client, database_name=DATABASE_NAME)
        self.assertEquals(len(regions), 3)
        self.assertEquals(regions[0], region)
        self.assertEquals(regions[1], self.region_1)
        self.assertEquals(regions[2], self.region_2)
开发者ID:MrMonacle,项目名称:garpr,代码行数:69,代码来源:test_dao.py

示例7: TestDAO

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
class TestDAO(unittest.TestCase):
    def setUp(self):
        self.region = 'test'
        self.mongo_client = mongomock.MongoClient()
        self.dao = Dao(self.region, mongo_client=self.mongo_client, new=True)

        self.player_1_id = ObjectId()
        self.player_2_id = ObjectId()
        self.player_3_id = ObjectId()
        self.player_4_id = ObjectId()
        self.player_5_id = ObjectId()
        self.player_1 = Player('gaR', ['gar', 'garr'], TrueskillRating(), False, id=self.player_1_id)
        self.player_2 = Player('sfat', ['sfat', 'miom | sfat'], TrueskillRating(), False, id=self.player_2_id)
        self.player_3 = Player('mango', ['mango'], 
                               TrueskillRating(trueskill_rating=trueskill.Rating(mu=2, sigma=3)), 
                               True, id=self.player_3_id)
        self.player_4 = Player('shroomed', ['shroomed'], TrueskillRating(), False, id=self.player_4_id)
        self.player_5 = Player('pewpewu', ['pewpewu'], TrueskillRating(), False, id=self.player_5_id)

        # only includes players 1-3
        self.players = [self.player_1, self.player_2, self.player_3]

        self.tournament_id_1 = ObjectId()
        self.tournament_type_1 = 'tio'
        self.tournament_raw_1 = 'raw1'
        self.tournament_date_1 = datetime(2013, 10, 16)
        self.tournament_name_1 = 'tournament 1'
        self.tournament_players_1 = [self.player_1_id, self.player_2_id, self.player_3_id, self.player_4_id]
        self.tournament_matches_1 = [
                MatchResult(winner=self.player_1_id, loser=self.player_2_id),
                MatchResult(winner=self.player_3_id, loser=self.player_4_id)
        ]

        # tournament 2 is earlier than tournament 1, but inserted after
        self.tournament_id_2 = ObjectId()
        self.tournament_type_2 = 'challonge'
        self.tournament_raw_2 = 'raw2'
        self.tournament_date_2 = datetime(2013, 10, 10)
        self.tournament_name_2 = 'tournament 2'
        self.tournament_players_2 = [self.player_5_id, self.player_2_id, self.player_3_id, self.player_4_id]
        self.tournament_matches_2 = [
                MatchResult(winner=self.player_5_id, loser=self.player_2_id),
                MatchResult(winner=self.player_3_id, loser=self.player_4_id)
        ]

        self.tournament_1 = Tournament(self.tournament_type_1,
                                       self.tournament_raw_1,
                                       self.tournament_date_1, 
                                       self.tournament_name_1,
                                       self.tournament_players_1,
                                       self.tournament_matches_1,
                                       id=self.tournament_id_1)

        self.tournament_2 = Tournament(self.tournament_type_2,
                                       self.tournament_raw_2,
                                       self.tournament_date_2, 
                                       self.tournament_name_2,
                                       self.tournament_players_2,
                                       self.tournament_matches_2,
                                       id=self.tournament_id_2)

        self.tournament_ids = [self.tournament_id_1, self.tournament_id_2]
        self.tournaments = [self.tournament_1, self.tournament_2]

        self.ranking_entry_1 = RankingEntry(1, self.player_1_id, 20)
        self.ranking_entry_2 = RankingEntry(2, self.player_2_id, 19)
        self.ranking_entry_3 = RankingEntry(3, self.player_3_id, 17.5)
        self.ranking_entry_4 = RankingEntry(3, self.player_4_id, 16.5)

        self.ranking_time_1 = datetime(2013, 4, 20)
        self.ranking_time_2 = datetime(2013, 4, 21)
        self.ranking_1 = Ranking(self.ranking_time_1, self.tournament_ids, 
                                 [self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_3])
        self.ranking_2 = Ranking(self.ranking_time_2, self.tournament_ids, 
                                 [self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_4])

        self.rankings = [self.ranking_1, self.ranking_2]

        for player in self.players:
            self.dao.add_player(player)

        for tournament in self.tournaments:
            self.dao.insert_tournament(tournament)

        for ranking in self.rankings:
            self.dao.insert_ranking(ranking)

    def test_init_with_invalid_region(self):
        print Dao.get_all_regions(mongo_client=self.mongo_client)
        # create a dao with an existing region
        Dao(self.region, mongo_client=self.mongo_client, new=False)

        # create a dao with a new region
        with self.assertRaises(RegionNotFoundException):
            Dao('newregion', mongo_client=self.mongo_client, new=False)

    def test_get_all_regions(self):
        # add another region
        region = 'newregion'
        Dao(region, mongo_client=self.mongo_client, new=True)
#.........这里部分代码省略.........
开发者ID:novacourtois,项目名称:garpr,代码行数:103,代码来源:test_dao.py

示例8: TestDAO

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]

#.........这里部分代码省略.........
                           hashed_password='browns',
                           admin_level='REGION')

        self.superadmin_id = '123456'
        self.superadmin_full_name = 'superadmin'
        self.superadmin_admin_regions = []
        self.superadmin = User(
            id=self.superadmin_id,
            user_admin_regions=self.superadmin_admin_regions,
            username=self.superadmin_full_name,
            salt='nacl',
            hashed_password='browns',
            admin_level='SUPER'
        )

        self.users = [self.user_1, self.user_2, self.superadmin]

        self.region_1 = Region(id='norcal', display_name='Norcal')
        self.region_2 = Region(id='texas', display_name='Texas')

        self.regions = [self.region_1, self.region_2]

        for region in self.regions:
            Dao.insert_region(region, self.mongo_client,
                              database_name=DATABASE_NAME)

        self.norcal_dao = Dao('norcal', self.mongo_client,
                              database_name=DATABASE_NAME)

        for player in self.players:
            self.norcal_dao.insert_player(player)

        for tournament in self.tournaments:
            self.norcal_dao.insert_tournament(tournament)

        for pending_tournament in self.pending_tournaments:
            self.norcal_dao.insert_pending_tournament(pending_tournament)

        for ranking in self.rankings:
            self.norcal_dao.insert_ranking(ranking)

        for user in self.users:
            self.norcal_dao.insert_user(user)



    def tearDown(self):
        self.mongo_client.drop_database(DATABASE_NAME)

    def test_init_with_invalid_region(self):
        # create a dao with an existing region
        Dao('norcal', self.mongo_client, database_name=DATABASE_NAME)

        # create a dao with a non existant region, should return none
        self.assertIsNone(Dao('newregion', self.mongo_client,
                              database_name=DATABASE_NAME))

    def test_get_all_regions(self):
        # add another region
        region = Region(id='newregion', display_name='New Region')
        Dao.insert_region(region, self.mongo_client,
                          database_name=DATABASE_NAME)

        regions = Dao.get_all_regions(
            self.mongo_client, database_name=DATABASE_NAME)
        self.assertEquals(len(regions), 3)
开发者ID:BrandonCookeDev,项目名称:garpr,代码行数:70,代码来源:test_dao.py

示例9: TestServer

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
class TestServer(unittest.TestCase):
    def setUp(self):
        self.mongo_client_patcher = patch('server.mongo_client', new=mongomock.MongoClient())
        self.mongo_client = self.mongo_client_patcher.start()

        server.app.config['TESTING'] = True
        self.app = server.app.test_client()

        self.norcal_dao = Dao(NORCAL_REGION_NAME, mongo_client=self.mongo_client, new=True)
        self.texas_dao = Dao(TEXAS_REGION_NAME, mongo_client=self.mongo_client, new=True)

        self._import_files()
        rankings.generate_ranking(self.norcal_dao)
        rankings.generate_ranking(self.texas_dao)

    def _import_files(self):
        for f in NORCAL_FILES:
            scraper = TioScraper(f[0], f[1])
            self._import_players(scraper, self.norcal_dao)
            self.norcal_dao.insert_tournament(Tournament.from_scraper('tio', scraper, self.norcal_dao))

        for f in TEXAS_FILES:
            scraper = TioScraper(f[0], f[1])
            self._import_players(scraper, self.texas_dao)
            self.texas_dao.insert_tournament(Tournament.from_scraper('tio', scraper, self.texas_dao))

    def _import_players(self, scraper, dao):
        for player in scraper.get_players():
            db_player = dao.get_player_by_alias(player)
            if db_player is None:
                db_player = Player(player, [player.lower()], TrueskillRating(), False)
                dao.add_player(db_player)

    def test_get_region_list(self):
        data = self.app.get('/regions').data
        self.assertEquals(json.loads(data), {'regions': ['norcal', 'texas']})

    def test_get_player_list(self):
        def for_region(data, dao):
            json_data = json.loads(data)

            self.assertEquals(json_data.keys(), ['players'])
            players_list = json_data['players']
            players_from_db = dao.get_all_players()
            self.assertEquals(len(players_list), len(players_from_db))

            for player in players_list:
                expected_keys = set(['id', 'name'])
                self.assertEquals(set(player.keys()), expected_keys)
                self.assertEquals(ObjectId(player['id']), dao.get_player_by_alias(player['name']).id)

        data = self.app.get('/norcal/players').data
        for_region(data, self.norcal_dao)

        data = self.app.get('/texas/players').data
        for_region(data, self.texas_dao)

    def test_get_player(self):
        player = self.norcal_dao.get_player_by_alias('gar')
        data = self.app.get('/norcal/players/' + str(player.id)).data
        json_data = json.loads(data)

        self.assertEquals(len(json_data.keys()), 5)
        self.assertEquals(json_data['id'], str(player.id))
        self.assertEquals(json_data['name'], 'gar')
        self.assertEquals(json_data['aliases'], ['gar'])
        self.assertEquals(json_data['exclude'], False)
        self.assertTrue(json_data['rating']['mu'] > 25.9)
        self.assertTrue(json_data['rating']['sigma'] > 3.89)

        player = self.texas_dao.get_player_by_alias('wobbles')
        data = self.app.get('/texas/players/' + str(player.id)).data
        json_data = json.loads(data)

        self.assertEquals(len(json_data.keys()), 5)
        self.assertEquals(json_data['id'], str(player.id))
        self.assertEquals(json_data['name'], 'Wobbles')
        self.assertEquals(json_data['aliases'], ['wobbles'])
        self.assertEquals(json_data['exclude'], False)
        self.assertTrue(json_data['rating']['mu'] > 44.5)
        self.assertTrue(json_data['rating']['sigma'] > 3.53)

    def test_get_tournament_list(self):
        def for_region(data, dao):
            json_data = json.loads(data)

            self.assertEquals(json_data.keys(), ['tournaments'])
            tournaments_list = json_data['tournaments']
            tournaments_from_db = dao.get_all_tournaments()
            self.assertEquals(len(tournaments_list), len(tournaments_from_db))

            for tournament in tournaments_list:
                tournament_from_db = dao.get_tournament_by_id(ObjectId(tournament['id']))
                expected_keys = set(['id', 'name', 'date'])
                self.assertEquals(set(tournament.keys()), expected_keys)
                self.assertEquals(tournament['id'], str(tournament_from_db.id))
                self.assertEquals(tournament['name'], tournament_from_db.name)
                self.assertEquals(tournament['date'], tournament_from_db.date.strftime('%x'))

        data = self.app.get('/norcal/tournaments').data
#.........这里部分代码省略.........
开发者ID:novacourtois,项目名称:garpr,代码行数:103,代码来源:test_server.py

示例10: TestServer

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
class TestServer(unittest.TestCase):
    def setUp(self):
        self.mongo_client_patcher = patch('server.mongo_client', new=mongomock.MongoClient())
        self.mongo_client = self.mongo_client_patcher.start()

        server.app.config['TESTING'] = True
        self.app = server.app.test_client()

        self.norcal_region = Region('norcal', 'Norcal')
        self.texas_region = Region('texas', 'Texas')
        Dao.insert_region(self.norcal_region, self.mongo_client)
        Dao.insert_region(self.texas_region, self.mongo_client)

        self.norcal_dao = Dao(NORCAL_REGION_NAME, mongo_client=self.mongo_client)
        self.texas_dao = Dao(TEXAS_REGION_NAME, mongo_client=self.mongo_client)

        self._import_files()

        now = datetime(2014, 11, 1)
        rankings.generate_ranking(self.norcal_dao, now=now)
        rankings.generate_ranking(self.texas_dao, now=now)

        self.user_id = 'asdf'
        self.user_full_name = 'full name'
        self.user_admin_regions = ['norcal', 'nyc']
        self.user = User(self.user_id, self.user_admin_regions, full_name=self.user_full_name)
        self.norcal_dao.insert_user(self.user)

    def _import_files(self):
        for f in NORCAL_FILES:
            scraper = TioScraper(f[0], f[1])
            self._import_players(scraper, self.norcal_dao)
            player_map = self.norcal_dao.get_player_id_map_from_player_aliases(scraper.get_players())
            self.norcal_dao.insert_tournament(Tournament.from_scraper('tio', scraper, player_map, self.norcal_dao.region_id))

        for f in TEXAS_FILES:
            scraper = TioScraper(f[0], f[1])
            self._import_players(scraper, self.texas_dao)
            player_map = self.texas_dao.get_player_id_map_from_player_aliases(scraper.get_players())
            self.texas_dao.insert_tournament(Tournament.from_scraper('tio', scraper, player_map, self.texas_dao.region_id))

    def _import_players(self, scraper, dao):
        for player in scraper.get_players():
            db_player = dao.get_player_by_alias(player)
            if db_player is None:
                db_player = Player(
                        player, 
                        [player.lower()], 
                        {dao.region_id: TrueskillRating()},
                        [dao.region_id])
                dao.insert_player(db_player)

    def test_get_region_list(self):
        data = self.app.get('/regions').data

        expected_region_dict = {
                'regions': [
                    {'id': 'norcal', 'display_name': 'Norcal'},
                    {'id': 'texas', 'display_name': 'Texas'}
                ]
        }

        self.assertEquals(json.loads(data), expected_region_dict)

    def test_get_player_list(self):
        def for_region(json_data, dao):
            self.assertEquals(json_data.keys(), ['players'])
            players_list = json_data['players']
            players_from_db = dao.get_all_players()
            self.assertEquals(len(players_list), len(players_from_db))

            for player in players_list:
                expected_keys = set(['id', 'name'])
                self.assertEquals(set(player.keys()), expected_keys)
                self.assertEquals(ObjectId(player['id']), dao.get_player_by_alias(player['name']).id)

        data = self.app.get('/norcal/players').data
        json_data = json.loads(data)
        self.assertEquals(len(json_data['players']), 65)
        for_region(json_data, self.norcal_dao)

        data = self.app.get('/texas/players').data
        json_data = json.loads(data)
        self.assertEquals(len(json_data['players']), 41)
        for_region(json_data, self.texas_dao)

    def test_get_player_list_with_alias(self):
        player = self.norcal_dao.get_player_by_alias('gar')

        data = self.app.get('/norcal/players?alias=gar').data
        json_data = json.loads(data)
        self.assertEquals(len(json_data['players']), 1)

        json_player = json_data['players'][0]
        expected_keys = set(['id', 'name'])
        self.assertEquals(set(json_player.keys()), expected_keys)
        self.assertEquals(ObjectId(json_player['id']), player.id)

    def test_get_player_list_case_insensitive(self):
        player = self.norcal_dao.get_player_by_alias('gar')
#.........这里部分代码省略.........
开发者ID:AJAnderson,项目名称:garpr,代码行数:103,代码来源:test_server.py

示例11: TestRankings

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import insert_tournament [as 别名]
class TestRankings(unittest.TestCase):
    def setUp(self):
        self.region = 'test'
        self.mongo_client = mongomock.MongoClient()
        self.dao = Dao(self.region, mongo_client=self.mongo_client, new=True)

        self.player_1_id = ObjectId()
        self.player_2_id = ObjectId()
        self.player_3_id = ObjectId()
        self.player_4_id = ObjectId()
        self.player_5_id = ObjectId()
        self.player_1 = Player('gaR', ['gar', 'garr'], TrueskillRating(), False, id=self.player_1_id)
        self.player_2 = Player('sfat', ['sfat', 'miom | sfat'], TrueskillRating(), False, id=self.player_2_id)
        self.player_3 = Player('mango', ['mango'], 
                               TrueskillRating(trueskill_rating=trueskill.Rating(mu=2, sigma=3)), 
                               True, id=self.player_3_id)
        self.player_4 = Player('shroomed', ['shroomed'], TrueskillRating(), False, id=self.player_4_id)
        self.player_5 = Player('pewpewu', ['pewpewu'], TrueskillRating(), False, id=self.player_5_id)

        self.players = [self.player_1, self.player_2, self.player_3, self.player_4, self.player_5]

        self.tournament_id_1 = ObjectId()
        self.tournament_type_1 = 'tio'
        self.tournament_raw_1 = 'raw1'
        self.tournament_date_1 = datetime(2013, 10, 16)
        self.tournament_name_1 = 'tournament 1'
        self.tournament_players_1 = [self.player_1_id, self.player_2_id, self.player_3_id, self.player_4_id]
        self.tournament_matches_1 = [
                MatchResult(winner=self.player_1_id, loser=self.player_2_id),
                MatchResult(winner=self.player_3_id, loser=self.player_4_id)
        ]

        # tournament 2 is earlier than tournament 1, but inserted after
        self.tournament_id_2 = ObjectId()
        self.tournament_type_2 = 'challonge'
        self.tournament_raw_2 = 'raw2'
        self.tournament_date_2 = datetime(2013, 10, 10)
        self.tournament_name_2 = 'tournament 2'
        self.tournament_players_2 = [self.player_5_id, self.player_2_id, self.player_3_id, self.player_4_id]
        self.tournament_matches_2 = [
                MatchResult(winner=self.player_5_id, loser=self.player_2_id),
                MatchResult(winner=self.player_3_id, loser=self.player_4_id)
        ]

        self.tournament_1 = Tournament(self.tournament_type_1,
                                       self.tournament_raw_1,
                                       self.tournament_date_1, 
                                       self.tournament_name_1,
                                       self.tournament_players_1,
                                       self.tournament_matches_1,
                                       id=self.tournament_id_1)

        self.tournament_2 = Tournament(self.tournament_type_2,
                                       self.tournament_raw_2,
                                       self.tournament_date_2, 
                                       self.tournament_name_2,
                                       self.tournament_players_2,
                                       self.tournament_matches_2,
                                       id=self.tournament_id_2)

        self.tournament_ids = [self.tournament_id_1, self.tournament_id_2]
        self.tournaments = [self.tournament_1, self.tournament_2]

        for player in self.players:
            self.dao.add_player(player)

        for tournament in self.tournaments:
            self.dao.insert_tournament(tournament)

    # all tournaments are within the active range and will be included
    @patch('rankings.datetime', spec=True)
    def test_generate_rankings(self, mock_datetime):
        now = datetime(2013, 10, 17)
        mock_datetime.now.return_value = now

        # assert rankings before they get reset
        self.assertEquals(self.dao.get_player_by_id(self.player_1_id).rating, self.player_1.rating)
        self.assertEquals(self.dao.get_player_by_id(self.player_2_id).rating, self.player_2.rating)
        self.assertEquals(self.dao.get_player_by_id(self.player_3_id).rating, self.player_3.rating)
        self.assertEquals(self.dao.get_player_by_id(self.player_4_id).rating, self.player_4.rating)
        self.assertEquals(self.dao.get_player_by_id(self.player_5_id).rating, self.player_5.rating)

        rankings.generate_ranking(self.dao)

        # assert rankings after ranking calculation
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_1_id).rating.trueskill_rating.mu, 
                                28.458, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_1_id).rating.trueskill_rating.sigma, 
                                7.201, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_2_id).rating.trueskill_rating.mu, 
                                18.043, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_2_id).rating.trueskill_rating.sigma, 
                                6.464, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_3_id).rating.trueskill_rating.mu, 
                                31.230, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_3_id).rating.trueskill_rating.sigma, 
                                6.523, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_4_id).rating.trueskill_rating.mu, 
                                18.770, delta=delta)
        self.assertAlmostEquals(self.dao.get_player_by_id(self.player_4_id).rating.trueskill_rating.sigma, 
#.........这里部分代码省略.........
开发者ID:novacourtois,项目名称:garpr,代码行数:103,代码来源:test_rankings.py


注:本文中的dao.Dao.insert_tournament方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。