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

36 statements  

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

1"""Module for testing the presenters of the /api/v1/trainings/coaches endpoints.""" 

2 

3import json 

4 

5from typing import AsyncGenerator 

6 

7import pytest 

8 

9from deepdiff import DeepDiff 

10from kwai_api.v1.trainings.coaches.presenters import ( 

11 JsonApiCoachesPresenter, 

12 JsonApiTrainingCoachesPresenter, 

13) 

14from kwai_bc_training.coaches.coach import CoachEntity, CoachIdentifier 

15from kwai_core.domain.presenter import IterableResult 

16from kwai_core.domain.value_objects.name import Name 

17from kwai_core.domain.value_objects.unique_id import UniqueId 

18 

19 

20@pytest.fixture 

21def coach() -> CoachEntity: 

22 """A fixture for a coach.""" 

23 return CoachEntity( 

24 id=CoachIdentifier(1), 

25 active=True, 

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

27 uuid=UniqueId.generate(), 

28 ) 

29 

30 

31async def coach_generator(coach) -> AsyncGenerator[CoachEntity, None]: 

32 """A generator for coaches.""" 

33 yield coach 

34 

35 

36@pytest.fixture 

37def iterable_result(coach) -> IterableResult[CoachEntity]: 

38 """A fixture that creates an iterable result of coaches.""" 

39 return IterableResult[CoachEntity](count=1, iterator=coach_generator(coach)) 

40 

41 

42async def test_presenter(coach, iterable_result): 

43 """Test a presenter for training coaches.""" 

44 presenter = JsonApiCoachesPresenter() 

45 await presenter.present(iterable_result) 

46 document = presenter.get_document() 

47 assert document is not None, "The presenter should return a document" 

48 

49 expected_coach_json = { 

50 "meta": { 

51 "count": 1, 

52 "limit": 0, 

53 "offset": 0, 

54 }, 

55 "data": [ 

56 { 

57 "id": str(coach.uuid), 

58 "type": "coaches", 

59 "attributes": {"name": "Kano Jigoro"}, 

60 } 

61 ], 

62 } 

63 

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

65 diff = DeepDiff(json_resource, expected_coach_json, ignore_order=True) 

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

67 

68 

69async def test_presenter_for_training_coaches(make_training): 

70 """Test a presenter for training coaches.""" 

71 presenter = JsonApiTrainingCoachesPresenter() 

72 training = make_training() 

73 presenter.present(training) 

74 document = presenter.get_document() 

75 assert document is not None, "The presenter should return a document" 

76 

77 expected_json = { 

78 "meta": {"count": 1, "offset": 0, "limit": 0}, 

79 "data": [ 

80 { 

81 "id": str(list(training.coaches)[0].id), 

82 "type": "training_coaches", 

83 "attributes": { 

84 "payed": False, 

85 "present": False, 

86 "head": False, 

87 "remark": "", 

88 }, 

89 "relationships": { 

90 "coach": { 

91 "data": { 

92 "id": str(list(training.coaches)[0].coach.uuid), 

93 "type": "coaches", 

94 } 

95 } 

96 }, 

97 } 

98 ], 

99 "included": [ 

100 { 

101 "id": str(list(training.coaches)[0].coach.uuid), 

102 "type": "coaches", 

103 "attributes": {"name": "Kano Jigoro"}, 

104 }, 

105 ], 

106 } 

107 

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

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

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