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 ) 

50 } 

51 ), 

52 ) 

53 

54 

55@pytest.fixture 

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

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

58 return { 

59 "data": { 

60 "id": "1", 

61 "type": "training_schedules", 

62 "meta": { 

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

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

65 }, 

66 "attributes": { 

67 "name": "U11 Trainings", 

68 "description": "Trainings for U11", 

69 "weekday": 5, 

70 "start_time": "19:00:00", 

71 "end_time": "20:00:00", 

72 "timezone": "Europe/Brussels", 

73 "active": True, 

74 "location": "", 

75 "remark": "", 

76 }, 

77 "relationships": { 

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

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

80 }, 

81 }, 

82 "included": [ 

83 { 

84 "id": "1", 

85 "type": "coaches", 

86 "attributes": { 

87 "name": "Jigoro Kano", 

88 }, 

89 } 

90 ], 

91 } 

92 

93 

94def test_present_training_schedule(training_schedule, expected_training_schedule_json): 

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

96 presenter = JsonApiTrainingSchedulePresenter() 

97 presenter.present(training_schedule) 

98 document = presenter.get_document() 

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

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

101 

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

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

104 

105 

106def test_present_training_schedule_with_team( 

107 training_schedule, expected_training_schedule_json 

108): 

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

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

111 training_schedule = replace(training_schedule, team=team) 

112 presenter = JsonApiTrainingSchedulePresenter() 

113 presenter.present(training_schedule) 

114 document = presenter.get_document() 

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

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

117 

118 expected_json = deepcopy(expected_training_schedule_json) 

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

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

121 "type": "teams", 

122 } 

123 expected_json["included"].append( 

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

125 ) 

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

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