Move test game creation to factory
This commit is contained in:
parent
27baebb7bf
commit
2f00bff5f9
44
tests/factory.py
Normal file
44
tests/factory.py
Normal file
@ -0,0 +1,44 @@
|
||||
import pytest
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
from sqlmodel.pool import StaticPool
|
||||
|
||||
from typing import Literal
|
||||
from in_game.gameplay_db import Game, Lineup
|
||||
|
||||
|
||||
@pytest.fixture(name='session')
|
||||
def session_fixture():
|
||||
engine = create_engine(
|
||||
'sqlite://', connect_args={'check_same_thread': False}, poolclass=StaticPool
|
||||
)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
RAW_GAMES = [
|
||||
Game(away_team_id=1, home_team_id=2, channel_id=1234, season=9),
|
||||
Game(away_team_id=3, home_team_id=4, channel_id=5678, season=9, active=False, is_pd=False, ranked=True, week_num=6, game_num=9, away_roster_id=69, home_roster_id=420, first_message=12345678, ai_team='home', game_type='minor-league')
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(name='new_games')
|
||||
def new_games_fixture():
|
||||
return RAW_GAMES
|
||||
|
||||
|
||||
@pytest.fixture(name='new_games_with_lineups')
|
||||
def new_games_with_lineups_fixture():
|
||||
game_1 = RAW_GAMES[0]
|
||||
game_2 = RAW_GAMES[1]
|
||||
|
||||
all_lineups = []
|
||||
for team_id in [1, 2]:
|
||||
for (order, pos) in [(1, 'C'), (2, '1B'), (3, '2B'), (4, '3B'), (5, 'SS'), (6, 'LF'), (7, 'CF'), (8, 'RF'), (9, 'DH'), (10, 'P')]:
|
||||
all_lineups.append(Lineup(team_id=team_id, card_id=order, player_id=68+order, position=pos, batting_order=order, game=game_1))
|
||||
|
||||
for team_id in [3, 4]:
|
||||
for (order, pos) in [(1, 'C'), (2, '1B'), (3, '2B'), (4, '3B'), (5, 'SS'), (6, 'LF'), (7, 'CF'), (8, 'RF'), (9, 'DH'), (10, 'P')]:
|
||||
all_lineups.append(Lineup(team_id=team_id, card_id=order, player_id=100+order, position=pos, batting_order=order, game=game_2))
|
||||
|
||||
return [game_1, game_2]
|
||||
@ -1,26 +1,7 @@
|
||||
import pytest
|
||||
from sqlmodel import Session, SQLModel, create_engine
|
||||
from sqlmodel.pool import StaticPool
|
||||
from sqlmodel import Session
|
||||
|
||||
from in_game.gameplay_db import Game
|
||||
|
||||
|
||||
@pytest.fixture(name='session')
|
||||
def session_fixture():
|
||||
engine = create_engine(
|
||||
'sqlite://', connect_args={'check_same_thread': False}, poolclass=StaticPool
|
||||
)
|
||||
SQLModel.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture(name='new_games')
|
||||
def new_games_fixture():
|
||||
return [
|
||||
Game(away_team_id=1, home_team_id=2, channel_id=1234, season=9),
|
||||
Game(away_team_id=3, home_team_id=4, channel_id=5678, season=9, active=False, is_pd=False, ranked=True, week_num=6, game_num=9, away_roster_id=69, home_roster_id=420, first_message=12345678, ai_team='home', game_type='minor-league')
|
||||
]
|
||||
from factory import session_fixture, new_games_fixture
|
||||
|
||||
|
||||
def test_create_game(session: Session, new_games: list[Game]):
|
||||
@ -54,3 +35,5 @@ def test_create_game(session: Session, new_games: list[Game]):
|
||||
assert game_2.first_message == 12345678
|
||||
assert game_2.ai_team == 'home'
|
||||
assert game_2.game_type == 'minor-league'
|
||||
|
||||
|
||||
|
||||
36
tests/test_gameplay_db_lineup.py
Normal file
36
tests/test_gameplay_db_lineup.py
Normal file
@ -0,0 +1,36 @@
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from in_game.gameplay_db import Game, Lineup
|
||||
from factory import session_fixture, new_games_with_lineups_fixture
|
||||
|
||||
|
||||
def test_create_lineup(session: Session, new_games_with_lineups: list[Game]):
|
||||
game_1 = new_games_with_lineups[0]
|
||||
game_2 = new_games_with_lineups[1]
|
||||
|
||||
assert len(game_1.lineups) == 20
|
||||
assert len(game_2.lineups) == 20
|
||||
|
||||
session.add(game_1)
|
||||
session.add(game_2)
|
||||
session.commit()
|
||||
|
||||
query_ss = session.exec(select(Lineup).where(Lineup.game == game_1, Lineup.position == 'SS', Lineup.active == True)).all()
|
||||
lineup_id_7 = session.get(Lineup, 7)
|
||||
lineup_id_21 = session.get(Lineup, 21)
|
||||
|
||||
assert len(query_ss) == 2
|
||||
assert lineup_id_7.position == 'CF'
|
||||
assert lineup_id_21.position == 'C'
|
||||
|
||||
|
||||
# def test_lineup_substitution(session: Session, new_games_with_lineups: list[Game]):
|
||||
# game_1 = new_games_with_lineups[0]
|
||||
# game_2 = new_games_with_lineups[1]
|
||||
|
||||
# session.add(game_1)
|
||||
# session.add(game_2)
|
||||
# session.commit()
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user