add and/or/not to filtergroups, add left and right shift to operators, add some tests, add *args to other functions that read data and use filter

This commit is contained in:
collerek
2021-04-19 19:49:42 +02:00
parent c49d21f605
commit 7a27778b44
8 changed files with 298 additions and 121 deletions

View File

@ -1,5 +1,5 @@
import json
from typing import Optional
from typing import Any, Dict, Optional, Set, Type, Union, cast
import databases
import pytest
@ -8,6 +8,7 @@ from fastapi import FastAPI
from starlette.testclient import TestClient
import ormar
from ormar.queryset.utils import translate_list_to_dict
from tests.settings import DATABASE_URL
app = FastAPI()
@ -84,6 +85,24 @@ to_exclude_ormar = {
}
def auto_exclude_id_field(to_exclude: Any) -> Union[Dict, Set]:
if isinstance(to_exclude, dict):
for key in to_exclude.keys():
to_exclude[key] = auto_exclude_id_field(to_exclude[key])
to_exclude["id"] = Ellipsis
return to_exclude
else:
return {"id"}
def generate_exclude_for_ids(model: Type[ormar.Model]) -> Dict:
to_exclude_base = translate_list_to_dict(model._iterate_related_models())
return cast(Dict, auto_exclude_id_field(to_exclude=to_exclude_base))
to_exclude_auto = generate_exclude_for_ids(model=Department)
@app.post("/departments/", response_model=Department)
async def create_department(department: Department):
await department.save_related(follow=True, save_all=True)

View File

@ -173,7 +173,6 @@ 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"
@ -203,7 +202,6 @@ 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"
@ -216,7 +214,6 @@ 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"
@ -234,7 +231,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)
@ -277,9 +274,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
@ -294,9 +291,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
@ -349,8 +346,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
@ -397,8 +394,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"
@ -407,8 +404,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"
@ -431,8 +428,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
@ -446,8 +443,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"
@ -457,8 +454,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"
@ -480,9 +477,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"
@ -496,10 +493,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"

View File

@ -54,6 +54,7 @@ def create_test_database():
def test_fields_access():
# basic access
assert Product.id._field == Product.Meta.model_fields["id"]
assert Product.id.id == Product.Meta.model_fields["id"]
assert isinstance(Product.id._field, BaseField)
assert Product.id._access_chain == "id"
assert Product.id._source_model == Product
@ -76,6 +77,9 @@ def test_fields_access():
assert curr_field._access_chain == "categories__products__rating"
assert curr_field._source_model == PriceList
with pytest.raises(AttributeError):
assert Product.category >= 3
@pytest.mark.parametrize(
"method, expected, expected_value",
@ -86,38 +90,33 @@ def test_fields_access():
("__ge__", "gte", "Test"),
("__gt__", "gt", "Test"),
("iexact", "iexact", "Test"),
("contains", "contains", "%Test%"),
("icontains", "icontains", "%Test%"),
("startswith", "startswith", "Test%"),
("istartswith", "istartswith", "Test%"),
("endswith", "endswith", "%Test"),
("iendswith", "iendswith", "%Test"),
("contains", "contains", "Test"),
("icontains", "icontains", "Test"),
("startswith", "startswith", "Test"),
("istartswith", "istartswith", "Test"),
("endswith", "endswith", "Test"),
("iendswith", "iendswith", "Test"),
("isnull", "isnull", "Test"),
("__contains__", "in", "Test"),
("__mod__", "contains", "%Test%"),
("in_", "in", "Test"),
("__lshift__", "in", "Test"),
("__rshift__", "isnull", True),
("__mod__", "contains", "Test"),
],
)
def test_operator_return_proper_filter_action(method, expected, expected_value):
action = getattr(Product.name, method)("Test")
assert action.source_model == Product
assert action.target_model == Product
assert action.operator == expected
assert action.filter_value == expected_value
group_ = getattr(Product.name, method)("Test")
assert group_._kwargs_dict == {f"name__{expected}": expected_value}
action = getattr(Product.category.name, method)("Test")
assert action.source_model == Product
assert action.target_model == Category
assert action.operator == expected
assert action.filter_value == expected_value
group_ = getattr(Product.category.name, method)("Test")
assert group_._kwargs_dict == {f"category__name__{expected}": expected_value}
action = getattr(PriceList.categories.products.rating, method)("Test")
assert action.source_model == PriceList
assert action.target_model == Product
assert action.operator == expected
assert action.filter_value == expected_value
group_ = getattr(PriceList.categories.products.rating, method)("Test")
assert group_._kwargs_dict == {
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
@ -138,6 +137,45 @@ def test_operator_return_proper_order_action(method, expected_direction):
assert not action.is_source_model_order
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 ) )")
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 ) )")
group = ((Product.name == "Test") & (Product.rating >= 3.0)) | (
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
group_str = str(group.get_text_clause())
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') ) )")
group = (Product.name % "Test") | (
(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
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') ) ) )")
# @pytest.mark.asyncio
# async def test_filtering_by_field_access():
# async with database:
@ -156,10 +194,10 @@ def test_operator_return_proper_order_action(method, expected_direction):
# TODO: Finish implementation
# * 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?
# all functions: delete, first, get, get_or_none, get_or_create, all, filter, exclude
# and same from queryset, should they also accept filter groups?
# * create filter groups for & and | (and ~ - NOT?)
# * accept OrderActions in order_by
#