Added /log single, all but uncapped complete Added check_uncapped_advance, ai on defense complete
166 lines
5.1 KiB
Python
166 lines
5.1 KiB
Python
import pytest
|
|
from sqlmodel import Session, select, func
|
|
|
|
from command_logic.logic_gameplay import advance_runners, get_obc, get_re24, get_wpa, complete_play, log_run_scored
|
|
from in_game.gameplay_models import Lineup, Play
|
|
from tests.factory import session_fixture, Game
|
|
|
|
|
|
def test_advance_runners(session: Session):
|
|
this_game = session.get(Game, 3)
|
|
play_1 = this_game.initialize_play(session)
|
|
|
|
assert play_1.starting_outs == 0
|
|
assert play_1.on_first_id is None
|
|
assert play_1.on_second_id is None
|
|
assert play_1.on_third_id is None
|
|
|
|
play_1.pa, play_1.ab, play_1.hit, play_1.double, play_1.batter_final = 1, 1, 1, 1, 2
|
|
|
|
play_2 = complete_play(session, play_1)
|
|
assert play_2.inning_half == play_1.inning_half
|
|
assert play_2.on_second_id == play_1.batter_id
|
|
|
|
play_2.pa, play_2.ab, play_2.hit, play_2.batter_final = 1, 1, 1, 1
|
|
advance_runners(session, play_2, 1)
|
|
session.add(play_2)
|
|
session.commit()
|
|
|
|
assert play_2.on_second_final == 3
|
|
assert play_2.batter_final == 1
|
|
|
|
play_3 = complete_play(session, play_2)
|
|
|
|
assert play_3.on_third is not None
|
|
assert play_3.on_second is None
|
|
assert play_3.on_first is not None
|
|
|
|
def test_get_obc():
|
|
assert get_obc() == 0
|
|
assert get_obc(on_first=True) == 1
|
|
assert get_obc(on_second=True) == 2
|
|
assert get_obc(on_third=True) == 3
|
|
assert get_obc(on_first=True, on_second=True) == 4
|
|
assert get_obc(on_first=True, on_third=True) == 5
|
|
assert get_obc(on_second=True, on_third=True) == 6
|
|
assert get_obc(on_first=True, on_second=True, on_third=True) == 7
|
|
|
|
|
|
def test_get_re24(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
this_play = game_1.current_play_or_none(session)
|
|
|
|
assert this_play is not None
|
|
assert get_re24(this_play, runs_scored=0, new_obc=0, new_starting_outs=2) == -.154
|
|
assert get_re24(this_play, runs_scored=1, new_obc=0, new_starting_outs=1) == 1
|
|
assert get_re24(this_play, runs_scored=0, new_obc=2, new_starting_outs=1) == 0.365
|
|
assert get_re24(this_play, runs_scored=0, new_obc=6, new_starting_outs=2) == 0.217
|
|
|
|
|
|
def test_get_wpa(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
next_play = game_1.current_play_or_none(session) # Starting wpa: 0.564
|
|
this_play = game_1.plays[0] # Starting wpa: 0.500
|
|
|
|
assert this_play != next_play
|
|
assert get_wpa(this_play, next_play) == -0.064
|
|
|
|
next_play.starting_outs = 2
|
|
next_play.away_score = 3 # Starting wpa: 0.347
|
|
|
|
assert get_wpa(this_play, next_play) == 0.153
|
|
|
|
|
|
def test_complete_play(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
this_play = game_1.current_play_or_none(session)
|
|
|
|
assert this_play.inning_half == 'top'
|
|
assert this_play.inning_num == 1
|
|
assert this_play.starting_outs == 1
|
|
|
|
this_play.pa, this_play.ab, this_play.so, this_play.outs = 1, 1, 1, 1
|
|
next_play = complete_play(session, this_play)
|
|
|
|
assert next_play.inning_half == 'top'
|
|
assert this_play.inning_num == 1
|
|
assert next_play.starting_outs == 2
|
|
assert next_play.is_tied
|
|
assert next_play.managerai == this_play.managerai
|
|
assert this_play.re24 == -0.154
|
|
|
|
next_play.pa, next_play.ab, next_play.double, next_play.batter_final = 1, 1, 1, 2
|
|
third_play = complete_play(session, next_play)
|
|
|
|
assert third_play.on_base_code == 2
|
|
assert next_play.re24 == 0.182
|
|
assert third_play.on_second == next_play.batter
|
|
|
|
third_play.pa, third_play.ab, third_play.hit
|
|
|
|
|
|
def test_complete_play_scratch(session: Session):
|
|
this_game = session.get(Game, 3)
|
|
|
|
assert len(this_game.plays) == 0
|
|
|
|
play_1 = this_game.initialize_play(session)
|
|
|
|
assert play_1.starting_outs == 0
|
|
assert play_1.on_first_id is None
|
|
assert play_1.on_second_id is None
|
|
assert play_1.on_third_id is None
|
|
|
|
play_1.hit = 1
|
|
|
|
assert play_1.hit == 1
|
|
|
|
play_1.pa, play_1.ab, play_1.hit, play_1.double, play_1.batter_final = 1, 1, 1, 1, 2
|
|
print(f'play_1: {play_1}')
|
|
|
|
play_2 = complete_play(session, play_1)
|
|
|
|
assert play_2.on_second is not None
|
|
|
|
|
|
def test_log_run_scored(session: Session):
|
|
game_1 = session.get(Game, 1)
|
|
lineup_1 = session.get(Lineup, 1)
|
|
play_1 = session.get(Play, 1)
|
|
|
|
assert play_1.run == 0
|
|
|
|
log_run_scored(session, lineup_1, play_1)
|
|
|
|
assert play_1.run == 1
|
|
|
|
play_2 = session.get(Play, 2)
|
|
play_2.pa, play_2.ab, play_2.double, play_2.batter_final = 1, 1, 1, 2
|
|
play_3 = complete_play(session, play_2)
|
|
|
|
assert play_3.on_second is not None
|
|
assert play_3.on_second.game == game_1
|
|
|
|
play_3.pa, play_3.ab, play_3.so, play_3.outs = 1, 1, 1, 1
|
|
play_3 = advance_runners(session, play_3, num_bases=0)
|
|
|
|
assert play_3.on_second_final == 2
|
|
|
|
play_4 = complete_play(session, play_3)
|
|
|
|
assert play_4.starting_outs == 2
|
|
assert play_4.on_second is not None
|
|
|
|
play_4.pa, play_4.ab, play_4.error = 1, 1, 1
|
|
log_run_scored(session, play_4.on_second, play_4)
|
|
|
|
runs = session.exec(select(func.sum(Play.run)).where(Play.game == game_1)).one()
|
|
e_runs = session.exec(select(func.sum(Play.e_run)).where(Play.game == game_1)).one()
|
|
|
|
assert runs == 2
|
|
assert e_runs == 1
|
|
|
|
|
|
|
|
|