Spaces:
Running on T4
Running on T4
File size: 7,214 Bytes
fe40b87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | """N16-N19: goals, history, milestones."""
import uuid
from tests.factories import make_account, make_team_row
WEEKLY = {"practice_days": 4, "drill_sets": 9}
def goal_body(**overrides):
return {"type": "groups", "target": 3, "metric_id": None, "due": "2 weeks", "weekly": WEEKLY,
"note": "Keep the back hip loaded.", **overrides}
async def pair():
coach = await make_account("coach", name="Jamie")
player = await make_account("player")
await make_team_row(coach, player)
return coach, player
def url(player, mode="h"):
return f"/v1/players/{player['id']}/goals/{mode}"
async def test_no_goal_is_null(client):
coach, player = await pair()
for who in (coach, player):
response = await client.get(f"/v1/players/{player['id']}/goals?mode=h", headers=who["headers"])
assert response.status_code == 200 and response.json() == {"goal": None}
async def test_set_and_read_goal(client):
coach, player = await pair()
response = await client.put(url(player), json=goal_body(), headers=coach["headers"])
assert response.status_code == 200, response.text
goal = response.json()["goal"]
assert goal["version"] == 1 and goal["set_by"] == "Jamie" and goal["set_by_id"] == str(coach["id"])
assert goal["target"] == 3 and goal["metric_name"] is None and goal["weekly"] == WEEKLY
assert goal["due_at"].endswith("23:59:59Z") or "23:59:59" in goal["due_at"]
read = await client.get(f"/v1/players/{player['id']}/goals?mode=h", headers=player["headers"])
assert read.json()["goal"] == goal
other_mode = await client.get(f"/v1/players/{player['id']}/goals?mode=p", headers=player["headers"])
assert other_mode.json() == {"goal": None}
notes = await client.get("/v1/me/notifications", headers=player["headers"])
if notes.status_code == 200:
assert any(n["kind"] == "drill" for n in notes.json()["items"])
async def test_identical_put_is_noop_and_change_supersedes(client):
coach, player = await pair()
first = (await client.put(url(player), json=goal_body(), headers=coach["headers"])).json()["goal"]
again = await client.put(url(player), json=goal_body(), headers=coach["headers"])
assert again.status_code == 200 and again.json()["goal"]["id"] == first["id"]
changed = (await client.put(url(player), json=goal_body(target=2, expected_version=1),
headers=coach["headers"])).json()["goal"]
assert changed["version"] == 2 and changed["id"] != first["id"]
history = (await client.get(f"/v1/players/{player['id']}/goals/history?mode=h",
headers=player["headers"])).json()
assert [i["id"] for i in history["items"]] == [first["id"]]
item = history["items"][0]
assert item["outcome"] == "superseded" and item["met"] is None
assert item["text"] == "Get 3 of 4 groups on target" and " to " in item["range"]
assert history["next_cursor"] is None
async def test_stale_version(client):
coach, player = await pair()
await client.put(url(player), json=goal_body(), headers=coach["headers"])
response = await client.put(url(player), json=goal_body(target=2, expected_version=5), headers=coach["headers"])
assert response.status_code == 409 and response.json()["error"]["code"] == "STALE_VERSION"
async def test_player_cannot_set_goal(client):
_, player = await pair()
response = await client.put(url(player), json=goal_body(), headers=player["headers"])
assert response.status_code == 403
async def test_validation(client):
coach, player = await pair()
bad = [goal_body(target=0), goal_body(target=5), goal_body(target=2.5), goal_body(type="score", target=49),
goal_body(type="score", target=101), goal_body(type="param", target=10),
goal_body(type="param", target=10, metric_id="nope"),
goal_body(type="param", target=600, metric_id="hip_shoulder_separation_at_foot_down"),
goal_body(metric_id="hip_shoulder_separation_at_foot_down"), goal_body(due="3 weeks"),
goal_body(weekly={"practice_days": 8, "drill_sets": 9}),
goal_body(weekly={"practice_days": 4, "drill_sets": 31}), goal_body(note="x" * 161),
goal_body(extra=1)]
for body in bad:
response = await client.put(url(player), json=body, headers=coach["headers"])
assert response.status_code == 400, (body, response.text)
assert response.json()["error"]["code"] == "VALIDATION"
assert (await client.put(url(player), json=goal_body(), headers=coach["headers"])).status_code == 200
ok = goal_body(type="param", target=12.5, metric_id="hip_shoulder_separation_at_foot_down")
response = await client.put(url(player), json=ok, headers=coach["headers"])
assert response.status_code == 200
assert response.json()["goal"]["metric_name"] == "Hip Shoulder Separation at Foot Down"
assert response.json()["goal"]["target"] == 12.5
assert (await client.put(url(player), json=goal_body(type="score", target=80, due="End of month"),
headers=coach["headers"])).status_code == 200
async def test_cross_team_and_other_player_404(client):
coach, player = await pair()
other_coach = await make_account("coach")
await make_team_row(other_coach)
other_player = await make_account("player")
await make_team_row(await make_account("coach"), other_player)
assert (await client.put(url(player), json=goal_body(), headers=other_coach["headers"])).status_code == 404
path = f"/v1/players/{player['id']}/goals?mode=h"
assert (await client.get(path, headers=other_coach["headers"])).status_code == 404
assert (await client.get(path, headers=other_player["headers"])).status_code == 404
assert (await client.get(f"/v1/players/{uuid.uuid4()}/goals?mode=h", headers=coach["headers"])).status_code == 404
assert (await client.get(f"/v1/players/{player['id']}/goals", headers=coach["headers"])).status_code == 400
async def test_milestones_empty(client):
_, player = await pair()
response = await client.get("/v1/me/milestones?mode=h", headers=player["headers"])
assert response.status_code == 200 and response.json() == {"items": [], "next_cursor": None}
async def test_new_academy_coach_does_not_see_old_academy_goals(client):
old_coach, player = await pair()
assert (await client.put(url(player), json=goal_body(), headers=old_coach["headers"])).status_code == 200
assert (await client.put(url(player), json=goal_body(target=2), headers=old_coach["headers"])).status_code == 200
new_coach = await make_account("coach")
await make_team_row(new_coach, player) # the player moves to a second academy
path = f"/v1/players/{player['id']}/goals?mode=h"
history = f"/v1/players/{player['id']}/goals/history?mode=h"
assert (await client.get(path, headers=new_coach["headers"])).json() == {"goal": None}
assert (await client.get(history, headers=new_coach["headers"])).json()["items"] == []
own = await client.get(path, headers=player["headers"])
assert own.json()["goal"]["target"] == 2
assert len((await client.get(history, headers=player["headers"])).json()["items"]) == 1
|