|
5 | 5 | from django.utils.functional import SimpleLazyObject
|
6 | 6 | from py.test import raises
|
7 | 7 |
|
| 8 | +from django.db.models import Q |
| 9 | + |
8 | 10 | import graphene
|
9 | 11 | from graphene.relay import Node
|
10 | 12 |
|
|
13 | 15 | from ..fields import DjangoConnectionField
|
14 | 16 | from ..types import DjangoObjectType
|
15 | 17 | from ..settings import graphene_settings
|
16 |
| -from .models import Article, Reporter |
| 18 | +from .models import Article, Reporter, Film, FilmDetails |
17 | 19 |
|
18 | 20 | pytestmark = pytest.mark.django_db
|
19 | 21 |
|
@@ -366,6 +368,61 @@ class Query(graphene.ObjectType):
|
366 | 368 | assert result.data == expected
|
367 | 369 |
|
368 | 370 |
|
| 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 | + |
369 | 426 | @pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
|
370 | 427 | reason="django-filter should be installed")
|
371 | 428 | def test_should_query_node_multiple_filtering():
|
|
0 commit comments