added hack to pass as pydantic model in fastapi, tests for fastapi

This commit is contained in:
collerek
2020-08-04 21:37:25 +02:00
parent 345fd227d1
commit eb99f28431
8 changed files with 282 additions and 13 deletions

View File

@ -0,0 +1,42 @@
import json
from typing import Optional
import databases
import pydantic
import sqlalchemy
from fastapi import FastAPI
from fastapi.testclient import TestClient
app = FastAPI()
import orm
from tests.settings import DATABASE_URL
database = databases.Database(DATABASE_URL, force_rollback=True)
metadata = sqlalchemy.MetaData()
class Item(orm.Model):
__tablename__ = "users"
__metadata__ = metadata
__database__ = database
id = orm.Integer(primary_key=True)
name = orm.String(length=100)
@app.post("/items/", response_model=Item)
async def create_item(item: Item):
return item
client = TestClient(app)
def test_read_main():
response = client.post("/items/", json={'name': 'test', 'id': 1})
print(response.json())
assert response.status_code == 200
assert response.json() == {'name': 'test', 'id': 1}
item = Item(**response.json())
assert item.id == 1