diff --git a/.travis.yml b/.travis.yml index e18db5236..3c9b44bbd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,20 +46,20 @@ after_success: fi env: matrix: - - TEST_TYPE=build DJANGO_VERSION=1.11 DJANGO_FILTER_VERSION=1.0.2 + - TEST_TYPE=build DJANGO_VERSION=1.11 matrix: fast_finish: true include: - python: '2.7' - env: TEST_TYPE=build DJANGO_VERSION=1.6 DJANGO_FILTER_VERSION=1.0.0 + env: TEST_TYPE=build DJANGO_VERSION=1.6 - python: '2.7' - env: TEST_TYPE=build DJANGO_VERSION=1.7 DJANGO_FILTER_VERSION=1.0.0 + env: TEST_TYPE=build DJANGO_VERSION=1.7 - python: '2.7' - env: TEST_TYPE=build DJANGO_VERSION=1.8 DJANGO_FILTER_VERSION=1.0.2 + env: TEST_TYPE=build DJANGO_VERSION=1.8 - python: '2.7' - env: TEST_TYPE=build DJANGO_VERSION=1.9 DJANGO_FILTER_VERSION=1.0.2 + env: TEST_TYPE=build DJANGO_VERSION=1.9 - python: '2.7' - env: TEST_TYPE=build DJANGO_VERSION=1.10 DJANGO_FILTER_VERSION=1.0.2 + env: TEST_TYPE=build DJANGO_VERSION=1.10 - python: '2.7' env: TEST_TYPE=lint deploy: diff --git a/graphene_django/filter/tests/test_fields.py b/graphene_django/filter/tests/test_fields.py index c95e2d70a..1b24ff2e7 100644 --- a/graphene_django/filter/tests/test_fields.py +++ b/graphene_django/filter/tests/test_fields.py @@ -14,11 +14,13 @@ if DJANGO_FILTER_INSTALLED: import django_filters + from django_filters import FilterSet, NumberFilter + from graphene_django.filter import (GlobalIDFilter, DjangoFilterConnectionField, GlobalIDMultipleChoiceFilter) from graphene_django.filter.tests.filters import ArticleFilter, PetFilter, ReporterFilter else: - pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed')) + pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed or not compatible')) pytestmark.append(pytest.mark.django_db) @@ -365,3 +367,170 @@ class Query(ObjectType): all_reporters = DjangoFilterConnectionField(ReporterFilterNode) assert ReporterFilterNode._meta.fields['child_reporters'].node_type == ReporterFilterNode + + +def test_should_query_filter_node_limit(): + class ReporterFilter(FilterSet): + limit = NumberFilter(method='filter_limit') + + def filter_limit(self, queryset, name, value): + return queryset[:value] + + class Meta: + model = Reporter + fields = ['first_name', ] + + class ReporterType(DjangoObjectType): + + class Meta: + model = Reporter + interfaces = (Node, ) + + class ArticleType(DjangoObjectType): + + class Meta: + model = Article + interfaces = (Node, ) + filter_fields = ('lang', ) + + class Query(ObjectType): + all_reporters = DjangoFilterConnectionField( + ReporterType, + filterset_class=ReporterFilter + ) + + def resolve_all_reporters(self, args, context, info): + return Reporter.objects.order_by('a_choice') + + Reporter.objects.create( + first_name='Bob', + last_name='Doe', + email='bobdoe@example.com', + a_choice=2 + ) + r = Reporter.objects.create( + first_name='John', + last_name='Doe', + email='johndoe@example.com', + a_choice=1 + ) + + Article.objects.create( + headline='Article Node 1', + pub_date=datetime.now(), + reporter=r, + editor=r, + lang='es' + ) + Article.objects.create( + headline='Article Node 2', + pub_date=datetime.now(), + reporter=r, + editor=r, + lang='en' + ) + + schema = Schema(query=Query) + query = ''' + query NodeFilteringQuery { + allReporters(limit: 1) { + edges { + node { + id + firstName + articles(lang: "es") { + edges { + node { + id + lang + } + } + } + } + } + } + } + ''' + + expected = { + 'allReporters': { + 'edges': [{ + 'node': { + 'id': 'UmVwb3J0ZXJUeXBlOjI=', + 'firstName': 'John', + 'articles': { + 'edges': [{ + 'node': { + 'id': 'QXJ0aWNsZVR5cGU6MQ==', + 'lang': 'ES' + } + }] + } + } + }] + } + } + + result = schema.execute(query) + assert not result.errors + assert result.data == expected + + +def test_should_query_filter_node_double_limit_raises(): + class ReporterFilter(FilterSet): + limit = NumberFilter(method='filter_limit') + + def filter_limit(self, queryset, name, value): + return queryset[:value] + + class Meta: + model = Reporter + fields = ['first_name', ] + + class ReporterType(DjangoObjectType): + + class Meta: + model = Reporter + interfaces = (Node, ) + + class Query(ObjectType): + all_reporters = DjangoFilterConnectionField( + ReporterType, + filterset_class=ReporterFilter + ) + + def resolve_all_reporters(self, args, context, info): + return Reporter.objects.order_by('a_choice')[:2] + + Reporter.objects.create( + first_name='Bob', + last_name='Doe', + email='bobdoe@example.com', + a_choice=2 + ) + r = Reporter.objects.create( + first_name='John', + last_name='Doe', + email='johndoe@example.com', + a_choice=1 + ) + + schema = Schema(query=Query) + query = ''' + query NodeFilteringQuery { + allReporters(limit: 1) { + edges { + node { + id + firstName + } + } + } + } + ''' + + result = schema.execute(query) + assert len(result.errors) == 1 + assert str(result.errors[0]) == ( + 'Received two sliced querysets (high mark) in the connection, please slice only in one.' + ) diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index ae765e20f..3594000a9 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -5,15 +5,12 @@ from django.utils.functional import SimpleLazyObject from py.test import raises -from django_filters import FilterSet, NumberFilter - import graphene from graphene.relay import Node from ..utils import DJANGO_FILTER_INSTALLED from ..compat import MissingType, JSONField from ..fields import DjangoConnectionField -from ..filter.fields import DjangoFilterConnectionField from ..types import DjangoObjectType from .models import Article, Reporter @@ -455,170 +452,3 @@ class Query(graphene.ObjectType): result = schema.execute(query) assert not result.errors assert result.data == expected - - -def test_should_query_filter_node_limit(): - class ReporterFilter(FilterSet): - limit = NumberFilter(method='filter_limit') - - def filter_limit(self, queryset, name, value): - return queryset[:value] - - class Meta: - model = Reporter - fields = ['first_name', ] - - class ReporterType(DjangoObjectType): - - class Meta: - model = Reporter - interfaces = (Node, ) - - class ArticleType(DjangoObjectType): - - class Meta: - model = Article - interfaces = (Node, ) - filter_fields = ('lang', ) - - class Query(graphene.ObjectType): - all_reporters = DjangoFilterConnectionField( - ReporterType, - filterset_class=ReporterFilter - ) - - def resolve_all_reporters(self, args, context, info): - return Reporter.objects.order_by('a_choice') - - Reporter.objects.create( - first_name='Bob', - last_name='Doe', - email='bobdoe@example.com', - a_choice=2 - ) - r = Reporter.objects.create( - first_name='John', - last_name='Doe', - email='johndoe@example.com', - a_choice=1 - ) - - Article.objects.create( - headline='Article Node 1', - pub_date=datetime.date.today(), - reporter=r, - editor=r, - lang='es' - ) - Article.objects.create( - headline='Article Node 2', - pub_date=datetime.date.today(), - reporter=r, - editor=r, - lang='en' - ) - - schema = graphene.Schema(query=Query) - query = ''' - query NodeFilteringQuery { - allReporters(limit: 1) { - edges { - node { - id - firstName - articles(lang: "es") { - edges { - node { - id - lang - } - } - } - } - } - } - } - ''' - - expected = { - 'allReporters': { - 'edges': [{ - 'node': { - 'id': 'UmVwb3J0ZXJUeXBlOjI=', - 'firstName': 'John', - 'articles': { - 'edges': [{ - 'node': { - 'id': 'QXJ0aWNsZVR5cGU6MQ==', - 'lang': 'ES' - } - }] - } - } - }] - } - } - - result = schema.execute(query) - assert not result.errors - assert result.data == expected - - -def test_should_query_filter_node_double_limit_raises(): - class ReporterFilter(FilterSet): - limit = NumberFilter(method='filter_limit') - - def filter_limit(self, queryset, name, value): - return queryset[:value] - - class Meta: - model = Reporter - fields = ['first_name', ] - - class ReporterType(DjangoObjectType): - - class Meta: - model = Reporter - interfaces = (Node, ) - - class Query(graphene.ObjectType): - all_reporters = DjangoFilterConnectionField( - ReporterType, - filterset_class=ReporterFilter - ) - - def resolve_all_reporters(self, args, context, info): - return Reporter.objects.order_by('a_choice')[:2] - - Reporter.objects.create( - first_name='Bob', - last_name='Doe', - email='bobdoe@example.com', - a_choice=2 - ) - r = Reporter.objects.create( - first_name='John', - last_name='Doe', - email='johndoe@example.com', - a_choice=1 - ) - - schema = graphene.Schema(query=Query) - query = ''' - query NodeFilteringQuery { - allReporters(limit: 1) { - edges { - node { - id - firstName - } - } - } - } - ''' - - result = schema.execute(query) - assert len(result.errors) == 1 - assert str(result.errors[0]) == ( - 'Received two sliced querysets (high mark) in the connection, please slice only in one.' - )