def test_does_team_play_in_games_list(self): team1 = Team("Team 1", 5, False) team2 = Team("Team 2", 5, False) team3 = Team("Team 3", 5, False) team4 = Team("Team 4", 5, False) game1 = Game(1, 1, team1, team2, 0, 0, False, False, None) game2 = Game(1, 1, team3, team4, 0, 0, False, False, None) game_list_1 = [game1, game2] game_list_2 = [game2] self.assertTrue( Scheduler.does_team_play_in_games_list(game_list_1, team1)) self.assertTrue( Scheduler.does_team_play_in_games_list(game_list_1, team2)) self.assertTrue( Scheduler.does_team_play_in_games_list(game_list_1, team3)) self.assertTrue( Scheduler.does_team_play_in_games_list(game_list_1, team4)) self.assertFalse( Scheduler.does_team_play_in_games_list(game_list_2, team1)) self.assertFalse( Scheduler.does_team_play_in_games_list(game_list_2, team2)) self.assertTrue( Scheduler.does_team_play_in_games_list(game_list_2, team3)) self.assertTrue( Scheduler.does_team_play_in_games_list(game_list_2, team4))
def test_stats(self): t1_skill = 10 t2_skill = 0 team1 = Team("T1", t1_skill, True, "1") team2 = Team("T2", t2_skill, True, "2") record1 = Record(1, team1, 1, 0, 0, 0, 0, 0, 0, "3") record2 = Record(1, team2, 1, 0, 0, 0, 0, 0, 0, "3") rules = GameRules("Rules", True, "4") for i in range(1000): game = Game(1, 1, team1, team2, 0, 0, False, False, rules, str(i)) game.play() record1.process_game(game.home_score, game.away_score) record2.process_game(game.away_score, game.home_score) print() print("Results") print(RecordView.get_table_header()) print( RecordView.get_table_row( RecordService.get_view_from_model(record1))) print( RecordView.get_table_row( RecordService.get_view_from_model(record2)))
def __init__(self, game): home_team_dto = TeamDTO.get_dto(game.home_team) away_team_dto = TeamDTO.get_dto(game.away_team) game_rules_dto = GameRulesDTO.get_dto(game.rules) Game.__init__(self, game.year, game.day, home_team_dto, away_team_dto, game.home_score, game.away_score, game.complete, game.processed, game_rules_dto, game.oid)
def test_play(self): game = Game(5, 25, Team("T1", 5, True, "1"), Team("T1", 4, True, "2"), 0, 0, False, False, GameRules("Rules", True, ""), "") np.random.seed = 1235 game.play() self.assertTrue(game.complete)
def __init__(self, competition, sub_competition, day, home_team, away_team, home_score, away_score, complete, game_processed, rules, oid=None): self.sub_competition = sub_competition self.competition = competition Game.__init__(self, competition.year, day, home_team, away_team, home_score, away_score, complete, game_processed, rules, oid)
def test_does_team_play_in_game(self): team1 = Team("Team 1", 5, True) team2 = Team("Team 2", 5, True) team3 = Team("Team 3", 5, True) game = Game(1, 1, team2, team3, 0, 0, False, False, None) self.assertTrue(Scheduler.does_team_play_in_game(game, team2)) self.assertTrue(Scheduler.does_team_play_in_game(game, team3)) self.assertFalse(Scheduler.does_team_play_in_game(game, team1))
def test_get_first_day_for_game(self): session = self.setup_basic_test() Database.clean_up_database(session) team_repo = TeamRepository() rules_repo = GameRulesRepository() team1 = Team("t1", 5, True, "A") team2 = Team("t2", 5, True, "B") team3 = Team("t3", 5, True, "C") team4 = Team("t4", 5, True, "D") new_teams = [team1, team2, team3, team4] [team_repo.add(team, session) for team in new_teams] new_gr = GameRules("Rules 12", True, "T") rules_repo.add(new_gr, session) session.commit() gr = rules_repo.get_by_name("Rules 12", session) teams = team_repo.get_all(session) game1 = Game(1, 5, teams[0], teams[1], 0, 1, True, True, gr, "K") games = [game1] [self.get_repo().add(game, session) for game in games] session.commit() # first test, should be none because teams[0] plays on day 5 game2 = GameDTO(Game(1, -1, teams[0], teams[2], 0, 1, True, True, gr, "L")) result = self.get_repo().get_list_days_teams_play_on(1, 1, 100, game2, session) self.assertEquals(5, result[0]) game2.day = 1 self.get_repo().add(game2, session) session.commit() result = self.get_repo().get_list_days_teams_play_on(1, 1, 100, game2, session) print(result)
def test_add_no_existing_teams(self): session = self.setup_basic_test() name_1 = "Team GA1" name_2 = "Team GA2" team_1 = Team(name_1, 12, True, self.get_id()) team_2 = Team(name_2, 12, True, self.get_id()) self.get_repo().add(Game(1, 15, team_1, team_2, 5, 4, True, False, GameRules("Rules", True, self.get_id()), self.get_id()), session) session.commit() g_list = self.get_repo().get_all(session) self.assertEqual(1, len(g_list))
def test_get_my_complete_and_unprocessed(self): session = self.setup_basic_test() Database.clean_up_database(session) team_repo = TeamRepository() rules_repo = GameRulesRepository() team1 = Team("t1", 5, True, "A") team2 = Team("t2", 5, True, "B") team_repo.add(team1, session) team_repo.add(team2, session) gr = GameRules("Rules 12", True, "T") rules_repo.add(gr, session) session.commit() team1 = team_repo.get_by_name("t1", session) team2 = team_repo.get_by_name("t2", session) gr = rules_repo.get_by_name("Rules 12", session) game1 = Game(1, 1, team1, team2, 0, 0, True, False, gr, "1") game2 = Game(1, 1, team1, team2, 0, 0, False, False, gr, "2") game3 = Game(1, 2, team1, team2, 0, 0, False, False, gr, "3") game4 = Game(1, 2, team1, team2, 0, 0, False, False, gr, "4") game5 = Game(1, 3, team1, team2, 0, 0, True, False, gr, "5") game6 = Game(1, 3, team1, team2, 0, 0, False, False, gr, "6") game7 = Game(1, 4, team1, team2, 0, 0, True, False, gr, "7") game8 = Game(2, 1, team1, team2, 0, 0, True, False, gr, "8") games = [game1, game2, game3, game4, game5, game6, game7, game8] [self.get_repo().add(g, session) for g in games] session.commit() result = list(self.get_repo().get_by_unprocessed_and_complete(1, 1, 4, session)) self.assertEqual(3, len(result)) result = list(self.get_repo().get_by_unprocessed_and_complete(1, 1, 2, session)) self.assertEqual(1, len(result)) result = list(self.get_repo().get_by_unprocessed_and_complete(2, 1, 25, session)) self.assertEqual(1, len(result))
def test_does_any_team_play_in_other_list(self): team1 = Team("Team 1", 5, False) team2 = Team("Team 2", 5, False) team3 = Team("Team 3", 5, False) team4 = Team("Team 4", 5, False) team5 = Team("Team 5", 5, False) team6 = Team("Team 6", 5, False) team7 = Team("Team 6", 5, False) team8 = Team("Team 6", 5, False) game1 = Game(1, 1, team1, team2, 0, 0, False, False, None) game2 = Game(1, 1, team3, team4, 0, 0, False, False, None) game3 = Game(1, 1, team5, team6, 0, 0, False, False, None) game4 = Game(1, 1, team1, team3, 0, 0, False, False, None) game5 = Game(1, 1, team2, team5, 0, 0, False, False, None) game6 = Game(1, 1, team4, team6, 0, 0, False, False, None) game7 = Game(1, 1, team7, team8, 0, 0, False, False, None) game_list_1 = [game1, game2, game3] game_list_2 = [game2, game3, game4] game_list_3 = [game1, game2] game_list_4 = [game6] game_list_5 = [game1, game2] game_list_6 = [game3, game7] # all teams play in all other games self.assertTrue( Scheduler.does_any_team_play_in_other_list(game_list_1, game_list_2)) # only one home team plays in other day self.assertTrue( Scheduler.does_any_team_play_in_other_list(game_list_4, game_list_3)) # no teams play self.assertFalse( Scheduler.does_any_team_play_in_other_list(game_list_5, game_list_6))
def create_game_from_schedule_game(self, schedule_game, session=None): commit = False if session is None: session = self.repo.get_session() commit = True team_a = self.team_repo.get_by_oid(schedule_game.home_team, session) team_b = self.team_repo.get_by_oid(schedule_game.away_team, session) rules = self.game_rules_repo.get_by_oid(schedule_game.rules, session) if team_a is None: raise AttributeError("Team A cannot be none.") if team_b is None: raise AttributeError("Team B cannot be none.") game = Game(schedule_game.year, schedule_game.day, team_a, team_b, 0, 0, False, False, rules, self.get_new_id()) if commit: session.Commit() return game
def get_add_record(self): return GameDTO(Game(1, 2, TeamDTO(Team("Home team", 250, True)), TeamDTO(Team("Away Team", 251, False)), 3, 4, False, True, GameRulesDTO(GameRules("Rules Name", False))))