major-domo-v2/tests/test_models_play.py
Cal Corum 0daa6f4491 fix: key plays score text shows "tied at X" instead of "Team up X-X" (closes #48)
The else branch in descriptive_text() caught both "away leading" and
"tied" cases, always overwriting the tied text. Changed to if/elif/else
so tied scores display correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 15:36:38 -06:00

130 lines
3.8 KiB
Python

"""Tests for Play model descriptive_text method.
Covers score text generation for key plays display, specifically
ensuring tied games show 'tied at X' instead of 'Team up X-X'.
"""
from models.play import Play
from models.player import Player
from models.team import Team
def _make_team(abbrev: str) -> Team:
"""Create a minimal Team for descriptive_text tests."""
return Team.model_construct(
id=1,
abbrev=abbrev,
sname=abbrev,
lname=f"Team {abbrev}",
season=13,
)
def _make_player(name: str, team: Team) -> Player:
"""Create a minimal Player for descriptive_text tests."""
return Player.model_construct(id=1, name=name, wara=0.0, season=13, team_id=team.id)
def _make_play(**overrides) -> Play:
"""Create a Play with sensible defaults for descriptive_text tests."""
tst_team = _make_team("TST")
opp_team = _make_team("OPP")
defaults = dict(
id=1,
game_id=1,
play_num=1,
on_base_code="000",
inning_half="top",
inning_num=7,
batting_order=1,
starting_outs=2,
away_score=0,
home_score=0,
outs=1,
batter_id=10,
batter=_make_player("Test Batter", tst_team),
batter_team=tst_team,
pitcher_id=20,
pitcher=_make_player("Test Pitcher", opp_team),
pitcher_team=opp_team,
)
defaults.update(overrides)
return Play.model_construct(**defaults)
class TestDescriptiveTextScoreText:
"""Tests for score text in Play.descriptive_text (issue #48)."""
def test_tied_score_shows_tied_at(self):
"""When scores are equal after the play, should show 'tied at X' not 'Team up X-X'."""
away = _make_team("BSG")
home = _make_team("DEN")
# Top 7: away scores 1 RBI, making it 2-2
play = _make_play(
inning_half="top",
inning_num=7,
away_score=1,
home_score=2,
rbi=1,
hit=1,
)
result = play.descriptive_text(away, home)
assert "tied at 2" in result
assert "up" not in result
def test_home_team_leading(self):
"""When home team leads, should show 'HOME up X-Y'."""
away = _make_team("BSG")
home = _make_team("DEN")
play = _make_play(
inning_half="top",
away_score=0,
home_score=3,
outs=1,
)
result = play.descriptive_text(away, home)
assert "DEN up 3-0" in result
def test_away_team_leading(self):
"""When away team leads, should show 'AWAY up X-Y'."""
away = _make_team("BSG")
home = _make_team("DEN")
play = _make_play(
inning_half="bot",
away_score=5,
home_score=2,
outs=1,
)
result = play.descriptive_text(away, home)
assert "BSG up 5-2" in result
def test_tied_at_zero(self):
"""Tied at 0-0 should show 'tied at 0'."""
away = _make_team("BSG")
home = _make_team("DEN")
play = _make_play(
inning_half="top",
away_score=0,
home_score=0,
outs=1,
)
result = play.descriptive_text(away, home)
assert "tied at 0" in result
def test_rbi_creates_tie_bottom_inning(self):
"""Bottom inning RBI that ties the game should show 'tied at X'."""
away = _make_team("BSG")
home = _make_team("DEN")
# Bot 5: home scores 2 RBI, tying at 4-4
play = _make_play(
inning_half="bot",
inning_num=5,
away_score=4,
home_score=2,
rbi=2,
hit=1,
)
result = play.descriptive_text(away, home)
assert "tied at 4" in result
assert "up" not in result