Skip to content

Commit d5c65b8

Browse files
committed
added bug test representation
1 parent a447605 commit d5c65b8

File tree

5 files changed

+114
-4
lines changed

5 files changed

+114
-4
lines changed

fastapi_jsonapi/data_layers/filtering/sqlalchemy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from fastapi_jsonapi.data_typing import TypeModel, TypeSchema
2727
from fastapi_jsonapi.exceptions import InvalidFilters, InvalidType
2828
from fastapi_jsonapi.exceptions.json_api import HTTPException
29-
from fastapi_jsonapi.schema import get_model_field, get_relationships
29+
from fastapi_jsonapi.schema import JSONAPISchemaIntrospectionError, get_model_field, get_relationships
3030

3131
log = logging.getLogger(__name__)
3232

@@ -288,7 +288,10 @@ def get_model_column(
288288
schema: Type[TypeSchema],
289289
field_name: str,
290290
) -> InstrumentedAttribute:
291-
model_field = get_model_field(schema, field_name)
291+
try:
292+
model_field = get_model_field(schema, field_name)
293+
except JSONAPISchemaIntrospectionError as e:
294+
raise InvalidFilters(str(e))
292295

293296
try:
294297
return getattr(model, model_field)

fastapi_jsonapi/schema.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ class JSONAPIResultDetailSchema(BaseJSONAPIResultSchema):
122122
]
123123

124124

125+
class JSONAPISchemaIntrospectionError(Exception):
126+
pass
127+
128+
125129
def get_model_field(schema: Type["TypeSchema"], field: str) -> str:
126130
"""
127131
Get the model field of a schema field.
@@ -145,7 +149,7 @@ class ComputerSchema(pydantic_base):
145149
schema=schema.__name__,
146150
field=field,
147151
)
148-
raise Exception(msg)
152+
raise JSONAPISchemaIntrospectionError(msg)
149153
return field
150154

151155

tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,5 +387,6 @@ class Delta(Base):
387387
__tablename__ = "delta"
388388

389389
id = Column(Integer, primary_key=True, autoincrement=True)
390+
name = Column(String)
390391
gammas: List["Gamma"] = relationship("Gamma", back_populates="delta", lazy="noload")
391392
betas: List["Beta"] = relationship("Beta", secondary="beta_delta_binding", back_populates="deltas", lazy="noload")

tests/schemas.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ class GammaSchema(BaseModel):
466466

467467

468468
class DeltaSchema(BaseModel):
469+
name: str
469470
gammas: Optional["GammaSchema"] = Field(
470471
None,
471472
relationship=RelationshipInfo(

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919

2020
from fastapi_jsonapi.views.view_base import ViewBase
2121
from tests.common import is_postgres_tests
22-
from tests.fixtures.app import build_app_custom
22+
from tests.fixtures.app import ResourceInfoDTO, build_app_custom, build_custom_app_by_schemas
2323
from tests.fixtures.entities import build_workplace, create_user
2424
from tests.misc.utils import fake
2525
from tests.models import (
26+
Alpha,
27+
Beta,
2628
Computer,
2729
ContainsTimestamp,
2830
CustomUUIDItem,
31+
Delta,
32+
Gamma,
2933
Post,
3034
PostComment,
3135
SelfRelationship,
@@ -34,8 +38,12 @@
3438
Workplace,
3539
)
3640
from tests.schemas import (
41+
AlphaSchema,
42+
BetaSchema,
3743
CustomUserAttributesSchema,
3844
CustomUUIDItemAttributesSchema,
45+
DeltaSchema,
46+
GammaSchema,
3947
PostAttributesBaseSchema,
4048
PostCommentAttributesBaseSchema,
4149
SelfRelationshipSchema,
@@ -2740,6 +2748,99 @@ async def test_join_by_relationships_works_correctly_with_many_filters_for_one_f
27402748
"meta": {"count": 0, "totalPages": 1},
27412749
}
27422750

2751+
async def test_join_by_relationships_for_one_model_by_different_join_chains(
2752+
self,
2753+
async_session: AsyncSession,
2754+
):
2755+
app = build_custom_app_by_schemas(
2756+
[
2757+
ResourceInfoDTO(
2758+
path="/alpha",
2759+
resource_type="alpha",
2760+
model=Alpha,
2761+
schema_=AlphaSchema,
2762+
),
2763+
ResourceInfoDTO(
2764+
path="/beta",
2765+
resource_type="beta",
2766+
model=Beta,
2767+
schema_=BetaSchema,
2768+
),
2769+
ResourceInfoDTO(
2770+
path="/gamma",
2771+
resource_type="gamma",
2772+
model=Gamma,
2773+
schema_=GammaSchema,
2774+
),
2775+
ResourceInfoDTO(
2776+
path="/delta",
2777+
resource_type="delta",
2778+
model=Delta,
2779+
schema_=DeltaSchema,
2780+
),
2781+
],
2782+
)
2783+
2784+
# acc_1 = Account(name="account-1")
2785+
# role_1 = Role(delta=acc_1)
2786+
# user_1 = User()
2787+
2788+
delta_1 = Delta(name="delta_1")
2789+
beta_1, beta_2 = Beta(), Beta()
2790+
2791+
gamma_1 = Gamma(delta=delta_1)
2792+
gamma_1.betas = [beta_1]
2793+
gamma_2 = Gamma(delta=delta_1)
2794+
gamma_2.betas = [beta_2]
2795+
delta_1.betas = [beta_1, beta_2]
2796+
2797+
delta_2 = Delta(name="delta_2")
2798+
beta_3 = Beta()
2799+
beta_4 = Beta()
2800+
gamma_3 = Gamma(delta=delta_2)
2801+
gamma_3.betas = [beta_3]
2802+
gamma_4 = Gamma(delta=delta_2)
2803+
gamma_4.betas = [beta_4]
2804+
delta_2.betas = [beta_3, beta_4]
2805+
2806+
alpha_1 = Alpha(beta=beta_1, gamma=gamma_3)
2807+
alpha_2 = Alpha(beta=beta_3, gamma=gamma_3)
2808+
2809+
async_session.add_all(
2810+
[
2811+
delta_1,
2812+
delta_2,
2813+
gamma_1,
2814+
gamma_2,
2815+
gamma_3,
2816+
gamma_4,
2817+
alpha_1,
2818+
alpha_2,
2819+
],
2820+
)
2821+
await async_session.commit()
2822+
2823+
async with AsyncClient(app=app, base_url="http://test") as client:
2824+
params = {
2825+
"filter": json.dumps(
2826+
[
2827+
{"name": "beta.gammas.delta.name", "op": "ilike", "val": delta_1.name},
2828+
{"name": "gamma.delta.name", "op": "ilike", "val": delta_2.name},
2829+
],
2830+
),
2831+
}
2832+
2833+
resource_type = "alpha"
2834+
url = app.url_path_for(f"get_{resource_type}_list")
2835+
response = await client.get(url, params=params)
2836+
2837+
assert response.status_code == status.HTTP_200_OK, response.text
2838+
assert response.json() == {
2839+
"data": [{"attributes": {}, "id": str(alpha_1.id), "type": "alpha"}],
2840+
"jsonapi": {"version": "1.0"},
2841+
"meta": {"count": 1, "totalPages": 1},
2842+
}
2843+
27432844

27442845
ASCENDING = ""
27452846
DESCENDING = "-"

0 commit comments

Comments
 (0)