Skip to content

Commit 8f7b83f

Browse files
committed
tests coverage read custom model id field
1 parent 91fa388 commit 8f7b83f

File tree

8 files changed

+147
-2
lines changed

8 files changed

+147
-2
lines changed

Diff for: tests/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
user_3,
5252
workplace_1,
5353
workplace_2,
54+
age_rating_g,
5455
)
5556
from tests.fixtures.user import ( # noqa
5657
user_attributes,

Diff for: tests/fixtures/app.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from fastapi_jsonapi.data_typing import TypeModel
4040
from fastapi_jsonapi.views.view_base import ViewBase
4141

42-
from .models import Alpha, Beta, CustomUUIDItem, Delta, Gamma, Task
42+
from .models import Alpha, Beta, CustomUUIDItem, Delta, Gamma, Task, AgeRating
4343
from .schemas import (
4444
AlphaSchema,
4545
BetaSchema,
@@ -49,6 +49,9 @@
4949
TaskInSchema,
5050
TaskPatchSchema,
5151
TaskSchema,
52+
AgeRatingSchema,
53+
AgeRatingCreateSchema,
54+
AgeRatingUpdateSchema,
5255
)
5356
from .views import ViewBaseGeneric
5457

@@ -162,6 +165,17 @@ def add_routers(app_plain: FastAPI):
162165
schema_in_patch=UserPatchSchema,
163166
schema_in_post=UserInSchema,
164167
)
168+
builder.add_resource(
169+
path="/age-ratings",
170+
tags=["Age Ratings"],
171+
resource_type="age-rating",
172+
view=ViewBaseGeneric,
173+
model=AgeRating,
174+
schema=AgeRatingSchema,
175+
schema_in_post=AgeRatingCreateSchema,
176+
schema_in_patch=AgeRatingUpdateSchema,
177+
model_id_field_name="name",
178+
)
165179
builder.initialize()
166180

167181
return app_plain

Diff for: tests/fixtures/entities.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from textwrap import dedent
12
from typing import Awaitable, Callable, Optional
23

34
import pytest
@@ -16,7 +17,7 @@
1617
Workplace,
1718
)
1819
from tests.common import is_postgres_tests
19-
from tests.fixtures.models import Task
20+
from tests.fixtures.models import Task, AgeRating
2021
from tests.misc.utils import fake
2122

2223

@@ -551,3 +552,24 @@ async def workplace_2(
551552
async_session: AsyncSession,
552553
):
553554
yield await create_workplace(async_session, name="workplace_2")
555+
556+
557+
@async_fixture()
558+
async def age_rating_g(async_session: AsyncSession):
559+
age_rating = AgeRating(
560+
name="G",
561+
description=dedent(
562+
"""G – General Audiences
563+
564+
All ages admitted.
565+
Nothing that would offend parents for viewing by children.
566+
"""
567+
),
568+
)
569+
async_session.add(age_rating)
570+
await async_session.commit()
571+
572+
yield age_rating
573+
574+
await async_session.delete(age_rating)
575+
await async_session.commit()

Diff for: tests/fixtures/models/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
from tests.fixtures.models.gamma import Gamma
1010
from tests.fixtures.models.self_relationship import SelfRelationship
1111
from tests.fixtures.models.task import Task
12+
from tests.fixtures.models.age_rating import AgeRating
1213

1314
__all__ = (
15+
"AgeRating",
1416
"Alpha",
1517
"Beta",
1618
"BetaDeltaBinding",

Diff for: tests/fixtures/models/age_rating.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from sqlalchemy import (
2+
Identity,
3+
String,
4+
Text,
5+
)
6+
from sqlalchemy.orm import Mapped, mapped_column
7+
8+
from examples.api_for_sqlalchemy.models.base import Base
9+
10+
11+
class AgeRating(Base):
12+
__tablename__ = "age_rating"
13+
14+
@property
15+
def id(self):
16+
"""
17+
Compatibility with JSON:API for reading in view
18+
"""
19+
return self.name
20+
21+
name: Mapped[str] = mapped_column(
22+
String(20),
23+
Identity(always=False),
24+
primary_key=True,
25+
)
26+
description: Mapped[str] = mapped_column(
27+
Text(),
28+
default="",
29+
server_default="",
30+
)
31+
32+
def __str__(self) -> str:
33+
return self.name

Diff for: tests/fixtures/schemas/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
TaskSchema,
1616
)
1717

18+
from .age_rating import (
19+
AgeRatingBaseSchema,
20+
AgeRatingCreateSchema,
21+
AgeRatingUpdateSchema,
22+
AgeRatingSchema,
23+
)
24+
1825
__all__ = (
1926
"AlphaSchema",
2027
"BetaSchema",
@@ -28,4 +35,8 @@
2835
"TaskInSchema",
2936
"TaskPatchSchema",
3037
"TaskSchema",
38+
"AgeRatingBaseSchema",
39+
"AgeRatingCreateSchema",
40+
"AgeRatingUpdateSchema",
41+
"AgeRatingSchema",
3142
)

Diff for: tests/fixtures/schemas/age_rating.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Annotated
2+
3+
from annotated_types import MaxLen, MinLen
4+
from pydantic import BaseModel, ConfigDict
5+
6+
name_constrained = Annotated[
7+
str,
8+
MinLen(1),
9+
MaxLen(20),
10+
]
11+
12+
13+
class AgeRatingBaseSchema(BaseModel):
14+
model_config = ConfigDict(
15+
from_attributes=True,
16+
)
17+
name: str
18+
description: str
19+
20+
21+
class AgeRatingCreateSchema(AgeRatingBaseSchema):
22+
name: name_constrained
23+
24+
25+
class AgeRatingUpdateSchema(AgeRatingBaseSchema):
26+
name: name_constrained | None = None
27+
description: str | None = None
28+
29+
30+
class AgeRatingSchema(AgeRatingBaseSchema):
31+
"""
32+
Age Rating
33+
"""
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Any
2+
3+
from fastapi import FastAPI
4+
from httpx import AsyncClient
5+
from starlette import status
6+
7+
from tests.fixtures.models import AgeRating
8+
from tests.fixtures.schemas import AgeRatingBaseSchema
9+
10+
11+
async def test_get_user_with_bio_relation(
12+
app: FastAPI,
13+
client: AsyncClient,
14+
age_rating_g: AgeRating,
15+
):
16+
url = app.url_path_for("get_age-rating_list")
17+
response = await client.get(url)
18+
assert response.status_code == status.HTTP_200_OK
19+
response_data = response.json()
20+
assert "data" in response_data, response_data
21+
entities: list[dict[str, Any]] = response_data["data"]
22+
for entity in entities:
23+
assert entity["id"] == entity["attributes"]["name"]
24+
25+
rating_g_data = next(
26+
filter(lambda e: e["id"] == age_rating_g.id, entities),
27+
)
28+
expected_rating_g_data = AgeRatingBaseSchema.model_validate(age_rating_g).model_dump()
29+
assert rating_g_data["attributes"] == expected_rating_g_data

0 commit comments

Comments
 (0)