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

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

51 } 

52 ), 

53 ) 

54 

55 

56@pytest.fixture 

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

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

59 return { 

60 "data": { 

61 "id": "1", 

62 "type": "training_schedules", 

63 "meta": { 

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

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

66 }, 

67 "attributes": { 

68 "name": "U11 Trainings", 

69 "description": "Trainings for U11", 

70 "weekday": 5, 

71 "start_time": "19:00:00", 

72 "end_time": "20:00:00", 

73 "timezone": "Europe/Brussels", 

74 "active": True, 

75 "location": "", 

76 "remark": "", 

77 }, 

78 "relationships": { 

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

80 "coaches": {"data": [{"id": "1", "type": "coaches"}]}, 

81 }, 

82 }, 

83 "included": [ 

84 { 

85 "id": "1", 

86 "type": "coaches", 

87 "attributes": { 

88 "name": "Kano Jigoro", 

89 }, 

90 } 

91 ], 

92 } 

93 

94 

95def test_present_training_schedule(training_schedule, expected_training_schedule_json): 

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

97 presenter = JsonApiTrainingSchedulePresenter() 

98 presenter.present(training_schedule) 

99 document = presenter.get_document() 

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

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

102 

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

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

105 

106 

107def test_present_training_schedule_with_team( 

108 training_schedule, expected_training_schedule_json 

109): 

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

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

112 training_schedule = replace(training_schedule, team=team) 

113 presenter = JsonApiTrainingSchedulePresenter() 

114 presenter.present(training_schedule) 

115 document = presenter.get_document() 

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

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

118 

119 expected_json = deepcopy(expected_training_schedule_json) 

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

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

122 "type": "teams", 

123 } 

124 expected_json["included"].append( 

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

126 ) 

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

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