95 lines
3.2 KiB
Python
95 lines
3.2 KiB
Python
"""Die Ziehung: Streifen, Stopps, Gewinner."""
|
|
|
|
from app import suggestions
|
|
from app.suggestions import BRUCH_DAUER, DREH_DAUER, STREIFEN_LAENGE, STREIFEN_ZIEL
|
|
|
|
|
|
def vorschlagen(client, movie_id):
|
|
antwort = client.post("/api/suggestions", json={"movie_id": movie_id})
|
|
assert antwort.status_code == 200
|
|
return antwort.json()
|
|
|
|
|
|
def test_zweiter_vorschlag_ersetzt_den_ersten(client, film):
|
|
vorschlagen(client, film(1))
|
|
neu = vorschlagen(client, film(2))
|
|
|
|
liste = client.get("/api/suggestions").json()
|
|
assert [e["movie_id"] for e in liste["items"]] == [2]
|
|
assert liste["mine"] == neu["id"]
|
|
|
|
|
|
def test_dreh_liefert_streifen_und_gewinner(client, client2, film):
|
|
vorschlagen(client, film(1))
|
|
vorschlagen(client2, film(2))
|
|
|
|
ergebnis = client.post("/api/spin").json()
|
|
|
|
assert ergebnis["started"] is True
|
|
assert [k["movie_id"] for k in ergebnis["karten"]] == [1, 2]
|
|
assert len(ergebnis["streifen"]) == STREIFEN_LAENGE
|
|
assert set(ergebnis["streifen"]) <= {0, 1}
|
|
stopps = ergebnis["stopps"]
|
|
assert len(stopps) in (1, 2)
|
|
assert all(set(s) == {"position", "versatz"} for s in stopps)
|
|
assert stopps[0]["position"] == STREIFEN_ZIEL
|
|
assert ergebnis["gewinner"] == ergebnis["karten"][ergebnis["index"]]
|
|
|
|
|
|
def test_ziehung_haelt_auf_dem_gewinner(client, client2, film):
|
|
vorschlagen(client, film(1))
|
|
vorschlagen(client2, film(2))
|
|
|
|
stopp_anzahlen = set()
|
|
for _ in range(60):
|
|
e = suggestions.ziehen()
|
|
stopps = e["stopps"]
|
|
stopp_anzahlen.add(len(stopps))
|
|
# Der letzte Halt zeigt den Gewinner
|
|
assert e["streifen"][stopps[-1]["position"]] == e["index"]
|
|
assert all(-0.5 < s["versatz"] < 0.5 for s in stopps)
|
|
if len(stopps) == 2:
|
|
# Bruch: ein paar Poster zurück, der erste Halt zeigt einen anderen
|
|
assert 1 <= stopps[0]["position"] - stopps[1]["position"] <= 4
|
|
assert e["streifen"][stopps[0]["position"]] != e["index"]
|
|
assert e["dauer"] == DREH_DAUER + BRUCH_DAUER
|
|
else:
|
|
assert e["dauer"] == DREH_DAUER
|
|
# Beide Fälle kamen vor
|
|
assert stopp_anzahlen == {1, 2}
|
|
|
|
|
|
def test_zweiter_dreh_waehrend_laufender_ziehung(client, film):
|
|
vorschlagen(client, film(1))
|
|
assert client.post("/api/spin").json()["started"] is True
|
|
|
|
assert client.post("/api/spin").json()["started"] is False
|
|
|
|
stand = client.get("/api/spin").json()
|
|
assert stand["running"] is True
|
|
assert stand["elapsed"] >= 0
|
|
assert stand["result"]["gewinner"]["movie_id"] == 1
|
|
|
|
|
|
def test_ohne_vorschlaege_kein_dreh(client):
|
|
assert client.post("/api/spin").status_code == 400
|
|
assert client.get("/api/spin").json() == {"running": False, "result": None}
|
|
|
|
|
|
def test_gesehene_filme_gewinnen_nicht(client, client2, film):
|
|
vorschlagen(client, film(1))
|
|
vorschlagen(client2, film(2))
|
|
client.post("/api/watched", json={"movie_id": 1})
|
|
|
|
for _ in range(50):
|
|
assert suggestions.ziehen()["gewinner"]["movie_id"] == 2
|
|
|
|
|
|
def test_runde_zuruecksetzen(client, film):
|
|
vorschlagen(client, film(1))
|
|
client.post("/api/spin")
|
|
|
|
assert client.delete("/api/suggestions/alle").json() == {"removed": 1}
|
|
assert client.get("/api/spin").json()["running"] is False
|
|
assert client.get("/api/suggestions").json()["items"] == []
|