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:
@ -10,6 +10,7 @@ from fastapi import FastAPI
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
import ormar
|
||||
from ormar import property_field
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
app = FastAPI()
|
||||
@ -74,8 +75,8 @@ class RandomModel(ormar.Model):
|
||||
server_default=sqlalchemy.func.now()
|
||||
)
|
||||
|
||||
@property
|
||||
def full_name(self):
|
||||
@property_field
|
||||
def full_name(self) -> str:
|
||||
return " ".join([self.first_name, self.last_name])
|
||||
|
||||
|
||||
@ -140,7 +141,6 @@ async def create_user4(user: User2):
|
||||
@app.post("/random/", response_model=RandomModel)
|
||||
async def create_user5(user: RandomModel):
|
||||
user = await user.save()
|
||||
print('returning')
|
||||
return user
|
||||
|
||||
|
||||
@ -170,24 +170,20 @@ def test_excluding_fields_in_endpoints():
|
||||
"last_name": "Doe",
|
||||
}
|
||||
|
||||
print('before call')
|
||||
response = client.post("/users/", json=user2)
|
||||
created_user = User(**response.json())
|
||||
assert created_user.pk is not None
|
||||
assert created_user.password is None
|
||||
|
||||
print('before call')
|
||||
response = client.post("/users2/", json=user)
|
||||
created_user2 = User(**response.json())
|
||||
assert created_user2.pk is not None
|
||||
assert created_user2.password is None
|
||||
|
||||
# response has only 3 fields from UserBase
|
||||
print('before call')
|
||||
response = client.post("/users3/", json=user)
|
||||
assert list(response.json().keys()) == ["email", "first_name", "last_name"]
|
||||
|
||||
print('before call')
|
||||
response = client.post("/users4/", json=user)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
@ -197,40 +193,41 @@ def test_excluding_fields_in_endpoints():
|
||||
"category",
|
||||
]
|
||||
|
||||
# user3 = {"last_name": "Test"}
|
||||
# print('before call')
|
||||
# response = client.post("/random/", json=user3)
|
||||
# assert list(response.json().keys()) == [
|
||||
# "id",
|
||||
# "password",
|
||||
# "first_name",
|
||||
# "last_name",
|
||||
# "created_date",
|
||||
# "full_name",
|
||||
# ]
|
||||
# assert response.json().get("full_name") == "John Test"
|
||||
#
|
||||
# RandomModel.Meta.include_props_in_fields = False
|
||||
# user3 = {"last_name": "Test"}
|
||||
# print('before call')
|
||||
# response = client.post("/random/", json=user3)
|
||||
# assert list(response.json().keys()) == [
|
||||
# "id",
|
||||
# "password",
|
||||
# "first_name",
|
||||
# "last_name",
|
||||
# "created_date",
|
||||
# "full_name",
|
||||
# ]
|
||||
# assert response.json().get("full_name") == "John Test"
|
||||
|
||||
|
||||
def test_adding_fields_in_endpoints():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
user3 = {"last_name": "Test"}
|
||||
response = client.post("/random/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"created_date",
|
||||
"full_name",
|
||||
]
|
||||
assert response.json().get("full_name") == "John Test"
|
||||
|
||||
RandomModel.Meta.include_props_in_fields = False
|
||||
user3 = {"last_name": "Test"}
|
||||
response = client.post("/random/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
"password",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"created_date",
|
||||
"full_name",
|
||||
]
|
||||
assert response.json().get("full_name") == "John Test"
|
||||
|
||||
|
||||
def test_adding_fields_in_endpoints2():
|
||||
client = TestClient(app)
|
||||
with client as client:
|
||||
RandomModel.Meta.include_props_in_dict = True
|
||||
user3 = {"last_name": "Test"}
|
||||
print('before call')
|
||||
response = client.post("/random2/", json=user3)
|
||||
assert list(response.json().keys()) == [
|
||||
"id",
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
# type: ignore
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from ormar import ModelDefinitionError, property_field
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
@ -19,15 +21,15 @@ class Song(ormar.Model):
|
||||
name: str = ormar.String(max_length=100)
|
||||
sort_order: int = ormar.Integer()
|
||||
|
||||
@property
|
||||
@property_field
|
||||
def sorted_name(self):
|
||||
return f"{self.sort_order}: {self.name}"
|
||||
|
||||
@property
|
||||
@property_field
|
||||
def sample(self):
|
||||
return "sample"
|
||||
|
||||
@property
|
||||
@property_field
|
||||
def sample2(self):
|
||||
return "sample2"
|
||||
|
||||
@ -66,3 +68,12 @@ async def test_sort_order_on_main_model():
|
||||
assert "sample" not in check_include
|
||||
assert "sample2" in check_include
|
||||
assert "sorted_name" in check_include
|
||||
|
||||
|
||||
def test_wrong_definition():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class WrongModel(ormar.Model): # pragma: no cover
|
||||
@property_field
|
||||
def test(self, aa=10, bb=30):
|
||||
pass
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user