def setUp(self): today = datetime.now(tz=MELBOURNE_TIMEZONE) year = today.year self.match_results_data = fake_match_results_data( ROW_COUNT, (year, year + 1)) # Save records in DB ml_model = MLModelFactory(name="tipresias") for match_data in self.match_results_data.to_dict("records"): match_date = (match_data["date"].to_pydatetime().replace( tzinfo=MELBOURNE_TIMEZONE)) match_attrs = { "start_date_time": match_date, "round_number": match_data["round_number"], "venue": match_data["venue"], } prediction_attrs = { "prediction__ml_model": ml_model, "prediction__predicted_winner__name": np.random.choice( [match_data["home_team"], match_data["away_team"]]), } team_match_attrs = { "home_team_match__team__name": match_data["home_team"], "away_team_match__team__name": match_data["away_team"], "home_team_match__score": match_data["home_score"], "away_team_match__score": match_data["away_score"], } FullMatchFactory(**match_attrs, **prediction_attrs, **team_match_attrs) self.send_email_command = send_email.Command()
def setUp(self): self.ml_model = factories.MLModelFactory(name="test_estimator", is_principal=True, used_in_competitions=True) candy = CandyStore(seasons=TIP_SEASON_RANGE) fixtures = data_factories.fake_fixture_data(fixtures=candy.fixtures( to_dict=None)) predictions = data_factories.fake_prediction_data( match_data=candy.fixtures(to_dict=None)) match_results = data_factories.fake_match_results_data( match_results=candy.match_results(to_dict=None)) self.fixture_data = [ fixtures.query("year == @season") for season in range(*TIP_SEASON_RANGE) ] self.prediction_data = [ predictions.query("year == @season") for season in range(*TIP_SEASON_RANGE) ] self.match_results_data = [ match_results.query("year == @season") for season in range(*TIP_SEASON_RANGE) ] self.api = api
def test_update_score(self): match_result = data_factories.fake_match_results_data().iloc[0, :] team_match = TeamMatchFactory(team__name=match_result["home_team"], at_home=True, score=0) team_match.update_score(match_result) self.assertEqual(team_match.score, match_result["home_score"])
def setUp(self, mock_data_import): # pylint: disable=arguments-differ joblib.dump = Mock() min_seed_year = int(seed_db.YEAR_RANGE.split("-")[0]) # Min year needs to be greather than 2010, or weird stuff can happen # due to betting data only going back to 2010 self.assertGreater(min_seed_year, 2010) # We only need a couple of valid years to test functionality self.years = (int(min_seed_year), int(min_seed_year + 2)) # Need to add extra year at the beginning to provide at least one year's worth # of training data data_years = (self.years[0] - 1, self.years[1]) self.match_results_data_frame = fake_match_results_data( MATCH_COUNT_PER_YEAR, data_years, clean=True ) prediction_data = [] for match_result in self.match_results_data_frame.query( # Only returning prediction data for matches in seed_db year range "year >= @min_seed_year" ).to_dict("records"): match_data = { "home_team": match_result["home_team"], "away_team": match_result["away_team"], "year": match_result["year"], "round_number": match_result["round_number"], } prediction_data.append( fake_prediction_data( match_data=match_data, ml_model_name="test_estimator" ) ) mock_data_import.fetch_prediction_data = Mock( return_value=pd.concat(prediction_data).reset_index() ) mock_data_import.fetch_match_results_data = Mock( side_effect=self.__match_results_side_effect ) mock_data_import.fetch_ml_model_info = Mock( return_value=[ {"name": "test_estimator", "filepath": "some/filepath/model.pkl"} ] ) self.seed_command = seed_db.Command( data_importer=mock_data_import, fetch_data=False )
def test_update_results(self, mock_update_result): match_results = data_factories.fake_match_results_data() calls = [] for _idx, match_result in match_results.iterrows(): FullMatchFactory( home_team_match__score=0, away_team_match__score=0, 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"], venue=match_result["venue"], ) calls.append(call(match_result)) Match.update_results(match_results) self.assertEqual(mock_update_result.call_count, len(match_results))
def setUp(self): self.match_results_data = fake_match_results_data( seasons=(PREDICTION_YEAR, PREDICTION_YEAR + 1) ) ml_models = [ MLModel.objects.get(is_principal=True), MLModel.objects.filter( is_principal=False, used_in_competitions=True ).first(), ] for match_data in self.match_results_data.to_dict("records"): match_date = timezone.localtime(match_data["date"].to_pydatetime()) match_attrs = { "start_date_time": match_date, "round_number": match_data["round_number"], "venue": match_data["venue"], } prediction_attrs = { "prediction__ml_model": ml_models[0], "prediction__predicted_winner__name": np.random.choice( [match_data["home_team"], match_data["away_team"]] ), "prediction_two__ml_model": ml_models[1], "prediction_two__predicted_winner__name": np.random.choice( [match_data["home_team"], match_data["away_team"]] ), } team_match_attrs = { "home_team_match__team__name": match_data["home_team"], "away_team_match__team__name": match_data["away_team"], "home_team_match__score": match_data["home_score"], "away_team_match__score": match_data["away_score"], } FullMatchFactory( with_predictions=True, **match_attrs, **prediction_attrs, **team_match_attrs, ) self.send_email_command = send_email.Command()
def setUp(self): joblib.dump = MagicMock() min_seed_year = int(seed_db.YEAR_RANGE.split("-", maxsplit=1)[0]) # Min year needs to be greather than 2010, or weird stuff can happen # due to betting data only going back to 2010 self.assertGreater(min_seed_year, 2010) # We only need a couple of valid years to test functionality self.years = (int(min_seed_year), int(min_seed_year + 2)) # Need to add extra year at the beginning to provide at least one year's worth # of training data data_years = (self.years[0] - 1, self.years[1]) self.match_results_data_frame = fake_match_results_data( seasons=data_years) prediction_data = [] matches_to_predict = self.match_results_data_frame.query( # Only returning prediction data for matches in seed_db year range "year >= @min_seed_year") prediction_data = fake_prediction_data(match_data=matches_to_predict, ml_model_name="test_estimator") mock_data_import = MagicMock() mock_data_import.fetch_match_predictions = MagicMock( return_value=prediction_data.to_dict("records")) mock_data_import.fetch_matches = MagicMock( side_effect=self.__match_results_side_effect) mock_data_import.fetch_ml_models = MagicMock( return_value=[{ "name": "test_estimator", "filepath": "some/filepath/model.pkl" }]) self.seed_command = seed_db.Command(data_importer=mock_data_import, fetch_data=False, verbose=0)
def test_get_or_create_from_raw_data(self): self.assertEqual(TeamMatch.objects.count(), 0) team_matches = TeamMatch.get_or_create_from_raw_data( self.match, self.fixture_data[0]) self.assertEqual(TeamMatch.objects.count(), 2) for team_match in team_matches: self.assertIsInstance(team_match, TeamMatch) self.assertEqual(team_match.match, self.match) self.assertEqual(team_match.score, 0) with self.subTest("when associated team matches already exist"): existing_team_matches = TeamMatch.get_or_create_from_raw_data( self.match, self.fixture_data[0]) self.assertEqual(TeamMatch.objects.count(), 2) self.assertEqual(team_matches, existing_team_matches) with self.subTest("but teams in match data are different"): with self.assertRaisesRegex( AssertionError, r"Team names in the teammatch_set"): TeamMatch.get_or_create_from_raw_data( self.match, self.fixture_data[1]) with self.subTest("when the raw data has match results"): new_match = MatchFactory() match_data = data_factories.fake_match_results_data().to_dict( "records")[0] home_team, away_team = TeamMatch.get_or_create_from_raw_data( new_match, match_data) self.assertEqual(home_team.score, match_data["home_score"]) self.assertEqual(away_team.score, match_data["away_score"])
def test_update_result(self): with self.subTest("When the match hasn't been played yet"): match = FullMatchFactory( future=True, with_predictions=True, home_team_match__score=0, away_team_match__score=0, ) match.update_result(pd.DataFrame()) # It doesn't update match scores score_sum = sum(match.teammatch_set.values_list("score", flat=True)) self.assertEqual(score_sum, 0) # It doesn't update prediction correctness self.assertEqual( match.prediction_set.filter(is_correct__in=[True, False]).count(), 0, ) # It doesn't update match winner or margin self.assertIsNone(match.winner) self.assertIsNone(match.margin) with self.subTest("When the match doesn't have results yet"): with self.subTest("and has been played within the last week"): yesterday = timezone.now() - timedelta(days=1) match = FullMatchFactory( with_predictions=True, start_date_time=yesterday, home_team_match__score=0, away_team_match__score=0, prediction__is_correct=None, prediction_two__is_correct=None, ) match.winner = None match.margin = None match.update_result(pd.DataFrame()) # It doesn't update match scores score_sum = sum(match.teammatch_set.values_list("score", flat=True)) self.assertEqual(score_sum, 0) # It doesn't update prediction correctness self.assertEqual( match.prediction_set.filter(is_correct__in=[True, False]).count(), 0, ) # It doesn't update match winner or margin self.assertIsNone(match.winner) self.assertIsNone(match.margin) with self.subTest("and has been played over a week ago"): eight_days_ago = timezone.now() - timedelta(days=8) match = FullMatchFactory( with_predictions=True, start_date_time=eight_days_ago, home_team_match__score=0, away_team_match__score=0, prediction__is_correct=None, prediction_two__is_correct=None, ) with self.assertRaisesRegex( AssertionError, "Didn't find any match data rows" ): match.update_result(pd.DataFrame()) # It doesn't update match scores score_sum = sum(match.teammatch_set.values_list("score", flat=True)) self.assertEqual(score_sum, 0) # It doesn't update prediction correctness self.assertEqual( match.prediction_set.filter(is_correct__in=[True, False]).count(), 0, ) match_results = data_factories.fake_match_results_data() match_result = match_results.iloc[0, :] match = FullMatchFactory( with_predictions=True, home_team_match__score=0, away_team_match__score=0, 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"], ) winner_name = ( match_result["home_team"] if match_result["home_score"] > match_result["away_score"] else match_result["away_team"] ) winner = Team.objects.get(name=winner_name) match.prediction_set.update(predicted_winner=winner) # We expect a data frame, so can't reuse the match_result series match.update_result(match_results.iloc[:1, :]) # It updates match scores match_scores = set(match.teammatch_set.values_list("score", flat=True)) match_data_scores = set(match_result[["home_score", "away_score"]]) self.assertEqual(match_scores, match_data_scores) # It updates prediction correctness self.assertGreaterEqual(match.prediction_set.filter(is_correct=True).count(), 1) # It updates match winner and margin winner_is_correct = match.winner == winner self.assertTrue(winner_is_correct or match.is_draw) self.assertEqual(match.margin, max(match_scores) - min(match_scores))