105 lines
4.5 KiB
Python
105 lines
4.5 KiB
Python
"""
|
|
"Jakter Jesper"-visningsmodus for frittstående runder (migrasjon 093, del 2
|
|
av ADR-106). Speiler tests/test_net_hunt_display.py (org-individuell) sitt
|
|
mønster og bevis: løpende tall skiller seg fra standard midt i runden, men
|
|
konvergerer eksakt ved 18 fullførte hull.
|
|
"""
|
|
|
|
from fastapi import HTTPException
|
|
import pytest
|
|
|
|
from app.auth import CurrentUser
|
|
from app.routers.rounds import HoleUpdate, RoundUpdate, get_leaderboard, get_round, update_hole, update_round
|
|
|
|
from tests.conftest import create_hole, create_participant, create_round, create_user
|
|
|
|
import app.db as app_db
|
|
|
|
|
|
async def _score(round_id, participant_id, user_id, scores: list[int]) -> None:
|
|
user = CurrentUser(user_id=user_id)
|
|
for n, gross in enumerate(scores, start=1):
|
|
await update_hole(
|
|
round_id, participant_id, n,
|
|
HoleUpdate(score=gross, expected_version=None),
|
|
user=user,
|
|
)
|
|
|
|
|
|
async def _setup_stroke_round(user_id, course_handicap=12):
|
|
async with app_db.plain_connection() as conn:
|
|
round_id = await create_round(conn, user_id)
|
|
participant_id = await create_participant(
|
|
conn, round_id, user_id=user_id, is_owner=True, course_handicap_snapshot=course_handicap,
|
|
)
|
|
for n in range(1, 19):
|
|
await create_hole(conn, participant_id, hole_number=n, par=4, stroke_index=n)
|
|
await conn.execute("UPDATE round SET play_format = 'stroke' WHERE id = $1", round_id)
|
|
return round_id, participant_id
|
|
|
|
|
|
async def test_hunt_differs_mid_round_but_converges_at_completion(pool):
|
|
user_id = await create_user()
|
|
round_id, participant_id = await _setup_stroke_round(user_id, course_handicap=12)
|
|
|
|
# Hull 1, par -- stroke_index 1 (<=12) -> mottar 1 slag under standard.
|
|
await _score(round_id, participant_id, user_id, [4])
|
|
|
|
out = await get_round(round_id, user=CurrentUser(user_id=user_id))
|
|
# Standard (default): netto = 4-1=3, til par = 3-4 = -1.
|
|
assert out.my_net_score_to_par == -1
|
|
|
|
await update_round(round_id, RoundUpdate(net_display_style="hunt"), user=CurrentUser(user_id=user_id))
|
|
out = await get_round(round_id, user=CurrentUser(user_id=user_id))
|
|
# Hunt: gross(4) - FULLT course handicap(12) = -8, til par = -8-4 = -12.
|
|
assert out.my_net_score_to_par == -12
|
|
|
|
lb = await get_leaderboard(round_id, user=CurrentUser(user_id=user_id))
|
|
assert lb.net_display_style == "hunt"
|
|
assert lb.entries[0].net_score_to_par == -12
|
|
assert lb.entries[0].course_handicap == 12
|
|
|
|
# Fullfør runden med par på alle resterende hull -- flatt hele veien
|
|
# under hunt (alltid par -> gross-til-par alltid 0).
|
|
await _score(round_id, participant_id, user_id, [4] * 17)
|
|
out = await get_round(round_id, user=CurrentUser(user_id=user_id))
|
|
assert out.my_net_score_to_par == -12
|
|
|
|
# Bytt tilbake til standard nå som runden er fullført -- KONVERGERER
|
|
# til nøyaktig samme tall (selve beviset på ekvivalensen).
|
|
await update_round(round_id, RoundUpdate(net_display_style="standard"), user=CurrentUser(user_id=user_id))
|
|
out = await get_round(round_id, user=CurrentUser(user_id=user_id))
|
|
assert out.my_net_score_to_par == -12
|
|
|
|
lb = await get_leaderboard(round_id, user=CurrentUser(user_id=user_id))
|
|
assert lb.entries[0].net_score_to_par == -12
|
|
|
|
|
|
async def test_hunt_rejected_for_non_stroke_play_format(pool):
|
|
user_id = await create_user()
|
|
round_id, participant_id = await _setup_stroke_round(user_id)
|
|
async with app_db.plain_connection() as conn:
|
|
await conn.execute("UPDATE round SET play_format = 'stableford' WHERE id = $1", round_id)
|
|
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
await update_round(round_id, RoundUpdate(net_display_style="hunt"), user=CurrentUser(user_id=user_id))
|
|
assert exc_info.value.status_code == 400
|
|
|
|
|
|
async def test_switching_away_from_stroke_rejected_while_hunt_active(pool):
|
|
user_id = await create_user()
|
|
round_id, participant_id = await _setup_stroke_round(user_id)
|
|
await update_round(round_id, RoundUpdate(net_display_style="hunt"), user=CurrentUser(user_id=user_id))
|
|
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
await update_round(round_id, RoundUpdate(play_format="stableford"), user=CurrentUser(user_id=user_id))
|
|
assert exc_info.value.status_code == 400
|
|
|
|
# Men lov når begge felt endres sammen i samme kall.
|
|
out = await update_round(
|
|
round_id,
|
|
RoundUpdate(play_format="stableford", net_display_style="standard"),
|
|
user=CurrentUser(user_id=user_id),
|
|
)
|
|
assert out.play_format == "stableford"
|
|
assert out.net_display_style == "standard"
|