def admin_import_game_list(): results = {'errors': [], 'success':False, 'warnings': []} if not logged_in(): results['errors'].append("Permission denied") return dumps(results) file = request.files['file'] if file and allowed_file(file.filename): content = (file.read()).decode("UTF-8") print(content) lines = content.replace("\r", "") lines = lines.split("\n") team = LeagueList(lines) team.import_league() results['errors'] = team.errors results['warnings'] = team.warnings results['success'] = True if len(results['errors']) > 0: results['success'] = False else: results['errors'] = "File should ba CSV" results['success'] = False return dumps(results)
def admin_import_game_list(): results = {'errors': [], 'success': False, 'warnings': []} if not logged_in(): results['errors'].append("Permission denied") return dumps(results) file = request.files['file'] if file and allowed_file(file.filename): content = (file.read()).decode("UTF-8") print(content) lines = content.replace("\r", "") lines = lines.split("\n") team = LeagueList(lines) team.import_league() results['errors'] = team.errors results['warnings'] = team.warnings results['success'] = True if len(results['errors']) > 0: results['success'] = False else: results['errors'] = "File should be a CSV" results['success'] = False return dumps(results)
class TestImportGames(TestSetup): TEST = [ "League:,Monday and Wednesday,,,", "Home Team,Away Team,Date,Time,Field", "Domus Green,Chainsaw Black,2015-10-01", "12:00", "WP1"] TOO_SHORT = ["Home Team,Away Team,Date,Time,Field",] TOO_FEW_COLUMNS = ["League:,Monday and Wednesday,,,", "Home Team,Away Team,Date,Time", "Domus Green,Chainsaw Black,2015-10-01", "12:00", "WP1" ] MISSING_HOME_NAME = [ "League:,Monday and Wednesday,,,", ",Away Team,Date,Time", "Domus Green,Chainsaw Black,2015-10-01", "12:00", "WP1" ] def testParseHeader(self): self.tl = LeagueList(TestImportGames.TEST) l,h = self.tl.parse_header(TestImportGames.TEST[0:2]) self.assertEqual(l, 'Monday and Wednesday') self.assertEqual(h, ["Home Team", "Away Team", "Date", "Time", "Field"]) def testCheckHeader(self): # check valid header self.tl = LeagueList(TestImportGames.TEST) valid = self.tl.check_header(TestImportGames.TEST[0:2]) self.assertEqual(valid, True) # check a header that is too short self.tl = LeagueList(TestImportGames.TOO_SHORT) valid = self.tl.check_header(TestImportGames.TOO_SHORT[0:2]) self.assertEqual(valid, False) # check a header that has too few columns self.tl = LeagueList(TestImportGames.TOO_FEW_COLUMNS) valid = self.tl.check_header(TestImportGames.TOO_FEW_COLUMNS[0:2]) self.assertEqual(valid, False) # check a header that is missing a column self.tl = LeagueList(TestImportGames.MISSING_HOME_NAME) valid = self.tl.check_header(TestImportGames.TOO_FEW_COLUMNS[0:2]) self.assertEqual(valid, False) def testGetLeagueID(self): self.addLeagues() self.tl = LeagueList(TestImportGames.TEST) team = self.tl.get_league_id("Monday & Wedneday") self.assertEqual(team, 1) self.tl = LeagueList(TestImportGames.TEST) try: team = self.tl.get_league_id("No League") self.assertEqual(True, False, "League does not exist error should be raised") except: pass def testImportGame(self): self.addTeamWithLegaue() self.show_results = True # add games to the league self.valid_test = [ "League:,Monday & Wednesday,,,", "Home Team,Away Team,Date,Time,Field", "Domus Green,Chainsaw Black,2015-10-01,12:00,WP1"] self.tl = LeagueList(self.valid_test) self.tl.league_id = 1 self.tl.set_columns_indices(self.valid_test[1].split(",")) self.tl.set_teams() self.tl.import_game(self.valid_test[2]) self.assertEqual(self.tl.warnings, []) self.assertEqual(self.tl.errors, []) # not a team in the league self.valid_test = [ "League:,Monday and Wednesday,,,", "Home Team,Away Team,Date,Time,Field", "Domus Black,Chainsaw Black,2015-10-01,12:00, WP1"] self.tl = LeagueList(self.valid_test) self.tl.league_id = 1 self.tl.set_columns_indices(self.valid_test[1].split(",")) self.tl.set_teams() self.tl.import_game(self.valid_test[2]) self.assertEqual(self.tl.warnings, []) self.assertEqual(self.tl.errors, ["Domus Black is not a team in the league"]) def testValidCases(self): self.addTeamWithLegaue() # import a set of good games self.valid_test = [ "League:,Monday & Wedneday,,,", "Home Team,Away Team,Date,Time,Field", "Domus Green,Chainsaw Black,2015-10-01,12:00,WP1"] self.tl = LeagueList(self.valid_test) self.tl.import_league() self.assertEqual([], self.tl.warnings) self.assertEqual([], self.tl.errors) def testInvalidCases(self): self.addTeamWithLegaue() # test bad header self.bad_header = [ "League:,Monday & Wedneday,,,", "Home Team,Away Team,Date,Time,sdjfkhskdj", "Domus Green,Chainsaw Black,2015-10-01,12:00,WP1"] self.tl = LeagueList(self.bad_header) self.tl.import_league() # test bad league self.bad_league = [ "Leaguex:,Monday & Wedneday,,,", "Home Team,Away Team,Date,Time,Field", "Domus Green,Chainsaw Black,2015-10-01,12:00,WP1"] self.tl = LeagueList(self.bad_league) try: self.tl.import_league() except LeagueDoesNotExist: pass # test bad game self.bad_game = [ "League:,Monday & Wedneday,,,", "Home Team,Away Team,Date,Time,Field", "Domus Green,Chainsaw Black,2015-xx-01,12:00,WP1"] self.tl = LeagueList(self.bad_game) try: self.tl.import_league() self.assertEqual(True, False, "should raise error") except InvalidField: pass self.bad_team = [ "League:,Monday & Wedneday,,,", "Home Team,Away Team,Date,Time,Field", "X Green,Chainsaw Black,2015-10-01,12:00,WP1"] # test bad team in game self.tl = LeagueList(self.bad_team) self.tl.import_league() self.assertEqual(self.tl.warnings, []) self.assertEqual(['X Green is not a team in the league'], self.tl.errors)
class TestImportGames(TestSetup): TEST = [ "League:,Monday and Wednesday,,,", "Home Team,Away Team,Date,Time,Field", "Domus Green,Chainsaw Black,2015-10-01", "12:00", "WP1"] TOO_SHORT = ["Home Team,Away Team,Date,Time,Field"] TOO_FEW_COLUMNS = ["League:,Monday and Wednesday,,,", "Home Team,Away Team,Date,Time", "Domus Green,Chainsaw Black,2015-10-01", "12:00", "WP1" ] MISSING_HOME_NAME = [ "League:,Monday and Wednesday,,,", ",Away Team,Date,Time", "Domus Green,Chainsaw Black,2015-10-01", "12:00", "WP1" ] def testParseHeader(self): self.tl = LeagueList(TestImportGames.TEST, session=MockSession(self)) l, h = self.tl.parse_header(TestImportGames.TEST[0: 2]) self.assertEqual(l, 'Monday and Wednesday') self.assertEqual(h, ["Home Team", "Away Team", "Date", "Time", "Field"]) def testCheckHeader(self): # check valid header self.tl = LeagueList(TestImportGames.TEST, session=MockSession(self)) valid = self.tl.check_header(TestImportGames.TEST[0:2]) self.assertEqual(valid, True) # check a header that is too short self.tl = LeagueList(TestImportGames.TOO_SHORT, session=MockSession(self)) valid = self.tl.check_header(TestImportGames.TOO_SHORT[0:2]) self.assertEqual(valid, False) # check a header that has too few columns self.tl = LeagueList(TestImportGames.TOO_FEW_COLUMNS, session=MockSession(self)) valid = self.tl.check_header(TestImportGames.TOO_FEW_COLUMNS[0:2]) self.assertEqual(valid, False) # check a header that is missing a column self.tl = LeagueList(TestImportGames.MISSING_HOME_NAME, session=MockSession(self)) valid = self.tl.check_header(TestImportGames.TOO_FEW_COLUMNS[0:2]) self.assertEqual(valid, False) def testGetLeagueID(self): league_name = "Test Import Fake League" league = self.add_league(league_name) self.tl = LeagueList(TestImportGames.TEST, session=MockSession(self)) league_id = self.tl.get_league_id(league_name) self.assertEqual(league['league_id'], league_id) self.tl = LeagueList(TestImportGames.TEST, session=MockSession(self)) try: self.tl.get_league_id("No League") self.assertEqual(True, False, "League does not exist error should be raised") except Exception: pass def testImportGame(self): # add a league, sponsor and two teams league_name = "Test Import Fake League" league = self.add_league(league_name) sponsor = self.add_sponsor("Import Test Sponsor") t1 = self.add_team("Green", sponsor, league) t2 = self.add_team("Black", sponsor, league) # a valid request to add a game day = datetime.date.today().strftime("%Y-%m-%d") game_entry = "{},{},{},12:00,WP1".format(t1['team_name'], t2['team_name'], day) self.valid_test = ["League:,{},,,".format(league_name), "Home Team,Away Team,Date,Time,Field", game_entry] self.tl = LeagueList(self.valid_test, session=MockSession(self)) self.tl.league_id = league['league_id'] self.tl.set_columns_indices(self.valid_test[1].split(",")) self.tl.set_teams() self.tl.import_game(self.valid_test[2]) self.assertEqual(self.tl.warnings, []) self.assertEqual(self.tl.errors, []) # an invalid request - one team not in the league game_entry = "{},{},{},12:00,WP1".format("NOT A TEAM", t2['team_name'], day) self.valid_test = ["League:,{},,,".format(league_name), "Home Team,Away Team,Date,Time,Field", game_entry] self.tl = LeagueList(self.valid_test, session=MockSession(self)) self.tl.league_id = league['league_id'] self.tl.set_columns_indices(self.valid_test[1].split(",")) self.tl.set_teams() self.tl.import_game(self.valid_test[2]) self.assertEqual(self.tl.warnings, []) self.assertEqual(self.tl.errors, ["NOT A TEAM is not a team in the league"]) def testValidCases(self): # add a league, sponsor and two teams league_name = "Test Import Fake League" league = self.add_league(league_name) sponsor = self.add_sponsor("Import Test Sponsor") t1 = self.add_team("Green", sponsor, league) t2 = self.add_team("Black", sponsor, league) # import a set of good games day = datetime.date.today().strftime("%Y-%m-%d") game_entry = "{},{},{},12:00,WP1".format(t1['team_name'], t2['team_name'], day) self.valid_test = ["League:,{},,,".format(league_name), "Home Team,Away Team,Date,Time,Field", game_entry] self.tl = LeagueList(self.valid_test, session=MockSession(self)) self.tl.import_league() self.assertEqual([], self.tl.warnings) self.assertEqual([], self.tl.errors) def testInvalidCases(self): # add a league, sponsor and two teams league_name = "Test Import Fake League" league = self.add_league(league_name) sponsor = self.add_sponsor("Import Test Sponsor") t1 = self.add_team("Green", sponsor, league) t2 = self.add_team("Black", sponsor, league) # test bad header # import a set of good games day = datetime.date.today().strftime("%Y-%m-%d") game_entry = "{},{},{},12:00,WP1".format(t1['team_name'], t2['team_name'], day) self.bad_header = ["League:,{},,,".format(league_name), "Home Team,Away Team,Date,Time,asjdl9798u", game_entry] self.tl = LeagueList(self.bad_header) self.tl.import_league() # test bad league self.bad_league = ["Leaguex:,{}xas,,,".format(league_name), "Home Team,Away Team,Date,Time,Field", game_entry] self.tl = LeagueList(self.bad_league, session=MockSession(self)) try: self.tl.import_league() except LeagueDoesNotExist: pass # test bad game game_entry = "{},{},2015-XX-01,12:00,WP1".format(t1['team_name'], t2['team_name']) self.bad_game = ["League:,{},,,".format(league_name), "Home Team,Away Team,Date,Time,Field", game_entry] self.tl = LeagueList(self.bad_game, session=MockSession(self)) try: self.tl.import_league() self.assertEqual(True, False, "should raise error") except InvalidField: pass # test bad team in game bad_team = "xaasdasd3" game_entry = "{},{},{},12:00,WP1".format(bad_team, t2['team_name'], day) self.bad_team = ["League:,{},,,".format(league_name), "Home Team,Away Team,Date,Time,Field", game_entry] self.tl = LeagueList(self.bad_team, session=MockSession(self)) self.tl.import_league() self.assertEqual(self.tl.warnings, []) self.assertEqual(['{} is not a team in the league'.format(bad_team)], self.tl.errors)