252 lines
7.5 KiB
Python
252 lines
7.5 KiB
Python
import pytest
|
|
from sqlalchemy import delete as sadelete
|
|
from sqlalchemy.sql.functions import sum, count
|
|
from sqlmodel import Session, delete, func
|
|
|
|
from command_logic.logic_gameplay import complete_play, get_scorebug_embed, homeruns, is_game_over, singles, strikeouts, undo_play
|
|
from in_game.gameplay_models import Game, Lineup, GameCardsetLink, Play, Team, select
|
|
from in_game.gameplay_queries import get_channel_game_or_none, get_active_games_by_team, get_db_ready_decisions
|
|
from tests.factory import session_fixture
|
|
|
|
|
|
def test_create_game(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
game_2 = session.get(Game, 2)
|
|
|
|
assert game_1.away_team_id == 31
|
|
assert game_1.home_team_id == 400
|
|
assert game_1.channel_id == 1234
|
|
assert game_1.season == 9
|
|
assert game_1.active == True
|
|
assert game_1.is_pd == True
|
|
assert game_1.ranked == False
|
|
assert game_1.week == None
|
|
assert game_1.game_num == None
|
|
assert game_1.away_roster_id == None
|
|
assert game_1.home_roster_id == None
|
|
assert game_1.first_message == None
|
|
assert game_1.ai_team == 'away'
|
|
assert game_1.game_type == 'minor-league'
|
|
assert game_2.active == False
|
|
assert game_2.is_pd == True
|
|
assert game_2.ranked == True
|
|
assert game_2.week == 6
|
|
assert game_2.game_num == 9
|
|
assert game_2.away_roster_id == 69
|
|
assert game_2.home_roster_id == 420
|
|
assert game_2.first_message == '12345678'
|
|
assert game_2.ai_team == 'home'
|
|
assert game_2.game_type == 'minor-league'
|
|
|
|
|
|
def test_select_all(session: Session):
|
|
games = session.exec(select(Game)).all()
|
|
assert len(games) == 3
|
|
|
|
session.execute(sadelete(Game))
|
|
session.commit()
|
|
|
|
games = session.exec(select(Game)).all()
|
|
assert len(games) == 0
|
|
|
|
|
|
def test_games_by_channel(session: Session):
|
|
assert get_channel_game_or_none(session, 1234) is not None
|
|
assert get_channel_game_or_none(session, 5678) is not None
|
|
assert get_channel_game_or_none(session, 69) is None
|
|
|
|
game_2 = session.get(Game, 2)
|
|
game_2.active = True
|
|
session.add(game_2)
|
|
session.commit()
|
|
|
|
with pytest.raises(Exception) as exc_info:
|
|
get_channel_game_or_none(session, 5678)
|
|
|
|
assert str(exc_info) == "<ExceptionInfo Exception('Too many games found in get_channel_game_or_none') tblen=2>"
|
|
|
|
game_3 = session.get(Game, 3)
|
|
game_2.active = False
|
|
game_3.active = False
|
|
session.add(game_2)
|
|
session.add(game_3)
|
|
session.commit()
|
|
|
|
assert get_channel_game_or_none(session, 5678) is None
|
|
|
|
|
|
def test_games_by_team(session: Session):
|
|
team_69 = session.get(Team, 69)
|
|
assert len(get_active_games_by_team(session, team=team_69)) == 1
|
|
|
|
game_2 = session.get(Game, 2)
|
|
game_2.active = True
|
|
session.add(game_2)
|
|
session.commit()
|
|
|
|
assert len(get_active_games_by_team(session, team=team_69)) == 2
|
|
|
|
game_3 = session.get(Game, 3)
|
|
game_3.active = False
|
|
game_2.active = False
|
|
session.add(game_3)
|
|
session.add(game_2)
|
|
session.commit()
|
|
|
|
assert len(get_active_games_by_team(session, team=team_69)) == 0
|
|
|
|
|
|
def test_delete_game(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
# session.execute(sadelete(GameCardsetLink).where(GameCardsetLink.game == game_1))
|
|
# session.delete(game_1)
|
|
session.delete(game_1)
|
|
|
|
bad_lineups = session.exec(select(Lineup).where(Lineup.game_id == 1)).all()
|
|
bad_links = session.exec(select(GameCardsetLink).where(GameCardsetLink.game_id == 1)).all()
|
|
|
|
assert len(bad_lineups) == 0
|
|
assert len(bad_links) == 0
|
|
|
|
|
|
# async def test_get_scorebug(session: Session):
|
|
# game_1 = session.get(Game, 1)
|
|
# # scorebug = game_1.get_scorebug_embed(session)
|
|
# scorebug = await get_scorebug_embed(session, game_1)
|
|
|
|
# assert scorebug.title == 'CornBelters @ Black Bears - Minor League'
|
|
# assert scorebug.color.value == int('a6ce39', 16)
|
|
|
|
# game_3 = session.get(Game, 3)
|
|
# # scorebug = game_3.get_scorebug_embed(session)
|
|
# scorebug = await get_scorebug_embed(session, game_3)
|
|
|
|
# assert '0 Outs' in scorebug.fields[0].value
|
|
|
|
|
|
def test_sum_function(session: Session):
|
|
"""
|
|
Testing out the sqlalchemy functions
|
|
"""
|
|
st = select(
|
|
sum(Game.away_team_id).label('nice'),
|
|
count(Game.active)
|
|
)
|
|
print(f'sql: {st}')
|
|
result = session.exec(st)
|
|
print(f'result: {result}')
|
|
one_result = result.one()
|
|
print(f'one: {one_result}')
|
|
print(f'sum: {one_result[0]} / count: {one_result[1]}')
|
|
|
|
assert False == False
|
|
|
|
|
|
def test_current_play_or_none(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
this_play = game_1.initialize_play(session)
|
|
|
|
assert this_play.play_num == 2
|
|
|
|
this_play.complete = True
|
|
session.add(this_play)
|
|
session.commit()
|
|
|
|
|
|
def test_initialize_play(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
game_3 = session.get(Game, 3)
|
|
g1_play = game_1.initialize_play(session)
|
|
|
|
assert game_1.id == 1
|
|
|
|
play_count = session.exec(select(Play.id)).all()
|
|
|
|
assert len(play_count) == 2
|
|
|
|
g3_play = game_3.initialize_play(session)
|
|
play_count = session.exec(select(Play.id)).all()
|
|
|
|
assert g3_play.play_num == 1
|
|
assert g3_play.starting_outs == 0
|
|
assert len(play_count) == 3
|
|
|
|
|
|
async def test_undo_play(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
this_play = game_1.initialize_play(session)
|
|
|
|
assert this_play.play_num == 2
|
|
|
|
all_play_ids = session.exec(select(Play.id, Play.play_num).where(Play.game == game_1)).all()
|
|
|
|
assert len(all_play_ids) == 2
|
|
assert all_play_ids[0][0] == 1
|
|
assert all_play_ids[1][1] == 2
|
|
|
|
await singles(session, None, this_play, '*')
|
|
play_3 = complete_play(session, this_play)
|
|
|
|
await singles(session, None, play_3, '*')
|
|
play_4 = complete_play(session, play_3)
|
|
on_second_play_4 = play_4.on_second
|
|
|
|
await singles(session, None, play_4, '*')
|
|
play_5 = complete_play(session, play_4)
|
|
|
|
assert len(play_5.game.plays) == 5
|
|
assert play_5.on_base_code == 7
|
|
|
|
new_play = undo_play(session, this_play)
|
|
|
|
assert len(new_play.game.plays) == 4
|
|
assert new_play.play_num == 4
|
|
assert new_play.on_second == on_second_play_4
|
|
|
|
|
|
def test_game_model_dump(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
game_data = game_1.model_dump()
|
|
|
|
assert game_data['active'] == True
|
|
assert game_data['away_team_id'] == 31
|
|
assert game_data['home_team_id'] == 400
|
|
assert game_data['game_type'] == 'minor-league'
|
|
|
|
|
|
def test_final_inning(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
final_inning = session.exec(select(func.max(Play.inning_num)).where(Play.game == game_1)).one()
|
|
|
|
assert final_inning == 1
|
|
|
|
|
|
async def test_db_ready_decisions(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
this_play = game_1.initialize_play(session)
|
|
|
|
assert this_play.play_num == 2
|
|
|
|
this_play = await strikeouts(session, None, this_play)
|
|
this_play = complete_play(session, this_play)
|
|
|
|
this_play = await strikeouts(session, None, this_play)
|
|
this_play = complete_play(session, this_play)
|
|
|
|
assert this_play.starting_outs == 0
|
|
assert this_play.inning_half == 'bot'
|
|
|
|
this_play = await homeruns(session, None, this_play, 'no-doubt')
|
|
this_play = complete_play(session, this_play)
|
|
|
|
decisions = get_db_ready_decisions(session, game_1, 69)
|
|
|
|
assert len(decisions) == 2
|
|
assert decisions[0]['pitcher_team_id'] == 400
|
|
assert decisions[1]['pitcher_team_id'] == 31
|
|
assert decisions[0]['game_finished'] == 1
|
|
assert decisions[0]['is_start'] == True
|
|
assert decisions[1]['game_finished'] == 1
|
|
assert decisions[1]['is_start'] == True
|
|
|