"""Tests for Elo rating"""
import pytest
from app.ml.elo import EloRating


def test_expected_score():
    """Test expected score calculation"""
    # Equal ratings should give 0.5
    assert abs(EloRating.expected_score(1500, 1500) - 0.5) < 0.01
    
    # Higher rating should have higher expected score
    assert EloRating.expected_score(1600, 1500) > 0.5
    assert EloRating.expected_score(1500, 1600) < 0.5


def test_update_rating():
    """Test rating update"""
    rating = 1500
    expected = 0.5
    actual = 1.0  # Win
    
    new_rating = EloRating.update_rating(rating, expected, actual)
    assert new_rating > rating  # Should increase after win


def test_calculate_ratings():
    """Test rating calculation for a match"""
    team_ratings = {1: 1500, 2: 1500}
    
    # Home team wins
    EloRating.calculate_ratings(team_ratings, 1, 2, 2, 1)
    
    assert team_ratings[1] > 1500  # Home team rating increased
    assert team_ratings[2] < 1500  # Away team rating decreased


def test_calculate_ratings_draw():
    """Test rating calculation for a draw"""
    team_ratings = {1: 1500, 2: 1500}
    
    # Draw
    EloRating.calculate_ratings(team_ratings, 1, 2, 1, 1)
    
    # Ratings should be close (home advantage makes home slightly higher)
    assert abs(team_ratings[1] - team_ratings[2]) < 50

