Skip to content

Commit

Permalink
fix relation serialize issue when relation is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
voidZXL committed Aug 23, 2024
1 parent e08e359 commit 90f7724
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/server/app/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async def get_top_articles(cls, *pks):
articles: List[ArticleBase] = orm.Field('contents__article')
# test multi+fk

follower_names: List[str] = orm.Field('followers.username')
# @property
# def total_views(self) -> int:
# return sum([article.views for article in self.articles])
Expand Down
8 changes: 6 additions & 2 deletions tests/test_1_orm/test_schema_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_serialize_users(self, service):
assert len(res) == 2
assert res[0]["username"] == "alice"
assert res[0]["followers_num"] == 2
assert set(res[0].follower_names) == {'bob', 'jack'}
assert res[0]["followings_num"] == 1
assert set(res[0]["liked_slugs"]) == {"about-tech", "some-news", "big-shot"}
assert res[0]["@views"] == 103
Expand All @@ -41,6 +42,7 @@ def test_serialize_users(self, service):
sup = UserSchema.init(5)
assert len(sup.articles) == 0
assert sup.articles_num == 0
assert sup.follower_names == []

def test_scope_and_excludes(self):
from app.schema import UserSchema, UserQuery
Expand Down Expand Up @@ -87,8 +89,8 @@ def test_init_articles(self, service):
assert content.article.id == 1

def test_related_qs(self):
from app.schema import UserBase, ArticleSchema, UserQuery
from app.models import Article, Follow, User
from app.schema import UserBase, ArticleSchema
from app.models import Article, User
from typing import List, Optional
from utilmeta.core import orm
from django.db import models
Expand Down Expand Up @@ -155,6 +157,7 @@ async def test_async_init_users(self):
assert user.sum_views == 103
assert user.top_articles[0].author_tag["name"] == "alice"
assert user.top_articles[0].views == 103
assert set(user.follower_names) == {'bob', 'jack'}

# --------------
bob = await UserSchema.ainit(
Expand All @@ -170,6 +173,7 @@ async def test_async_init_users(self):
sup = UserSchema.init(5)
assert len(sup.articles) == 0
assert sup.articles_num == 0
assert sup.follower_names == []

@pytest.mark.asyncio
async def test_async_init_users_with_sync_query(self):
Expand Down
1 change: 1 addition & 0 deletions utilmeta/core/orm/fields/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def setup(self, options: utype.Options):
# if not self.model_field.is_exp:
# expression need to be isolated, otherwise multiple many included query will blow the query
self.isolated = True
self.related_single = False

elif not self.model_field.is_concrete:
self.isolated = True
Expand Down

0 comments on commit 90f7724

Please sign in to comment.