Skip to content

Commit d2517d5

Browse files
committed
perf: Test the list type PropertyField.
1 parent 1374901 commit d2517d5

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

fastapi_amis_admin/crud/_sqlmodel.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ def _create_schema_filter(self) -> Type[SchemaFilterT]:
305305
def _create_schema_read(self) -> Type[SchemaReadT]:
306306
if self.schema_read:
307307
return self.schema_read
308-
if not self.read_fields:
309-
return super()._create_schema_read()
308+
self.read_fields = self.read_fields or self.schema_model.__fields__.values()
310309
self.read_fields = self.parser.filter_insfield(self.read_fields, save_class=(ModelField,))
311310
modelfields = [self.parser.get_modelfield(ins, deepcopy=True) for ins in self.read_fields]
312311
return schema_create_by_modelfield(f"{self.schema_name_prefix}Read", modelfields, orm_mode=True)
@@ -343,7 +342,7 @@ def _create_schema_create(self) -> Type[SchemaCreateT]:
343342

344343
def read_item(self, obj: ModelT) -> SchemaReadT:
345344
"""read database data and parse to schema_read"""
346-
parse = self.schema_read.from_orm if self.schema_read.Config.orm_mode else self.schema_read.parse_obj
345+
parse = self.schema_read.from_orm if getattr(self.schema_read.Config, "orm_mode", False) else self.schema_read.parse_obj
347346
return parse(obj)
348347

349348
def update_item(self, obj: ModelT, values: Dict[str, Any]) -> None:
@@ -352,6 +351,8 @@ def update_item(self, obj: ModelT, values: Dict[str, Any]) -> None:
352351
if isinstance(v, dict) and hasattr(obj, k):
353352
# Relational attributes, nested;such as: setattr(article.content, "body", "new body")
354353
self.update_item(getattr(obj, k), v)
354+
elif isinstance(v, list): # todo
355+
pass
355356
else:
356357
setattr(obj, k, v)
357358

tests/test_crud/conftest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sqlmodel import SQLModel
88

99
from tests.conftest import async_db as db
10-
from tests.models import Article, ArticleContent, Category, User
10+
from tests.models import Article, ArticleContent, ArticleTagLink, Category, Tag, User
1111

1212
pytestmark = pytest.mark.asyncio
1313

@@ -72,3 +72,15 @@ async def fake_articles(async_session, fake_users, fake_categorys, fake_article_
7272
async_session.add_all(data)
7373
await async_session.commit()
7474
return data
75+
76+
77+
@pytest.fixture
78+
async def fake_article_tags(async_session, fake_articles) -> List[Tag]:
79+
# add tags
80+
data = [Tag(id=i, name=f"Tag_{i}") for i in range(1, 6)]
81+
async_session.add_all(data)
82+
await async_session.commit()
83+
# add article_tag_link
84+
async_session.add_all([ArticleTagLink(article_id=i, tag_id=i) for i in range(1, 6)])
85+
await async_session.commit()
86+
return data

tests/test_crud/test_SQLModelCrud_fields.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import List
2+
13
from fastapi import FastAPI
24
from httpx import AsyncClient
35
from sqlmodel.sql.expression import Select
@@ -7,7 +9,7 @@
79
from fastapi_amis_admin.crud.parser import LabelField, PropertyField
810
from fastapi_amis_admin.models import Field
911
from tests.conftest import async_db as db
10-
from tests.models import Article, ArticleContent, Category, User
12+
from tests.models import Article, ArticleContent, Category, Tag, User
1113

1214

1315
async def test_pk_name(app: FastAPI, async_client: AsyncClient, fake_users):
@@ -273,7 +275,7 @@ class ArticleCrud(SQLModelCrud):
273275
assert items["description"] == "Description_1"
274276

275277

276-
async def test_read_fields_relationship(app: FastAPI, async_client: AsyncClient, fake_articles):
278+
async def test_read_fields_relationship(app: FastAPI, async_client: AsyncClient, fake_articles, fake_article_tags):
277279
class ArticleCrud(SQLModelCrud):
278280
router_prefix = "/article"
279281
read_fields = [
@@ -282,6 +284,7 @@ class ArticleCrud(SQLModelCrud):
282284
PropertyField(name="category", type_=Category), # Relationship attribute
283285
# Article.category, # Relationship todo support
284286
PropertyField(name="content_text", type_=str), # property attribute
287+
PropertyField(name="tags", type_=List[Tag]), # property attribute
285288
]
286289

287290
ins = ArticleCrud(Article, db.engine).register_crud()
@@ -293,15 +296,15 @@ class ArticleCrud(SQLModelCrud):
293296
assert "title" in ins.schema_read.__fields__
294297
assert "description" in ins.schema_read.__fields__
295298
assert "category" in ins.schema_read.__fields__
299+
assert "tags" in ins.schema_read.__fields__
296300
# test api
297301
res = await async_client.get("/article/item/1")
298302
items = res.json()["data"]
299-
print(items)
300303
assert "id" not in items
301304
assert "category" in items
302305
assert items["category"]["name"] == "Category_1"
303306
assert "content_text" in items
304-
# assert items["user"]["username"] == "User_1"
307+
assert items["tags"][0]["name"] == "Tag_1"
305308

306309

307310
async def test_update_fields_relationship(app: FastAPI, async_client: AsyncClient, fake_articles, async_session):

tests/test_crud/test_SQLModelCrud_schemas.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import List, Optional
22

33
from fastapi import FastAPI
44
from httpx import AsyncClient
@@ -7,7 +7,7 @@
77

88
from fastapi_amis_admin.crud import SQLModelCrud
99
from tests.conftest import async_db as db
10-
from tests.models import Article, ArticleContent, Category, User
10+
from tests.models import Article, ArticleContent, Category, Tag, User
1111

1212

1313
async def test_schema_update(app: FastAPI, async_client: AsyncClient, fake_users):
@@ -152,14 +152,15 @@ class UserCrud(SQLModelCrud):
152152
assert items
153153

154154

155-
async def test_schema_read_relationship(app: FastAPI, async_client: AsyncClient, fake_articles):
155+
async def test_schema_read_relationship(app: FastAPI, async_client: AsyncClient, fake_articles, fake_article_tags):
156156
class ArticleRead(SQLModel): # must be SQLModel, not BaseModel
157157
id: int
158158
title: str
159159
description: str
160160
category: Optional[Category] = None # Relationship
161161
content: Optional[ArticleContent] = None # Relationship
162162
user: Optional[User] = None # Relationship
163+
tags: List[Tag] = [] # Relationship
163164

164165
class ArticleCrud(SQLModelCrud):
165166
router_prefix = "/article"
@@ -176,6 +177,7 @@ class ArticleCrud(SQLModelCrud):
176177
assert schemas["ArticleRead"]["properties"]["category"]["$ref"] == "#/components/schemas/Category"
177178
assert "content" in schemas["ArticleRead"]["properties"]
178179
assert "user" in schemas["ArticleRead"]["properties"]
180+
assert "tags" in schemas["ArticleRead"]["properties"]
179181

180182
# test api
181183
res = await async_client.get("/article/item/1")
@@ -187,6 +189,7 @@ class ArticleCrud(SQLModelCrud):
187189
assert items["category"]["id"] == 1
188190
assert items["content"]["id"] == 1
189191
assert items["user"]["id"] == 1
192+
assert items["tags"][0]["id"] == 1
190193

191194

192195
async def test_schema_update_relationship(app: FastAPI, async_client: AsyncClient, fake_articles, async_session):

0 commit comments

Comments
 (0)