Skip to content

Commit cc03da0

Browse files
committed
Moved tests to filter field tests
1 parent b30b40e commit cc03da0

File tree

3 files changed

+176
-177
lines changed

3 files changed

+176
-177
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,20 @@ after_success:
4646
fi
4747
env:
4848
matrix:
49-
- TEST_TYPE=build DJANGO_VERSION=1.11 DJANGO_FILTER_VERSION=1.0.2
49+
- TEST_TYPE=build DJANGO_VERSION=1.11
5050
matrix:
5151
fast_finish: true
5252
include:
5353
- python: '2.7'
54-
env: TEST_TYPE=build DJANGO_VERSION=1.6 DJANGO_FILTER_VERSION=1.0.0
54+
env: TEST_TYPE=build DJANGO_VERSION=1.6
5555
- python: '2.7'
56-
env: TEST_TYPE=build DJANGO_VERSION=1.7 DJANGO_FILTER_VERSION=1.0.0
56+
env: TEST_TYPE=build DJANGO_VERSION=1.7
5757
- python: '2.7'
58-
env: TEST_TYPE=build DJANGO_VERSION=1.8 DJANGO_FILTER_VERSION=1.0.2
58+
env: TEST_TYPE=build DJANGO_VERSION=1.8
5959
- python: '2.7'
60-
env: TEST_TYPE=build DJANGO_VERSION=1.9 DJANGO_FILTER_VERSION=1.0.2
60+
env: TEST_TYPE=build DJANGO_VERSION=1.9
6161
- python: '2.7'
62-
env: TEST_TYPE=build DJANGO_VERSION=1.10 DJANGO_FILTER_VERSION=1.0.2
62+
env: TEST_TYPE=build DJANGO_VERSION=1.10
6363
- python: '2.7'
6464
env: TEST_TYPE=lint
6565
deploy:

graphene_django/filter/tests/test_fields.py

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
if DJANGO_FILTER_INSTALLED:
1616
import django_filters
17+
from django_filters import FilterSet, NumberFilter
18+
1719
from graphene_django.filter import (GlobalIDFilter, DjangoFilterConnectionField,
1820
GlobalIDMultipleChoiceFilter)
1921
from graphene_django.filter.tests.filters import ArticleFilter, PetFilter, ReporterFilter
2022
else:
21-
pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed'))
23+
pytestmark.append(pytest.mark.skipif(True, reason='django_filters not installed or not compatible'))
2224

2325
pytestmark.append(pytest.mark.django_db)
2426

@@ -365,3 +367,170 @@ class Query(ObjectType):
365367
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
366368

