Skip to content

Commit

Permalink
Add tags to manga type
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirVondukr committed Feb 23, 2024
1 parent 0bff8e1 commit 23f3f4c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
7 changes: 7 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Manga {
titleSlug: String!
createdAt: DateTime!
updatedAt: DateTime!
tags: [MangaTag!]!
}

union MangaCreateError = ValidationErrors
Expand Down Expand Up @@ -56,6 +57,12 @@ type MangaPagePaginationResult {
pageInfo: PagePaginationInfo!
}

type MangaTag {
id: ID!
name: String!
slug: String!
}

input MangaTagFilter {
include: [String!] = null
exclude: [String!] = null
Expand Down
4 changes: 2 additions & 2 deletions src/app/adapters/graphql/apps/manga/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
class MangaTagGQL(DTOMixin[MangaTag]):
id: strawberry.ID
name: str
name_slug: str
slug: str

@classmethod
def from_dto(cls, model: MangaTag) -> Self:
return cls(
id=strawberry.ID(str(model.id)),
name=model.name,
name_slug=model.name_slug,
slug=model.name_slug,
)


Expand Down
18 changes: 12 additions & 6 deletions src/app/core/domain/manga/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ async def execute(
self,
keys: Sequence[UUID],
) -> Sequence[Sequence[MangaTag]]:
stmt = select(
manga_manga_tag_secondary_table.c.manga_id,
MangaTag,
).join(
manga_manga_tag_secondary_table,
manga_manga_tag_secondary_table.c.tag_id == MangaTag.id,
stmt = (
select(
manga_manga_tag_secondary_table.c.manga_id,
MangaTag,
)
.join(
manga_manga_tag_secondary_table,
manga_manga_tag_secondary_table.c.tag_id == MangaTag.id,
)
.order_by(
MangaTag.name_slug,
)
)
tags = collections.defaultdict(list)
for manga_id, tag in await self._session.execute(stmt):
Expand Down
Empty file.
58 changes: 58 additions & 0 deletions tests/adapters/graphql/manga/fields/test_manga_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
from sqlalchemy.ext.asyncio import AsyncSession

from app.db.models import Manga
from tests.adapters.graphql.client import GraphQLClient
from tests.factories import MangaTagFactory

pytestmark = pytest.mark.anyio

_QUERY = """query ($id: ID!) {
manga(id: $id) {
id
tags {
__typename
id
name
slug
}
}
}
"""


def _tpl(manga: object) -> object:
return {
"data": {"manga": manga},
}


async def test_ok(
collection_size: int,
session: AsyncSession,
manga: Manga,
graphql_client: GraphQLClient,
) -> None:
manga.tags = MangaTagFactory.build_batch(size=collection_size)
manga.tags.sort(key=lambda tag: tag.name_slug)
session.add(manga)
await session.flush()

response = await graphql_client.query(
query=_QUERY,
variables={"id": str(manga.id)},
)
assert response == _tpl(
{
"id": str(manga.id),
"tags": [
{
"__typename": "MangaTag",
"id": str(tag.id),
"name": tag.name,
"slug": tag.name_slug,
}
for tag in manga.tags
],
},
)
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import pkgutil
from collections.abc import AsyncIterator
from datetime import datetime
from typing import cast

import aioinject
import dotenv
import httpx
import pytest
from _pytest.fixtures import SubRequest
from aioinject import Object
from asgi_lifespan import LifespanManager
from fastapi import FastAPI
Expand Down Expand Up @@ -81,3 +83,8 @@ async def resolver(
@pytest.fixture
def now() -> datetime:
return utc_now()


@pytest.fixture(params=[0, 1, 10])
def collection_size(request: SubRequest) -> int:
return cast(int, request.param)

0 comments on commit 23f3f4c

Please sign in to comment.