Coverage for src/tests/api/v1/training_schedules/test_presenters.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the training schedule presenters.""" 

2 

3import json 

4 

5from copy import deepcopy 

6from dataclasses import replace 

7from typing import Any 

8 

9import pytest 

10 

11from deepdiff import DeepDiff 

12from kwai_api.v1.training_schedules.presenters import ( 

13 JsonApiTrainingSchedulePresenter, 

14) 

15from kwai_bc_training.coaches.coach import CoachEntity, CoachIdentifier 

16from kwai_bc_training.teams.team import TeamEntity, TeamIdentifier 

17from kwai_bc_training.trainings.training_schedule import ( 

18 TrainingScheduleEntity, 

19 TrainingScheduleIdentifier, 

20) 

21from kwai_core.domain.value_objects.identifier import IntIdentifier 

22from kwai_core.domain.value_objects.name import Name 

23from kwai_core.domain.value_objects.owner import Owner 

24from kwai_core.domain.value_objects.time_period import TimePeriod 

25from kwai_core.domain.value_objects.unique_id import UniqueId 

26from kwai_core.domain.value_objects.weekday import Weekday 

27 

28 

29@pytest.fixture 

30def training_schedule() -> TrainingScheduleEntity: 

31 """A fixture for a training schedule.""" 

32 return TrainingScheduleEntity( 

33 id=TrainingScheduleIdentifier(1), 

34 name="U11 Trainings", 

35 description="Trainings for U11", 

36 weekday=Weekday.FRIDAY, 

37 period=TimePeriod.create_from_string("19:00", "20:00", "Europe/Brussels"), 

38 owner=Owner( 

39 id=IntIdentifier(1), 

40 uuid=UniqueId.generate(), 

41 name=Name(first_name="Jigoro", last_name="Kano"), 

42 ), 

43 coaches=frozenset( 

44 { 

45 CoachEntity( 

46 id=CoachIdentifier(1), 

47 name=Name(first_name="Jigoro", last_name="Kano"), 

48 active=True, 

49 uuid=UniqueId.generate(), 

50 head=False, 

51 ) 

52 } 

53 ), 

54 ) 

55 

56 

57@pytest.fixture 

58def expected_training_schedule_json(training_schedule) -> dict[str, Any]: 

59 """A fixture for a JSON:API resource of a training schedule.""" 

60 coach_id = str(list(training_schedule.coaches)[0].uuid) 

61 return { 

62 "data": { 

63 "id": "1", 

64 "type": "training_schedules", 

65 "meta": { 

66 "created_at": str(training_schedule.traceable_time.created_at), 

67 "updated_at": str(training_schedule.traceable_time.updated_at), 

68 }, 

69 "attributes": { 

70 "name": "U11 Trainings", 

71 "description": "Trainings for U11", 

72 "weekday": 5, 

73 "start_time": "19:00:00", 

74 "end_time": "20:00:00", 

75 "timezone": "Europe/Brussels", 

76 "active": True, 

77 "location": "", 

78 "remark": "", 

79 }, 

80 "relationships": { 

81 "team": {"data": None}, 

82 "coaches": {"data": [{"id": coach_id, "type": "coaches"}]}, 

83 }, 

84 }, 

85 "included": [ 

86 { 

87 "id": coach_id, 

88 "type": "coaches", 

89 "attributes": { 

90 "name": "Kano Jigoro", 

91 }, 

92 } 

93 ], 

94 } 

95 

96 

97def test_present_training_schedule(training_schedule, expected_training_schedule_json): 

98 """Test a presenter for a training schedule.""" 

99 presenter = JsonApiTrainingSchedulePresenter() 

100 presenter.present(training_schedule) 

101 document = presenter.get_document() 

102 assert document is not None, "There should be a JSON:API document" 

103 json_resource = json.loads(document.model_dump_json()) 

104 

105 diff = DeepDiff(json_resource, expected_training_schedule_json, ignore_order=True) 

106 assert not diff, f"JSON structure is not expected:{diff}" 

107 

108 

109def test_present_training_schedule_with_team( 

110 training_schedule, expected_training_schedule_json 

111): 

112 """Test a presenter for a training schedule with a team.""" 

113 team = TeamEntity(id=TeamIdentifier(1), name="U15") 

114 training_schedule = replace(training_schedule, team=team) 

115 presenter = JsonApiTrainingSchedulePresenter() 

116 presenter.present(training_schedule) 

117 document = presenter.get_document() 

118 assert document is not None, "There should be a JSON:API document" 

119 json_resource = json.loads(document.model_dump_json()) 

120 

121 expected_json = deepcopy(expected_training_schedule_json) 

122 expected_json["data"]["relationships"]["team"]["data"] = { 

123 "id": str(team.id), 

124 "type": "teams", 

125 } 

126 expected_json["included"].append( 

127 {"type": "teams", "id": "1", "attributes": {"name": "U15"}} 

128 ) 

129 diff = DeepDiff(json_resource, expected_json, ignore_order=True) 

130 assert not diff, f"JSON structure is not expected:{diff}"