Skip to content

Commit

Permalink
change how row columns are accessed
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackYps committed Jun 4, 2024
1 parent 4e58a15 commit 4e94706
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 64 deletions.
28 changes: 14 additions & 14 deletions service/league_service/league_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def update_data(self):
# (division_index, subdivision_index) indices.
divisions_by_league = defaultdict(list)
for row in division_rows:
divisions_by_league[row[league.c.technical_name]].append(row)
divisions_by_league[row.technical_name].append(row)

self._leagues_by_rating_type = defaultdict(list)
for league_name, division_list in divisions_by_league.items():
Expand All @@ -85,24 +85,24 @@ async def update_data(self):
placement_games_returning_player = division_list[0][league_season.c.placement_games_returning_player]
division_list.sort(
key=lambda row: (
row[league_season_division.c.division_index],
row[league_season_division_subdivision.c.subdivision_index],
row[league_season_division.c.id],
row.division_index,
row.subdivision_index,
row.id,
)
)
self._leagues_by_rating_type[rating_type].append(
League(
league_name,
[
LeagueDivision(
row[league_season_division_subdivision.c.id],
row[league_season_division_subdivision.c.min_rating],
row[league_season_division_subdivision.c.max_rating],
row[league_season_division_subdivision.c.highest_score],
row.id,
row.min_rating,
row.max_rating,
row.highest_score,
)
for row in division_list
],
division_list[0][league_season.c.id],
division_list[0].id,
placement_games,
placement_games_returning_player,
rating_type,
Expand Down Expand Up @@ -158,10 +158,10 @@ async def _load_score(self, player_id: PlayerID, league: League) -> LeagueScore:
return LeagueScore(None, None, 0, returning_player)

return LeagueScore(
row[league_season_score.c.subdivision_id],
row[league_season_score.c.score],
row[league_season_score.c.game_count],
row[league_season_score.c.returning_player],
row.subdivision_id,
row.score,
row.game_count,
row.returning_player,
)

async def is_returning_player(self, player_id: PlayerID, rating_type: str) -> bool:
Expand All @@ -180,7 +180,7 @@ async def is_returning_player(self, player_id: PlayerID, rating_type: str) -> bo
)
result = await conn.execute(sql)
row = result.fetchone()
if row is None or row[league_season_score.c.subdivision_id] is None:
if row is None or row.subdivision_id is None:
return False
else:
return True
Expand Down
42 changes: 21 additions & 21 deletions service/season_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def check_season_end(self):
result = await conn.execute(sql)
rows = result.fetchall()

max_date = max(row[league_season.c.end_date] for row in rows)
max_date = max(row.end_date for row in rows)

if max_date < datetime.now() + timedelta(days=SEASON_GENERATION_DAYS_BEFORE_SEASON_END):
try:
Expand Down Expand Up @@ -58,7 +58,7 @@ async def generate_season(self):
async def update_db(self, conn, league_row, start_date, end_date):
season_sql = (
select(league_season)
.where(league_season.c.league_id == league_row[league.c.id])
.where(league_season.c.league_id == league_row.id)
.order_by(desc(league_season.c.season_number))
.limit(1)
)
Expand All @@ -67,21 +67,21 @@ async def update_db(self, conn, league_row, start_date, end_date):
if season_row is None:
self._logger.warning(
"No season found for league %s. Skipping this league",
league_row[league.c.technical_name]
league_row.technical_name
)
return
result = await conn.execute(select(func.max(league_season.c.id)))
season_id = result.scalar() + 1
season_number = season_row[league_season.c.season_number] + 1
season_number = season_row.season_number + 1
season_insert_sql = (
insert(league_season)
.values(
id=season_id,
league_id=season_row[league_season.c.league_id],
leaderboard_id=season_row[league_season.c.leaderboard_id],
placement_games=season_row[league_season.c.placement_games],
league_id=season_row.league_id,
leaderboard_id=season_row.leaderboard_id,
placement_games=season_row.placement_games,
season_number=season_number,
name_key=season_row[league_season.c.name_key],
name_key=season_row.name_key,
start_date=start_date,
end_date=end_date,
)
Expand All @@ -90,22 +90,22 @@ async def update_db(self, conn, league_row, start_date, end_date):

division_sql = (
select(league_season_division)
.where(league_season_division.c.league_season_id == season_row[league_season.c.id])
.where(league_season_division.c.league_season_id == season_row.id)
)
result = await conn.execute(division_sql)
season_division_rows = result.fetchall()
if not season_division_rows:
self._logger.warning(
"No divisions found for season id %s. No divisions could be created. "
"Now season id %s has no divisions as well. This needs to be fixed manually",
season_row[league_season.c.id],
season_row.id,
season_id
)
return
result = await conn.execute(select(func.max(league_season_division.c.id)))
division_id = result.scalar()
for division_row in season_division_rows:
division_index = division_row[league_season_division.c.division_index]
division_index = division_row.division_index
division_id += 1
division_insert_sql = (
insert(league_season_division)
Expand All @@ -114,43 +114,43 @@ async def update_db(self, conn, league_row, start_date, end_date):
league_season_id=season_id,
division_index=division_index,
description_key=(
f"{season_row[league_season.c.name_key]}_{season_number}.division.{division_index}"
f"{season_row.name_key}_{season_number}.division.{division_index}"
),
name_key=division_row[league_season_division.c.name_key],
name_key=division_row.name_key,
)
)
await conn.execute(division_insert_sql)

subdivision_sql = (
select(league_season_division_subdivision)
.where(league_season_division_subdivision.c.league_season_division_id ==
division_row[league_season_division.c.id])
division_row.id)
)
result = await conn.execute(subdivision_sql)
subdivision_rows = result.fetchall()
if not subdivision_rows:
self._logger.warning(
"No subdivisions found for division id %s. No subdivisions could be created. "
"Now division id %s has no subdivisions as well. This needs to be fixed manually",
division_row[league_season_division.c.id],
division_row.id,
division_id
)
return
for subdivision_row in subdivision_rows:
subdivision_index = subdivision_row[league_season_division_subdivision.c.subdivision_index]
subdivision_index = subdivision_row.subdivision_index
subdivision_insert_sql = (
insert(league_season_division_subdivision)
.values(
league_season_division_id=division_id,
subdivision_index=subdivision_index,
description_key=(
f"{season_row[league_season.c.name_key]}_{season_number}"
f"{season_row.name_key}_{season_number}"
f".subdivision.{division_index}.{subdivision_index}"
),
name_key=subdivision_row[league_season_division_subdivision.c.name_key],
min_rating=subdivision_row[league_season_division_subdivision.c.min_rating],
max_rating=subdivision_row[league_season_division_subdivision.c.max_rating],
highest_score=subdivision_row[league_season_division_subdivision.c.highest_score],
name_key=subdivision_row.name_key,
min_rating=subdivision_row.min_rating,
max_rating=subdivision_row.max_rating,
highest_score=subdivision_row.highest_score,
)
)
await conn.execute(subdivision_insert_sql)
32 changes: 16 additions & 16 deletions tests/unit_tests/test_league_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ async def test_persist_score_new_player(league_service, database):
rows = result.fetchall()
assert len(rows) == 1
for row in rows:
assert row["game_id"] == 1
assert row["login_id"] == 5
assert row["league_season_id"] == 2
assert row["subdivision_id_before"] == 3
assert row["subdivision_id_after"] == 3
assert row["score_before"] == 6
assert row["score_after"] == 5
assert row["game_count"] == 43
assert row.game_id == 1
assert row.login_id == 5
assert row.league_season_id == 2
assert row.subdivision_id_before == 3
assert row.subdivision_id_after == 3
assert row.score_before == 6
assert row.score_after == 5
assert row.game_count == 43


