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

37 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 Any, 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 

17 

18 

19@pytest.fixture 

20def coach() -> CoachEntity: 

21 """A fixture for a coach.""" 

22 return CoachEntity( 

23 id=CoachIdentifier(1), 

24 active=True, 

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

26 ) 

27 

28 

29@pytest.fixture 

30def expected_coach_json() -> dict[str, Any]: 

31 """A fixture with the expected JSON structure.""" 

32 return { 

33 "meta": { 

34 "count": 1, 

35 "limit": 0, 

36 "offset": 0, 

37 }, 

38 "data": [ 

39 { 

40 "id": "1", 

41 "type": "coaches", 

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

43 } 

44 ], 

45 } 

46 

47 

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

49 """A generator for coaches.""" 

50 yield coach 

51 

52 

53@pytest.fixture 

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

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

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

57 

58 

59async def test_presenter(coach, expected_coach_json, iterable_result): 

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

61 presenter = JsonApiCoachesPresenter() 

62 await presenter.present(iterable_result) 

63 document = presenter.get_document() 

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

65 

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

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

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

69 

70 

71async def test_presenter_for_training_coaches(make_training): 

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

73 presenter = JsonApiTrainingCoachesPresenter() 

74 training = make_training() 

75 presenter.present(training) 

76 document = presenter.get_document() 

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

78 

79 expected_json = { 

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

81 "data": [ 

82 { 

83 "id": "0", 

84 "type": "training_coaches", 

85 "attributes": { 

86 "name": "Jigoro Kano", 

87 "payed": False, 

88 "present": False, 

89 "head": False, 

90 "remark": "", 

91 }, 

92 } 

93 ], 

94 } 

95 

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

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

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