def test_minute_representation(self): """ Tests the representation of the current minute of the match :return: None """ now = datetime.utcnow() match = Match(finished=True, kickoff=now.strftime("%Y-%m-%d:%H-%M-%S")) self.assertEqual(match.minute_display, "Ende") match.finished = False match.kickoff = (now + timedelta(days=1)).strftime("%Y-%m-%d:%H-%M-%S") self.assertEqual(match.minute_display, "-") match.kickoff = (now - timedelta(days=1)).strftime("%Y-%m-%d:%H-%M-%S") self.assertEqual(match.minute_display, "90.") match.kickoff = (now - timedelta(minutes=20))\ .strftime("%Y-%m-%d:%H-%M-%S") self.assertEqual(match.minute_display, "21.") match.kickoff = (now - timedelta(minutes=46)) \ .strftime("%Y-%m-%d:%H-%M-%S") self.assertEqual(match.minute_display, "45.") match.kickoff = (now - timedelta(minutes=50)) \ .strftime("%Y-%m-%d:%H-%M-%S") self.assertEqual(match.minute_display, "HZ") match.kickoff = (now - timedelta(minutes=80)) \ .strftime("%Y-%m-%d:%H-%M-%S") self.assertEqual(match.minute_display, "61.")
def setUp(self): """ Sets up data for the tests :return: """ super().setUp() self.team_one, self.team_two, self.player, old_match, _ = \ self.generate_sample_match_data() self.team_three = Team(name="ZZ", short_name="ZZ", abbreviation="ZZ", icon_svg="ZZ", icon_png="ZZ") self.db.session.add(self.team_three) self.db.session.delete(old_match) self.db.session.commit() self.match_one = Match(home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff="2019-01-01:01:02:01", started=True, finished=True, home_current_score=1, away_current_score=1, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) self.match_two = Match(home_team=self.team_two, away_team=self.team_one, matchday=2, kickoff="2019-01-01:01:02:02", started=True, finished=True, home_current_score=2, away_current_score=1, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) self.match_three = Match(home_team=self.team_one, away_team=self.team_three, matchday=3, kickoff="2019-01-01:01-02-03", started=True, finished=True, home_current_score=2, away_current_score=1, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) self.db.session.add(self.match_one) self.db.session.add(self.match_two) self.db.session.add(self.match_three) self.db.session.commit()
def setUp(self): """ Sets up data for the tests :return: """ super().setUp() self.second_user = User(username="******", email="AA", password_hash="AA", confirmation_hash="AA", confirmed=True) team_one, team_two, _, old_match, _ = self.generate_sample_match_data() self.db.session.delete(old_match) self.db.session.commit() self.db.session.add(self.second_user) self.db.session.add( Match(home_team=team_one, away_team=team_two, matchday=1, kickoff="2019-01-01:01-02-03", started=True, finished=True, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE)) self.db.session.commit()
def test_due_when_bets_placed(self): """ Tests if the is_due method works correctly when bets are placed :return: None """ now = datetime.utcnow() reminder = ReminderSettings(user=self.user_two, reminder_time=600, reminder_type=ReminderType.EMAIL) self.db.session.delete(self.match) self.db.session.commit() match_one = Match( home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff=(now + timedelta(minutes=5)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) match_two = Match( home_team=self.team_two, away_team=self.team_one, matchday=1, kickoff=(now + timedelta(minutes=7)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) self.db.session.add(match_one) self.db.session.add(match_two) self.db.session.commit() self.assertEqual(len(reminder.get_due_matches()), 2) self.generate_sample_bet(self.user_two, match_one) self.assertEqual(len(reminder.get_due_matches()), 1) self.generate_sample_bet(self.user_two, match_two) self.assertEqual(len(reminder.get_due_matches()), 0)
def generate_sample_match_data(self) \ -> Tuple[Team, Team, Player, Match, Goal]: """ Generates some sample match data :return: A tuple consisting of two teams, a player, a match and a goal """ team_one = Team(name="A", short_name="B", abbreviation="C", icon_svg="D1", icon_png="D2") team_two = Team(name="E", short_name="F", abbreviation="G", icon_svg="H1", icon_png="H2") player = Player(name="I", team_abbreviation=team_one.abbreviation) match = Match(matchday=1, kickoff="2017-01-01:01-02-03", finished=True, started=True, home_team_abbreviation=team_one.abbreviation, away_team_abbreviation=team_two.abbreviation, home_current_score=1, away_current_score=0, home_ht_score=0, away_ht_score=0, home_ft_score=1, away_ft_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) goal = Goal(home_team_abbreviation=match.home_team_abbreviation, away_team_abbreviation=match.away_team_abbreviation, season=match.season, league=match.league, matchday=match.matchday, player_name=player.name, player_team_abbreviation=player.team_abbreviation, minute=67, minute_et=None, home_score=1, away_score=0, own_goal=False, penalty=False) self.db.session.add(team_one) self.db.session.add(team_two) self.db.session.add(player) self.db.session.add(match) self.db.session.add(goal) self.db.session.commit() return team_one, team_two, player, match, goal
def test_score_representations(self): """ Tests the score representation attributes :return: None """ match = Match(home_ht_score=0, away_ht_score=1, home_ft_score=2, away_ft_score=3, home_current_score=4, away_current_score=5) self.assertEqual(match.ht_score, "0:1") self.assertEqual(match.ft_score, "2:3") self.assertEqual(match.current_score, "4:5")
def test_uniqueness(self): """ Tests that unique attributes are correctly checked :return: None """ self._test_uniqueness([ Match(home_team=self.match.home_team, away_team=self.match.away_team, matchday=self.match.matchday, kickoff="2019-01-01:01-02-03", started=False, finished=False, season=self.config.season()) ])
def parse_match(match_data: Dict[str, Any], league: str, season: int) -> Match: """ Parses a Match object from JSON match data :param match_data: The match data to parse :param league: The league :param season: The season :return: The generated Match object """ ht_home = 0 ht_away = 0 ft_home = 0 ft_away = 0 for result in match_data["MatchResults"]: if result["ResultName"] == "Halbzeit": ht_home = result["PointsTeam1"] ht_away = result["PointsTeam2"] elif result["ResultName"] == "Endergebnis": ft_home = result["PointsTeam1"] ft_away = result["PointsTeam2"] else: # pragma: no cover pass cur_home = max(ht_home, ft_home) cur_away = max(ht_away, ft_away) kickoff = match_data["MatchDateTimeUTC"] kickoff = datetime.strptime(kickoff, "%Y-%m-%dT%H:%M:%SZ") started = datetime.utcnow() > kickoff kickoff = kickoff.strftime("%Y-%m-%d:%H-%M-%S") home_team_abbreviation = get_team_data(match_data["Team1"]["TeamName"])[2] away_team_abbreviation = get_team_data(match_data["Team2"]["TeamName"])[2] match = Match(home_team_abbreviation=home_team_abbreviation, away_team_abbreviation=away_team_abbreviation, season=season, league=league, matchday=match_data["Group"]["GroupOrderID"], home_current_score=cur_home, away_current_score=cur_away, home_ht_score=ht_home, away_ht_score=ht_away, home_ft_score=ft_home, away_ft_score=ft_away, kickoff=kickoff, started=started, finished=match_data["MatchIsFinished"]) return match
def setUp(self): """ Generates sample match data :return: None """ super().setUp() team_one, team_two, _, old_match, _ = self.generate_sample_match_data() self.db.session.delete(old_match) self.db.session.commit() self.db.session.add(Match( home_team=team_one, away_team=team_two, matchday=1, kickoff="2019-01-01:01-02-03", started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE )) self.db.session.commit()
def test_missing_column_data(self): """ Tests that missing column data is handled correctly :return: None """ self._test_missing_column_data([ Match(away_team=self.team_two, matchday=1, kickoff="2019-01-01:01-02-03", started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season()), Match(home_team=self.team_one, matchday=1, kickoff="2019-01-01:01-02-03", started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season()), Match(home_team=self.team_one, away_team=self.team_two, kickoff="2019-01-01:01-02-03", started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season()), Match(home_team=self.team_one, away_team=self.team_two, matchday=1, started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season()), Match(home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff="2019-01-01:01-02-03", finished=False, home_current_score=0, away_current_score=0, season=self.config.season()), Match(home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff="2019-01-01:01-02-03", started=False, home_current_score=0, away_current_score=0, season=self.config.season()), Match(home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff="2019-01-01:01-02-03", started=False, finished=False, away_current_score=0, season=self.config.season()), Match(home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff="2019-01-01:01-02-03", started=False, finished=False, home_current_score=0, season=self.config.season()) ])
def test_due(self): """ Tests if due matches can be found correctly Dates: Now ----- Reminder 1 ----- Match 1 ----- Reminder 2 ----- Match 2 -0----------10min-----------30min-----------60min---------120min- :return: None """ now = datetime.utcnow() new_user = User(username="******", email="Z", confirmed=True, password_hash="Z", confirmation_hash="Z") reminder_one = ReminderSettings(user=self.user_two, reminder_time=600, reminder_type=ReminderType.EMAIL) reminder_two = ReminderSettings(user=new_user, reminder_time=3600, reminder_type=ReminderType.EMAIL) self.db.session.delete(self.match) self.db.session.commit() match_one = Match( home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff=(now + timedelta(minutes=30)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) match_two = Match( home_team=self.team_two, away_team=self.team_one, matchday=1, kickoff=(now + timedelta(minutes=120)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) self.db.session.add(match_one) self.db.session.add(match_two) self.db.session.add(new_user) self.db.session.commit() self.assertEqual(reminder_one.get_due_matches(), []) self.assertEqual(reminder_two.get_due_matches(), [match_one]) with self.context: with mock.patch("bundesliga_tippspiel.db.settings." "ReminderSettings.send_email") as mocked: reminder_one.send_reminder() self.assertEqual(0, mocked.call_count) reminder_two.send_reminder() self.assertEqual(1, mocked.call_count) reminder_one.send_reminder() reminder_two.send_reminder() self.assertEqual(1, mocked.call_count) self.assertEqual(reminder_one.get_due_matches(), []) self.assertEqual(reminder_two.get_due_matches(), []) reminder_one.set_reminder_time(10000) reminder_two.set_reminder_time(10000) self.assertEqual(reminder_two.get_due_matches(), [match_one, match_two]) self.assertEqual(reminder_two.get_due_matches(), [match_one, match_two]) with self.context: with mock.patch("bundesliga_tippspiel.db.settings." "ReminderSettings.send_email") as mocked: reminder_one.send_reminder() self.assertEqual(1, mocked.call_count) reminder_two.send_reminder() self.assertEqual(2, mocked.call_count) reminder_one.send_reminder() reminder_two.send_reminder() self.assertEqual(2, mocked.call_count)