def test_retrieve_team_does_not_exist(session):
    season2006 = Season.get_or_create(SeasonOfYear.SPRING, 2006, session)
    season2007 = Season.get_or_create(SeasonOfYear.SPRING, 2007, session)
    username = fake.user_name()
    team = Team.create(name=username, season=season2006, session=session)

    with pytest.raises(NoResultFound):
        Team.get_by_name(name=username, season=season2007, session=session)
def test_create_and_retrieve_anime_by_name_and_alias(session):
    season = Season.get_or_create(SeasonOfYear.SPRING, 2006, session)
    anime = Anime.create(1234, "The Melancholy of Haruhi Suzumiya", season,
                         session)

    assert anime == Anime.get_by_name("The Melancholy of Haruhi Suzumiya",
                                      session)

    anime.add_alias("Suzumiya Haruhi no Yuutsu")

    assert anime == Anime.get_by_name("Suzumiya Haruhi no Yuutsu", session)
def test_bench_swap(
    # patches
    season_config_mock,
    # fixtures
    session,
    config_functor,
):
    config_function = config_functor(
        sections=["weekly info", "season info"],
        kv={"current-week": 0, "min-weeks-between-bench-swaps": 3},
    )
    season_config_mock.getint.side_effect = config_function

    season = Season.get_or_create(SeasonOfYear.SPRING, 2006, session)
    haruhi = Anime.create(1234, "The Melancholy of Haruhi Suzumiya", season, session)
    bebop = Anime.create(4321, "Cowboy Bebop", season, session)
    team = Team.create(name=fake.user_name(), season=season, session=session)
    team.add_anime_to_team(haruhi)
    team.add_anime_to_team(bebop, True)

    with pytest.raises(AssertionError):
        # this should fail since bench and active are swapped
        team.bench_swap(active_anime=bebop, bench_anime=haruhi, week=0)

    team.bench_swap(active_anime=haruhi, bench_anime=bebop, week=0)

    week_snapshot = team.get_anime(week=0)
    assert week_snapshot == WeekSnapshotOfTeamAnime(
        week=0, active=[bebop], bench=[haruhi]
    )

    with pytest.raises(AssertionError):
        # this should fail since we just swapped this week
        team.bench_swap(active_anime=bebop, bench_anime=haruhi, week=0)

    for week in range(1, 5):
        season.init_new_week(week)

    config_function = config_functor(
        sections=["weekly info", "season info"],
        kv={"current-week": 4, "min-weeks-between-bench-swaps": 3},
    )
    season_config_mock.getint.side_effect = config_function

    team.bench_swap(active_anime=bebop, bench_anime=haruhi, week=4)
    week_snapshot = team.get_anime(week=4)
    assert week_snapshot == WeekSnapshotOfTeamAnime(
        week=4, active=[haruhi], bench=[bebop]
    )
def test_init_new_team_weekly_anime(
    # factories
    orm_season_factory,
    orm_team_factory,
    team_weekly_anime_factory,
    # fixtures
    session,
):
    season = orm_season_factory()
    teams = orm_team_factory.create_batch(10, season=season)
    for team in teams:
        for week in range(1, 4, 1):
            team_weekly_anime_factory.create_batch(5, team=team, week=week)

    start_new_week = StartNewWeek(current_week=4)
    start_new_week._execute(session=session,
                            season=Season.from_orm_season(season, session))

    for team in teams:
        anime_in_week_4 = (session.query(TeamWeeklyAnime).filter(
            TeamWeeklyAnime.week == 4,
            TeamWeeklyAnime.team_id == team.id).all())
        assert len(anime_in_week_4) == 5
Ejemplo n.º 5
0
 def execute(self) -> None:
     with session_scope() as session:
         season = Season.get_or_create(
             season_of_year=self.season_of_year, year=self.year, session=session,
         )
         self._execute(session, season)
