def test_from_pending_tournament_throws_exception(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] alias_to_id_map = [ AliasMapping(player_alias=self.player_1.name, player_id=None) ] pending_tournament = PendingTournament(name=self.name, type=self.type, date=self.date, regions=['norcal'], raw_id=self.raw_id, players=player_aliases, matches=matches, alias_to_id_map=alias_to_id_map) with self.assertRaises(Exception) as e: Tournament.from_pending_tournament(pending_tournament) self.assertTrue('Alias gar has no ID in map' in str(e.exception))
def test_from_pending_tournament(self): # we need MatchResults with aliases (instead of IDs) match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) player_aliases = [p.name for p in self.players] matches = [match_1, match_2] pending_tournament = PendingTournament( name=self.name, type=self.type, date=self.date, raw_id=self.raw_id, regions=['norcal'], players=player_aliases, matches=matches, alias_to_id_map=self.alias_to_id_map) tournament = Tournament.from_pending_tournament(pending_tournament) self.assertIsNone(tournament.id) self.assertEqual(tournament.type, self.type) self.assertEqual(tournament.raw_id, self.raw_id) self.assertEqual(tournament.date, self.date) self.assertEqual(tournament.name, self.name) self.assertEqual(tournament.matches[0].winner, self.matches[0].winner) self.assertEqual(tournament.matches[0].loser, self.matches[0].loser) self.assertEqual(tournament.matches[1].winner, self.matches[1].winner) self.assertEqual(tournament.matches[1].loser, self.matches[1].loser) self.assertEqual(tournament.players, self.player_ids) self.assertEqual(tournament.regions, ['norcal'])
def test_load(self): pending_tournament = PendingTournament.load( self.pending_tournament_json_dict, context='db') self.assertEqual(pending_tournament.id, self.id) self.assertEqual(pending_tournament.type, self.type) self.assertEqual(pending_tournament.raw_id, self.raw_id) self.assertEqual(pending_tournament.date, self.date) self.assertEqual(pending_tournament.name, self.name) self.assertEqual(pending_tournament.matches, self.matches) self.assertEqual(pending_tournament.players, self.players) self.assertEqual(pending_tournament.regions, self.regions) self.assertEqual(pending_tournament.alias_to_id_map, self.alias_to_id_map)
def post(self, region): dao = Dao(region, mongo_client=mongo_client) user = get_user_from_request(request, dao) if not user: return 'Permission denied', 403 if not is_user_admin_for_region(user, region): return 'Permission denied', 403 parser = reqparse.RequestParser() parser.add_argument('type', type=str, location='json') parser.add_argument('data', type=unicode, location='json') parser.add_argument('bracket', type=str, location='json') args = parser.parse_args() print "in server post, actually got this far!" if args['data'] is None: return "data required", 400 the_bytes = bytearray(args['data'], "utf8") if the_bytes[0] == 0xef: print "found magic numbers" return "magic numbers!", 503 type = args['type'] data = args['data'] if type == 'tio': if args['bracket'] is None: return "Missing bracket name", 400 data_bytes = bytes(data) if data_bytes[0] == '\xef': data = data[:3] scraper = TioScraper(data, args['bracket']) elif type == 'challonge': scraper = ChallongeScraper(data) else: return "Unknown type", 400 pending_tournament = PendingTournament.from_scraper(type, scraper, region) pending_tournament.alias_to_id_map = alias_service.get_alias_to_id_map_in_list_format( dao, pending_tournament.players) new_id = dao.insert_pending_tournament(pending_tournament) return_dict = { 'id': str(new_id) } return return_dict
def post(self, region): dao = Dao(region, mongo_client=mongo_client) user = get_user_from_request(request, dao) if not user: return 'Permission denied', 403 if not is_user_admin_for_region(user, region): return 'Permission denied', 403 parser = reqparse.RequestParser() parser.add_argument('type', type=str, location='json') parser.add_argument('data', type=unicode, location='json') parser.add_argument('bracket', type=str, location='json') args = parser.parse_args() print "in server post, actually got this far!" if args['data'] is None: return "data required", 400 the_bytes = bytearray(args['data'], "utf8") if the_bytes[0] == 0xef: print "found magic numbers" return "magic numbers!", 503 type = args['type'] data = args['data'] if type == 'tio': if args['bracket'] is None: return "Missing bracket name", 400 data_bytes = bytes(data) if data_bytes[0] == '\xef': data = data[:3] scraper = TioScraper(data, args['bracket']) elif type == 'challonge': scraper = ChallongeScraper(data) else: return "Unknown type", 400 pending_tournament = PendingTournament.from_scraper( type, scraper, region) pending_tournament.alias_to_id_map = alias_service.get_alias_to_id_map_in_list_format( dao, pending_tournament.players) new_id = dao.insert_pending_tournament(pending_tournament) return_dict = {'id': str(new_id)} return return_dict
def test_from_scraper(self): mock_scraper = mock.Mock(spec=ChallongeScraper) mock_scraper.get_players.return_value = self.players mock_scraper.get_matches.return_value = self.matches mock_scraper.get_raw.return_value = '' mock_scraper.get_date.return_value = self.date mock_scraper.get_name.return_value = self.name mock_scraper.get_url.return_value = '' pending_tournament, _ = PendingTournament.from_scraper( self.type, mock_scraper, 'norcal') self.assertEqual(pending_tournament.type, self.type) self.assertEqual(pending_tournament.date, self.date) self.assertEqual(pending_tournament.name, self.name) self.assertEqual(pending_tournament.matches, self.matches) self.assertEqual(pending_tournament.players, self.players) self.assertEqual(pending_tournament.regions, ['norcal'])
def post(self, region): dao = Dao(region, mongo_client=mongo_client) user = get_user_from_access_token(request.headers, dao) if not is_user_admin_for_region(user, region): return 'Permission denied', 403 parser = reqparse.RequestParser() parser.add_argument('type', type=str, location='json') parser.add_argument('data', type=unicode, location='json') parser.add_argument('bracket', type=str, location='json') args = parser.parse_args() if args['data'] is None: return "data required", 400 type = args['type'] data = args['data'] if type == 'tio': if args['bracket'] is None: return "Missing bracket name", 400 scraper = TioScraper(data, args['bracket']) elif type == 'challonge': scraper = ChallongeScraper(data) else: return "Unknown type", 400 pending_tournament = PendingTournament.from_scraper(type, scraper, region) pending_tournament.alias_to_id_map = alias_service.get_alias_to_id_map_in_list_format( dao, pending_tournament.players) new_id = dao.insert_pending_tournament(pending_tournament) return_dict = { 'id': str(new_id) } return return_dict
def post(self, region): dao = Dao(region, mongo_client=mongo_client) user = get_user_from_access_token(request.headers, dao) if not is_user_admin_for_region(user, region): return 'Permission denied', 403 parser = reqparse.RequestParser() parser.add_argument('type', type=str, location='json') parser.add_argument('data', type=unicode, location='json') parser.add_argument('bracket', type=str, location='json') args = parser.parse_args() if args['data'] is None: return "data required", 400 type = args['type'] data = args['data'] if type == 'tio': if args['bracket'] is None: return "Missing bracket name", 400 scraper = TioScraper(data, args['bracket']) elif type == 'challonge': scraper = ChallongeScraper(data) else: return "Unknown type", 400 pending_tournament = PendingTournament.from_scraper( type, scraper, region) pending_tournament.alias_to_id_map = alias_service.get_alias_to_id_map_in_list_format( dao, pending_tournament.players) new_id = dao.insert_pending_tournament(pending_tournament) return_dict = {'id': str(new_id)} return return_dict
def setUp(self): self.mongo_client.drop_database(DATABASE_NAME) 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(name='gaR', aliases=['gar', 'garr'], ratings={ 'norcal': Rating(), 'texas': Rating() }, regions=['norcal', 'texas'], id=self.player_1_id) self.player_2 = Player(name='sfat', aliases=['sfat', 'miom | sfat'], ratings={'norcal': Rating()}, regions=['norcal'], id=self.player_2_id) self.player_3 = Player(name='mango', aliases=['mango', 'gar'], ratings={'norcal': Rating(mu=2, sigma=3)}, regions=['socal'], id=self.player_3_id) self.player_4 = Player(name='shroomed', aliases=['shroomed'], ratings={'norcal': Rating()}, regions=['norcal'], id=self.player_4_id) self.player_5 = Player(name='pewpewu', aliases=['pewpewu'], ratings={'norcal': Rating()}, regions=['norcal', 'socal'], id=self.player_5_id) self.merge_player_1 = Player(name='CLGsFaT', aliases=['clg | sfat'], ratings={'norcal': Rating()}, regions=['norcal'], id=ObjectId()) # 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_id_1 = ObjectId() 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 = [ Match(winner=self.player_1_id, loser=self.player_2_id), Match(winner=self.player_3_id, loser=self.player_4_id) ] self.tournament_regions_1 = ['norcal'] # tournament 2 is earlier than tournament 1, but inserted after self.tournament_id_2 = ObjectId() self.tournament_type_2 = 'challonge' self.tournament_raw_id_2 = ObjectId() 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 = [ 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(id=self.tournament_id_1, name=self.tournament_name_1, type=self.tournament_type_1, date=self.tournament_date_1, regions=self.tournament_regions_1, raw_id=self.tournament_raw_id_1, players=self.tournament_players_1, matches=self.tournament_matches_1) self.tournament_2 = Tournament(id=self.tournament_id_2, name=self.tournament_name_2, type=self.tournament_type_2, date=self.tournament_date_2, regions=self.tournament_regions_2, raw_id=self.tournament_raw_id_2, players=self.tournament_players_2, matches=self.tournament_matches_2) self.tournament_ids = [self.tournament_id_1, self.tournament_id_2] self.tournaments = [self.tournament_1, self.tournament_2] self.pending_tournament_id_1 = ObjectId() self.pending_tournament_type_1 = 'tio' self.pending_tournament_raw_id_1 = ObjectId() self.pending_tournament_date_1 = datetime(2013, 10, 11) self.pending_tournament_name_1 = 'pending tournament 1' self.pending_tournament_players_1 = [ self.player_1.name, self.player_2.name, self.player_3.name, self.player_4.name ] self.pending_tournament_matches_1 = [ AliasMatch(winner=self.player_1.name, loser=self.player_2.name), AliasMatch(winner=self.player_3.name, loser=self.player_4.name) ] self.pending_tournament_regions_1 = ['norcal'] self.pending_tournament_1 = PendingTournament( id=self.pending_tournament_id_1, name=self.pending_tournament_name_1, type=self.pending_tournament_type_1, date=self.pending_tournament_date_1, regions=self.pending_tournament_regions_1, raw_id=self.pending_tournament_raw_id_1, players=self.pending_tournament_players_1, matches=self.pending_tournament_matches_1) self.pending_tournaments = [self.pending_tournament_1] self.ranking_entry_1 = RankingEntry(rank=1, player=self.player_1_id, rating=20) self.ranking_entry_2 = RankingEntry(rank=2, player=self.player_2_id, rating=19) self.ranking_entry_3 = RankingEntry(rank=3, player=self.player_3_id, rating=17.5) self.ranking_entry_4 = RankingEntry(rank=3, player=self.player_4_id, rating=16.5) self.ranking_time_1 = datetime(2013, 4, 20) self.ranking_time_2 = datetime(2013, 4, 21) self.ranking_1 = Ranking(id=ObjectId(), region='norcal', time=self.ranking_time_1, tournaments=self.tournament_ids, ranking=[ self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_3 ]) self.ranking_2 = Ranking(id=ObjectId(), region='norcal', time=self.ranking_time_2, tournaments=self.tournament_ids, ranking=[ self.ranking_entry_1, self.ranking_entry_2, self.ranking_entry_4 ]) self.ranking_3 = Ranking( id=ObjectId(), region='norcal', time=self.ranking_time_2, tournaments=self.tournament_ids, ranking=[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(id=self.user_id_1, admin_regions=self.user_admin_regions_1, username='******', salt='nacl', hashed_password='******') self.user_id_2 = 'asdfasdf' self.user_full_name_2 = 'Full Name' self.user_admin_regions_2 = [] self.user_2 = User(id=self.user_id_2, admin_regions=self.user_admin_regions_2, username=self.user_full_name_2, salt='nacl', hashed_password='******') self.users = [self.user_1, self.user_2] 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 setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id) self.match_1 = AliasMatch( winner=self.player_1.name, loser=self.player_2.name) self.match_2 = AliasMatch( winner=self.player_3.name, loser=self.player_4.name) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.players = [self.player_1.name, self.player_2.name, self.player_3.name, self.player_4.name] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.url = 'http://challonge.com/test' self.pending_tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'players': self.players, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions, 'alias_to_id_map': [am.dump(context='db') for am in self.alias_to_id_map], 'url': self.url } self.pending_tournament = PendingTournament( id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.players, matches=self.matches, alias_to_id_map=self.alias_to_id_map)
class TestPendingTournament(unittest.TestCase): def setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id) self.match_1 = AliasMatch( winner=self.player_1.name, loser=self.player_2.name) self.match_2 = AliasMatch( winner=self.player_3.name, loser=self.player_4.name) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.players = [self.player_1.name, self.player_2.name, self.player_3.name, self.player_4.name] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.url = 'http://challonge.com/test' self.pending_tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'players': self.players, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions, 'alias_to_id_map': [am.dump(context='db') for am in self.alias_to_id_map], 'url': self.url } self.pending_tournament = PendingTournament( id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.players, matches=self.matches, alias_to_id_map=self.alias_to_id_map) def test_dump(self): self.assertEqual(self.pending_tournament.dump( context='db'), self.pending_tournament_json_dict) def test_load(self): pending_tournament = PendingTournament.load( self.pending_tournament_json_dict, context='db') self.assertEqual(pending_tournament.id, self.id) self.assertEqual(pending_tournament.type, self.type) self.assertEqual(pending_tournament.raw_id, self.raw_id) self.assertEqual(pending_tournament.date, self.date) self.assertEqual(pending_tournament.name, self.name) self.assertEqual(pending_tournament.matches, self.matches) self.assertEqual(pending_tournament.players, self.players) self.assertEqual(pending_tournament.regions, self.regions) self.assertEqual(pending_tournament.alias_to_id_map, self.alias_to_id_map) def test_set_alias_id_mapping_new(self): self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) new_alias = 'new alias' new_object_id = ObjectId() self.pending_tournament.set_alias_id_mapping(new_alias, new_object_id) self.assertEqual(len(self.pending_tournament.alias_to_id_map), 5) mapping = self.pending_tournament.alias_to_id_map[4] self.assertEqual(mapping.player_alias, new_alias) self.assertEqual(mapping.player_id, new_object_id) def test_set_alias_id_mapping_existing(self): self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) new_object_id = ObjectId() self.pending_tournament.set_alias_id_mapping( self.player_1.name, new_object_id) self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) mapping = self.pending_tournament.alias_to_id_map[0] self.assertEqual(mapping.player_alias, self.player_1.name) self.assertEqual(mapping.player_id, new_object_id) def test_delete_alias_id_mapping(self): self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) deleted_mapping = self.pending_tournament.delete_alias_id_mapping( self.player_1.name) self.assertEqual(len(self.pending_tournament.alias_to_id_map), 3) self.assertFalse( deleted_mapping in self.pending_tournament.alias_to_id_map) def test_from_scraper(self): mock_scraper = mock.Mock(spec=ChallongeScraper) mock_scraper.get_players.return_value = self.players mock_scraper.get_matches.return_value = self.matches mock_scraper.get_raw.return_value = '' mock_scraper.get_date.return_value = self.date mock_scraper.get_name.return_value = self.name mock_scraper.get_url.return_value = '' pending_tournament, _ = PendingTournament.from_scraper( self.type, mock_scraper, 'norcal') self.assertEqual(pending_tournament.type, self.type) self.assertEqual(pending_tournament.date, self.date) self.assertEqual(pending_tournament.name, self.name) self.assertEqual(pending_tournament.matches, self.matches) self.assertEqual(pending_tournament.players, self.players) self.assertEqual(pending_tournament.regions, ['norcal'])
def setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id) self.match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) self.match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.players = [ self.player_1.name, self.player_2.name, self.player_3.name, self.player_4.name ] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.url = 'http://challonge.com/test' self.excluded = False self.pending_tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'players': self.players, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions, 'alias_to_id_map': [am.dump(context='db') for am in self.alias_to_id_map], 'excluded': False, 'url': self.url } self.pending_tournament = PendingTournament( id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.players, matches=self.matches, excluded=self.excluded, alias_to_id_map=self.alias_to_id_map)
class TestPendingTournament(unittest.TestCase): def setUp(self): 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_6_id = ObjectId() self.player_1 = Player(name='gar', id=self.player_1_id) self.player_2 = Player(name='sfat', id=self.player_2_id) self.player_3 = Player(name='shroomed', id=self.player_3_id) self.player_4 = Player(name='ppu', id=self.player_4_id) self.player_5 = Player(name='ss', id=self.player_5_id) self.player_6 = Player(name='hmw', id=self.player_6_id) self.match_1 = Match(winner=self.player_1_id, loser=self.player_2_id) self.match_2 = Match(winner=self.player_3_id, loser=self.player_4_id) self.match_1 = AliasMatch(winner=self.player_1.name, loser=self.player_2.name) self.match_2 = AliasMatch(winner=self.player_3.name, loser=self.player_4.name) self.alias_to_id_map = [ AliasMapping(player_alias=self.player_1.name, player_id=self.player_1_id), AliasMapping(player_alias=self.player_2.name, player_id=self.player_2_id), AliasMapping(player_alias=self.player_3.name, player_id=self.player_3_id), AliasMapping(player_alias=self.player_4.name, player_id=self.player_4_id) ] self.id = ObjectId() self.type = 'tio' self.raw_id = ObjectId() self.date = datetime.now() self.name = 'tournament' self.players = [ self.player_1.name, self.player_2.name, self.player_3.name, self.player_4.name ] self.matches = [self.match_1, self.match_2] self.regions = ['norcal', 'texas'] self.url = 'http://challonge.com/test' self.excluded = False self.pending_tournament_json_dict = { '_id': self.id, 'type': self.type, 'raw_id': self.raw_id, 'date': self.date, 'name': self.name, 'players': self.players, 'matches': [m.dump(context='db') for m in self.matches], 'regions': self.regions, 'alias_to_id_map': [am.dump(context='db') for am in self.alias_to_id_map], 'excluded': False, 'url': self.url } self.pending_tournament = PendingTournament( id=self.id, name=self.name, type=self.type, date=self.date, regions=self.regions, url=self.url, raw_id=self.raw_id, players=self.players, matches=self.matches, excluded=self.excluded, alias_to_id_map=self.alias_to_id_map) def test_dump(self): self.assertEqual(self.pending_tournament.dump(context='db'), self.pending_tournament_json_dict) def test_load(self): pending_tournament = PendingTournament.load( self.pending_tournament_json_dict, context='db') self.assertEqual(pending_tournament.id, self.id) self.assertEqual(pending_tournament.type, self.type) self.assertEqual(pending_tournament.raw_id, self.raw_id) self.assertEqual(pending_tournament.date, self.date) self.assertEqual(pending_tournament.name, self.name) self.assertEqual(pending_tournament.matches, self.matches) self.assertEqual(pending_tournament.players, self.players) self.assertEqual(pending_tournament.regions, self.regions) self.assertEqual(pending_tournament.alias_to_id_map, self.alias_to_id_map) def test_set_alias_id_mapping_new(self): self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) new_alias = 'new alias' new_object_id = ObjectId() self.pending_tournament.set_alias_id_mapping(new_alias, new_object_id) self.assertEqual(len(self.pending_tournament.alias_to_id_map), 5) mapping = self.pending_tournament.alias_to_id_map[4] self.assertEqual(mapping.player_alias, new_alias) self.assertEqual(mapping.player_id, new_object_id) def test_set_alias_id_mapping_existing(self): self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) new_object_id = ObjectId() self.pending_tournament.set_alias_id_mapping(self.player_1.name, new_object_id) self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) mapping = self.pending_tournament.alias_to_id_map[0] self.assertEqual(mapping.player_alias, self.player_1.name) self.assertEqual(mapping.player_id, new_object_id) def test_delete_alias_id_mapping(self): self.assertEqual(len(self.pending_tournament.alias_to_id_map), 4) deleted_mapping = self.pending_tournament.delete_alias_id_mapping( self.player_1.name) self.assertEqual(len(self.pending_tournament.alias_to_id_map), 3) self.assertFalse( deleted_mapping in self.pending_tournament.alias_to_id_map) def test_from_scraper(self): mock_scraper = mock.Mock(spec=ChallongeScraper) mock_scraper.get_players.return_value = self.players mock_scraper.get_matches.return_value = self.matches mock_scraper.get_raw.return_value = '' mock_scraper.get_date.return_value = self.date mock_scraper.get_name.return_value = self.name mock_scraper.get_url.return_value = '' pending_tournament, _ = PendingTournament.from_scraper( self.type, mock_scraper, 'norcal') self.assertEqual(pending_tournament.type, self.type) self.assertEqual(pending_tournament.date, self.date) self.assertEqual(pending_tournament.name, self.name) self.assertEqual(pending_tournament.matches, self.matches) self.assertEqual(pending_tournament.players, self.players) self.assertEqual(pending_tournament.regions, ['norcal'])