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

48 statements  

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

1"""Module for testing the coaches presenters of the club API.""" 

2 

3import json 

4 

5import pytest 

6 

7from deepdiff import DeepDiff 

8from kwai_api.v1.club.coaches.presenters import ( 

9 JsonApiCoachesPresenter, 

10 JsonApiCoachPresenter, 

11 JsonApiPublicCoachesPresenter, 

12 JsonApiPublicCoachPresenter, 

13) 

14from kwai_bc_club.domain.club_coach import ClubCoachEntity 

15from kwai_core.domain.presenter import IterableResult 

16from typing_extensions import AsyncGenerator 

17 

18 

19@pytest.fixture 

20def coach(make_coach): 

21 """Fixture for a coach.""" 

22 return make_coach() 

23 

24 

25@pytest.fixture 

26def expected_public_coach_json(coach): 

27 """The expected JSON structure for public data of the coach.""" 

28 return { 

29 "data": { 

30 "id": str(coach.id), 

31 "type": "coaches", 

32 "meta": { 

33 "created_at": str(coach.traceable_time.created_at), 

34 "updated_at": str(coach.traceable_time.updated_at), 

35 }, 

36 "attributes": { 

37 "diploma": coach.diploma, 

38 "description": coach.description, 

39 "name": str(coach.name), 

40 }, 

41 } 

42 } 

43 

44 

45def test_public_coach_presenter(coach, expected_public_coach_json): 

46 """Test a presenter for public data of one coach.""" 

47 document = JsonApiPublicCoachPresenter().present(coach).get_document() 

48 assert document is not None, "There should be a document" 

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

50 

51 diff = DeepDiff(json_resource, expected_public_coach_json, ignore_order=True) 

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

53 

54 

55@pytest.fixture 

56def expected_coach_json(coach): 

57 """The expected coach JSON structure.""" 

58 return { 

59 "data": { 

60 "id": str(coach.id), 

61 "type": "coaches", 

62 "meta": { 

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

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

65 }, 

66 "attributes": { 

67 "diploma": coach.diploma, 

68 "description": coach.description, 

69 "name": str(coach.name), 

70 "remark": "", 

71 "active": True, 

72 "license": str(coach.member.license.number), 

73 "license_end_date": str(coach.member.license.end_date), 

74 "birthdate": str(coach.member.person.birthdate), 

75 }, 

76 "relationships": { 

77 "user": {"data": None}, 

78 "member": {"data": {"id": str(coach.member.uuid), "type": "members"}}, 

79 }, 

80 }, 

81 "included": [], 

82 } 

83 

84 

85def test_coach_presenter(coach, expected_coach_json): 

86 """Test the coach presenter.""" 

87 document = JsonApiCoachPresenter().present(coach).get_document() 

88 assert document is not None, "There should be a document" 

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

90 

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

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

93 

94 

95async def coach_generator(make_coach) -> AsyncGenerator[ClubCoachEntity, None]: 

96 """A generator for coaches.""" 

97 yield make_coach() 

98 yield make_coach() 

99 

100 

101@pytest.fixture 

102def iterable_result(make_coach) -> IterableResult[ClubCoachEntity]: 

103 """A fixture that creates an iterable result for coaches.""" 

104 return IterableResult[ClubCoachEntity]( 

105 count=2, iterator=coach_generator(make_coach) 

106 ) 

107 

108 

109async def test_public_coaches_presenter(iterable_result): 

110 """Test a presenter for public data with an iterable result containing coaches.""" 

111 presenter = JsonApiPublicCoachesPresenter() 

112 await presenter.present(iterable_result) 

113 document = presenter.get_document() 

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

115 assert document.meta.count == 2, "Count should be 2" 

116 assert len(document.data) == 2, "There should be 2 resources" 

117 

118 

119async def test_coaches_presenter(iterable_result): 

120 """Test a presenter with an iterable result containing coaches.""" 

121 presenter = JsonApiCoachesPresenter() 

122 await presenter.present(iterable_result) 

123 document = presenter.get_document() 

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

125 assert document.meta.count == 2, "Count should be 2" 

126 assert len(document.data) == 2, "There should be 2 resources"