mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 17:46:57 +03:00
Adding tests for DjangoFilterConnectionField
This commit is contained in:
parent
4a087ecb24
commit
d959cf5a84
24
graphene/contrib/django/tests/filters.py
Normal file
24
graphene/contrib/django/tests/filters.py
Normal 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
|
||||
|
63
graphene/contrib/django/tests/test_fields.py
Normal file
63
graphene/contrib/django/tests/test_fields.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user