예제 #1
0
def test_from_future_fixtures(fauna_session):
    fixture_matches = data_factories.fake_fixture_data()
    next_match = fixture_matches.iloc[
        np.random.randint(0, len(fixture_matches)), :]
    patched_date = next_match["date"].to_pydatetime() - timedelta(days=1)
    upcoming_round_number = next_match["round_number"]
    upcoming_fixture_matches = fixture_matches.query(
        "round_number == @upcoming_round_number")

    with freeze_time(patched_date):
        assert fauna_session.execute(select(func.count(
            Match.id))).scalar() == 0

        matches = Match.from_future_fixtures(fauna_session,
                                             upcoming_fixture_matches,
                                             upcoming_round_number)
        created_match_count = len(matches)

        assert created_match_count == len(
            upcoming_fixture_matches.query("date > @patched_date"))

        for match in matches:
            fauna_session.add(match)

        fauna_session.commit()

        # With existing matches in DB
        matches = Match.from_future_fixtures(fauna_session,
                                             upcoming_fixture_matches,
                                             upcoming_round_number)

        assert len(matches) == 0
        total_match_count = fauna_session.execute(select(func.count(
            Match.id))).scalar()
        assert total_match_count == created_match_count
예제 #2
0
def test_from_fixture(fauna_session):
    fixture_matches = data_factories.fake_fixture_data()
    next_match = fixture_matches.iloc[
        np.random.randint(0, len(fixture_matches)), :]

    team_matches = TeamMatch.from_fixture(fauna_session, next_match)

    for team_match in team_matches:
        if team_match.at_home:
            assert team_match.team.name == next_match["home_team"]
        else:
            assert team_match.team.name == next_match["away_team"]
예제 #3
0
    def test_update_fixture_data(
        self, MockMatch, MockSession, MockDataImporter, mock_data_export
    ):
        with freeze_time(datetime(2020, 5, 1, tzinfo=pytz.UTC)):
            right_now = datetime.now(tz=pytz.UTC)
            this_year = right_now.year
            fixture = data_factories.fake_fixture_data(
                seasons=(this_year, this_year + 1)
            )
            upcoming_round = int(
                fixture.query("date > @right_now")["round_number"].min()
            )

            mock_data_import = MagicMock()
            mock_data_import.fetch_fixture_data = MagicMock(return_value=fixture)
            MockDataImporter.return_value = mock_data_import

            mock_data_export.update_fixture_data = MagicMock()

            mock_db_session = MagicMock()
            mock_db_session.add = MagicMock()
            mock_db_session.commit = MagicMock()
            MockSession.return_value = mock_db_session

            fixture_to_update = fixture.query(
                "round_number == @upcoming_round & date > @right_now"
            )
            mock_matches = [
                Match(
                    start_date_time=match["date"],
                    venue=match["venue"],
                    round_number=match["round_number"],
                )
                for _, match in fixture_to_update.iterrows()
            ]
            MockMatch.from_future_fixtures = MagicMock(return_value=mock_matches)

            self.api.update_fixture_data()

            # It posts data to main app
            mock_data_export.update_fixture_data.assert_called()
            call_args = mock_data_export.update_fixture_data.call_args[0]

            data_are_equal = (call_args[0] == fixture_to_update).all().all()
            self.assertTrue(data_are_equal)
            self.assertEqual(call_args[1], upcoming_round)

            # It saves data to DB
            MockMatch.from_future_fixtures.assert_called()
            self.assertEqual(mock_db_session.add.call_count, len(mock_matches))
            mock_db_session.commit.assert_called()
예제 #4
0
def test_update_fixture_data(status_code, expected_error):
    responses.add(
        responses.POST,
        f"{settings.TIPRESIAS_APP}/fixtures",
        status=status_code,
        json="Stuff happened",
    )

    fake_fixture = data_factories.fake_fixture_data()
    upcoming_round = np.random.randint(1, 24)

    if expected_error is None:
        data_export.update_fixture_data(fake_fixture, upcoming_round)
    else:
        with pytest.raises(expected_error, match=str(status_code)):
            data_export.update_fixture_data(fake_fixture, upcoming_round)
예제 #5
0
    def setUp(self):
        candy = CandyStore(seasons=MATCH_SEASON_RANGE)
        fixtures = data_factories.fake_fixture_data(
            fixtures=candy.fixtures(to_dict=None)
        )
        predictions = data_factories.fake_prediction_data(
            fixtures=candy.fixtures(to_dict=None)
        )

        self.fixture_return_values = [
            fixtures.query("year == @season") for season in MOCK_IMPORT_SEASONS
        ]
        self.prediction_return_values = [
            predictions.query("year == @season") for season in MOCK_IMPORT_SEASONS
        ]

        self.mock_submitter = FootyTipsSubmitter()
        self.mock_submitter.submit_tips = MagicMock()

        self.api = api
예제 #6
0
def test_from_future_fixtures_with_skipped_round(fauna_session):
    fixture_matches = data_factories.fake_fixture_data()
    first_match, next_match = _get_matches_from_different_days(fixture_matches)

    patched_date = next_match["date"].to_pydatetime() - timedelta(days=1)
    upcoming_round_number = int(next_match["round_number"]) + 2

    fauna_session.add(
        Match(
            start_date_time=first_match["date"].to_pydatetime(),
            round_number=first_match["round_number"],
            venue=first_match["venue"],
        ))
    fauna_session.commit()

    with freeze_time(patched_date):
        with pytest.raises(
                AssertionError,
                match=
                "Expected upcoming round number to be 1 greater than previous round",
        ):
            Match.from_future_fixtures(fauna_session, fixture_matches,
                                       upcoming_round_number)