|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from gamesforai.ratings import Rating, rate_pair |
| 6 | + |
| 7 | + |
| 8 | +def test_native_rating_reference_and_json_checkpoint(): |
| 9 | + current = Rating(rd=200) |
| 10 | + updated = current.update([(Rating(rating=1400, rd=30), 1), |
| 11 | + (Rating(rating=1550, rd=100), 0), |
| 12 | + (Rating(rating=1700, rd=300), 0)]) |
| 13 | + assert updated.rating == pytest.approx(1464.06, abs=0.01) |
| 14 | + assert updated.rd == pytest.approx(151.52, abs=0.01) |
| 15 | + assert updated.games_played == 3 |
| 16 | + assert Rating(**json.loads(json.dumps(updated.to_dict()))) == updated |
| 17 | + assert current.rd == 200 |
| 18 | + |
| 19 | + |
| 20 | +def test_pair_symmetry_anchor_and_inactive_period(): |
| 21 | + original = Rating() |
| 22 | + winner, loser = rate_pair(original, original, 1) |
| 23 | + assert winner.rating + loser.rating == pytest.approx(3000) |
| 24 | + assert winner.games_played == loser.games_played == 1 |
| 25 | + idle = winner.update() |
| 26 | + assert idle.rating == winner.rating and idle.rd > winner.rd |
| 27 | + anchor = Rating(rating=1800, rd=0) |
| 28 | + learner, fixed = rate_pair(original, anchor, 1, fixed_right=True) |
| 29 | + assert learner.rating > winner.rating |
| 30 | + assert fixed.rating == anchor.rating and fixed.rd == 0 |
| 31 | + assert original.interval95 == (800, 2200) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("kwargs", [ |
| 35 | + {"rd": -1}, {"volatility": 0}, {"rating": float("nan")}, |
| 36 | + {"games_played": -1}, {"games_played": True}, |
| 37 | +]) |
| 38 | +def test_invalid_rating_inputs(kwargs): |
| 39 | + with pytest.raises(ValueError): |
| 40 | + Rating(**kwargs) |
| 41 | + |
| 42 | + |
| 43 | +def test_invalid_results_and_anchor_flags(): |
| 44 | + original = Rating() |
| 45 | + for score in (-1, 0.25, 2, float("inf")): |
| 46 | + with pytest.raises(ValueError): |
| 47 | + rate_pair(original, original, score) |
| 48 | + with pytest.raises(ValueError): |
| 49 | + rate_pair(original, original, 1, fixed_left=1) |
| 50 | + with pytest.raises(ValueError): |
| 51 | + original.update([(original, 1)], tau=0) |
0 commit comments