Skip to content

Commit

Permalink
test: 新增conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
amisadmin committed Mar 28, 2022
1 parent cdac7f6 commit 8d1b29f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 67 deletions.
11 changes: 11 additions & 0 deletions tests/test_crud/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

from tests.test_crud.main import app


@pytest.fixture(scope='session', autouse=True)
def startup():
import asyncio
# asyncio.run(app.router.startup())
loop = asyncio.get_event_loop()
loop.run_until_complete(app.router.startup())
2 changes: 0 additions & 2 deletions tests/test_crud/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ async def startup():
await conn.run_sync(SQLModel.metadata.drop_all)
await conn.run_sync(SQLModel.metadata.create_all)


asyncio.run(startup())
129 changes: 64 additions & 65 deletions tests/test_crud/test_crud.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,75 @@
from unittest import TestCase
from fastapi.testclient import TestClient
from tests.test_crud.main import app

client = TestClient(app)


class TestSQLModelCrud(TestCase):
def test_register_crud():
response = client.get('/openapi.json')

def test_register_crud(self):
response = client.get('/openapi.json')
# test paths
paths = response.json()['paths']
assert '/category/list' in paths
assert '/category/item' in paths
assert '/category/item/{item_id}' in paths
assert '/tag/list' in paths
assert '/tag/item' in paths
assert '/tag/item/{item_id}' in paths

# test paths
paths = response.json()['paths']
assert '/category/list' in paths
assert '/category/item' in paths
assert '/category/item/{item_id}' in paths
assert '/tag/list' in paths
assert '/tag/item' in paths
assert '/tag/item/{item_id}' in paths
# test schemas
schemas = response.json()['components']['schemas']
assert 'Category' in schemas
assert 'CategoryFilter' in schemas
assert 'CategoryList' in schemas
assert 'CategoryUpdate' in schemas
assert 'ItemListSchema_CategoryList_' in schemas
assert 'TagFilter' in schemas
assert 'TagList' in schemas
assert 'TagUpdate' in schemas

# test schemas
schemas = response.json()['components']['schemas']
assert 'Category' in schemas
assert 'CategoryFilter' in schemas
assert 'CategoryList' in schemas
assert 'CategoryUpdate' in schemas
assert 'ItemListSchema_CategoryList_' in schemas
assert 'TagFilter' in schemas
assert 'TagList' in schemas
assert 'TagUpdate' in schemas

def test_crud_one(self):
# create one
res = client.post('/category/item', json={"name": 'category_name', "description": "description"})
category = res.json().get('data')
assert category['id'] > 0, res.json()
assert category['name'] == 'category_name', res.json()
# update one
res = client.put(f'/category/item/{category["id"]}', json={"name": "category_name1"})
count = res.json()['data']
assert count == 1
# read one
res = client.get(f'/category/item/{category["id"]}')
category_new = res.json()['data']
assert category_new['id'] == category["id"]
assert category_new['name'] == "category_name1"
# delete one
res = client.delete(f'/category/item/{category["id"]}')
count = res.json()['data']
assert count == 1, res.text
def test_crud_one():
# create one
res = client.post('/category/item', json={"name": 'category_name', "description": "description"})
category = res.json().get('data')
assert category['id'] > 0, res.json()
assert category['name'] == 'category_name', res.json()
# update one
res = client.put(f'/category/item/{category["id"]}', json={"name": "category_name1"})
count = res.json()['data']
assert count == 1
# read one
res = client.get(f'/category/item/{category["id"]}')
category_new = res.json()['data']
assert category_new['id'] == category["id"]
assert category_new['name'] == "category_name1"
# delete one
res = client.delete(f'/category/item/{category["id"]}')
count = res.json()['data']
assert count == 1, res.text

def test_crud_bulk(self):
# create one
category_name = 'category_name'
count = 2
# create bulk
categorys = [{'id': i + 1, "name": f'{category_name}_{i}', "description": "description"} for i in range(count)]
res = client.post('/category/item', json=categorys)
assert res.json()['data'] == count, res.json()
# update bulk
item_ids = ','.join([str(category['id']) for category in categorys])
res = client.put(f'/category/item/{item_ids}', json={"description": "description2"})
assert res.json()['data'] == count, res.json()
# read bulk
res = client.get(f'/category/item/{item_ids}')
categorys_new = res.json()['data']
assert len(categorys_new) == count, res.json()
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()
# delete one
res = client.delete(f'/category/item/{item_ids}')
assert res.json()['data'] == count, res.json()

def test_crud_bulk():
# create one
category_name = 'category_name'
count = 2
# create bulk
categorys = [{'id': i + 1, "name": f'{category_name}_{i}', "description": "description"} for i in range(count)]
res = client.post('/category/item', json=categorys)
assert res.json()['data'] == count, res.json()
# update bulk
item_ids = ','.join([str(category['id']) for category in categorys])
res = client.put(f'/category/item/{item_ids}', json={"description": "description2"})
assert res.json()['data'] == count, res.json()
# read bulk
res = client.get(f'/category/item/{item_ids}')
categorys_new = res.json()['data']
assert len(categorys_new) == count, res.json()
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()
# delete one
res = client.delete(f'/category/item/{item_ids}')
assert res.json()['data'] == count, res.json()

0 comments on commit 8d1b29f

Please sign in to comment.