Coverage for src/tests/api/v1/training_schedules/test_endpoints.py: 98%

47 statements  

« 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.""" 

2 

3from typing import Any 

4 

5import pytest 

6 

7from fastapi import status 

8from fastapi.testclient import TestClient 

9 

10 

11pytestmark = pytest.mark.api 

12 

13 

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 

20 

21 

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() 

25 

26 response = client.get("/api/v1/training_schedules") 

27 assert response.status_code == status.HTTP_200_OK 

28 

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" 

33 

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 ) 

41 

42 

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() 

46 

47 response = client.get(f"/api/v1/training_schedules/{training_schedule.id}") 

48 assert response.status_code == status.HTTP_200_OK 

49 

50 json = response.json() 

51 assert "data" in json, "There should be a data list in the response" 

52 

53 

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() 

74 

75 

76async def test_update_training_schedule( 

77 secure_client: TestClient, make_training_schedule_in_db 

78): 

79 """Test update training schedule.""" 

80 training_schedule = await make_training_schedule_in_db() 

81 

82 payload = { 

83 "data": { 

84 "id": str(training_schedule.id), 

85 "type": "training_schedules", 

86 "attributes": { 

87 "name": training_schedule.name, 

88 "description": training_schedule.description, 

89 "weekday": 1, 

90 "start_time": "19:00", 

91 "end_time": "20:00", 

92 "timezone": "Europe/Brussels", 

93 "active": True, 

94 "location": "Sports Hall", 

95 "remark": "Updated with API", 

96 }, 

97 "relationships": {"team": {"data": None}, "coaches": {"data": []}}, 

98 } 

99 } 

100 response = secure_client.patch( 

101 f"/api/v1/training_schedules/{training_schedule.id}", json=payload 

102 ) 

103 assert response.status_code == status.HTTP_200_OK, response.json() 

104 

105 

106async def test_delete_training_schedule( 

107 secure_client: TestClient, make_training_schedule_in_db 

108): 

109 """Test delete training schedule.""" 

110 training_schedule = await make_training_schedule_in_db() 

111 

112 response = secure_client.delete( 

113 f"/api/v1/training_schedules/{training_schedule.id}" 

114 ) 

115 assert response.status_code == status.HTTP_200_OK 

116 

117 

118async def test_get_trainings(client: TestClient, make_training_schedule_in_db): 

119 """Test get trainings from a training schedule.""" 

120 training_schedule = await make_training_schedule_in_db() 

121 

122 response = client.get( 

123 f"/api/v1/training_schedules/{training_schedule.id}/trainings" 

124 ) 

125 assert response.status_code == status.HTTP_200_OK 

126 

127 json = response.json() 

128 assert "meta" in json, "There should be a meta object in the response" 

129 assert "data" in json, "There should be a data list in the response"