|
7 | 7 | from fastapi_amis_admin.crud.parser import LabelField, PropertyField |
8 | 8 | from fastapi_amis_admin.models import Field |
9 | 9 | from tests.conftest import async_db as db |
10 | | -from tests.models import Article, Category, User |
| 10 | +from tests.models import Article, ArticleContent, Category, User |
11 | 11 |
|
12 | 12 |
|
13 | 13 | async def test_pk_name(app: FastAPI, async_client: AsyncClient, fake_users): |
@@ -302,3 +302,43 @@ class ArticleCrud(SQLModelCrud): |
302 | 302 | assert items["category"]["name"] == "Category_1" |
303 | 303 | assert "content_text" in items |
304 | 304 | # assert items["user"]["username"] == "User_1" |
| 305 | + |
| 306 | + |
| 307 | +async def test_update_fields_relationship(app: FastAPI, async_client: AsyncClient, fake_articles, async_session): |
| 308 | + class ArticleCrud(SQLModelCrud): |
| 309 | + router_prefix = "/article" |
| 310 | + update_exclude = {"content": {"id"}} |
| 311 | + update_fields = [ |
| 312 | + Article.description, |
| 313 | + PropertyField(name="content", type_=ArticleContent), # Relationship attribute |
| 314 | + ] |
| 315 | + |
| 316 | + ins = ArticleCrud(Article, db.engine).register_crud() |
| 317 | + |
| 318 | + app.include_router(ins.router) |
| 319 | + |
| 320 | + # test schemas |
| 321 | + assert "id" not in ins.schema_update.__fields__ |
| 322 | + assert "title" not in ins.schema_update.__fields__ |
| 323 | + assert "description" in ins.schema_update.__fields__ |
| 324 | + assert "content" in ins.schema_update.__fields__ |
| 325 | + |
| 326 | + # test api |
| 327 | + res = await async_client.put( |
| 328 | + "/article/item/1", |
| 329 | + json={ |
| 330 | + "title": "new_title", |
| 331 | + "description": "new_description", |
| 332 | + "content": { |
| 333 | + "id": 22, # will be ignored by `update_exclude` |
| 334 | + "content": "new_content", |
| 335 | + }, |
| 336 | + }, |
| 337 | + ) |
| 338 | + assert res.json()["data"] == 1 |
| 339 | + article = await async_session.get(Article, 1) |
| 340 | + assert article.title != "new_title" |
| 341 | + assert article.description == "new_description" |
| 342 | + |
| 343 | + content = await async_session.get(ArticleContent, 1) |
| 344 | + assert content.content == "new_content" |
0 commit comments