From 887896c40aa221e600ef98ab088b387f476537c4 Mon Sep 17 00:00:00 2001 From: squak Date: Fri, 4 Feb 2022 13:32:02 +0300 Subject: [PATCH] feat: simplify and expand FilterSet classes --- tests/filtersets.py | 81 ++++++++---------------------- tests/object_types.py | 11 ++-- tests/test_input_type_factories.py | 11 ++-- 3 files changed, 34 insertions(+), 69 deletions(-) diff --git a/tests/filtersets.py b/tests/filtersets.py index e56d80d..35d1ae9 100644 --- a/tests/filtersets.py +++ b/tests/filtersets.py @@ -1,6 +1,5 @@ """FilterSet classes.""" -import django_filters from graphene_django_filter import AdvancedFilterSet from .models import Task, TaskGroup, User @@ -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',), + } diff --git a/tests/object_types.py b/tests/object_types.py index df34c38..c98ed16 100644 --- a/tests/object_types.py +++ b/tests/object_types.py @@ -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',), } @@ -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'), } @@ -73,7 +76,7 @@ class Meta: fields = '__all__' filter_fields = { 'name': ('exact', 'contains'), - 'priority': ('exact', 'gte'), + 'priority': ('exact', 'gte', 'lte'), 'tasks': ('exact',), } diff --git a/tests/test_input_type_factories.py b/tests/test_input_type_factories.py index 258097f..29a7fa2 100644 --- a/tests/test_input_type_factories.py +++ b/tests/test_input_type_factories.py @@ -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'), @@ -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: @@ -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', )