367369
assert ReporterFilterNode._meta.fields['child_reporters'].node_type == ReporterFilterNode
370+
371+
372+
def test_should_query_filter_node_limit():
373+
class ReporterFilter(FilterSet):
374+
limit = NumberFilter(method='filter_limit')
375+
376+
def filter_limit(self, queryset, name, value):
377+
return queryset[:value]
378+
379+
class Meta:
380+
model = Reporter
381+
fields = ['first_name', ]
382+
383+
class ReporterType(DjangoObjectType):
384+
385+
class Meta:
386+
model = Reporter
387+
interfaces = (Node, )
388+
389+
class ArticleType(DjangoObjectType):
390+
391+
class Meta:
392+
model = Article
393+
interfaces = (Node, )
394+
filter_fields = ('lang', )
395+
396+
class Query(ObjectType):
397+
all_reporters = DjangoFilterConnectionField(
398+
ReporterType,
399+
filterset_class=ReporterFilter
400+
)
401+
402+
def resolve_all_reporters(self, args, context, info):
403+
return Reporter.objects.order_by('a_choice')
404+
405+
Reporter.objects.create(
406+
first_name='Bob',
407+
last_name='Doe',
408+
email='bobdoe@example.com',
409+
a_choice=2
410+
)
411+
r = Reporter.objects.create(
412+
first_name='John',
413+
last_name='Doe',
414+
email='johndoe@example.com',
415+
a_choice=1
416+
)
417+
418+
Article.objects.create(
419+
headline='Article Node 1',
420+
pub_date=datetime.now(),
421+
reporter=r,
422+
editor=r,
423+
lang='es'
424+
)
425+
Article.objects.create(
426+
headline='Article Node 2',
427+
pub_date=datetime.now(),
428+
reporter=r,
429+
editor=r,
430+
lang='en'
431+
)
432+
433+
schema = Schema(query=Query)
434+
query = '''
435+
query NodeFilteringQuery {
436+
allReporters(limit: 1) {
437+
edges {
438+
node {
439+
id
440+
firstName
441+
articles(lang: "es") {
442+
edges {
443+
node {
444+
id
445+
lang
446+
}
447+
}
448+
}
449+
}
450+
}
451+
}
452+
}
453+
'''
454+
455+
expected = {
456+
'allReporters': {
457+
'edges': [{
458+
'node': {
459+
'id': 'UmVwb3J0ZXJUeXBlOjI=',
460+
'firstName': 'John',
461+
'articles': {
462+
'edges': [{
463+
'node': {
464+
'id': 'QXJ0aWNsZVR5cGU6MQ==',
465+
'lang': 'ES'
466+
}
467+
}]
468+
}
469+
}
470+
}]
471+
}
472+
}
473+
474+
result = schema.execute(query)
475+
assert not result.errors
476+
assert result.data == expected
477+
478+
479+
def test_should_query_filter_node_double_limit_raises():
480+
class ReporterFilter(FilterSet):
481+
limit = NumberFilter(method='filter_limit')
482+
483+
def filter_limit(self, queryset, name, value):
484+
return queryset[:value]
485+
486+
class Meta:
487+
model = Reporter
488+
fields = ['first_name', ]
489+
490+
class ReporterType(DjangoObjectType):
491+
492+
class Meta:
493+
model = Reporter
494+
interfaces = (Node, )
495+
496+
class Query(ObjectType):
497+
all_reporters = DjangoFilterConnectionField(
498+
ReporterType,
499+
filterset_class=ReporterFilter
500+
)
501+
502+
def resolve_all_reporters(self, args, context, info):
503+
return Reporter.objects.order_by('a_choice')[:2]
504+
505+
Reporter.objects.create(
506+
first_name='Bob',
507+
last_name='Doe',
508+
email='bobdoe@example.com',
509+
a_choice=2
510+
)
511+
r = Reporter.objects.create(
512+
first_name='John',
513+
last_name='Doe',
514+
email='johndoe@example.com',
515+
a_choice=1
516+
)
517+
518+
schema = Schema(query=Query)
519+
query = '''
520+
query NodeFilteringQuery {
521+
allReporters(limit: 1) {
522+
edges {
523+
node {
524+
id
525+
firstName
526+
}
527+
}
528+
}
529+
}
530+
'''
531+
532+
result = schema.execute(query)
533+
assert len(result.errors) == 1
534+
assert str(result.errors[0]) == (
535+
'Received two sliced querysets (high mark) in the connection, please slice only in one.'
536+
)

graphene_django/tests/test_query.py

Lines changed: 0 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
from django.utils.functional import SimpleLazyObject
66
from py.test import raises
77

8-
from django_filters import FilterSet, NumberFilter
9-
108
import graphene
119
from graphene.relay import Node
1210

1311
from ..utils import DJANGO_FILTER_INSTALLED
1412
from ..compat import MissingType, JSONField
1513
from ..fields import DjangoConnectionField
16-
from ..filter.fields import DjangoFilterConnectionField
1714
from ..types import DjangoObjectType
1815
from .models import Article, Reporter
1916

