def test_add_one_existing_team(self): session = self.setup_basic_test() team_repo = TeamRepository() name_1 = "Team GA1" name_2 = "Team GA2" team_repo.add(Team(name_1, 12, True, self.get_id()), session) session.commit() team_1 = team_repo.get_by_name(name_1, session) 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_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_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 get_team_repo(self): return TeamRepository()
class GameService(BaseService): team_service = TeamService() record_service = RecordService() repo = GameRepository() team_repo = TeamRepository() record_repo = RecordRepository() game_rules_repo = GameRulesRepository() 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 create_games(self, team_list, year, start_day, rules, rounds, home_and_away, session=None): if session is None: session = self.repo.get_session() scheduler = Scheduler() team_ids = [i.oid for i in team_list] schedule_games = [] # schedule_games = scheduler.schedule_games(team_ids, rules.oid, year, start_day, home_and_away) for i in range(rounds): schedule_games.extend( scheduler.schedule_games(team_ids, rules.oid, year, start_day, home_and_away)) start_day = max([sg.day for sg in schedule_games]) start_day += 1 new_games = [ self.create_game_from_schedule_game(sg, session) for sg in schedule_games ] [self.repo.add(g, session) for g in new_games] session.commit() @staticmethod def games_to_game_day_view(games): game_vm = [GameService.game_to_vm(g) for g in games] return GameDayViewModel(None, game_vm[0].day, game_vm[0].year, game_vm) @staticmethod def game_to_vm(g): return GameViewModel(g.oid, g.year, g.day, g.home_team.name, g.home_team.oid, g.away_team.name, g.away_team.oid, g.home_score, g.away_score, g.complete) def get_all_games(self): session = self.get_session() return [self.game_to_vm(g) for g in self.repo.get_all(session)] def play_game(self, game_list, random, session=None): commit = session is None session = self.get_session(session) for g in game_list: game = self.repo.get_by_oid(g.oid, session) game.play() self.commit(session, commit) def get_games_for_days(self, year, first_day, last_day, session=None): commit = session is None session = self.get_session() result = self.repo.get_games_by_day(year, first_day, last_day, session) self.commit(session, commit) return [self.game_to_vm(g) for g in result] def play_games_for_days(self, year, first_day, last_day, random, session=None): commit = session is None session = self.get_session(session) games = self.repo.get_games_by_day(year, first_day, last_day, session) self.play_game(games, random, session) self.commit(session, commit) return [ self.game_to_vm(g) for g in self.repo.get_games_by_day( year, first_day, last_day, session) ] def get_incomplete_games_for_days(self, year, first_day, last_day, session=None): session = self.get_session(session) return [ self.game_to_vm(g) for g in self.repo.get_incomplete_or_unprocessed_games_by_day( year, first_day, last_day, session) ] def get_complete_and_unprocessed_games_for_days(self, year, first_day, last_day, session=None): session = self.get_session(session) return [ self.game_to_vm(g) for g in self.repo.get_by_unprocessed_and_complete( year, first_day, last_day, session) ] # processing games needs to go to a higher level so we can know how to process a given game def process_games_for_days(self, year, first_day, last_day, session=None): commit = session is None session = self.get_session(session) games_to_process = self.repo.get_by_unprocessed_and_complete( year, first_day, last_day, session) for game in games_to_process: home_record = self.record_repo.get_by_team_and_year( game.home_team.oid, year, session) away_record = self.record_repo.get_by_team_and_year( game.away_team.oid, year, session) home_record.process_game(game.home_score, game.away_score) away_record.process_game(game.away_score, game.home_score) game.processed = True self.commit(session, commit) def process_games_before(self, year, before_this_day, session=None): raise NotImplementedError def get_incomplete_games_by_year_count(self, year, session=None): session = self.get_session(session) repo = GameRepository() return repo.get_incomplete_or_unprocessed_games_by_year_count( year, session)