paper-dynasty-gameplay-webapp/tests/unit/services/test_ui_service.py
Cal Corum 1c24161e76 CLAUDE: Achieve 100% test pass rate with comprehensive AI service testing
- Fix TypeError in check_steal_opportunity by properly mocking catcher defense
- Correct tag_from_third test calculation to account for all adjustment conditions
- Fix pitcher replacement test by setting appropriate allowed runners threshold
- Add comprehensive test coverage for AI service business logic
- Implement VS Code testing panel configuration with pytest integration
- Create pytest.ini for consistent test execution and warning management
- Add test isolation guidelines and factory pattern implementation
- Establish 102 passing tests with zero failures

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-28 17:55:34 -05:00

170 lines
6.1 KiB
Python

"""
Unit tests for UIService.
Tests business logic extracted from models, particularly team display formatting.
"""
import pytest
from unittest.mock import Mock
from sqlmodel import Session
from app.services.ui_service import UIService
from tests.factories.team_factory import TeamFactory
class TestUIService:
"""Test UIService functionality."""
@pytest.fixture
def mock_session(self):
"""Create mock database session."""
return Mock(spec=Session)
@pytest.fixture
def ui_service(self, mock_session):
"""Create UIService instance with mock session."""
return UIService(mock_session)
def test_format_team_display_with_custom_logo_and_color(self, ui_service):
"""Test team display formatting with custom logo and color."""
team = TeamFactory.build(
id=123,
abbrev="LAD",
lname="Los Angeles Dodgers",
logo="https://example.com/dodgers-logo.png",
color="005a9c",
season=9
)
result = ui_service.format_team_display(team)
assert result['title'] == "Los Angeles Dodgers"
assert result['color'] == "005a9c"
assert result['footer_text'] == "Paper Dynasty Season 9"
assert result['footer_icon'] == "https://paper-dynasty.s3.us-east-1.amazonaws.com/static-images/sba-logo.png"
assert result['thumbnail'] == "https://example.com/dodgers-logo.png"
assert result['team_id'] == 123
assert result['abbrev'] == "LAD"
assert result['season'] == 9
def test_format_team_display_with_defaults(self, ui_service):
"""Test team display formatting with default logo and color."""
team = TeamFactory.build(
id=456,
abbrev="SF",
lname="San Francisco Giants",
logo=None, # Should use default
color=None, # Should use default
season=8
)
result = ui_service.format_team_display(team)
# Should use default values
assert result['color'] == "a6ce39" # SBA_COLOR default
assert result['thumbnail'] == "https://paper-dynasty.s3.us-east-1.amazonaws.com/static-images/sba-logo.png" # SBA_LOGO default
assert result['title'] == "San Francisco Giants"
assert result['team_id'] == 456
def test_format_team_display_with_empty_color(self, ui_service):
"""Test team display formatting with empty string color."""
team = TeamFactory.build(
abbrev="NYY",
lname="New York Yankees",
color="", # Empty string should trigger default
season=9
)
result = ui_service.format_team_display(team)
# Empty string should trigger default color
assert result['color'] == "a6ce39"
def test_format_team_display_ai_team(self, ui_service):
"""Test team display formatting for AI team."""
ai_team = TeamFactory.build_ai_team(
id=789,
abbrev="AI1",
season=10
)
result = ui_service.format_team_display(ai_team)
assert result['title'] == "AI Team"
assert result['team_id'] == 789
assert result['abbrev'] == "AI1"
assert result['season'] == 10
def test_format_team_display_different_seasons(self, ui_service):
"""Test team display formatting across different seasons."""
team_s8 = TeamFactory.build(lname="Season 8 Team", season=8)
team_s9 = TeamFactory.build(lname="Season 9 Team", season=9)
team_s10 = TeamFactory.build(lname="Season 10 Team", season=10)
result_s8 = ui_service.format_team_display(team_s8)
result_s9 = ui_service.format_team_display(team_s9)
result_s10 = ui_service.format_team_display(team_s10)
assert result_s8['footer_text'] == "Paper Dynasty Season 8"
assert result_s9['footer_text'] == "Paper Dynasty Season 9"
assert result_s10['footer_text'] == "Paper Dynasty Season 10"
def test_format_team_display_preserves_all_fields(self, ui_service):
"""Test that all expected fields are present in formatted output."""
team = TeamFactory.build()
result = ui_service.format_team_display(team)
expected_fields = [
'title', 'color', 'footer_text', 'footer_icon',
'thumbnail', 'team_id', 'abbrev', 'season'
]
for field in expected_fields:
assert field in result, f"Missing field: {field}"
def test_format_team_display_error_handling(self, ui_service):
"""Test error handling in team display formatting."""
# Test with None object should cause an AttributeError
with pytest.raises(AttributeError):
ui_service.format_team_display(None)
def test_format_team_display_logging(self, ui_service):
"""Test that proper logging occurs during team formatting."""
team = TeamFactory.build(abbrev="LOG")
# This should not raise an exception and should complete successfully
result = ui_service.format_team_display(team)
assert result is not None
assert 'title' in result
def test_format_team_display_hex_color_variations(self, ui_service):
"""Test various hex color formats."""
test_colors = [
"ff0000", # 6-digit hex
"000000", # Black
"ffffff", # White
"a6ce39", # Default SBA color
"005a9c", # Dodgers blue
]
for color in test_colors:
team = TeamFactory.build(color=color)
result = ui_service.format_team_display(team)
assert result['color'] == color
def test_format_team_display_special_characters_in_names(self, ui_service):
"""Test team names with special characters."""
special_names = [
"Team with Spaces",
"Team-with-Hyphens",
"Team's with Apostrophes",
"Team & Ampersands",
"Team (with Parentheses)",
]
for name in special_names:
team = TeamFactory.build(lname=name)
result = ui_service.format_team_display(team)
assert result['title'] == name