Skip to content

Commit 17c1824

Browse files
committed
added test for relationship to self
1 parent e8c92b2 commit 17c1824

3 files changed

Lines changed: 128 additions & 18 deletions

File tree

tests/models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,22 @@ def process_result_value(self, value, dialect):
231231
return UUID(value)
232232

233233

234-
class MiscCases(Base):
234+
class IdCast(Base):
235235
id = Column(UUIDType, primary_key=True)
236+
237+
238+
class SelfRelationship(Base):
239+
id = Column(Integer, primary_key=True)
240+
name = Column(String)
241+
self_relationship_id = Column(
242+
Integer,
243+
ForeignKey(
244+
"selfrelationships.id",
245+
name="fk_self_relationship_id",
246+
ondelete="CASCADE",
247+
onupdate="CASCADE",
248+
),
249+
nullable=True,
250+
)
251+
# parent = relationship("SelfRelationship", back_populates="s")
252+
self_relationship = relationship("SelfRelationship", remote_side=[id])

tests/schemas.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,14 @@ class Config:
332332
id: int
333333

334334

335-
class MiscCasesAttributesSchema(BaseModel):
336-
pass
335+
class IdCastSchema(BaseModel):
336+
id: UUID = Field(client_can_set_id=True)
337337

338338

339-
class MiscCasesSchema(MiscCasesAttributesSchema):
340-
id: UUID = Field(client_can_set_id=True)
339+
class SelfRelationshipSchema(BaseModel):
340+
name: str
341+
self_relationship: Optional["SelfRelationshipSchema"] = Field(
342+
relationship=RelationshipInfo(
343+
resource_type="self_relationship",
344+
),
345+
)

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616
from tests.misc.utils import fake
1717
from tests.models import (
1818
Computer,
19-
MiscCases,
19+
IdCast,
2020
Post,
2121
PostComment,
22+
SelfRelationship,
2223
User,
2324
UserBio,
2425
Workplace,
2526
)
2627
from tests.schemas import (
27-
MiscCasesSchema,
28+
IdCastSchema,
2829
PostAttributesBaseSchema,
2930
PostCommentAttributesBaseSchema,
31+
SelfRelationshipSchema,
3032
UserAttributesBaseSchema,
3133
UserBioBaseSchema,
3234
UserInSchemaAllowIdOnPost,
@@ -822,20 +824,20 @@ async def test_create_user_and_fetch_data(self, client: AsyncClient):
822824
assert response_data["data"]["attributes"] == create_user_body["data"]["attributes"]
823825
assert response_data["data"]["id"] == user_id
824826

825-
def _build_app_custom(self, schema, schema_in_post, model=None) -> FastAPI:
827+
def _build_app_custom(self, schema, schema_in_post, model, resource_type: str = "misc") -> FastAPI:
826828
router: APIRouter = APIRouter()
827829

828830
RoutersJSONAPI(
829831
router=router,
830-
path="/users",
831-
tags=["User"],
832+
path="/misc",
833+
tags=["Misc"],
832834
class_detail=DetailViewBaseGeneric,
833835
class_list=ListViewBaseGeneric,
834836
schema=schema,
835-
resource_type="user",
837+
resource_type="misc",
836838
schema_in_patch=UserPatchSchema,
837839
schema_in_post=schema_in_post,
838-
model=model or User,
840+
model=model,
839841
)
840842

841843
app = build_app_plain()
@@ -844,7 +846,12 @@ def _build_app_custom(self, schema, schema_in_post, model=None) -> FastAPI:
844846
return app
845847

846848
async def test_create_id_by_client(self):
847-
app = self._build_app_custom(UserSchema, UserInSchemaAllowIdOnPost)
849+
app = self._build_app_custom(
850+
UserSchema,
851+
UserInSchemaAllowIdOnPost,
852+
User,
853+
"user",
854+
)
848855

849856
new_id = str(fake.pyint(100, 999))
850857
attrs = UserAttributesBaseSchema(
@@ -860,7 +867,7 @@ async def test_create_id_by_client(self):
860867
}
861868

862869
async with AsyncClient(app=app, base_url="http://test") as client:
863-
res = await client.post("/users", json=create_user_body)
870+
res = await client.post("/misc", json=create_user_body)
864871
assert res.status_code == status.HTTP_201_CREATED, res.text
865872
assert res.json() == {
866873
"data": {
@@ -873,25 +880,106 @@ async def test_create_id_by_client(self):
873880
}
874881

875882
async def test_create_id_by_client_uuid_type(self):
876-
app = self._build_app_custom(MiscCasesSchema, MiscCasesSchema, MiscCases)
883+
app = self._build_app_custom(IdCastSchema, IdCastSchema, IdCast)
877884

878885
new_id = str(uuid4())
879-
create_user_body = {
886+
create_body = {
880887
"data": {
881888
"attributes": {},
882889
"id": new_id,
883890
},
884891
}
885892

886893
async with AsyncClient(app=app, base_url="http://test") as client:
887-
res = await client.post("/users", json=create_user_body)
894+
res = await client.post("/misc", json=create_body)
888895
assert res.status_code == status.HTTP_201_CREATED, res.text
889896
assert res.json() == {
890897
"data": {
891898
"attributes": {},
892899
"id": new_id,
893-
"type": "user",
900+
"type": "misc",
901+
},
902+
"jsonapi": {"version": "1.0"},
903+
"meta": None,
904+
}
905+
906+
async def test_create_with_relationship_to_the_same_table(self):
907+
app = self._build_app_custom(
908+
SelfRelationshipSchema,
909+
SelfRelationshipSchema,
910+
SelfRelationship,
911+
)
912+
913+
async with AsyncClient(app=app, base_url="http://test") as client:
914+
create_body = {
915+
"data": {
916+
"attributes": {
917+
"name": "parent",
918+
},
919+
},
920+
}
921+
922+
res = await client.post("/misc", json=create_body)
923+
assert res.status_code == status.HTTP_201_CREATED, res.text
924+
925+
response_json = res.json()
926+
assert response_json["data"]
927+
assert (parent_object_id := response_json["data"].get("id"))
928+
assert response_json == {
929+
"data": {
930+
"attributes": {
931+
"name": "parent",
932+
},
933+
"id": parent_object_id,
934+
"relationships": None,
935+
"type": "self_relationship",
936+
},
937+
"jsonapi": {"version": "1.0"},
938+
"meta": None,
939+
}
940+
941+
create_with_relationship_body = {
942+
"data": {
943+
"attributes": {
944+
"name": "child",
945+
},
946+
"relationships": {
947+
"self_relationship": {
948+
"data": {
949+
"type": "self_relationship",
950+
"id": parent_object_id,
951+
},
952+
},
953+
},
894954
},
955+
}
956+
res = await client.post("/misc?include=self_relationship", json=create_with_relationship_body)
957+
assert res.status_code == status.HTTP_201_CREATED, res.text
958+
959+
response_json = res.json()
960+
assert response_json["data"]
961+
assert (child_object_id := response_json["data"].get("id"))
962+
assert res.json() == {
963+
"data": {
964+
"attributes": {"name": "child"},
965+
"id": child_object_id,
966+
"relationships": {
967+
"self_relationship": {
968+
"data": {
969+
"id": parent_object_id,
970+
"type": "self_relationship",
971+
},
972+
},
973+
},
974+
"type": "self_relationship",
975+
},
976+
"included": [
977+
{
978+
"attributes": {"name": "parent"},
979+
"id": parent_object_id,
980+
"type": "self_relationship",
981+
},
982+
],
895983
"jsonapi": {"version": "1.0"},
896984
"meta": None,
897985
}

0 commit comments

Comments
 (0)