Add ESC data and improve scoring system functionality

This commit is contained in:
2025-06-10 22:30:08 +02:00
parent a9ab9ff75a
commit 2fd9d7f548
7 changed files with 269 additions and 113 deletions

61
tests/test_esc.py Normal file
View File

@@ -0,0 +1,61 @@
import os
import sys
import orjson
sys.path.append(os.path.join(os.path.dirname(__file__), "..")) # noqa: PTH120, PTH118
import tempfile
from pathlib import Path
from typing import TypedDict
import pytest
from typer.testing import CliRunner
from src.__main__ import app
data_path = Path(__file__).parent / "esc_data.json"
class ESCData(TypedDict):
"""TypedDict for ESC data."""
jury: dict[str, int]
televote: dict[str, int]
winner: str
with data_path.open("r", encoding="utf-8") as f:
data = orjson.loads(f.read())
ESC_DATA: dict[int, "ESCData"] = {int(year): value for year, value in data.items()}
TADA = "🎉"
@pytest.mark.parametrize(("year", "data"), ESC_DATA.items())
def test_esc_grand_final(year: int, data: ESCData) -> None:
"""Test the ESC grand final for a given year."""
jury_scores: dict[str, int] = data["jury"]
televote_scores: dict[str, int] = data["televote"]
expected_winner: str = data["winner"]
with tempfile.NamedTemporaryFile("w", delete=False, encoding="utf-8") as f:
for country, score in jury_scores.items():
f.write(f"{country} {score}\n")
inputs: list[str] = []
for country in reversed(list(jury_scores.keys())):
inputs.append(str(televote_scores[country])) # noqa: PERF401
inputs.append("y") # to confirm the winner
runner = CliRunner()
result = runner.invoke(app, ["--jury-path", f.name], input="\n".join(inputs))
try:
actual = result.output.split(TADA)[1].strip().split()[0]
except Exception:
pytest.fail(f"Could not parse winner from output:\n{result.output}", pytrace=False)
assert actual == expected_winner, f"For {year}, expected winner {expected_winner} but got {actual!r}"