Skip to content

Commit

Permalink
test: 新增筛选过滤数据测试
Browse files Browse the repository at this point in the history
  • Loading branch information
amisadmin committed Apr 4, 2022
1 parent 1822791 commit 1c12ccf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 0 additions & 1 deletion tests/test_crud/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from fastapi import FastAPI
from sqlmodel import SQLModel
import asyncio
from tests.test_crud.models import Category, Tag
from tests.test_crud.db import session_factory, engine
from fastapi_amis_admin.crud import SQLModelCrud
Expand Down
4 changes: 3 additions & 1 deletion tests/test_crud/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from typing import Optional, List
from sqlalchemy import Column, String, Text
from sqlmodel import SQLModel, Field, Relationship
Expand All @@ -8,7 +9,7 @@ class Category(SQLModel, table=True):
name: str = Field(title='CategoryName', sa_column=Column(String(100), unique=True, index=True, nullable=False))
description: str = Field(default='', title='CategoryDescription')
articles: List["Article"] = Relationship(back_populates="category")

create_time: datetime = Field(default_factory=datetime.utcnow, title='创建时间')

class ArticleTagLink(SQLModel, table=True):
tag_id: Optional[int] = Field(
Expand Down Expand Up @@ -41,3 +42,4 @@ class Article(SQLModel, table=True):
content_id: Optional[int] = Field(default=None, foreign_key="articlecontent.id", title='ArticleContentId')
content: Optional[ArticleContent] = Relationship(back_populates="article")
tags: List[Tag] = Relationship(back_populates="articles", link_model=ArticleTagLink)
create_time: datetime = Field(default_factory=datetime.utcnow, title='创建时间')
35 changes: 31 additions & 4 deletions tests/test_crud/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def test_crud_one():
def test_crud_bulk():
# create one
category_name = 'category_name'
count = 2
count = 3
# create bulk
categorys = [{'id': i + 1, "name": f'{category_name}_{i}', "description": "description"} for i in range(count)]
categorys = [{'id': i + 1, "name": f'{category_name}_{i}', "description": "description","create_time":f"2022-01-0{i+1} 00:00:00"} for i in range(count)]
res = client.post('/category/item', json=categorys)
assert res.json()['data'] == count, res.json()
# update bulk
Expand All @@ -68,8 +68,35 @@ def test_crud_bulk():
assert categorys_new[0]['description'] == "description2"
# list
res = client.post('/category/list')
categorys_new2 = res.json()['data']['items']
assert len(categorys_new2) == count, res.json()
categorys_new = res.json()['data']['items']
assert len(categorys_new) == count, res.json()

res = client.post('/category/list',json={"id":1})
categorys_new = res.json()['data']['items']
assert categorys_new[0]['id'] == 1, res.json()

res = client.post('/category/list',json={"name":"category_name_1"})
categorys_new = res.json()['data']['items']
assert categorys_new[0]['name'] == "category_name_1", res.json()

res = client.post('/category/list', json={"id": "[>]1"})
assert len(res.json()['data']['items']) == 2, res.json()

res = client.post('/category/list', json={"id": "[*]1,3"})
assert len(res.json()['data']['items']) == 2, res.json()

res = client.post('/category/list', json={"id": "[-]2,3"})
assert len(res.json()['data']['items']) == 2, res.json()

res = client.post('/category/list', json={"name": "[~]category_name%"})
assert len(res.json()['data']['items']) == 3, res.json()

res = client.post('/category/list', json={"create_time": "[-]2022-01-02 00:00:00,2022-01-04 00:00:00"})
assert len(res.json()['data']['items']) == 2, res.json()

res = client.post('/category/list', json={"create_time": "[-]2022-01-04 00:00:00,2022-01-05 00:00:00"})
assert len(res.json()['data']['items']) == 0, res.json()

# delete one
res = client.delete(f'/category/item/{item_ids}')
assert res.json()['data'] == count, res.json()

0 comments on commit 1c12ccf

Please sign in to comment.