Skip to content

Commit d39673b

Browse files
committed
create tests for multi include case
1 parent 6cce98e commit d39673b

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,77 @@ async def test_many_to_many_load_inner_includes_to_parents(
598598
assert ("child", ViewBase.get_db_item_id(child_4)) not in included_data
599599

600600

601+
@mark.parametrize(
602+
"include, expected_relationships",
603+
[
604+
(
605+
["posts", "posts.user"],
606+
["user"],
607+
),
608+
(
609+
["posts", "posts.comments"],
610+
["comments"],
611+
),
612+
(
613+
["posts", "posts.user", "posts.comments"],
614+
["user", "comments"],
615+
),
616+
],
617+
)
618+
async def test_get_users_with_posts_and_inner_includes(
619+
app: FastAPI,
620+
client: AsyncClient,
621+
user_1: User,
622+
user_2: User,
623+
user_1_posts: list[PostComment],
624+
user_1_post_for_comments: Post,
625+
user_2_comment_for_one_u1_post: PostComment,
626+
include: list[str],
627+
expected_relationships: list[str],
628+
):
629+
"""
630+
Test if requesting `posts.user` and `posts.comments`
631+
returns posts with both `user` and `comments`
632+
"""
633+
assert user_1_posts
634+
assert user_2_comment_for_one_u1_post.author_id == user_2.id
635+
include_param = ",".join(include)
636+
resource_type = "user"
637+
url = app.url_path_for(f"get_{resource_type}_list")
638+
url = f"{url}?filter[name]={user_1.name}&include={include_param}"
639+
response = await client.get(url)
640+
assert response.status_code == status.HTTP_200_OK, response.text
641+
response_json = response.json()
642+
643+
result_data = response_json["data"]
644+
645+
assert result_data == [
646+
{
647+
"id": str(user_1.id),
648+
"type": resource_type,
649+
"attributes": UserAttributesBaseSchema.from_orm(user_1).dict(),
650+
"relationships": {
651+
"posts": {
652+
"data": [
653+
# relationship info
654+
{"id": str(p.id), "type": "post"}
655+
# for every post
656+
for p in user_1_posts
657+
],
658+
},
659+
},
660+
},
661+
]
662+
included_data = response_json["included"]
663+
664+
included_posts = [item for item in included_data if item["type"] == "post"]
665+
for post in included_posts:
666+
post_relationships = set(post.get("relationships", {}))
667+
assert post_relationships.intersection(expected_relationships) == set(
668+
expected_relationships,
669+
), f"Expected relationships {expected_relationships} not found in post {post['id']}"
670+
671+
601672
async def test_method_not_allowed(app: FastAPI, client: AsyncClient):
602673
url = app.url_path_for("get_user_list")
603674
res = await client.put(url, json={})

0 commit comments

Comments
 (0)