clean the meta, more tests, partial update of the docs
This commit is contained in:
@ -43,7 +43,7 @@ class DateFieldsModel(ormar.Model):
|
||||
updated_date: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
|
||||
|
||||
|
||||
class Category(ormar.Model, DateFieldsModel, AuditModel):
|
||||
class Category(DateFieldsModel, AuditModel):
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "categories"
|
||||
metadata = metadata
|
||||
@ -54,7 +54,7 @@ class Category(ormar.Model, DateFieldsModel, AuditModel):
|
||||
code: int = ormar.Integer()
|
||||
|
||||
|
||||
class Subject(ormar.Model, DateFieldsModel):
|
||||
class Subject(DateFieldsModel):
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "subjects"
|
||||
metadata = metadata
|
||||
@ -74,7 +74,7 @@ def create_test_database():
|
||||
|
||||
def test_field_redefining_raises_error():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
class WrongField(ormar.Model, DateFieldsModel): # pragma: no cover
|
||||
class WrongField(DateFieldsModel): # pragma: no cover
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "wrongs"
|
||||
metadata = metadata
|
||||
@ -86,7 +86,7 @@ def test_field_redefining_raises_error():
|
||||
|
||||
def test_model_subclassing_non_abstract_raises_error():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
class WrongField2(ormar.Model, DateFieldsModelNoSubclass): # pragma: no cover
|
||||
class WrongField2(DateFieldsModelNoSubclass): # pragma: no cover
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "wrongs"
|
||||
metadata = metadata
|
||||
|
||||
77
tests/test_inheritance_concrete_fastapi.py
Normal file
77
tests/test_inheritance_concrete_fastapi.py
Normal file
@ -0,0 +1,77 @@
|
||||
import datetime
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from fastapi import FastAPI
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
from tests.settings import DATABASE_URL
|
||||
from tests.test_inheritance_concrete import Category, Subject, metadata
|
||||
|
||||
app = FastAPI()
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
app.state.database = database
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup() -> None:
|
||||
database_ = app.state.database
|
||||
if not database_.is_connected:
|
||||
await database_.connect()
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown() -> None:
|
||||
database_ = app.state.database
|
||||
if database_.is_connected:
|
||||
await database_.disconnect()
|
||||
|
||||
|
||||
@app.post("/subjects/", response_model=Subject)
|
||||
async def create_item(item: Subject):
|
||||
return item
|
||||
|
||||
|
||||
@app.post("/categories/", response_model=Category)
|
||||
async def create_category(category: Category):
|
||||
await category.save()
|
||||
return category
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
def test_read_main():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
test_category = dict(name="Foo", code=123, created_by="Sam", updated_by="Max")
|
||||
test_subject = dict(name="Bar")
|
||||
|
||||
response = client.post(
|
||||
"/categories/", json=test_category
|
||||
)
|
||||
assert response.status_code == 200
|
||||
cat = Category(**response.json())
|
||||
assert cat.name == 'Foo'
|
||||
assert cat.created_by == 'Sam'
|
||||
assert cat.created_date is not None
|
||||
assert cat.id == 1
|
||||
|
||||
cat_dict = cat.dict()
|
||||
cat_dict['updated_date'] = cat_dict['updated_date'].strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||
cat_dict['created_date'] = cat_dict['created_date'].strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||
test_subject['category'] = cat_dict
|
||||
response = client.post(
|
||||
"/subjects/", json=test_subject
|
||||
)
|
||||
assert response.status_code == 200
|
||||
sub = Subject(**response.json())
|
||||
assert sub.name == 'Bar'
|
||||
assert sub.category.pk == cat.pk
|
||||
assert isinstance(sub.updated_date, datetime.datetime)
|
||||
77
tests/test_inheritance_mixins_fastapi.py
Normal file
77
tests/test_inheritance_mixins_fastapi.py
Normal file
@ -0,0 +1,77 @@
|
||||
import datetime
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from fastapi import FastAPI
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
from tests.settings import DATABASE_URL
|
||||
from tests.test_inheritance_mixins import Category, Subject, metadata
|
||||
|
||||
app = FastAPI()
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
app.state.database = database
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup() -> None:
|
||||
database_ = app.state.database
|
||||
if not database_.is_connected:
|
||||
await database_.connect()
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown() -> None:
|
||||
database_ = app.state.database
|
||||
if database_.is_connected:
|
||||
await database_.disconnect()
|
||||
|
||||
|
||||
@app.post("/subjects/", response_model=Subject)
|
||||
async def create_item(item: Subject):
|
||||
return item
|
||||
|
||||
|
||||
@app.post("/categories/", response_model=Category)
|
||||
async def create_category(category: Category):
|
||||
await category.save()
|
||||
return category
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
def test_read_main():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
test_category = dict(name="Foo", code=123, created_by="Sam", updated_by="Max")
|
||||
test_subject = dict(name="Bar")
|
||||
|
||||
response = client.post(
|
||||
"/categories/", json=test_category
|
||||
)
|
||||
assert response.status_code == 200
|
||||
cat = Category(**response.json())
|
||||
assert cat.name == 'Foo'
|
||||
assert cat.created_by == 'Sam'
|
||||
assert cat.created_date is not None
|
||||
assert cat.id == 1
|
||||
|
||||
cat_dict = cat.dict()
|
||||
cat_dict['updated_date'] = cat_dict['updated_date'].strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||
cat_dict['created_date'] = cat_dict['created_date'].strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||
test_subject['category'] = cat_dict
|
||||
response = client.post(
|
||||
"/subjects/", json=test_subject
|
||||
)
|
||||
assert response.status_code == 200
|
||||
sub = Subject(**response.json())
|
||||
assert sub.name == 'Bar'
|
||||
assert sub.category.pk == cat.pk
|
||||
assert isinstance(sub.updated_date, datetime.datetime)
|
||||
Reference in New Issue
Block a user