47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
"""Tests for the ELO calculation module."""
|
|
|
|
import pytest
|
|
|
|
from app.elo import calculate_elo
|
|
|
|
|
|
class TestCalculateElo:
|
|
"""Tests for calculate_elo."""
|
|
|
|
def test_equal_ratings(self):
|
|
"""Equal ratings should produce symmetric changes."""
|
|
new_w, new_l = calculate_elo(1500.0, 1500.0, k_factor=32.0)
|
|
assert new_w > 1500.0
|
|
assert new_l < 1500.0
|
|
assert new_w - 1500.0 == pytest.approx(1500.0 - new_l, abs=0.01)
|
|
|
|
def test_higher_rated_wins(self):
|
|
"""Higher-rated winner should get a small gain."""
|
|
new_w, new_l = calculate_elo(1800.0, 1200.0, k_factor=32.0)
|
|
change = new_w - 1800.0
|
|
assert 0 < change < 16.0 # expected win → small gain
|
|
|
|
def test_lower_rated_wins(self):
|
|
"""Lower-rated winner (upset) should get a large gain."""
|
|
new_w, new_l = calculate_elo(1200.0, 1800.0, k_factor=32.0)
|
|
change = new_w - 1200.0
|
|
assert change > 16.0 # upset → large gain
|
|
|
|
def test_k_factor_scales_change(self):
|
|
"""Higher K-factor should produce larger rating changes."""
|
|
_, _ = calculate_elo(1500.0, 1500.0, k_factor=16.0)
|
|
new_w_16, _ = calculate_elo(1500.0, 1500.0, k_factor=16.0)
|
|
new_w_64, _ = calculate_elo(1500.0, 1500.0, k_factor=64.0)
|
|
assert (new_w_64 - 1500.0) > (new_w_16 - 1500.0)
|
|
|
|
def test_total_elo_preserved(self):
|
|
"""Total ELO should be preserved (zero-sum)."""
|
|
new_w, new_l = calculate_elo(1600.0, 1400.0, k_factor=32.0)
|
|
assert new_w + new_l == pytest.approx(1600.0 + 1400.0, abs=0.01)
|
|
|
|
def test_result_is_rounded(self):
|
|
"""Results should be rounded to 2 decimal places."""
|
|
new_w, new_l = calculate_elo(1500.0, 1500.0, k_factor=32.0)
|
|
assert new_w == round(new_w, 2)
|
|
assert new_l == round(new_l, 2)
|