Skip to content

Commit

Permalink
Automated seasons (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackYps authored Sep 25, 2022
1 parent f253b9e commit df0f14b
Show file tree
Hide file tree
Showing 11 changed files with 699 additions and 399 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ aio_pika = "*"
aiocron = "*"
prometheus_client = "*"
yoyo-migrations = "*"
pamqp = "==2.3.0"

[dev-packages]
pytest = "*"
Expand All @@ -27,6 +28,7 @@ python-coveralls = "*"
mock = "*"
pytest-mock = "*"
vulture = "*"
freezegun = "*"

[requires]
python_version = "3.7"
811 changes: 459 additions & 352 deletions Pipfile.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from service import config, db
from service.league_service import LeagueService
from service.message_queue_service import MessageQueueService
from service.season_generator import SeasonGenerator


async def main():
Expand Down Expand Up @@ -41,6 +42,9 @@ def signal_handler(sig: int, _frame):
league_service = LeagueService(database, mq_service)
await league_service.initialize()

season_generator = SeasonGenerator(database)
season_generator.initialize()

await done

# Cleanup
Expand Down
2 changes: 1 addition & 1 deletion service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__author__ = "Askaholic, Clemens Borys"
__author__ = "Askaholic, BlackYps, Clemens Borys"
__contact__ = "[email protected]"
__license__ = "GPLv3"
__copyright__ = "Copyright (c) 2020 " + __author__
1 change: 1 addition & 0 deletions service/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
MQ_VHOST = os.getenv("MQ_VHOST", "/faf-lobby")
MQ_PREFETCH_COUNT = int(os.getenv("MQ_PREFETCH_COUNT", 300))

SEASON_GENERATION_DAYS_BEFORE_SEASON_END = int(os.getenv("SEASON_GENERATION_DAYS_BEFORE_SEASON_END", 14))

EXCHANGE_NAME = os.getenv("EXCHANGE_NAME", "faf-rabbitmq")
QUEUE_NAME = os.getenv("QUEUE_NAME", "faf-league-service")
Expand Down
3 changes: 3 additions & 0 deletions service/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Column("league_id", Integer, ForeignKey("league.id")),
Column("leaderboard_id", Integer, ForeignKey("leaderboard.id")),
Column("name_key", String),
Column("season_number", Integer),
Column("placement_games", Integer),
Column("placement_games_returning_player", Integer),
Column("start_date", TIMESTAMP),
Expand All @@ -42,6 +43,8 @@
Column("id", Integer, primary_key=True),
Column("league_season_id", Integer, ForeignKey("league_season.id")),
Column("division_index", Integer),
Column("description_key", String),
Column("name_key", String),
)

