switch to decorator used to register property_fields and save it on Meta inner class to expose to cloned fastapi models

This commit is contained in:
collerek
2020-12-04 09:51:00 +01:00
parent 3b164c76de
commit 00ab8a6d1d
10 changed files with 175 additions and 143 deletions

View File

@ -3,9 +3,9 @@ import datetime
import databases
import pytest
import sqlalchemy
from pydantic import validator
import ormar
from ormar import property_field
from tests.settings import DATABASE_URL
database = databases.Database(DATABASE_URL, force_rollback=True)
@ -17,17 +17,27 @@ class Album(ormar.Model):
tablename = "albums"
metadata = metadata
database = database
include_props_in_dict = True
include_props_in_fields = True
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
timestamp: datetime.datetime = ormar.DateTime(pydantic_only=True)
@property
@property_field
def name10(self) -> str:
return self.name + "_10"
@property_field
def name20(self) -> str:
return self.name + "_20"
@property
def name30(self) -> str:
return self.name + "_30"
@property_field
def name40(self) -> str:
return self.name + "_40"
@pytest.fixture(autouse=True, scope="module")
def create_test_database():
@ -57,14 +67,13 @@ async def test_pydantic_only_fields():
assert "timestamp" in test_dict
assert test_dict["timestamp"] is None
assert album.name30 == "Hitchcock_30"
album.timestamp = datetime.datetime.now()
test_dict = album.dict()
assert "timestamp" in test_dict
assert test_dict["timestamp"] is not None
assert test_dict.get("name10") == "Hitchcock_10"
Album.Meta.include_props_in_dict = False
test_dict = album.dict()
assert "timestamp" in test_dict
assert test_dict["timestamp"] is not None
assert test_dict.get("name10", 'aa') == 'aa'
assert test_dict.get("name20") == "Hitchcock_20"
assert test_dict.get("name40") == "Hitchcock_40"
assert "name30" not in test_dict