Coverage for src/tests/api/v1/auth/authors/test_presenters.py: 100%
27 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 presenters for the /api/v1/auth/authors endpoint."""
3import json
5from typing import Any, AsyncGenerator
7import pytest
9from deepdiff import DeepDiff
10from kwai_api.v1.auth.authors.presenters import JsonApiAuthorsPresenter
11from kwai_bc_portal.domain.author import AuthorEntity, AuthorIdentifier
12from kwai_core.domain.presenter import IterableResult
13from kwai_core.domain.value_objects.unique_id import UniqueId
16@pytest.fixture
17def author() -> AuthorEntity:
18 """A fixture for an author entity."""
19 return AuthorEntity(
20 id=AuthorIdentifier(1),
21 uuid=UniqueId.generate(),
22 name="Jigoro Kano",
23 remark="Test author",
24 )
27async def author_iterator(author) -> AsyncGenerator[AuthorEntity, None]:
28 """A fixture that creates an async iterator with author entities."""
29 yield author
32@pytest.fixture
33def iterable_result(author) -> IterableResult[AuthorEntity]:
34 """A fixture that creates a IterableResult for author entities."""
35 return IterableResult[AuthorEntity](count=1, iterator=author_iterator(author))
38@pytest.fixture
39def expected_author_json(author) -> dict[str, Any]:
40 """A fixture for the expected json result."""
41 return {
42 "meta": {
43 "count": 1,
44 "offset": 0,
45 "limit": 0,
46 },
47 "data": [
48 {
49 "id": str(author.uuid),
50 "type": "authors",
51 "attributes": {
52 "name": "Jigoro Kano",
53 "remark": "Test author",
54 "active": True,
55 "editor": False,
56 },
57 "meta": {
58 "created_at": str(author.traceable_time.created_at),
59 "updated_at": None,
60 },
61 }
62 ],
63 }
66async def test_json_api_authors_presenter(iterable_result, expected_author_json):
67 """Test a presenter with an iterable result containing authors."""
68 presenter = JsonApiAuthorsPresenter()
69 document = (await presenter.present(iterable_result)).get_document()
70 assert document is not None, "The presenter should contain a document"
71 assert len(document.data) == 1, "There should be 1 resource"
72 json_resource = json.loads(document.model_dump_json())
74 diff = DeepDiff(json_resource, expected_author_json)
75 assert not diff, f"JSON structure is not as expected: {diff}"