Coverage for src/tests/api/v1/training_schedules/test_endpoints.py: 98%
52 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module for testing the training_schedules end point."""
3from typing import Any
5import pytest
7from fastapi import status
8from fastapi.testclient import TestClient
11pytestmark = pytest.mark.api
14def _find(resource_list: list[dict[str, Any]], id_: str):
15 """Search for a resource with the given id."""
16 for resource in resource_list:
17 if resource["id"] == id_:
18 return resource
19 return None
22async def test_get_training_schedules(client: TestClient, make_training_schedule_in_db):
23 """Test get training schedules api."""
24 training_schedule = await make_training_schedule_in_db()
26 response = client.get("/api/v1/training_schedules")
27 assert response.status_code == status.HTTP_200_OK
29 json = response.json()
30 assert "meta" in json, "There should be a meta object in the response"
31 assert "data" in json, "There should be a data list in the response"
32 assert len(json["data"]) > 0, "There should be at least one training"
34 training_schedule_resource = _find(json["data"], str(training_schedule.id))
35 assert training_schedule_resource["type"] == "training_schedules", (
36 "The resource should have the type 'training_schedules' '."
37 )
38 assert training_schedule_resource is not None, (
39 f"Training with id {training_schedule.id} should exist"
40 )
43async def test_get_training_schedule(client: TestClient, make_training_schedule_in_db):
44 """Test get training schedule api."""
45 training_schedule = await make_training_schedule_in_db()
47 response = client.get(f"/api/v1/training_schedules/{training_schedule.id}")
48 assert response.status_code == status.HTTP_200_OK
50 json = response.json()
51 assert "data" in json, "There should be a data list in the response"
54def test_create_training_schedule(secure_client: TestClient):
55 """Test create training schedule."""
56 payload = {
57 "data": {
58 "type": "training_schedules",
59 "attributes": {
60 "name": "U9 Training",
61 "description": "Monday training for U9",
62 "weekday": 1,
63 "start_time": "19:00",
64 "end_time": "20:00",
65 "timezone": "Europe/Brussels",
66 "active": True,
67 "location": "Sports Hall",
68 },
69 "relationships": {"team": {"data": None}, "coaches": {"data": []}},
70 }
71 }
72 response = secure_client.post("/api/v1/training_schedules", json=payload)
73 assert response.status_code == status.HTTP_201_CREATED, response.json()
76async def test_create_training_schedule_with_coach(
77 secure_client: TestClient, make_coach_in_db
78):
79 """Test create training schedule."""
80 coach = await make_coach_in_db()
81 payload = {
82 "data": {
83 "type": "training_schedules",
84 "attributes": {
85 "name": "U9 Training",
86 "description": "Monday training for U9",
87 "weekday": 1,
88 "start_time": "19:00",
89 "end_time": "20:00",
90 "timezone": "Europe/Brussels",
91 "active": True,
92 "location": "Sports Hall",
93 },
94 "relationships": {
95 "team": {"data": None},
96 "coaches": {"data": [{"id": str(coach.uuid), "type": "coaches"}]},
97 },
98 }
99 }
100 response = secure_client.post("/api/v1/training_schedules", json=payload)
101 assert response.status_code == status.HTTP_201_CREATED, response.json()
104async def test_update_training_schedule(
105 secure_client: TestClient, make_training_schedule_in_db
106):
107 """Test update training schedule."""
108 training_schedule = await make_training_schedule_in_db()
110 payload = {
111 "data": {
112 "id": str(training_schedule.id),
113 "type": "training_schedules",
114 "attributes": {
115 "name": training_schedule.name,
116 "description": training_schedule.description,
117 "weekday": 1,
118 "start_time": "19:00",
119 "end_time": "20:00",
120 "timezone": "Europe/Brussels",
121 "active": True,
122 "location": "Sports Hall",
123 "remark": "Updated with API",
124 },
125 "relationships": {"team": {"data": None}, "coaches": {"data": []}},
126 }
127 }
128 response = secure_client.patch(
129 f"/api/v1/training_schedules/{training_schedule.id}", json=payload
130 )
131 assert response.status_code == status.HTTP_200_OK, response.json()
134async def test_delete_training_schedule(
135 secure_client: TestClient, make_training_schedule_in_db
136):
137 """Test delete training schedule."""
138 training_schedule = await make_training_schedule_in_db()
140 response = secure_client.delete(
141 f"/api/v1/training_schedules/{training_schedule.id}"
142 )
143 assert response.status_code == status.HTTP_200_OK
146async def test_get_trainings(client: TestClient, make_training_schedule_in_db):
147 """Test get trainings from a training schedule."""
148 training_schedule = await make_training_schedule_in_db()
150 response = client.get(
151 f"/api/v1/training_schedules/{training_schedule.id}/trainings"
152 )
153 assert response.status_code == status.HTTP_200_OK
155 json = response.json()
156 assert "meta" in json, "There should be a meta object in the response"
157 assert "data" in json, "There should be a data list in the response"