diff --git a/graphene/contrib/django/tests/filters.py b/graphene/contrib/django/tests/filters.py new file mode 100644 index 00000000..a2cffa5e --- /dev/null +++ b/graphene/contrib/django/tests/filters.py @@ -0,0 +1,24 @@ +import django_filters + +from .models import Article, Pet + + +class ArticleFilter(django_filters.FilterSet): + + class Meta: + model = Article + fields = { + 'headline': ['exact', 'icontains'], + 'pub_date': ['gt', 'lt', 'exact'], + 'reporter': ['exact'], + } + order_by = True + + +class PetFilter(django_filters.FilterSet): + + class Meta: + model = Pet + fields = ['name'] + order_by = False + diff --git a/graphene/contrib/django/tests/test_fields.py b/graphene/contrib/django/tests/test_fields.py new file mode 100644 index 00000000..b765c04c --- /dev/null +++ b/graphene/contrib/django/tests/test_fields.py @@ -0,0 +1,63 @@ +from graphene import Schema +from graphene.contrib.django import DjangoFilterConnectionField, DjangoNode +from graphene.contrib.django.tests.filters import ArticleFilter, PetFilter +from graphene.contrib.django.tests.models import Article, Pet + +schema = Schema() + + +@schema.register +class ArticleNode(DjangoNode): + + class Meta: + model = Article + + +@schema.register +class PetNode(DjangoNode): + + class Meta: + model = Pet + + +def assert_arguments(field, *arguments): + ignore = ('after', 'before', 'first', 'last', 'o') + actual = [ + name + for name in field.arguments.arguments.keys() + if name not in ignore and not name.startswith('_') + ] + assert set(arguments) == set(actual), \ + 'Expected arguments ({}) did not match actual ({])'.format( + arguments, + actual + ) + + +def assert_orderable(field): + assert 'o' in field.arguments.arguments.keys(), \ + 'Field cannot be ordered' + + +def assert_not_orderable(field): + assert 'o' in field.arguments.arguments.keys(), \ + 'Field cannot be ordered' + + +def test_filter_explicit_filterset_arguments(): + field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleFilter) + assert_arguments(field, + 'headline', 'headlineIcontains', + 'pubDate', 'pubDateGt', 'pubDateLt', + 'reporter', + ) + + +def test_filter_explicit_filterset_orderable(): + field = DjangoFilterConnectionField(ArticleNode, filterset_class=ArticleFilter) + assert_orderable(field) + + +def test_filter_explicit_filterset_not_orderable(): + field = DjangoFilterConnectionField(PetNode, filterset_class=PetFilter) + assert_not_orderable(field) diff --git a/setup.py b/setup.py index fda136f1..a7fb389b 100644 --- a/setup.py +++ b/setup.py @@ -60,6 +60,7 @@ setup( 'django_filter>=0.10.0', ], tests_require=[ + 'django-filter>=0.11.0', 'pytest>=2.7.2', 'pytest-django', 'mock',