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

@ -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
#