From fdb6c3089f8d0d92e91e651b1a866015bc58ce79 Mon Sep 17 00:00:00 2001 From: collerek Date: Tue, 9 Mar 2021 10:27:20 +0100 Subject: [PATCH] fix minor code smells --- ormar/queryset/actions/order_action.py | 3 +- tests/test_or_filters.py | 97 ++++++++++++-------------- 2 files changed, 44 insertions(+), 56 deletions(-) diff --git a/ormar/queryset/actions/order_action.py b/ormar/queryset/actions/order_action.py index 2bba2d2..9d5f248 100644 --- a/ormar/queryset/actions/order_action.py +++ b/ormar/queryset/actions/order_action.py @@ -57,8 +57,7 @@ class OrderAction(QueryAction): prefix = f"{self.table_prefix}_" if self.table_prefix else "" if self.direction == "": return text(f"min({prefix}{self.table}" f".{self.field_alias})") - else: - return text(f"max({prefix}{self.table}" f".{self.field_alias}) desc") + return text(f"max({prefix}{self.table}" f".{self.field_alias}) desc") def get_text_clause(self) -> sqlalchemy.sql.expression.TextClause: """ diff --git a/tests/test_or_filters.py b/tests/test_or_filters.py index 1a483cc..6d6f8ab 100644 --- a/tests/test_or_filters.py +++ b/tests/test_or_filters.py @@ -57,24 +57,24 @@ async def test_or_filters(): books = ( await Book.objects.select_related("author") - .filter(ormar.or_(author__name="J.R.R. Tolkien", year__gt=1970)) - .all() + .filter(ormar.or_(author__name="J.R.R. Tolkien", year__gt=1970)) + .all() ) assert len(books) == 5 books = ( await Book.objects.select_related("author") - .filter(ormar.or_(author__name="J.R.R. Tolkien", year__lt=1995)) - .all() + .filter(ormar.or_(author__name="J.R.R. Tolkien", year__lt=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)) - .filter(author__name="J.R.R. Tolkien") - .all() + .filter(ormar.or_(year__gt=1960, year__lt=1940)) + .filter(author__name="J.R.R. Tolkien") + .all() ) assert len(books) == 2 assert books[0].title == "The Hobbit" @@ -82,13 +82,13 @@ async def test_or_filters(): books = ( await Book.objects.select_related("author") - .filter( + .filter( ormar.and_( ormar.or_(year__gt=1960, year__lt=1940), author__name="J.R.R. Tolkien", ) ) - .all() + .all() ) assert len(books) == 2 @@ -97,14 +97,14 @@ async def test_or_filters(): books = ( await Book.objects.select_related("author") - .filter( + .filter( ormar.or_( ormar.and_(year__gt=1960, author__name="J.R.R. Tolkien"), ormar.and_(year__lt=2000, author__name="Andrzej Sapkowski"), ) ) - .filter(title__startswith="The") - .all() + .filter(title__startswith="The") + .all() ) assert len(books) == 2 assert books[0].title == "The Silmarillion" @@ -112,7 +112,7 @@ async def test_or_filters(): books = ( await Book.objects.select_related("author") - .filter( + .filter( ormar.or_( ormar.and_( ormar.or_(year__gt=1960, year__lt=1940), @@ -121,7 +121,7 @@ async def test_or_filters(): ormar.and_(year__lt=2000, author__name="Andrzej Sapkowski"), ) ) - .all() + .all() ) assert len(books) == 3 assert books[0].title == "The Hobbit" @@ -130,29 +130,29 @@ async def test_or_filters(): books = ( await Book.objects.select_related("author") - .exclude( + .exclude( ormar.or_( ormar.and_(year__gt=1960, author__name="J.R.R. Tolkien"), ormar.and_(year__lt=2000, author__name="Andrzej Sapkowski"), ) ) - .filter(title__startswith="The") - .all() + .filter(title__startswith="The") + .all() ) 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( + .filter( ormar.or_( ormar.and_(year__gt=1960, author__name="J.R.R. Tolkien"), ormar.and_(year__lt=2000, author__name="Andrzej Sapkowski"), title__icontains="hobbit", ) ) - .filter(title__startswith="The") - .all() + .filter(title__startswith="The") + .all() ) assert len(books) == 3 assert not any( @@ -161,43 +161,43 @@ async def test_or_filters(): books = ( await Book.objects.select_related("author") - .filter(ormar.or_(year__gt=1980, year__lt=1910)) - .filter(title__startswith="The") - .limit(1) - .all() + .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() + .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() + .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() + .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" @@ -220,30 +220,19 @@ async def test_or_filters(): books = ( await Book.objects.select_related("author") - .filter(ormar.or_(author__name="J.R.R. Tolkien")) - .all() + .filter(ormar.or_(author__name="J.R.R. Tolkien")) + .all() ) assert len(books) == 3 books = ( await Book.objects.select_related("author") - .filter( + .filter( ormar.or_( ormar.and_(author__name__icontains="tolkien"), ormar.and_(author__name__icontains="sapkowski"), ) ) - .all() + .all() ) assert len(books) == 5 - - -# 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) -# add docs (V) -# fix querysetproxy (V)