|
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 |
|
|
17 | 19 | Article,
|
18 | 20 | CNNReporter,
|
19 | 21 | Reporter,
|
| 22 | + Film, |
| 23 | + FilmDetails, |
20 | 24 | )
|
21 | 25 |
|
22 | 26 | pytestmark = pytest.mark.django_db
|
@@ -431,6 +435,61 @@ class Query(graphene.ObjectType):
|
431 | 435 | assert result.data == expected
|
432 | 436 |
|
433 | 437 |
|
| 438 | +@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED, |
| 439 | + reason="django-filter should be installed") |
| 440 | +def test_should_query_node_filtering_with_distinct_queryset(): |
| 441 | + class FilmType(DjangoObjectType): |
| 442 | + |
| 443 | + class Meta: |
| 444 | + model = Film |
| 445 | + interfaces = (Node, ) |
| 446 | + filter_fields = ('genre',) |
| 447 | + |
| 448 | + class Query(graphene.ObjectType): |
| 449 | + films = DjangoConnectionField(FilmType) |
| 450 | + |
| 451 | + # def resolve_all_reporters_with_berlin_films(self, args, context, info): |
| 452 | + # return Reporter.objects.filter(Q(films__film__location__contains="Berlin") | Q(a_choice=1)) |
| 453 | + |
| 454 | + def resolve_films(self, args, context, info): |
| 455 | + return Film.objects.filter(Q(details__location__contains="Berlin") | Q(genre__in=['ot'])).distinct() |
| 456 | + |
| 457 | + f = Film.objects.create( |
| 458 | + ) |
| 459 | + fd = FilmDetails.objects.create( |
| 460 | + location="Berlin", |
| 461 | + film=f |
| 462 | + ) |
| 463 | + |
| 464 | + schema = graphene.Schema(query=Query) |
| 465 | + query = ''' |
| 466 | + query NodeFilteringQuery { |
| 467 | + films { |
| 468 | + edges { |
| 469 | + node { |
| 470 | + genre |
| 471 | + } |
| 472 | + } |
| 473 | + } |
| 474 | + } |
| 475 | + ''' |
| 476 | + |
| 477 | + expected = { |
| 478 | + 'films': { |
| 479 | + 'edges': [{ |
| 480 | + 'node': { |
| 481 | + 'genre': 'ot' |
| 482 | + } |
| 483 | + }] |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + result = schema.execute(query) |
| 488 | + assert not result.errors |
| 489 | + print(result.data) |
| 490 | + assert result.data == expected |
| 491 | + |
| 492 | + |
434 | 493 | @pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
|
435 | 494 | reason="django-filter should be installed")
|
436 | 495 | def test_should_query_node_multiple_filtering():
|
|
0 commit comments