Spaces:
Running on T4
Running on T4
Download backend/tests/integration/test_arm_care.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_arm_care.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/integration/test_arm_care.py
-
curl -L -o test_arm_care.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_arm_care.py
4.98 kB
| """N23-N25: arm care.""" | |
| from datetime import UTC, datetime, timedelta | |
| from tests.factories import make_account, make_team_row | |
| TODAY = datetime.now(UTC).date() | |
| UNAVAILABLE = "Age-specific policy and innings source not configured" | |
| async def test_empty_arm_care(client): | |
| player = await make_account("player") | |
| response = await client.get("/v1/me/arm-care", headers=player["headers"]) | |
| assert response.status_code == 200 | |
| assert response.json() == {"arm_care": { | |
| "outings": [], "feel": None, "feel_recorded_at": None, "daily_max": None, "innings_last_12_months": None, | |
| "innings_limit": None, "rest_status": "unknown", "next_eligible_date": None, "rule_version": None, | |
| "unavailable_reason": UNAVAILABLE}} | |
| async def test_add_outing_and_read(client): | |
| player = await make_account("player") | |
| response = await client.post("/v1/me/arm-care/outings", json={"pitches": 45}, headers=player["headers"]) | |
| assert response.status_code == 201, response.text | |
| body = response.json() | |
| outing = body["outing"] | |
| assert outing["pitches"] == 45 and outing["local_date"] == TODAY.isoformat() | |
| assert outing["label"] == "Game, logged by you" and outing["rest"] is None and outing["rest_days"] is None | |
| assert body["arm_care"]["outings"] == [outing] | |
| older = (TODAY - timedelta(days=3)).isoformat() | |
| await client.post("/v1/me/arm-care/outings", json={"pitches": 20, "local_date": older}, headers=player["headers"]) | |
| far = (TODAY - timedelta(days=100)).isoformat() | |
| await client.post("/v1/me/arm-care/outings", json={"pitches": 30, "local_date": far}, headers=player["headers"]) | |
| items = (await client.get("/v1/me/arm-care", headers=player["headers"])).json()["arm_care"]["outings"] | |
| assert [i["pitches"] for i in items] == [45, 20] | |
| ranged = await client.get(f"/v1/me/arm-care?from={far}&to={far}", headers=player["headers"]) | |
| assert [i["pitches"] for i in ranged.json()["arm_care"]["outings"]] == [30] | |
| assert (await client.get(f"/v1/me/arm-care?from={older}&to={far}", headers=player["headers"])).status_code == 400 | |
| async def test_outing_validation(client): | |
| player = await make_account("player") | |
| future = (TODAY + timedelta(days=5)).isoformat() | |
| for body in ({"pitches": 0}, {"pitches": 201}, {"pitches": "45"}, {"pitches": 45.5}, {"pitches": True}, | |
| {"pitches": 5, "local_date": future}, {"pitches": 5, "x": 1}, {}): | |
| response = await client.post("/v1/me/arm-care/outings", json=body, headers=player["headers"]) | |
| assert response.status_code == 400, (body, response.text) | |
| async def test_outing_idempotent_and_notifies_coach(client): | |
| coach = await make_account("coach") | |
| player = await make_account("player", name="Ahmed") | |
| await make_team_row(coach, player) | |
| headers = {**player["headers"], "Idempotency-Key": "o1"} | |
| first = await client.post("/v1/me/arm-care/outings", json={"pitches": 30}, headers=headers) | |
| second = await client.post("/v1/me/arm-care/outings", json={"pitches": 30}, headers=headers) | |
| assert first.json() == second.json() | |
| items = (await client.get("/v1/me/arm-care", headers=player["headers"])).json()["arm_care"]["outings"] | |
| assert len(items) == 1 | |
| from sqlalchemy import select | |
| from app.core.database import get_session_factory | |
| from app.modules.notifications.models import Notification | |
| async with get_session_factory()() as db: | |
| rows = (await db.scalars(select(Notification).where(Notification.recipient_id == coach["id"]))).all() | |
| assert [(n.kind, n.title) for n in rows] == [("armcare", "Ahmed logged 30 pitches")] | |
| async def test_feel(client): | |
| player = await make_account("player") | |
| first = await client.put("/v1/me/arm-care/feel", json={"feel": "tight"}, headers=player["headers"]) | |
| assert first.status_code == 200 and first.json()["feel"] == "tight" | |
| same = await client.put("/v1/me/arm-care/feel", json={"feel": "tight"}, headers=player["headers"]) | |
| assert same.json() == first.json() | |
| changed = await client.put("/v1/me/arm-care/feel", json={"feel": "good"}, headers=player["headers"]) | |
| assert changed.json()["feel"] == "good" | |
| care = (await client.get("/v1/me/arm-care", headers=player["headers"])).json()["arm_care"] | |
| assert care["feel"] == "good" and care["feel_recorded_at"] == changed.json()["recorded_at"] | |
| assert (await client.put("/v1/me/arm-care/feel", json={"feel": "ok"}, headers=player["headers"])).status_code == 400 | |
| async def test_coach_forbidden(client): | |
| coach = await make_account("coach") | |
| assert (await client.get("/v1/me/arm-care", headers=coach["headers"])).status_code == 403 | |
| assert (await client.post("/v1/me/arm-care/outings", json={"pitches": 5}, headers=coach["headers"]) | |
| ).status_code == 403 | |
| assert (await client.put("/v1/me/arm-care/feel", json={"feel": "good"}, headers=coach["headers"]) | |
| ).status_code == 403 | |
| assert (await client.get("/v1/me/arm-care")).status_code == 401 | |