Skip to content

Commit c68393f

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

File tree

8 files changed

+150
-2
lines changed

8 files changed

+150
-2
lines changed

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
refresh_db,
2222
)
2323
from tests.fixtures.entities import ( # noqa
24+
age_rating_g,
2425
child_1,
2526
child_2,
2627
child_3,

tests/fixtures/app.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,19 @@
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+
AgeRating,
44+
Alpha,
45+
Beta,
46+
CustomUUIDItem,
47+
Delta,
48+
Gamma,
49+
Task,
50+
)
4351
from .schemas import (
52+
AgeRatingCreateSchema,
53+
AgeRatingSchema,
54+
AgeRatingUpdateSchema,
4455
AlphaSchema,
4556
BetaSchema,
4657
CustomUUIDItemSchema,
@@ -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: 26 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,10 @@
1617
Workplace,
1718
)
1819
from tests.common import is_postgres_tests
19-
from tests.fixtures.models import Task
20+
from tests.fixtures.models import (
21+
AgeRating,
22+
Task,
23+
)
2024
from tests.misc.utils import fake
2125

2226

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

tests/fixtures/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from tests.fixtures.models.age_rating import AgeRating
12
from tests.fixtures.models.alpha import Alpha
23
from tests.fixtures.models.beta import Beta
34
from tests.fixtures.models.beta_delta_binding import BetaDeltaBinding
@@ -11,6 +12,7 @@
1112
from tests.fixtures.models.task import Task
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from .age_rating import (
2+
AgeRatingBaseSchema,
3+
AgeRatingCreateSchema,
4+
AgeRatingSchema,
5+
AgeRatingUpdateSchema,
6+
)
17
from .alpha import AlphaSchema
28
from .beta import BetaSchema
39
from .cascade_case import CascadeCaseSchema
@@ -16,6 +22,10 @@
1622
)
1723

1824
__all__ = (
25+
"AgeRatingBaseSchema",
26+
"AgeRatingCreateSchema",
27+
"AgeRatingSchema",
28+
"AgeRatingUpdateSchema",
1929
"AlphaSchema",
2030
"BetaSchema",
2131
"CascadeCaseSchema",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Annotated, Optional
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: Optional[name_constrained] = None
27+
description: Optional[str] = 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)