async def test_persist_score_old_player(league_service, database):
Expand All @@ -219,14 +219,14 @@ async def test_persist_score_old_player(league_service, database):
rows = result.fetchall()
assert len(rows) == 1
for row in rows:
assert row["game_id"] == 10
assert row["login_id"] == 1
assert row["league_season_id"] == 2
assert row["subdivision_id_before"] == 3
assert row["subdivision_id_after"] == 3
assert row["score_before"] == 6
assert row["score_after"] == 5
assert row["game_count"] == 43
assert row.game_id == 10
assert row.login_id == 1
assert row.league_season_id == 2
assert row.subdivision_id_before == 3
assert row.subdivision_id_after == 3
assert row.score_before == 6
assert row.score_after == 5
assert row.game_count == 43


async def test_persist_score_season_id_mismatch(league_service):
Expand Down
25 changes: 12 additions & 13 deletions tests/unit_tests/test_season_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ async def test_generate_season(season_generator, database):
seasons = await conn.execute(select(league_season))
rows = await seasons.fetchall()
assert len(rows) == 6
assert max(row[league_season.c.season_number] for row in rows) == 4
assert max(row.season_number for row in rows) == 4

divisions = await conn.execute(select(league_season_division))
rows = await divisions.fetchall()
assert len(rows) == 9
new_division_one = await conn.execute(select(league_season_division).where(league_season_division.c.id == 8))
row = await new_division_one.fetchone()
assert row[league_season_division.c.league_season_id] == 6
assert row[league_season_division.c.division_index] == 1
assert row[league_season_division.c.name_key] == "L3D1"
assert row[league_season_division.c.description_key] == "second_test_league.season.1_2.division.1"
assert row.league_season_id == 6
assert row.division_index == 1
assert row.name_key == "L3D1"
assert row.description_key == "second_test_league.season.1_2.division.1"

subdivisions = await conn.execute(select(league_season_division_subdivision))
rows = await subdivisions.fetchall()
Expand All @@ -64,13 +64,12 @@ async def test_generate_season(season_generator, database):
.where(league_season_division_subdivision.c.league_season_division_id == 8)
)
row = await new_subdivision.fetchone()
assert row[league_season_division_subdivision.c.subdivision_index] == 1
assert row[league_season_division_subdivision.c.name_key] == "L3D1S1"
assert (row[league_season_division_subdivision.c.description_key] ==
"second_test_league.season.1_2.subdivision.1.1")
assert row[league_season_division_subdivision.c.min_rating] == 0
assert row[league_season_division_subdivision.c.max_rating] == 3000
assert row[league_season_division_subdivision.c.highest_score] == 20
assert row.subdivision_index == 1
assert row.name_key == "L3D1S1"
assert row.description_key == "second_test_league.season.1_2.subdivision.1.1"
assert row.min_rating == 0
assert row.max_rating == 3000
assert row.highest_score == 20


async def test_generate_season_only_once(season_generator, database):
Expand All @@ -80,4 +79,4 @@ async def test_generate_season_only_once(season_generator, database):
seasons = await conn.execute(select(league_season))
rows = await seasons.fetchall()
assert len(rows) == 6
assert max(row[league_season.c.season_number] for row in rows) == 4
assert max(row.season_number for row in rows) == 4

0 comments on commit 4e94706

Please sign in to comment.