check complex prefixes in groups, refactor limit queries, finish docstrings, refactors and cleanup in long methods

This commit is contained in:
collerek
2021-03-07 12:50:40 +01:00
parent eeabb60200
commit 79ad69e355
12 changed files with 468 additions and 136 deletions

View File

@ -44,7 +44,6 @@ def test_or_group():
f"{result.actions[1].table_prefix}"
f"_books.title = 'bb' )"
)
assert not result.is_source_model_filter
def test_and_group():
@ -58,7 +57,6 @@ def test_and_group():
f"{result.actions[1].table_prefix}"
f"_books.title = 'bb' )"
)
assert not result.is_source_model_filter
def test_nested_and():
@ -77,7 +75,6 @@ def test_nested_and():
f"{book_prefix}"
f"_books.title = 'dd' ) )"
)
assert not result.is_source_model_filter
def test_nested_group_and_action():
@ -93,7 +90,6 @@ def test_nested_group_and_action():
f"{book_prefix}"
f"_books.title = 'dd' )"
)
assert not result.is_source_model_filter
def test_deeply_nested_or():
@ -120,7 +116,6 @@ def test_deeply_nested_or():
f"( {book_prefix}_books.year > 'xx' OR {book_prefix}_books.title = '22' ) ) )"
)
assert result_qry.replace("\n", "") == expected_qry.replace("\n", "")
assert not result.is_source_model_filter
def test_one_model_group():
@ -128,7 +123,6 @@ def test_one_model_group():
result.resolve(model_cls=Book)
assert len(result.actions) == 2
assert len(result._nested_groups) == 0
assert result.is_source_model_filter
def test_one_model_nested_group():
@ -138,7 +132,6 @@ def test_one_model_nested_group():
result.resolve(model_cls=Book)
assert len(result.actions) == 0
assert len(result._nested_groups) == 2
assert result.is_source_model_filter
def test_one_model_with_group():
@ -146,4 +139,3 @@ def test_one_model_with_group():
result.resolve(model_cls=Book)
assert len(result.actions) == 1
assert len(result._nested_groups) == 1
assert result.is_source_model_filter

View File

@ -122,3 +122,45 @@ async def test_load_all_multiple_instances_of_same_table_in_schema():
assert len(math_class.dict().get("students")) == 2
assert math_class.teachers[0].category.department.name == "Law Department"
assert math_class.students[0].category.department.name == "Math Department"
@pytest.mark.asyncio
async def test_filter_groups_with_instances_of_same_table_in_schema():
async with database:
await create_data()
math_class = (
await SchoolClass.objects.select_related(
["teachers__category__department", "students__category__department"]
)
.filter(
ormar.or_(
students__name="Jane",
teachers__category__name="Domestic",
students__category__name="Foreign",
)
)
.get(name="Math")
)
assert math_class.name == "Math"
assert math_class.students[0].name == "Jane"
assert len(math_class.dict().get("students")) == 2
assert math_class.teachers[0].category.department.name == "Law Department"
assert math_class.students[0].category.department.name == "Math Department"
classes = (
await SchoolClass.objects.select_related(
["students__category__department", "teachers__category__department"]
)
.filter(
ormar.and_(
ormar.or_(
students__name="Jane", students__category__name="Foreign"
),
teachers__category__department__name="Law Department",
)
)
.all()
)
assert len(classes) == 1
assert classes[0].teachers[0].category.department.name == "Law Department"
assert classes[0].students[0].category.department.name == "Math Department"

View File

@ -5,6 +5,7 @@ import pytest
import sqlalchemy
import ormar
from ormar.exceptions import QueryDefinitionError
from tests.settings import DATABASE_URL
database = databases.Database(DATABASE_URL)
@ -108,11 +109,60 @@ async def test_or_filters():
assert len(books) == 3
assert not any([x.title in ["The Silmarillion", "The Witcher"] for x in books])
books = (
await Book.objects.select_related("author")
.filter(ormar.or_(year__gt=1980, year__lt=1910))
.filter(title__startswith="The")
.limit(1)
.all()
)
assert len(books) == 1
assert books[0].title == "The Witcher"
books = (
await Book.objects.select_related("author")
.filter(ormar.or_(year__gt=1980, author__name="Andrzej Sapkowski"))
.filter(title__startswith="The")
.limit(1)
.all()
)
assert len(books) == 1
assert books[0].title == "The Witcher"
books = (
await Book.objects.select_related("author")
.filter(ormar.or_(year__gt=1980, author__name="Andrzej Sapkowski"))
.filter(title__startswith="The")
.limit(1)
.offset(1)
.all()
)
assert len(books) == 1
assert books[0].title == "The Tower of Fools"
books = (
await Book.objects.select_related("author")
.filter(ormar.or_(year__gt=1980, author__name="Andrzej Sapkowski"))
.filter(title__startswith="The")
.limit(1)
.offset(1)
.order_by("-id")
.all()
)
assert len(books) == 1
assert books[0].title == "The Witcher"
with pytest.raises(QueryDefinitionError):
await Book.objects.select_related("author").filter('wrong').all()
# TODO: Check / modify
# process and and or into filter groups (V)
# check exclude queries working (V)
# check complex prefixes properly resolved (V)
# fix limit -> change to where subquery to extract number of distinct pk values (V)
# finish docstrings (V)
# fix types for FilterAction and FilterGroup (X)
# when limit and no sql do not allow main model and other models
# check complex prefixes properly resolved
# fix types for FilterAction and FilterGroup (?)
# add docs