Skip to content

Commit

Permalink
Fix: rename NOT test case and add OR test case
Browse files Browse the repository at this point in the history
  • Loading branch information
OdysseyJ committed Jan 10, 2024
1 parent 433ceeb commit d783523
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions strawberry_django/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ def build_filter_kwargs(
filter_methods.append(filter_method)
continue

q_method = getattr(filters, f"q_{'n_' if negated else ''}{field_name}", None)
if q_method and (q_value := q_method(field_value)):
filter_kwargs &= q_value
continue

if django_model:
if field_name in ("AND", "OR", "NOT"): # noqa: PLR6201
if has_object_definition(field_value):
Expand Down
40 changes: 40 additions & 0 deletions tests/filters/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import strawberry
from django.db.models import Q
from strawberry import auto
from strawberry.annotation import StrawberryAnnotation
from strawberry.types import ExecutionResult
Expand Down Expand Up @@ -89,6 +90,14 @@ def filter(self, queryset):
return queryset.filter(name__icontains=self.name)


@strawberry_django.filters.filter(models.Fruit)
class QFilter:
search: Optional[str]

def q_search(self, value: str) -> Q:
return Q(name=value)


@strawberry_django.type(models.Vegetable, filters=VegetableFilter)
class Vegetable:
id: auto
Expand All @@ -107,6 +116,7 @@ class Fruit:
class Query:
fruits: List[Fruit] = strawberry_django.field()
field_filter: List[Fruit] = strawberry_django.field(filters=FieldFilter)
q_filter: List[Fruit] = strawberry_django.field(filters=QFilter)
type_filter: List[Fruit] = strawberry_django.field(filters=TypeFilter)
enum_filter: List[Fruit] = strawberry_django.field(filters=EnumFilter)
enum_lookup_filter: List[Fruit] = strawberry_django.field(filters=EnumLookupFilter)
Expand Down Expand Up @@ -247,6 +257,36 @@ def test_field_filter_method(query, fruits):
]


def test_q_filter_method(query, fruits):
result = query('{ fruits: qFilter(filters: { search: "strawberry" }) { id name } }')
assert not result.errors
assert result.data["fruits"] == [
{"id": "1", "name": "strawberry"},
]


def test_q_filter_method_with_nested_not_filter(query, fruits):
result = query(
'{ fruits: qFilter(filters: { NOT : { search: "strawberry" }}) { id name } }'
)
assert not result.errors
assert result.data["fruits"] == [
{"id": "2", "name": "raspberry"},
{"id": "3", "name": "banana"},
]


def test_q_filter_method_with_nested_or_filter(query, fruits):
result = query(
'{ fruits: qFilter(filters: { search: "strawberry", OR: { search: "raspberry" }}) { id name } }'
)
assert not result.errors
assert result.data["fruits"] == [
{"id": "1", "name": "strawberry"},
{"id": "2", "name": "raspberry"},
]


def test_type_filter_method(query, fruits):
result = query('{ fruits: typeFilter(filters: { name: "anana" }) { id name } }')
assert not result.errors
Expand Down

0 comments on commit d783523

Please sign in to comment.