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 head=False, 

29 ) 

30 

31 

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

33 """A generator for coaches.""" 

34 yield coach 

35 

36 

37@pytest.fixture 

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

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

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

41 

42 

43async def test_presenter(coach, iterable_result): 

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

45 presenter = JsonApiCoachesPresenter() 

46 await presenter.present(iterable_result) 

47 document = presenter.get_document() 

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

49 

50 expected_coach_json = { 

51 "meta": { 

52 "count": 1, 

53 "limit": 0, 

54 "offset": 0, 

55 }, 

56 "data": [ 

57 { 

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

59 "type": "coaches", 

60 "attributes": {"name": "Kano Jigoro", "head": False}, 

61 } 

62 ], 

63 } 

64 

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

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

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

68 

69 

70async def test_presenter_for_training_coaches(make_training): 

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

72 presenter = JsonApiTrainingCoachesPresenter() 

73 training = make_training() 

74 presenter.present(training) 

75 document = presenter.get_document() 

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

77 

78 expected_json = { 

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

80 "data": [ 

81 { 

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

83 "type": "training_coaches", 

84 "attributes": { 

85 "payed": False, 

86 "present": False, 

87 "head": False, 

88 "remark": "", 

89 }, 

90 "relationships": { 

91 "coach": { 

92 "data": { 

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

94 "type": "coaches", 

95 } 

96 } 

97 }, 

98 } 

99 ], 

100 "included": [ 

101 { 

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

103 "type": "coaches", 

104 "attributes": {"name": "Kano Jigoro", "head": False}, 

105 }, 

106 ], 

107 } 

108 

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

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

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