Skip to content

Commit 49d5d9b

Browse files
committed
tests coverage read custom model id field
1 parent 4fa471e commit 49d5d9b

File tree

8 files changed

+148
-2
lines changed

8 files changed

+148
-2
lines changed

tests/conftest.py

Lines changed: 1 addition & 0 deletions
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,

tests/fixtures/app.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@
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 (
43+
Alpha,
44+
Beta,
45+
CustomUUIDItem,
46+
Delta,
47+
Gamma,
48+
Task,
49+
AgeRating,
50+
)
4351
from .schemas import (
4452
AlphaSchema,
4553
BetaSchema,
@@ -49,6 +57,9 @@
4957
TaskInSchema,
5058
TaskPatchSchema,
5159
TaskSchema,
60+
AgeRatingSchema,
61+
AgeRatingCreateSchema,
62+
AgeRatingUpdateSchema,
5263
)
5364
from .views import ViewBaseGeneric
5465

@@ -162,6 +173,17 @@ def add_routers(app_plain: FastAPI):
162173
schema_in_patch=UserPatchSchema,
163174
schema_in_post=UserInSchema,
164175
)
176+
builder.add_resource(
177+
path="/age-ratings",
178+
tags=["Age Ratings"],
179+
resource_type="age-rating",
180+
view=ViewBaseGeneric,
181+
model=AgeRating,
182+
schema=AgeRatingSchema,
183+
schema_in_post=AgeRatingCreateSchema,
184+
schema_in_patch=AgeRatingUpdateSchema,
185+
model_id_field_name="name",
186+
)
165187
builder.initialize()
166188

167189
return app_plain

tests/fixtures/entities.py

Lines changed: 23 additions & 1 deletion
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()

tests/fixtures/models/__init__.py

Lines changed: 2 additions & 0 deletions
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",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 BaseWithoutId
9+
10+
11+
class AgeRating(BaseWithoutId):
12+
__tablename__ = "age_rating"
13+
14+
name: Mapped[str] = mapped_column(
15+
String(20),
16+
Identity(always=False),
17+
primary_key=True,
18+
)
19+
description: Mapped[str] = mapped_column(
20+
Text(),
21+
default="",
22+
server_default="",
23+
)
24+
25+
def __str__(self) -> str:
26+
return self.name

tests/fixtures/schemas/__init__.py

Lines changed: 11 additions & 0 deletions
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
)
Lines changed: 33 additions & 0 deletions
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+
"""
Lines changed: 29 additions & 0 deletions
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.name, 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)