league_season_division_subdivision = Table(
Expand Down
113 changes: 113 additions & 0 deletions service/generate_season.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
SET time_zone='+00:00';

SELECT @Seasonnumber := MAX(season_number) FROM league_season;
SET @Seasonnumber = @Seasonnumber + 1;

SET @start_date = ADDDATE(LAST_DAY(CURRENT_DATE), 1);
SET @end_date = LAST_DAY(ADD_MONTHS(CURRENT_DATE, 3));

-- season starts and ends at noon, so that all timezones see the same date in the client
INSERT INTO league_season (id, league_id, leaderboard_id, placement_games, season_number, name_key, start_date, end_date) VALUES
(@Seasonnumber * 3, 1, 2, 10, @Seasonnumber, "1v1_season", TIMESTAMP(@start_date, "12:00:00"), TIMESTAMP(@end_date, "12:00:00")),
(@Seasonnumber * 3 + 1, 2, 3, 10, @Seasonnumber, "2v2_season", TIMESTAMP(@start_date, "12:00:00"), TIMESTAMP(@end_date, "12:00:00")),
(@Seasonnumber * 3 + 2, 3, 4, 10, @Seasonnumber, "4v4_fs_season", TIMESTAMP(@start_date, "12:00:00"), TIMESTAMP(@end_date, "12:00:00"));

INSERT INTO league_season_division (id, league_season_id, division_index, description_key, name_key) VALUES
(@Seasonnumber * 18 - 6 + 1, @Seasonnumber * 3 , 1, CONCAT("1v1_season_", @Seasonnumber, ".division.1"), "bronze"),
(@Seasonnumber * 18 - 6 + 2, @Seasonnumber * 3 , 2, CONCAT("1v1_season_", @Seasonnumber, ".division.2"), "silver"),
(@Seasonnumber * 18 - 6 + 3, @Seasonnumber * 3 , 3, CONCAT("1v1_season_", @Seasonnumber, ".division.3"), "gold"),
(@Seasonnumber * 18 - 6 + 4, @Seasonnumber * 3 , 4, CONCAT("1v1_season_", @Seasonnumber, ".division.4"), "diamond"),
(@Seasonnumber * 18 - 6 + 5, @Seasonnumber * 3 , 5, CONCAT("1v1_season_", @Seasonnumber, ".division.5"), "master"),
(@Seasonnumber * 18 - 6 + 6, @Seasonnumber * 3 , 6, CONCAT("1v1_season_", @Seasonnumber, ".division.6"), "grandmaster"),
(@Seasonnumber * 18 - 6 + 7, @Seasonnumber * 3 + 1, 1, CONCAT("2v2_season_", @Seasonnumber, ".division.1"), "bronze"),
(@Seasonnumber * 18 - 6 + 8, @Seasonnumber * 3 + 1, 2, CONCAT("2v2_season_", @Seasonnumber, ".division.2"), "silver"),
(@Seasonnumber * 18 - 6 + 9, @Seasonnumber * 3 + 1, 3, CONCAT("2v2_season_", @Seasonnumber, ".division.3"), "gold"),
(@Seasonnumber * 18 - 6 + 10, @Seasonnumber * 3 + 1, 4, CONCAT("2v2_season_", @Seasonnumber, ".division.4"), "diamond"),
(@Seasonnumber * 18 - 6 + 11, @Seasonnumber * 3 + 1, 5, CONCAT("2v2_season_", @Seasonnumber, ".division.5"), "master"),
(@Seasonnumber * 18 - 6 + 12, @Seasonnumber * 3 + 1, 6, CONCAT("2v2_season_", @Seasonnumber, ".division.6"), "grandmaster"),
(@Seasonnumber * 18 - 6 + 13, @Seasonnumber * 3 + 2, 1, CONCAT("4v4_fs_season_", @Seasonnumber, ".division.1"), "bronze"),
(@Seasonnumber * 18 - 6 + 14, @Seasonnumber * 3 + 2, 2, CONCAT("4v4_fs_season_", @Seasonnumber, ".division.2"), "silver"),
(@Seasonnumber * 18 - 6 + 15, @Seasonnumber * 3 + 2, 3, CONCAT("4v4_fs_season_", @Seasonnumber, ".division.3"), "gold"),
(@Seasonnumber * 18 - 6 + 16, @Seasonnumber * 3 + 2, 4, CONCAT("4v4_fs_season_", @Seasonnumber, ".division.4"), "diamond"),
(@Seasonnumber * 18 - 6 + 17, @Seasonnumber * 3 + 2, 5, CONCAT("4v4_fs_season_", @Seasonnumber, ".division.5"), "master"),
(@Seasonnumber * 18 - 6 + 18, @Seasonnumber * 3 + 2, 6, CONCAT("4v4_fs_season_", @Seasonnumber, ".division.6"), "grandmaster");

INSERT INTO league_season_division_subdivision (league_season_division_id, subdivision_index, description_key, min_rating, max_rating, highest_score, name_key) VALUES
(@Seasonnumber * 18 - 6 + 1, 1, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.1.1"), -800, -40, 10, "V"),
(@Seasonnumber * 18 - 6 + 1, 2, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.1.2"), -40, 45, 10, "IV"),
(@Seasonnumber * 18 - 6 + 1, 3, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.1.3"), 45, 130, 10, "III"),
(@Seasonnumber * 18 - 6 + 1, 4, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.1.4"), 130, 215, 10, "II"),
(@Seasonnumber * 18 - 6 + 1, 5, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.1.5"), 215, 300, 10, "I"),
(@Seasonnumber * 18 - 6 + 2, 1, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.2.1"), 300, 385, 10, "V"),
(@Seasonnumber * 18 - 6 + 2, 2, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.2.2"), 385, 470, 10, "IV"),
(@Seasonnumber * 18 - 6 + 2, 3, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.2.3"), 470, 555, 10, "III"),
(@Seasonnumber * 18 - 6 + 2, 4, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.2.4"), 555, 640, 10, "II"),
(@Seasonnumber * 18 - 6 + 2, 5, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.2.5"), 640, 725, 10, "I"),
(@Seasonnumber * 18 - 6 + 3, 1, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.3.1"), 725, 810, 10, "V"),
(@Seasonnumber * 18 - 6 + 3, 2, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.3.2"), 810, 895, 10, "IV"),
(@Seasonnumber * 18 - 6 + 3, 3, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.3.3"), 895, 980, 10, "III"),
(@Seasonnumber * 18 - 6 + 3, 4, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.3.4"), 980, 1065, 10, "II"),
(@Seasonnumber * 18 - 6 + 3, 5, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.3.5"), 1065, 1150, 10, "I"),
(@Seasonnumber * 18 - 6 + 4, 1, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.4.1"), 1150, 1235, 10, "V"),
(@Seasonnumber * 18 - 6 + 4, 2, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.4.2"), 1235, 1320, 10, "IV"),
(@Seasonnumber * 18 - 6 + 4, 3, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.4.3"), 1320, 1405, 10, "III"),
(@Seasonnumber * 18 - 6 + 4, 4, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.4.4"), 1405, 1490, 10, "II"),
(@Seasonnumber * 18 - 6 + 4, 5, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.4.5"), 1490, 1575, 10, "I"),
(@Seasonnumber * 18 - 6 + 5, 1, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.5.1"), 1575, 1660, 10, "V"),
(@Seasonnumber * 18 - 6 + 5, 2, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.5.2"), 1660, 1745, 10, "IV"),
(@Seasonnumber * 18 - 6 + 5, 3, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.5.3"), 1745, 1830, 10, "III"),
(@Seasonnumber * 18 - 6 + 5, 4, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.5.4"), 1830, 1915, 10, "II"),
(@Seasonnumber * 18 - 6 + 5, 5, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.5.5"), 1915, 2000, 10, "I"),
(@Seasonnumber * 18 - 6 + 6, 1, CONCAT("1v1_season_", @Seasonnumber, ".subdivision.6.1"), 2000, 2850, 100, ""),
(@Seasonnumber * 18 - 6 + 7, 1, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.1.1"), -800, -40, 10, "V"),
(@Seasonnumber * 18 - 6 + 7, 2, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.1.2"), -40, 45, 10, "IV"),
(@Seasonnumber * 18 - 6 + 7, 3, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.1.3"), 45, 130, 10, "III"),
(@Seasonnumber * 18 - 6 + 7, 4, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.1.4"), 130, 215, 10, "II"),
(@Seasonnumber * 18 - 6 + 7, 5, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.1.5"), 215, 300, 10, "I"),
(@Seasonnumber * 18 - 6 + 8, 1, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.2.1"), 300, 385, 10, "V"),
(@Seasonnumber * 18 - 6 + 8, 2, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.2.2"), 385, 470, 10, "IV"),
(@Seasonnumber * 18 - 6 + 8, 3, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.2.3"), 470, 555, 10, "III"),
(@Seasonnumber * 18 - 6 + 8, 4, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.2.4"), 555, 640, 10, "II"),
(@Seasonnumber * 18 - 6 + 8, 5, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.2.5"), 640, 725, 10, "I"),
(@Seasonnumber * 18 - 6 + 9, 1, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.3.1"), 725, 810, 10, "V"),
(@Seasonnumber * 18 - 6 + 9, 2, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.3.2"), 810, 895, 10, "IV"),
(@Seasonnumber * 18 - 6 + 9, 3, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.3.3"), 895, 980, 10, "III"),
(@Seasonnumber * 18 - 6 + 9, 4, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.3.4"), 980, 1065, 10, "II"),
(@Seasonnumber * 18 - 6 + 9, 5, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.3.5"), 1065, 1150, 10, "I"),
(@Seasonnumber * 18 - 6 + 10, 1, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.4.1"), 1150, 1235, 10, "V"),
(@Seasonnumber * 18 - 6 + 10, 2, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.4.2"), 1235, 1320, 10, "IV"),
(@Seasonnumber * 18 - 6 + 10, 3, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.4.3"), 1320, 1405, 10, "III"),
(@Seasonnumber * 18 - 6 + 10, 4, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.4.4"), 1405, 1490, 10, "II"),
(@Seasonnumber * 18 - 6 + 10, 5, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.4.5"), 1490, 1575, 10, "I"),
(@Seasonnumber * 18 - 6 + 11, 1, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.5.1"), 1575, 1660, 10, "V"),
(@Seasonnumber * 18 - 6 + 11, 2, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.5.2"), 1660, 1745, 10, "IV"),
(@Seasonnumber * 18 - 6 + 11, 3, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.5.3"), 1745, 1830, 10, "III"),
(@Seasonnumber * 18 - 6 + 11, 4, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.5.4"), 1830, 1915, 10, "II"),
(@Seasonnumber * 18 - 6 + 11, 5, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.5.5"), 1915, 2000, 10, "I"),
(@Seasonnumber * 18 - 6 + 12, 1, CONCAT("2v2_season_", @Seasonnumber, ".subdivision.6.1"), 2000, 2850, 100, ""),
(@Seasonnumber * 18 - 6 + 13, 1, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.1.1"), -800, -40, 10, "V"),
(@Seasonnumber * 18 - 6 + 13, 2, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.1.2"), -40, 45, 10, "IV"),
(@Seasonnumber * 18 - 6 + 13, 3, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.1.3"), 45, 130, 10, "III"),
(@Seasonnumber * 18 - 6 + 13, 4, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.1.4"), 130, 215, 10, "II"),
(@Seasonnumber * 18 - 6 + 13, 5, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.1.5"), 215, 300, 10, "I"),
(@Seasonnumber * 18 - 6 + 14, 1, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.2.1"), 300, 385, 10, "V"),
(@Seasonnumber * 18 - 6 + 14, 2, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.2.2"), 385, 470, 10, "IV"),
(@Seasonnumber * 18 - 6 + 14, 3, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.2.3"), 470, 555, 10, "III"),
(@Seasonnumber * 18 - 6 + 14, 4, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.2.4"), 555, 640, 10, "II"),
(@Seasonnumber * 18 - 6 + 14, 5, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.2.5"), 640, 725, 10, "I"),
(@Seasonnumber * 18 - 6 + 15, 1, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.3.1"), 725, 810, 10, "V"),
(@Seasonnumber * 18 - 6 + 15, 2, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.3.2"), 810, 895, 10, "IV"),
(@Seasonnumber * 18 - 6 + 15, 3, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.3.3"), 895, 980, 10, "III"),
(@Seasonnumber * 18 - 6 + 15, 4, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.3.4"), 980, 1065, 10, "II"),
(@Seasonnumber * 18 - 6 + 15, 5, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.3.5"), 1065, 1150, 10, "I"),
(@Seasonnumber * 18 - 6 + 16, 1, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.4.1"), 1150, 1235, 10, "V"),
(@Seasonnumber * 18 - 6 + 16, 2, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.4.2"), 1235, 1320, 10, "IV"),
(@Seasonnumber * 18 - 6 + 16, 3, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.4.3"), 1320, 1405, 10, "III"),
(@Seasonnumber * 18 - 6 + 16, 4, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.4.4"), 1405, 1490, 10, "II"),
(@Seasonnumber * 18 - 6 + 16, 5, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.4.5"), 1490, 1575, 10, "I"),
(@Seasonnumber * 18 - 6 + 17, 1, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.5.1"), 1575, 1660, 10, "V"),
(@Seasonnumber * 18 - 6 + 17, 2, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.5.2"), 1660, 1745, 10, "IV"),
(@Seasonnumber * 18 - 6 + 17, 3, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.5.3"), 1745, 1830, 10, "III"),
(@Seasonnumber * 18 - 6 + 17, 4, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.5.4"), 1830, 1915, 10, "II"),
(@Seasonnumber * 18 - 6 + 17, 5, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.5.5"), 1915, 2000, 10, "I"),
(@Seasonnumber * 18 - 6 + 18, 1, CONCAT("4v4_fs_season_", @Seasonnumber, ".subdivision.6.1"), 2000, 2850, 100, "");
45 changes: 45 additions & 0 deletions service/season_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from datetime import datetime, timedelta

import aiocron
from sqlalchemy import select, text

from service.config import SEASON_GENERATION_DAYS_BEFORE_SEASON_END
from service.db import FAFDatabase
from service.db.models import league_season
from service.decorators import with_logger


@with_logger
class SeasonGenerator:
def __init__(self, database: FAFDatabase):
self._logger.info("Season generator created.")
self._db = database

def initialize(self):
self._update_cron = aiocron.crontab(
"0 0 * * *", func=self.check_season_end
)

async def check_season_end(self):
self._logger.debug("Checking if latest season ends soon.")
async with self._db.acquire() as conn:
sql = select([league_season])
result = await conn.execute(sql)
rows = await result.fetchall()

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

if max_date < datetime.now() + timedelta(days=SEASON_GENERATION_DAYS_BEFORE_SEASON_END):
try:
await self.generate_season()
except Exception:
self._logger.exception("Failed to generate new season")
else:
self._logger.info("Season successfully created!")

async def generate_season(self):
self._logger.info("Generating new season...")
async with self._db.acquire() as conn:
with open("service/generate_season.sql") as file:
query = text(file.read())
await conn.execute(query)
12 changes: 7 additions & 5 deletions tests/data/test-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ SET FOREIGN_KEY_CHECKS=1;

insert into leaderboard (id, technical_name) values
(1, "global"),
(2, "ladder_1v1");
(2, "ladder_1v1"),
(3, "tmm_2v2"),
(4, "tmm_4v4");

INSERT INTO league (id, technical_name, enabled, image_url, medium_image_url, small_image_url, name_key, description_key) VALUES
(1, "test_league", TRUE, "https://faf.com/", "https://faf.com/medium/", "https://faf.com/small/", "L1", "description_key"),
(2, "second_test_league", TRUE, "https://faf.com/", "https://faf.com/medium/", "https://faf.com/small/", "L2", "description_key"),
(3, "league_without_seasons", TRUE, "https://faf.com/", "https://faf.com/medium/", "https://faf.com/small/", "L3", "description_key");

INSERT INTO league_season (id, league_id, leaderboard_id, placement_games, placement_games_returning_player, season_number, name_key, start_date, end_date) VALUES
(1, 1, 1, 10, 3, 1, "season.1", NOW() - interval 2 year, NOW() - interval 1 year),
(2, 1, 1, 10, 3, 2, "season.2", NOW() - interval 1 year, NOW() + interval 1 year),
(3, 2, 2, 10, 3, 1, "season.3", NOW() - interval 2 year, NOW() + interval 1 year),
(4, 1, 1, 10, 3, 3, "season.4", NOW() + interval 1 year, NOW() + interval 2 year);
(1, 1, 1, 10, 3, 1, "season.1", NOW() - interval 2 week, NOW() - interval 1 week),
(2, 1, 1, 10, 3, 2, "season.2", NOW() - interval 1 week, NOW() + interval 1 week),
(3, 2, 2, 10, 3, 1, "season.3", NOW() - interval 2 week, NOW() + interval 1 week),
(4, 1, 1, 10, 3, 3, "season.4", NOW() + interval 1 week, NOW() + interval 2 week);

INSERT INTO league_season_division (id, league_season_id, division_index, name_key, description_key) VALUES
(1, 1, 1, "L1D1", "description_key"),
Expand Down
41 changes: 0 additions & 41 deletions tests/unit_tests/test_league_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import asyncio
from contextlib import asynccontextmanager

import pytest
from asynctest import CoroutineMock
from sqlalchemy import select
Expand All @@ -10,48 +7,10 @@
from service.league_service.league_service import ServiceNotReadyError
from service.league_service.typedefs import (InvalidScoreError, League,
LeagueScore)
from tests.utils import MockDatabase

pytestmark = pytest.mark.asyncio


@pytest.fixture
def database_context():
@asynccontextmanager
async def make_database(request):
def opt(val):
return request.config.getoption(val)

host, user, pw, name, port = (
opt("--mysql_host"),
opt("--mysql_username"),
opt("--mysql_password"),
opt("--mysql_database"),
opt("--mysql_port")
)
db = MockDatabase(asyncio.get_running_loop())

await db.connect(
host=host,
user=user,
password=pw or None,
port=port,
db=name
)

yield db

await db.close()

return make_database


@pytest.fixture
async def database(request, database_context):
async with database_context(request) as db:
yield db


@pytest.fixture
async def league_service(database, message_queue_service):
service = LeagueService(database, message_queue_service)
Expand Down
Loading

0 comments on commit df0f14b

Please sign in to comment.