Skip to content

Commit

Permalink
feat: simplify and expand FilterSet classes
Browse files Browse the repository at this point in the history
  • Loading branch information
SquakR committed Feb 4, 2022
1 parent fa3b08f commit 887896c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 69 deletions.
81 changes: 21 additions & 60 deletions tests/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""FilterSet classes."""

import django_filters
from graphene_django_filter import AdvancedFilterSet

from .models import Task, TaskGroup, User
Expand All @@ -9,78 +8,40 @@
class UserFilter(AdvancedFilterSet):
"""User FilterSet class for testing."""

first_name__startswith = django_filters.CharFilter(
field_name='first_name',
lookup_expr='startswith',
)
last_name__contains = django_filters.CharFilter(
field_name='last_name',
lookup_expr='contains',
)
birthday__range = django_filters.DateFromToRangeFilter(field_name='birthday')

class Meta:
model = User
fields = (
'first_name',
'last_name',
'email',
'is_active',
'birthday',
)
fields = {
'email': ('exact', 'startswith', 'contains'),
'first_name': ('exact', 'contains'),
'last_name': ('exact', 'contains'),
'is_active': ('exact',),
'birthday': ('exact',),
}


class TaskFilter(AdvancedFilterSet):
"""Task FilterSet class for testing."""

created_at__gt = django_filters.DateFilter(
field_name='created_at',
lookup_expr='gt',
)
completed_at__lt = django_filters.DateFilter(
field_name='completed_at',
lookup_expr='lt',
)
user__email__iexact = django_filters.CharFilter(
field_name='user__email',
lookup_expr='iexact',
)
user__email__contains = django_filters.CharFilter(
field_name='user__email',
lookup_expr='contains',
)
user__email__icontains = django_filters.CharFilter(
field_name='user__email',
lookup_expr='icontains',
)

class Meta:
model = Task
fields = (
'name',
'description',
'user',
'user__email',
'user__last_name',
)
fields = {
'name': ('exact', 'contains'),
'created_at': ('gt',),
'completed_at': ('lt',),
'description': ('exact', 'contains'),
'user': ('exact',),
'user__email': ('exact', 'iexact', 'contains', 'icontains'),
'user__last_name': ('exact', 'contains'),
}


class TaskGroupFilter(AdvancedFilterSet):
"""TaskGroup FilterSet class for testing."""

name__contains = django_filters.CharFilter(
field_name='name',
lookup_expr='contains',
)
priority_gte = django_filters.NumberFilter(
field_name='priority',
lookup_expr='gte',
)

class Meta:
model = TaskGroup
fields = (
'name',
'priority',
'tasks',
)
fields = {
'name': ('exact', 'contains'),
'priority': ('exact', 'gte', 'lte'),
'tasks': ('exact',),
}
11 changes: 7 additions & 4 deletions tests/object_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Meta:
interfaces = (graphene.relay.Node,)
fields = '__all__'
filter_fields = {
'email': ('exact',),
'first_name': ('exact', 'startswith'),
'last_name': ('exact', 'contains'),
'email': ('exact',),
'is_active': ('exact',),
'birthday': ('exact',),
}
Expand All @@ -43,10 +43,13 @@ class Meta:
interfaces = (graphene.relay.Node,)
fields = '__all__'
filter_fields = {
'name': ('exact',),
'created_at': ('exact', 'gt'),
'name': ('exact', 'contains'),
'created_at': ('gt',),
'completed_at': ('lt',),
'description': ('exact', 'contains'),
'user': ('exact',),
'user__email': ('exact', 'iexact', 'contains', 'icontains'),
'user__last_name': ('exact', 'contains'),
}


Expand All @@ -73,7 +76,7 @@ class Meta:
fields = '__all__'
filter_fields = {
'name': ('exact', 'contains'),
'priority': ('exact', 'gte'),
'priority': ('exact', 'gte', 'lte'),
'tasks': ('exact',),
}

Expand Down
11 changes: 6 additions & 5 deletions tests/test_input_type_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ def setUp(self) -> None:
),
)
self.task_filter_trees_roots = [
Node(name='name', children=[Node(name='exact')]),
Node(name='description', children=[Node(name='exact')]),
Node(name='name', children=[Node(name='exact'), Node(name='contains')]),
Node(name='created_at', children=[Node(name='gt')]),
Node(name='completed_at', children=[Node(name='lt')]),
Node(name='description', children=[Node(name='exact'), Node(name='contains')]),
Node(
name='user', children=[
Node(name='exact'),
Expand All @@ -53,12 +55,11 @@ def setUp(self) -> None:
Node(
name='last_name', children=[
Node(name='exact'),
Node(name='contains'),
],
),
],
),
Node(name='created_at', children=[Node(name='gt')]),
Node(name='completed_at', children=[Node(name='lt')]),
]

def test_sequence_to_tree(self) -> None:
Expand Down Expand Up @@ -124,7 +125,7 @@ def test_create_input_object_type(self) -> None:
def test_create_filter_input_subtype(self) -> None:
"""Test the `create_filter_input_subtype` function."""
input_object_type = create_filter_input_subtype(
self.task_filter_trees_roots[2],
self.task_filter_trees_roots[4],
TaskFilter,
'Task',
)
Expand Down

0 comments on commit 887896c

Please sign in to comment.