import pytest from sqlmodel import Session from command_logic.logic_gameplay import advance_runners, get_obc, get_re24, get_wpa, complete_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 # TODO: Test advance runners once "advance play" function is ready 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