Run Black formatter across 83 files and fix 1514 ruff violations: - E722: bare except → typed exceptions (17 fixes) - E711/E712/E721: comparison style fixes with noqa for SQLAlchemy (44 fixes) - F841: unused variable assignments (70 fixes) - F541/F401: f-string and import cleanup (1383 auto-fixes) Remaining 925 errors are all F403/F405 (star imports) — structural, requires converting to explicit imports in a separate effort. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
179 lines
4.8 KiB
Python
179 lines
4.8 KiB
Python
import pytest
|
|
from sqlmodel import Session, select, func
|
|
|
|
from command_logic.logic_gameplay import (
|
|
complete_play,
|
|
homeruns,
|
|
is_game_over,
|
|
singles,
|
|
strikeouts,
|
|
undo_play,
|
|
walks,
|
|
)
|
|
from in_game.gameplay_models import Play, Game
|
|
from in_game.gameplay_queries import get_db_ready_plays, get_last_team_play
|
|
from tests.factory import create_sample_play_for_scorebug
|
|
|
|
|
|
def test_create_play(session: Session):
|
|
all_plays = session.exec(select(Play)).all()
|
|
|
|
assert len(all_plays) == 2
|
|
|
|
play_1 = session.get(Play, 1)
|
|
|
|
assert play_1.game_id == 1
|
|
assert play_1.pa == 1
|
|
assert play_1.pitcher_id == 20
|
|
|
|
|
|
def test_get_current_play(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
curr_play = game_1.current_play_or_none(session)
|
|
|
|
assert curr_play.play_num == 2
|
|
|
|
|
|
def test_scorebug_ascii(session: Session):
|
|
new_play = create_sample_play_for_scorebug(session)
|
|
session.add(new_play)
|
|
session.commit()
|
|
|
|
assert (
|
|
new_play.scorebug_ascii
|
|
== "```\nNCB3 0 ○ ▲ 6\n WV4 0 ○ ○ 1 Out\n```"
|
|
)
|
|
|
|
|
|
def test_last_team_play(session: Session):
|
|
this_game = session.get(Game, 1)
|
|
this_team = this_game.away_team
|
|
|
|
last_play = get_last_team_play(session, this_game, this_team)
|
|
assert last_play is not None
|
|
assert last_play.play_num == 2
|
|
|
|
this_team = this_game.home_team
|
|
|
|
assert get_last_team_play(session, this_game, this_team, none_okay=True) is None
|
|
|
|
|
|
def test_query_scalars(session: Session):
|
|
this_game = session.get(Game, 1)
|
|
all_plays = session.exec(
|
|
select(Play.id, Play.error, Play.outs).where(Play.game == this_game)
|
|
).all()
|
|
|
|
assert len(all_plays) == 2
|
|
assert all_plays[0].error == 0
|
|
|
|
with pytest.raises(AttributeError) as exc_info:
|
|
all_plays[0].batter_id is None
|
|
|
|
assert str(exc_info) == "<ExceptionInfo AttributeError('batter_id') tblen=4>"
|
|
|
|
count_plays = session.exec(
|
|
select(func.count(Play.id)).where(Play.game == this_game)
|
|
).one()
|
|
|
|
assert count_plays == 2
|
|
|
|
outs = session.exec(select(func.sum(Play.outs)).where(Play.game == this_game)).one()
|
|
|
|
assert outs == 1
|
|
|
|
|
|
async def test_undo_play(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
play_2 = game_1.initialize_play(session)
|
|
|
|
assert play_2.play_num == 2
|
|
|
|
play_2 = await singles(session, None, play_2, single_type="*")
|
|
play_3 = complete_play(session, play_2)
|
|
|
|
assert play_3.play_num == 3
|
|
assert play_3.on_first == play_2.batter
|
|
|
|
play_3 = await singles(session, None, play_3, single_type="**")
|
|
play_4 = complete_play(session, play_3)
|
|
all_plays = session.exec(select(Play).where(Play.game == game_1)).all()
|
|
|
|
assert play_4.play_num == 4
|
|
assert play_4.on_first == play_3.batter
|
|
assert len(all_plays) == 4
|
|
|
|
undone_play = undo_play(session, play_4)
|
|
|
|
assert undone_play.play_num == 3
|
|
|
|
all_plays = session.exec(select(Play).where(Play.game == game_1)).all()
|
|
|
|
assert len(all_plays) == 3
|
|
|
|
|
|
def test_db_ready_plays(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
game_1.initialize_play(session)
|
|
|
|
db_ready_plays = get_db_ready_plays(session, game_1, 69)
|
|
plays = db_ready_plays["plays"]
|
|
|
|
assert len(plays) == 2
|
|
assert plays[0]["game_id"] == 69
|
|
assert plays[0]["on_first_id"] is None
|
|
assert plays[0]["catcher_id"] == 11
|
|
print(f'obc from return: {plays[0]["on_base_code"]}')
|
|
assert plays[0]["on_base_code"] == "000"
|
|
|
|
|
|
async def test_is_game_over(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
this_play = game_1.initialize_play(session)
|
|
|
|
assert is_game_over(this_play) is False
|
|
|
|
assert this_play.play_num == 2
|
|
assert this_play.starting_outs == 1
|
|
|
|
this_play = await strikeouts(session, None, this_play)
|
|
next_play = complete_play(session, this_play)
|
|
|
|
assert this_play.wpa == -0.015
|
|
|
|
this_play = await strikeouts(session, None, next_play)
|
|
this_play = complete_play(session, this_play)
|
|
|
|
assert this_play.starting_outs == 0
|
|
assert this_play.inning_half == "bot"
|
|
|
|
this_play.inning_num = 9
|
|
session.add(this_play)
|
|
session.commit()
|
|
session.refresh(this_play)
|
|
|
|
this_play = await strikeouts(session, None, this_play)
|
|
this_play = complete_play(session, this_play)
|
|
|
|
this_play = await homeruns(session, None, this_play, "no-doubt")
|
|
next_play = complete_play(session, this_play)
|
|
|
|
assert is_game_over(next_play) is True
|
|
assert this_play.wpa == 0.402
|
|
|
|
|
|
async def test_walks(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
play_2 = game_1.initialize_play(session)
|
|
|
|
this_play = await walks(session, None, play_2)
|
|
this_play = complete_play(session, this_play)
|
|
|
|
assert this_play.on_base_code == 1
|
|
|
|
|
|
def test_get_one(session: Session):
|
|
play_1 = session.exec(select(Play).where(Play.id == 1)).one()
|
|
|
|
assert play_1.play_num == 1
|