Skip to content

Commit 662f897

Browse files
committed
Add failing test case
1 parent 1e34dfb commit 662f897

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

graphene_django/tests/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class FilmDetails(models.Model):
1919

2020

2121
class Film(models.Model):
22+
genre = models.CharField(max_length=2, help_text='Genre', choices=[
23+
('do', 'Documentary'),
24+
('ot', 'Other')
25+
], default='ot')
2226
reporters = models.ManyToManyField('Reporter',
2327
related_name='films')
2428

graphene_django/tests/test_query.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from django.utils.functional import SimpleLazyObject
66
from py.test import raises
77

8+
from django.db.models import Q
9+
810
import graphene
911
from graphene.relay import Node
1012

@@ -13,7 +15,7 @@
1315
from ..fields import DjangoConnectionField
1416
from ..types import DjangoObjectType
1517
from ..settings import graphene_settings
16-
from .models import Article, Reporter
18+
from .models import Article, Reporter, Film, FilmDetails
1719

1820
pytestmark = pytest.mark.django_db
1921

@@ -366,6 +368,61 @@ class Query(graphene.ObjectType):
366368
assert result.data == expected
367369

368370

371+
@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
372+
reason="django-filter should be installed")
373+
def test_should_query_node_filtering_with_distinct_queryset():
374+
class FilmType(DjangoObjectType):
375+
376+
class Meta:
377+
model = Film
378+
interfaces = (Node, )
379+
filter_fields = ('genre',)
380+
381+
class Query(graphene.ObjectType):
382+
films = DjangoConnectionField(FilmType)
383+
384+
# def resolve_all_reporters_with_berlin_films(self, args, context, info):
385+
# return Reporter.objects.filter(Q(films__film__location__contains="Berlin") | Q(a_choice=1))
386+
387+
def resolve_films(self, args, context, info):
388+
return Film.objects.filter(Q(details__location__contains="Berlin") | Q(genre__in=['ot'])).distinct()
389+
390+
f = Film.objects.create(
391+
)
392+
fd = FilmDetails.objects.create(
393+
location="Berlin",
394+
film=f
395+
)
396+
397+
schema = graphene.Schema(query=Query)
398+
query = '''
399+
query NodeFilteringQuery {
400+
films {
401+
edges {
402+
node {
403+
genre
404+
}
405+
}
406+
}
407+
}
408+
'''
409+
410+
expected = {
411+
'films': {
412+
'edges': [{
413+
'node': {
414+
'genre': 'ot'
415+
}
416+
}]
417+
}
418+
}
419+
420+
result = schema.execute(query)
421+
assert not result.errors
422+
print(result.data)
423+
assert result.data == expected
424+
425+
369426
@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
370427
reason="django-filter should be installed")
371428
def test_should_query_node_multiple_filtering():

0 commit comments

Comments
 (0)