@@ -455,170 +452,3 @@ class Query(graphene.ObjectType):
455452
result = schema.execute(query)
456453
assert not result.errors
457454
assert result.data == expected
458-
459-
460-
def test_should_query_filter_node_limit():
461-
class ReporterFilter(FilterSet):
462-
limit = NumberFilter(method='filter_limit')
463-
464-
def filter_limit(self, queryset, name, value):
465-
return queryset[:value]
466-
467-
class Meta:
468-
model = Reporter
469-
fields = ['first_name', ]
470-
471-
class ReporterType(DjangoObjectType):
472-
473-
class Meta:
474-
model = Reporter
475-
interfaces = (Node, )
476-
477-
class ArticleType(DjangoObjectType):
478-
479-
class Meta:
480-
model = Article
481-
interfaces = (Node, )
482-
filter_fields = ('lang', )
483-
484-
class Query(graphene.ObjectType):
485-
all_reporters = DjangoFilterConnectionField(
486-
ReporterType,
487-
filterset_class=ReporterFilter
488-
)
489-
490-
def resolve_all_reporters(self, args, context, info):
491-
return Reporter.objects.order_by('a_choice')
492-
493-
Reporter.objects.create(
494-
first_name='Bob',
495-
last_name='Doe',
496-
email='bobdoe@example.com',
497-
a_choice=2
498-
)
499-
r = Reporter.objects.create(
500-
first_name='John',
501-
last_name='Doe',
502-
email='johndoe@example.com',
503-
a_choice=1
504-
)
505-
506-
Article.objects.create(
507-
headline='Article Node 1',
508-
pub_date=datetime.date.today(),
509-
reporter=r,
510-
editor=r,
511-
lang='es'
512-
)
513-
Article.objects.create(
514-
headline='Article Node 2',
515-
pub_date=datetime.date.today(),
516-
reporter=r,
517-
editor=r,
518-
lang='en'
519-
)
520-
521-
schema = graphene.Schema(query=Query)
522-
query = '''
523-
query NodeFilteringQuery {
524-
allReporters(limit: 1) {
525-
edges {
526-
node {
527-
id
528-
firstName
529-
articles(lang: "es") {
530-
edges {
531-
node {
532-
id
533-
lang
534-
}
535-
}
536-
}
537-
}
538-
}
539-
}
540-
}
541-
'''
542-
543-
expected = {
544-
'allReporters': {
545-
'edges': [{
546-
'node': {
547-
'id': 'UmVwb3J0ZXJUeXBlOjI=',
548-
'firstName': 'John',
549-
'articles': {
550-
'edges': [{
551-
'node': {
552-
'id': 'QXJ0aWNsZVR5cGU6MQ==',
553-
'lang': 'ES'
554-
}
555-
}]
556-
}
557-
}
558-
}]
559-
}
560-
}
561-
562-
result = schema.execute(query)
563-
assert not result.errors
564-
assert result.data == expected
565-
566-
567-
def test_should_query_filter_node_double_limit_raises():
568-
class ReporterFilter(FilterSet):
569-
limit = NumberFilter(method='filter_limit')
570-
571-
def filter_limit(self, queryset, name, value):
572-
return queryset[:value]
573-
574-
class Meta:
575-
model = Reporter
576-
fields = ['first_name', ]
577-
578-
class ReporterType(DjangoObjectType):
579-
580-
class Meta:
581-
model = Reporter
582-
interfaces = (Node, )
583-
584-
class Query(graphene.ObjectType):
585-
all_reporters = DjangoFilterConnectionField(
586-
ReporterType,
587-
filterset_class=ReporterFilter
588-
)
589-
590-
def resolve_all_reporters(self, args, context, info):
591-
return Reporter.objects.order_by('a_choice')[:2]
592-
593-
Reporter.objects.create(
594-
first_name='Bob',
595-
last_name='Doe',
596-
email='bobdoe@example.com',
597-
a_choice=2
598-
)
599-
r = Reporter.objects.create(
600-
first_name='John',
601-
last_name='Doe',
602-
email='johndoe@example.com',
603-
a_choice=1
604-
)
605-
606-
schema = graphene.Schema(query=Query)
607-
query = '''
608-
query NodeFilteringQuery {
609-
allReporters(limit: 1) {
610-
edges {
611-
node {
612-
id
613-
firstName
614-
}
615-
}
616-
}
617-
}
618-
'''
619-
620-
result = schema.execute(query)
621-
assert len(result.errors) == 1
622-
assert str(result.errors[0]) == (
623-
'Received two sliced querysets (high mark) in the connection, please slice only in one.'
624-
)

0 commit comments

Comments
 (0)