more tests for excluding parent fields, finished docs
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
@ -7,8 +6,6 @@ import sqlalchemy as sa
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
import ormar
|
||||
from ormar import ModelDefinitionError, property_field
|
||||
from ormar.exceptions import ModelError
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
metadata = sa.MetaData()
|
||||
@ -16,6 +13,24 @@ db = databases.Database(DATABASE_URL)
|
||||
engine = create_engine(DATABASE_URL)
|
||||
|
||||
|
||||
class User(ormar.Model):
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "users"
|
||||
metadata = metadata
|
||||
database = db
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=50, unique=True, index=True)
|
||||
|
||||
|
||||
class RelationalAuditModel(ormar.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
created_by: User = ormar.ForeignKey(User, nullable=False)
|
||||
updated_by: User = ormar.ForeignKey(User, nullable=False)
|
||||
|
||||
|
||||
class AuditModel(ormar.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
@ -48,6 +63,26 @@ class Category(DateFieldsModel, AuditModel):
|
||||
code: int = ormar.Integer()
|
||||
|
||||
|
||||
class Item(DateFieldsModel, AuditModel):
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "items"
|
||||
exclude_parent_fields = ["updated_by", "updated_date"]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=50, unique=True, index=True)
|
||||
code: int = ormar.Integer()
|
||||
updated_by: str = ormar.String(max_length=100, default="Bob")
|
||||
|
||||
|
||||
class Gun(RelationalAuditModel, DateFieldsModel):
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "guns"
|
||||
exclude_parent_fields = ["updated_by"]
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=50)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
metadata.create_all(engine)
|
||||
@ -65,3 +100,48 @@ def test_model_definition():
|
||||
assert "updated_date" not in model_fields
|
||||
assert "updated_date" not in sqlalchemy_columns
|
||||
assert "updated_date" not in pydantic_columns
|
||||
|
||||
assert "updated_by" not in Gun.Meta.model_fields
|
||||
assert "updated_by" not in Gun.Meta.table.c
|
||||
assert "updated_by" not in Gun.__fields__
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_model_works_as_expected():
|
||||
async with db:
|
||||
async with db.transaction(force_rollback=True):
|
||||
test = await Category(name="Cat", code=2, created_by="Joe").save()
|
||||
assert test.created_date is not None
|
||||
|
||||
test2 = await Category.objects.get(pk=test.pk)
|
||||
assert test2.name == "Cat"
|
||||
assert test2.created_by == "Joe"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exclude_with_redefinition():
|
||||
async with db:
|
||||
async with db.transaction(force_rollback=True):
|
||||
test = await Item(name="Item", code=3, created_by="Anna").save()
|
||||
assert test.created_date is not None
|
||||
assert test.updated_by == "Bob"
|
||||
|
||||
test2 = await Item.objects.get(pk=test.pk)
|
||||
assert test2.name == "Item"
|
||||
assert test2.code == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exclude_with_relation():
|
||||
async with db:
|
||||
async with db.transaction(force_rollback=True):
|
||||
user = await User(name="Michaił Kałasznikow").save()
|
||||
test = await Gun(name="AK47", created_by=user).save()
|
||||
assert test.created_date is not None
|
||||
|
||||
with pytest.raises(AttributeError):
|
||||
assert test.updated_by
|
||||
|
||||
test2 = await Gun.objects.select_related("created_by").get(pk=test.pk)
|
||||
assert test2.name == "AK47"
|
||||
assert test2.created_by.name == "Michaił Kałasznikow"
|
||||
|
||||
Reference in New Issue
Block a user