finish release notes, add more test
This commit is contained in:
@ -173,6 +173,7 @@ def test_init_of_abstract_model():
|
||||
|
||||
def test_duplicated_related_name_on_different_model():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class Bus3(Car2): # pragma: no cover
|
||||
class Meta:
|
||||
tablename = "buses3"
|
||||
@ -202,6 +203,7 @@ def test_field_redefining_in_concrete_models():
|
||||
|
||||
def test_model_subclassing_that_redefines_constraints_column_names():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class WrongField2(DateFieldsModel): # pragma: no cover
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "wrongs"
|
||||
@ -214,6 +216,7 @@ def test_model_subclassing_that_redefines_constraints_column_names():
|
||||
|
||||
def test_model_subclassing_non_abstract_raises_error():
|
||||
with pytest.raises(ModelDefinitionError):
|
||||
|
||||
class WrongField2(DateFieldsModelNoSubclass): # pragma: no cover
|
||||
class Meta(ormar.ModelMeta):
|
||||
tablename = "wrongs"
|
||||
@ -231,7 +234,7 @@ def test_params_are_inherited():
|
||||
|
||||
|
||||
def round_date_to_seconds(
|
||||
date: datetime.datetime,
|
||||
date: datetime.datetime,
|
||||
) -> datetime.datetime: # pragma: no cover
|
||||
if date.microsecond >= 500000:
|
||||
date = date + datetime.timedelta(seconds=1)
|
||||
@ -274,9 +277,9 @@ async def test_fields_inherited_from_mixin():
|
||||
|
||||
sub2 = (
|
||||
await Subject.objects.select_related("category")
|
||||
.order_by("-created_date")
|
||||
.exclude_fields("updated_date")
|
||||
.get()
|
||||
.order_by("-created_date")
|
||||
.exclude_fields("updated_date")
|
||||
.get()
|
||||
)
|
||||
assert round_date_to_seconds(sub2.created_date) == round_date_to_seconds(
|
||||
sub.created_date
|
||||
@ -291,9 +294,9 @@ async def test_fields_inherited_from_mixin():
|
||||
|
||||
sub3 = (
|
||||
await Subject.objects.prefetch_related("category")
|
||||
.order_by("-created_date")
|
||||
.exclude_fields({"updated_date": ..., "category": {"updated_date"}})
|
||||
.get()
|
||||
.order_by("-created_date")
|
||||
.exclude_fields({"updated_date": ..., "category": {"updated_date"}})
|
||||
.get()
|
||||
)
|
||||
assert round_date_to_seconds(sub3.created_date) == round_date_to_seconds(
|
||||
sub.created_date
|
||||
@ -346,8 +349,8 @@ async def test_inheritance_with_relation():
|
||||
"coowned_buses": {"created_date"},
|
||||
}
|
||||
)
|
||||
.prefetch_related(["coowned_trucks", "coowned_buses"])
|
||||
.get(name="Joe")
|
||||
.prefetch_related(["coowned_trucks", "coowned_buses"])
|
||||
.get(name="Joe")
|
||||
)
|
||||
assert joe_check.pk == joe.pk
|
||||
assert joe_check.coowned_trucks[0] == shelby
|
||||
@ -394,8 +397,8 @@ async def test_inheritance_with_multi_relation():
|
||||
|
||||
unicorn = (
|
||||
await Bus2.objects.select_related(["owner", "co_owners"])
|
||||
.order_by("-co_owners__name")
|
||||
.get()
|
||||
.order_by("-co_owners__name")
|
||||
.get()
|
||||
)
|
||||
assert unicorn.name == "Unicorn 2"
|
||||
assert unicorn.owner.name == "Sam"
|
||||
@ -404,8 +407,8 @@ async def test_inheritance_with_multi_relation():
|
||||
|
||||
unicorn = (
|
||||
await Bus2.objects.select_related(["owner", "co_owners"])
|
||||
.order_by("co_owners__name")
|
||||
.get()
|
||||
.order_by("co_owners__name")
|
||||
.get()
|
||||
)
|
||||
assert unicorn.name == "Unicorn 2"
|
||||
assert unicorn.owner.name == "Sam"
|
||||
@ -428,8 +431,8 @@ async def test_inheritance_with_multi_relation():
|
||||
"coowned_buses2": {"created_date"},
|
||||
}
|
||||
)
|
||||
.prefetch_related(["coowned_trucks2", "coowned_buses2"])
|
||||
.get(name="Joe")
|
||||
.prefetch_related(["coowned_trucks2", "coowned_buses2"])
|
||||
.get(name="Joe")
|
||||
)
|
||||
assert joe_check.pk == joe.pk
|
||||
assert joe_check.coowned_trucks2[0] == shelby
|
||||
@ -443,8 +446,8 @@ async def test_inheritance_with_multi_relation():
|
||||
|
||||
unicorn = (
|
||||
await Bus2.objects.select_related(["owner", "co_owners"])
|
||||
.filter(co_owners__name="Joe")
|
||||
.get()
|
||||
.filter(co_owners__name="Joe")
|
||||
.get()
|
||||
)
|
||||
assert unicorn.name == "Unicorn 2"
|
||||
assert unicorn.owner.name == "Sam"
|
||||
@ -454,8 +457,8 @@ async def test_inheritance_with_multi_relation():
|
||||
|
||||
unicorn = (
|
||||
await Bus2.objects.select_related(["owner", "co_owners"])
|
||||
.exclude(co_owners__name="Joe")
|
||||
.get()
|
||||
.exclude(co_owners__name="Joe")
|
||||
.get()
|
||||
)
|
||||
assert unicorn.name == "Unicorn 2"
|
||||
assert unicorn.owner.name == "Sam"
|
||||
@ -477,9 +480,9 @@ async def test_inheritance_with_multi_relation():
|
||||
|
||||
unicorns = (
|
||||
await Bus2.objects.select_related(["owner", "co_owners"])
|
||||
.filter(name__contains="Unicorn")
|
||||
.order_by("-name")
|
||||
.all()
|
||||
.filter(name__contains="Unicorn")
|
||||
.order_by("-name")
|
||||
.all()
|
||||
)
|
||||
assert unicorns[0].name == "Unicorn 3"
|
||||
assert unicorns[0].owner.name == "Joe"
|
||||
@ -493,10 +496,10 @@ async def test_inheritance_with_multi_relation():
|
||||
|
||||
unicorns = (
|
||||
await Bus2.objects.select_related(["owner", "co_owners"])
|
||||
.filter(name__contains="Unicorn")
|
||||
.order_by("-name")
|
||||
.limit(2, limit_raw_sql=True)
|
||||
.all()
|
||||
.filter(name__contains="Unicorn")
|
||||
.order_by("-name")
|
||||
.limit(2, limit_raw_sql=True)
|
||||
.all()
|
||||
)
|
||||
assert len(unicorns) == 2
|
||||
assert unicorns[1].name == "Unicorn 2"
|
||||
|
||||
@ -112,11 +112,11 @@ def test_operator_return_proper_filter_action(method, expected, expected_value):
|
||||
|
||||
group_ = getattr(PriceList.categories.products.rating, method)("Test")
|
||||
assert group_._kwargs_dict == {
|
||||
f"categories__products__rating__{expected}": expected_value}
|
||||
f"categories__products__rating__{expected}": expected_value
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("method, expected_direction",
|
||||
[("asc", ""), ("desc", "desc"), ])
|
||||
@pytest.mark.parametrize("method, expected_direction", [("asc", ""), ("desc", "desc"),])
|
||||
def test_operator_return_proper_order_action(method, expected_direction):
|
||||
action = getattr(Product.name, method)()
|
||||
assert action.source_model == Product
|
||||
@ -141,17 +141,20 @@ def test_combining_groups_together():
|
||||
group = (Product.name == "Test") & (Product.rating >= 3.0)
|
||||
group.resolve(model_cls=Product)
|
||||
assert len(group._nested_groups) == 2
|
||||
assert str(group.get_text_clause()) == ("( ( product.name = 'Test' ) AND"
|
||||
" ( product.rating >= 3.0 ) )")
|
||||
assert str(group.get_text_clause()) == (
|
||||
"( ( product.name = 'Test' ) AND" " ( product.rating >= 3.0 ) )"
|
||||
)
|
||||
|
||||
group = ~((Product.name == "Test") & (Product.rating >= 3.0))
|
||||
group.resolve(model_cls=Product)
|
||||
assert len(group._nested_groups) == 2
|
||||
assert str(group.get_text_clause()) == (" NOT ( ( product.name = 'Test' ) AND"
|
||||
" ( product.rating >= 3.0 ) )")
|
||||
assert str(group.get_text_clause()) == (
|
||||
" NOT ( ( product.name = 'Test' ) AND" " ( product.rating >= 3.0 ) )"
|
||||
)
|
||||
|
||||
group = ((Product.name == "Test") & (Product.rating >= 3.0)) | (
|
||||
Product.category.name << (["Toys", "Books"]))
|
||||
Product.category.name << (["Toys", "Books"])
|
||||
)
|
||||
group.resolve(model_cls=Product)
|
||||
assert len(group._nested_groups) == 2
|
||||
assert len(group._nested_groups[0]._nested_groups) == 2
|
||||
@ -159,22 +162,27 @@ def test_combining_groups_together():
|
||||
category_prefix = group._nested_groups[1].actions[0].table_prefix
|
||||
assert group_str == (
|
||||
"( ( ( product.name = 'Test' ) AND ( product.rating >= 3.0 ) ) "
|
||||
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) )")
|
||||
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) )"
|
||||
)
|
||||
|
||||
group = (Product.name % "Test") | (
|
||||
(Product.category.price_lists.name.startswith("Aa")) | (
|
||||
Product.category.name << (["Toys", "Books"])))
|
||||
(Product.category.price_lists.name.startswith("Aa"))
|
||||
| (Product.category.name << (["Toys", "Books"]))
|
||||
)
|
||||
group.resolve(model_cls=Product)
|
||||
assert len(group._nested_groups) == 2
|
||||
assert len(group._nested_groups[1]._nested_groups) == 2
|
||||
group_str = str(group.get_text_clause())
|
||||
price_list_prefix = group._nested_groups[1]._nested_groups[0].actions[
|
||||
0].table_prefix
|
||||
price_list_prefix = (
|
||||
group._nested_groups[1]._nested_groups[0].actions[0].table_prefix
|
||||
)
|
||||
category_prefix = group._nested_groups[1]._nested_groups[1].actions[0].table_prefix
|
||||
assert group_str == (
|
||||
f"( ( product.name LIKE '%Test%' ) "
|
||||
f"OR ( ( {price_list_prefix}_price_lists.name LIKE 'Aa%' ) "
|
||||
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) ) )")
|
||||
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) ) )"
|
||||
)
|
||||
|
||||
|
||||
# @pytest.mark.asyncio
|
||||
# async def test_filtering_by_field_access():
|
||||
@ -195,9 +203,6 @@ def test_combining_groups_together():
|
||||
# * overload operators and add missing functions that return FilterAction (V)
|
||||
# * return OrderAction for desc() and asc() (V)
|
||||
# * create filter groups for & and | (and ~ - NOT?) (V)
|
||||
|
||||
# * accept args in all functions that accept filters? or only filter and exclude?
|
||||
# * accept args in all functions that accept filters? or only filter and exclude? (V)
|
||||
# all functions: delete, first, get, get_or_none, get_or_create, all, filter, exclude
|
||||
# and same from queryset, should they also accept filter groups?
|
||||
# * accept OrderActions in order_by
|
||||
#
|
||||
# * accept OrderActions in order_by (V)
|
||||
|
||||
66
tests/test_model_definition/test_pydantic_fields.py
Normal file
66
tests/test_model_definition/test_pydantic_fields.py
Normal file
@ -0,0 +1,66 @@
|
||||
from typing import Optional
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from pydantic import EmailStr, HttpUrl, ValidationError
|
||||
|
||||
import ormar
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class BaseMeta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class Test(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
pass
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
# you need to pop non - db fields as ormar will complain that it's unknown field
|
||||
email = kwargs.pop("email", self.__fields__["email"].get_default())
|
||||
url = kwargs.pop("url", self.__fields__["url"].get_default())
|
||||
super().__init__(**kwargs)
|
||||
self.email = email
|
||||
self.url = url
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=200)
|
||||
email: Optional[EmailStr] # field optional - default to None
|
||||
url: HttpUrl = "www.example.com" # field with default
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_working_with_pydantic_fields():
|
||||
async with database:
|
||||
test = Test(name="Test", email="aka@go2.com")
|
||||
assert test.name == "Test"
|
||||
assert test.email == "aka@go2.com"
|
||||
assert test.url == "www.example.com"
|
||||
|
||||
test.email = "sdta@ada.pt"
|
||||
assert test.email == "sdta@ada.pt"
|
||||
|
||||
await test.save()
|
||||
test_check = await Test.objects.get()
|
||||
|
||||
assert test_check.name == "Test"
|
||||
assert test_check.email is None
|
||||
assert test_check.url == "www.example.com"
|
||||
|
||||
# TODO add validate assignment to pydantic config
|
||||
# test_check.email = 1
|
||||
@ -70,6 +70,14 @@ async def test_or_filters():
|
||||
assert len(books) == 4
|
||||
assert not any([x.title == "The Tower of Fools" for x in books])
|
||||
|
||||
books = (
|
||||
await Book.objects.select_related("author")
|
||||
.filter((Book.author.name == "J.R.R. Tolkien") | (Book.year < 1995))
|
||||
.all()
|
||||
)
|
||||
assert len(books) == 4
|
||||
assert not any([x.title == "The Tower of Fools" for x in books])
|
||||
|
||||
books = (
|
||||
await Book.objects.select_related("author")
|
||||
.filter(ormar.or_(year__gt=1960, year__lt=1940))
|
||||
@ -110,6 +118,26 @@ async def test_or_filters():
|
||||
assert books[0].title == "The Silmarillion"
|
||||
assert books[1].title == "The Witcher"
|
||||
|
||||
books = (
|
||||
await Book.objects.select_related("author")
|
||||
.filter(
|
||||
(
|
||||
(
|
||||
(Book.year > 1960) & (Book.author.name == "J.R.R. Tolkien")
|
||||
| (
|
||||
(Book.year < 2000)
|
||||
& (Book.author.name == "Andrzej Sapkowski")
|
||||
)
|
||||
)
|
||||
& (Book.title.startswith("The"))
|
||||
),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
assert len(books) == 2
|
||||
assert books[0].title == "The Silmarillion"
|
||||
assert books[1].title == "The Witcher"
|
||||
|
||||
books = (
|
||||
await Book.objects.select_related("author")
|
||||
.filter(
|
||||
|
||||
@ -122,11 +122,21 @@ async def test_sort_order_on_main_model():
|
||||
assert songs[1].name == "Song 2"
|
||||
assert songs[2].name == "Song 1"
|
||||
|
||||
songs = await Song.objects.order_by(Song.sort_order.desc()).all()
|
||||
assert songs[0].name == "Song 3"
|
||||
assert songs[1].name == "Song 2"
|
||||
assert songs[2].name == "Song 1"
|
||||
|
||||
songs = await Song.objects.order_by("sort_order").all()
|
||||
assert songs[0].name == "Song 1"
|
||||
assert songs[1].name == "Song 2"
|
||||
assert songs[2].name == "Song 3"
|
||||
|
||||
songs = await Song.objects.order_by(Song.sort_order.asc()).all()
|
||||
assert songs[0].name == "Song 1"
|
||||
assert songs[1].name == "Song 2"
|
||||
assert songs[2].name == "Song 3"
|
||||
|
||||
songs = await Song.objects.order_by("name").all()
|
||||
assert songs[0].name == "Song 1"
|
||||
assert songs[1].name == "Song 2"
|
||||
@ -145,6 +155,14 @@ async def test_sort_order_on_main_model():
|
||||
assert songs[2].name == "Song 2"
|
||||
assert songs[3].name == "Song 3"
|
||||
|
||||
songs = await Song.objects.order_by(
|
||||
[Song.sort_order.asc(), Song.name.asc()]
|
||||
).all()
|
||||
assert songs[0].name == "Song 1"
|
||||
assert songs[1].name == "Song 4"
|
||||
assert songs[2].name == "Song 2"
|
||||
assert songs[3].name == "Song 3"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sort_order_on_related_model():
|
||||
|
||||
Reference in New Issue
Block a user