Coverage for src/tests/api/v1/club/coaches/test_presenters.py: 100%
32 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« 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."""
3import json
5import pytest
7from deepdiff import DeepDiff
8from kwai_api.v1.club.coaches.presenters import (
9 JsonApiCoachesPresenter,
10 JsonApiCoachPresenter,
11)
12from kwai_bc_club.domain.club_coach import ClubCoachEntity
13from kwai_core.domain.presenter import IterableResult
14from typing_extensions import AsyncGenerator
17@pytest.fixture
18def coach(make_coach):
19 """Fixture for a coach."""
20 return make_coach()
23@pytest.fixture
24def expected_coach_json(coach):
25 """The expected coach JSON structure."""
26 return {
27 "data": {
28 "id": str(coach.uuid),
29 "type": "coaches",
30 "meta": {
31 "created_at": str(coach.traceable_time.created_at),
32 "updated_at": str(coach.traceable_time.updated_at),
33 },
34 "attributes": {
35 "diploma": coach.diploma,
36 "description": coach.description,
37 "name": str(coach.name),
38 "remark": "",
39 "active": True,
40 "head": False,
41 "license": str(coach.member.license.number),
42 "license_end_date": str(coach.member.license.end_date),
43 "birthdate": str(coach.member.person.birthdate),
44 },
45 "relationships": {
46 "user": {"data": None},
47 "member": {"data": {"id": str(coach.member.uuid), "type": "members"}},
48 },
49 },
50 "included": [],
51 }
54def test_coach_presenter(coach, expected_coach_json):
55 """Test the coach presenter."""
56 document = JsonApiCoachPresenter().present(coach).get_document()
57 assert document is not None, "There should be a document"
58 json_resource = json.loads(document.model_dump_json())
60 diff = DeepDiff(json_resource, expected_coach_json, ignore_order=True)
61 assert not diff, f"JSON structure is not expected:{diff}"
64async def coach_generator(make_coach) -> AsyncGenerator[ClubCoachEntity, None]:
65 """A generator for coaches."""
66 yield make_coach()
67 yield make_coach()
70@pytest.fixture
71def iterable_result(make_coach) -> IterableResult[ClubCoachEntity]:
72 """A fixture that creates an iterable result for coaches."""
73 return IterableResult[ClubCoachEntity](
74 count=2, iterator=coach_generator(make_coach)
75 )
78async def test_coaches_presenter(iterable_result):
79 """Test a presenter with an iterable result containing coaches."""
80 presenter = JsonApiCoachesPresenter()
81 await presenter.present(iterable_result)
82 document = presenter.get_document()
83 assert document is not None, "There should be a JSON:API document"
84 assert document.meta.count == 2, "Count should be 2"
85 assert len(document.data) == 2, "There should be 2 resources"