Adding filter tests

This commit is contained in:
Adam Charnock 2015-12-03 12:52:15 +00:00
parent b9f93918c8
commit 7bfeb086a4
2 changed files with 90 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import django_filters
from graphene.contrib.django.tests.models import Reporter
from .models import Article, Pet
@ -15,6 +16,14 @@ class ArticleFilter(django_filters.FilterSet):
order_by = True
class ReporterFilter(django_filters.FilterSet):
class Meta:
model = Reporter
fields = ['first_name', 'last_name', 'email', 'pets']
order_by = False
class PetFilter(django_filters.FilterSet):
class Meta:

View File

@ -1,9 +1,12 @@
from django.core.exceptions import ImproperlyConfigured
from py.test import raises
from django.db.models import Manager
from django.db.models.query import QuerySet
from graphene.contrib.django import DjangoNode
from graphene.contrib.django.resolvers import SimpleQuerySetConnectionResolver
from graphene.contrib.django.tests.models import Reporter
from graphene.contrib.django.resolvers import SimpleQuerySetConnectionResolver, FilterConnectionResolver
from graphene.contrib.django.tests.filters import ReporterFilter, ArticleFilter
from graphene.contrib.django.tests.models import Reporter, Article
class ReporterNode(DjangoNode):
@ -11,6 +14,11 @@ class ReporterNode(DjangoNode):
model = Reporter
class ArticleNode(DjangoNode):
class Meta:
model = Article
def test_simple_resolve():
reporter = Reporter(id=1, first_name='Cookie Monster')
resolver = SimpleQuerySetConnectionResolver(ReporterNode, on='articles')
@ -51,3 +59,74 @@ def test_simple_order():
assert 'WHERE' not in str(resolved.query)
assert 'ORDER BY' in str(resolved.query)
assert '"last_name" ASC' in str(resolved.query)
def test_filter_get_filterset_class_explicit():
reporter = Reporter(id=1, first_name='Cookie Monster')
resolver = FilterConnectionResolver(ReporterNode,
filterset_class=ReporterFilter)
resolver(inst=reporter, args={}, info=None)
assert resolver.get_filterset_class() == ReporterFilter, \
'ReporterFilter not returned'
def test_filter_get_filterset_class_implicit():
reporter = Reporter(id=1, first_name='Cookie Monster')
resolver = FilterConnectionResolver(ReporterNode)
resolver(inst=reporter, args={}, info=None)
assert resolver.get_filterset_class().__name__ == 'ReporterFilterSet'
def test_filter_get_filterset_class_error():
reporter = Reporter(id=1, first_name='Cookie Monster')
resolver = FilterConnectionResolver(ReporterNode)
resolver.model = None
with raises(ImproperlyConfigured) as excinfo:
resolver(inst=reporter, args={}, info=None)
assert "must define 'filterset_class' or 'model'" in str(excinfo.value)
def test_filter_filter():
reporter = Reporter(id=1, first_name='Cookie Monster')
resolver = FilterConnectionResolver(ReporterNode,
filterset_class=ReporterFilter)
resolved = resolver(inst=reporter, args={
'first_name': 'Elmo'
}, info=None)
assert '"first_name" = Elmo' in str(resolved.query)
assert 'ORDER BY' not in str(resolved.query)
def test_filter_filter_contains():
article = Article(id=1, headline='Cookie Monster eats fruit')
resolver = FilterConnectionResolver(ArticleNode,
filterset_class=ArticleFilter)
resolved = resolver(inst=article, args={
'headline__icontains': 'Elmo'
}, info=None)
assert '"headline" LIKE %Elmo%' in str(resolved.query)
def test_filter_order():
article = Article(id=1, headline='Cookie Monster eats fruit')
resolver = FilterConnectionResolver(ArticleNode,
filterset_class=ArticleFilter)
resolved = resolver(inst=article, args={
# TODO: This should be 'order', not 'o'
'o': 'headline'
}, info=None)
assert 'WHERE' not in str(resolved.query)
assert 'ORDER BY' in str(resolved.query)
assert '"headline" ASC' in str(resolved.query)
def test_filter_order_not_available():
reporter = Reporter(id=1, first_name='Cookie Monster')
resolver = FilterConnectionResolver(ReporterNode,
filterset_class=ReporterFilter)
resolved = resolver(inst=reporter, args={
# TODO: This should be 'order', not 'o'
'o': 'last_name'
}, info=None)
assert 'WHERE' not in str(resolved.query)
assert 'ORDER BY' not in str(resolved.query)