Adding tests for DjangoFilterConnectionField

This commit is contained in:
Adam Charnock 2015-12-03 11:09:34 +00:00
parent 4a087ecb24
commit d959cf5a84
3 changed files with 88 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -60,6 +60,7 @@ setup(
'django_filter>=0.10.0', 'django_filter>=0.10.0',
], ],
tests_require=[ tests_require=[
'django-filter>=0.11.0',
'pytest>=2.7.2', 'pytest>=2.7.2',
'pytest-django', 'pytest-django',
'mock', 'mock',