def test_create_anime_twice_throws_exception(session):
    season = Season.get_or_create(SeasonOfYear.SPRING, 2006, session)
    Anime.create(1234, "The Melancholy of Haruhi Suzumiya", season, session)
    with pytest.raises(IntegrityError):
        Anime.create(1234, "Suzumiya Haruhi no Yuutsu", season, session)
def test_retrieve_anime_does_not_exist(session):
    season = Season.get_or_create(SeasonOfYear.SPRING, 2006, session)
    Anime.create(1234, "The Melancholy of Haruhi Suzumiya", season, session)

    with pytest.raises(NoResultFound):
        Anime.get_by_name("Suzumiya Haruhi no Yuutsu", session)
Ejemplo n.º 8
0
    def _execute(self, session: Session, season: Season) -> None:
        anime_simulcast_region_counts = self.get_anime_simulcast_region_counts(
            session)
        licensed_anime = self.get_licensed_anime(session)

        anime_list = season.get_all_anime()

        anime_ids_collected = [
            row[0] for row in session.query(orm.AnimeWeeklyStat.anime_id).join(
                orm.Anime).filter(
                    orm.AnimeWeeklyStat.week == self.current_week).filter(
                        orm.Anime.season_id == season._entity.id).all()
        ]

        if anime_ids_collected:
            action = input(
                "At least some anime stats have been collected for this week"
                " already. How should we proceed (overwrite/collect-missing/abort)?"
            )
            if action == "collect-missing":
                anime_list = (anime for anime in anime_list
                              if anime.mal_id not in anime_ids_collected)
            elif action == "overwrite":
                pass
            else:
                return

        # for each anime, get the number of teams that have it on active
        anime_active_counts = dict(
            session.query(orm.Anime.id, func.count("*")).join(
                orm.TeamWeeklyAnime.anime).filter(
                    orm.TeamWeeklyAnime.week == self.current_week).filter(
                        orm.Anime.season_id == season._entity.id).filter(
                            orm.TeamWeeklyAnime.bench == 0).group_by(
                                orm.Anime.id).all())
        double_score_max_num_teams = math.floor(
            self.percent_ownership_for_double_score / 100 *
            len(list(season.get_all_teams())))

        # casting until update in sqlalchemy-stubs
        for anime in cast(List[Anime], anime_list):
            print(f"Populating stats for {anime}")
            try:
                stat_data = self.get_anime_stats_from_jikan(anime)
            except jikanpy.exceptions.APIException as e:
                print(
                    f"Jikan servers did not handle our request very well, skipping: {e}"
                )
                continue

            if (self.is_week_to_calculate("scoring.simulcast")
                    and anime.mal_id not in anime_simulcast_region_counts):
                print(f"{anime} doesn't have an entry in simulcast file")

            stat_data.total_points = self.calculate_anime_weekly_points(
                anime,
                stat_data,
                anime_active_counts.get(anime.mal_id, 0),
                double_score_max_num_teams,
                anime_simulcast_region_counts.get(anime.mal_id, 0),
                anime.mal_id in licensed_anime,
            )

            anime_weekly_stat = orm.AnimeWeeklyStat()
            for key, value in attr.asdict(stat_data).items():
                setattr(anime_weekly_stat, key, value)

            session.merge(anime_weekly_stat)
            time.sleep(self.jikan_request_interval)
 def _execute(self, session: Session, season: Season) -> None:
     season.init_new_week(self.current_week)
def test_create_team_twice_throws_exception(session):
    season = Season.get_or_create(SeasonOfYear.SPRING, 2006, session)
    username = fake.user_name()
    Team.create(name=username, season=season, session=session)
    with pytest.raises(AssertionError):
        Team.create(name=username, season=season, session=session)
def test_create_and_retrieve_team_by_name(session):
    season = Season.get_or_create(SeasonOfYear.SPRING, 2006, session)
    username = fake.user_name()
    team = Team.create(name=username, season=season, session=session)

    assert team == Team.get_by_name(name=username, season=season, session=session)