def test_update_results(fauna_session): match_results = data_factories.fake_match_results_data() match_results_to_update = match_results.iloc[:3, :] matches = [] for _idx, match_result in match_results_to_update.iterrows(): match = model_factories.FullMatchFactory( start_date_time=match_result["date"], round_number=match_result["round_number"], home_team_match__team__name=match_result["home_team"], away_team_match__team__name=match_result["away_team"], ) matches.append(match) Match.update_results(matches, match_results) queried_matches = fauna_session.execute(sql.select(Match)).scalars().all() for match in queried_matches: assert match.margin is not None assert match.winner_id is not None for team_match in match.team_matches: assert team_match.score is not None for prediction in match.predictions: assert prediction.is_correct is not None
def test_update_result(start_date_time, expected_winner): match_results = data_factories.fake_match_results_data() home_team_name = expected_winner or np.random.choice(TeamName.values()) home_team = models.Team(name=home_team_name) home_team_results = match_results.query("home_team == @home_team_name") random_row_idx = np.random.randint(0, len(home_team_results)) match_result = home_team_results.iloc[random_row_idx:( random_row_idx + 1), :].assign(home_score=lambda df: df["away_score"] * 2) match = Match( start_date_time=start_date_time, venue=Fake.company(), round_number=np.random.randint(1, 100), team_matches=[ models.TeamMatch( team=home_team, at_home=True, ), models.TeamMatch( team=models.Team(name=match_result.iloc[0, :]["away_team"]), at_home=False, ), ], predictions=[ models.Prediction( ml_model=models.MLModel(name=Fake.company(), prediction_type="margin"), predicted_winner=home_team, predicted_margin=np.random.randint(0, 100), ) ], ) match.update_result(match_result) if expected_winner is None: assert match.margin is None assert match.winner is None for team_match in match.team_matches: assert team_match.score is None for prediction in match.predictions: assert prediction.is_correct is None else: assert match.margin == abs(match_result.iloc[0, :]["home_score"] - match_result.iloc[0, :]["away_score"]) assert match.winner.name == home_team.name for team_match in match.team_matches: assert team_match.score is not None for prediction in match.predictions: assert prediction.is_correct is not None
def test_update_match_results(status_code, expected_error): responses.add( responses.POST, f"{settings.TIPRESIAS_APP}/matches", status=status_code, json="Stuff happened", ) fake_match_results = data_factories.fake_match_results_data() if expected_error is None: data_export.update_match_results(fake_match_results) else: with pytest.raises(expected_error, match=str(status_code)): data_export.update_match_results(fake_match_results)
def test_update_results(mock_update_result): match_results = data_factories.fake_match_results_data() calls = [] matches = [] for _idx, match_result in match_results.iterrows(): home_team = models.Team(name=match_result["home_team"]) match = Match( start_date_time=match_result["date"], venue=Fake.company(), round_number=match_result["round_number"], team_matches=[ models.TeamMatch( team=home_team, at_home=True, ), models.TeamMatch( team=models.Team(name=match_result["away_team"]), at_home=False, ), ], predictions=[ models.Prediction( ml_model=models.MLModel(name=Fake.company(), prediction_type="margin"), predicted_winner=home_team, predicted_margin=np.random.randint(0, 100), ) ], ) matches.append(match) calls.append(mock.call(match_result)) Match.update_results(matches, match_results) assert mock_update_result.call_count == len(match_results)
}, r"score"), ], ) def test_team_match_validation(attribute, error_message): default_input = { "team_id": np.random.randint(1, 100), "match_id": np.random.randint(1, 100), "score": np.random.randint(0, 100), "at_home": FAKE.pybool(), } with pytest.raises(ValidationError, match=error_message): TeamMatch(**{ **default_input, **attribute, }) match_results = data_factories.fake_match_results_data() def test_update_score(): random_row_idx = np.random.randint(0, len(match_results)) match_result = match_results.iloc[random_row_idx, :] team_match = TeamMatch(team=models.Team(name=match_result["home_team"]), at_home=True) team_match.update_score(match_result) assert team_match.score == match_result["home_score"]
def test_update_match_results( self, MockDataImporter, mock_data_export, MockSession ): season = MATCH_SEASON_RANGE[0] seasons = (season, season + 1) mock_db_session = MagicMock() mock_db_session.add = MagicMock() mock_db_session.commit = MagicMock() mock_db_session.execute = MagicMock() MockSession.return_value = mock_db_session # We want a date roughly in the middle of the season to make sure # we get matches before and after in the fixture with freeze_time(datetime(season, 7, 1, tzinfo=pytz.UTC)): matches = data_factories.fake_match_data(seasons=seasons) right_now = datetime.now(tz=pytz.UTC) # pylint: disable=unused-variable last_match_round_number = matches.query("date < @right_now")[ "round_number" ].max() match_results = data_factories.fake_match_results_data( matches, round_number=last_match_round_number ) mock_data_import = MagicMock() mock_data_import.fetch_fixture_data = MagicMock(return_value=matches) mock_data_import.fetch_match_results_data = MagicMock( return_value=match_results ) MockDataImporter.return_value = mock_data_import mock_data_export.update_match_results = MagicMock() mock_scalars = MagicMock() mock_scalars.all = MagicMock( return_value=[ models.Match( start_date_time=data["date"], round_number=data["round_number"], venue=Fake.company(), team_matches=[ models.TeamMatch( team=models.Team(name=data["home_team"]), at_home=True ), models.TeamMatch( team=models.Team(name=data["away_team"]), at_home=False ), ], ) for _, data in match_results.iterrows() ] ) mock_result = MagicMock() mock_result.scalars = MagicMock(return_value=mock_scalars) mock_db_session.execute = MagicMock(return_value=mock_result) self.api.update_match_results(verbose=0) # It posts data to main app mock_data_export.update_match_results.assert_called() # It posts data for the round from the most-recent match call_args = mock_data_export.update_match_results.call_args[0] data_are_equal = ( (call_args[0]["round_number"] == last_match_round_number).all().all() ) self.assertTrue(data_are_equal) mock_db_session.commit.assert_called()