|
| 1 | +from datetime import date |
| 2 | +from django.db import connection |
| 3 | +from django.test import TestCase |
| 4 | +from django.test.utils import CaptureQueriesContext |
| 5 | +import graphene |
| 6 | +import pytest |
| 7 | + |
| 8 | +from .. import registry |
| 9 | +from ..fields import DjangoConnectionField |
| 10 | +from ..optimization import optimize_queryset |
| 11 | +from ..types import DjangoObjectType |
| 12 | +from .models import ( |
| 13 | + Article as ArticleModel, |
| 14 | + Reporter as ReporterModel, |
| 15 | + Pet as PetModel |
| 16 | +) |
| 17 | + |
| 18 | +pytestmark = pytest.mark.django_db |
| 19 | + |
| 20 | +registry.reset_global_registry() |
| 21 | + |
| 22 | + |
| 23 | +class Article(DjangoObjectType): |
| 24 | + class Meta: |
| 25 | + model = ArticleModel |
| 26 | + interfaces = (graphene.relay.Node,) |
| 27 | + |
| 28 | + |
| 29 | +class Reporter(DjangoObjectType): |
| 30 | + class Meta: |
| 31 | + model = ReporterModel |
| 32 | + |
| 33 | + |
| 34 | +class Pet(DjangoObjectType): |
| 35 | + class Meta: |
| 36 | + model = PetModel |
| 37 | + |
| 38 | + |
| 39 | +class RootQuery(graphene.ObjectType): |
| 40 | + article = graphene.Field(Article, id=graphene.ID()) |
| 41 | + articles = DjangoConnectionField(Article) |
| 42 | + |
| 43 | + def resolve_article(self, args, context, info): |
| 44 | + qs = ArticleModel.objects |
| 45 | + qs = optimize_queryset(ArticleModel, qs, info.field_asts[0]) |
| 46 | + return qs.get(**args) |
| 47 | + |
| 48 | + |
| 49 | +schema = graphene.Schema(query=RootQuery) |
| 50 | + |
| 51 | + |
| 52 | +class TestOptimization(TestCase): |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def setUpTestData(cls): |
| 56 | + cls.reporter = ReporterModel.objects.create( |
| 57 | + first_name='Clark', last_name='Kent', |
| 58 | + email='ckent@dailyplanet.com', a_choice='this' |
| 59 | + ) |
| 60 | + cls.editor = ReporterModel.objects.create( |
| 61 | + first_name='Perry', last_name='White', |
| 62 | + email='pwhite@dailyplanet.com', a_choice='this' |
| 63 | + ) |
| 64 | + cls.article = ArticleModel.objects.create( |
| 65 | + headline='Superman Saves the Day', |
| 66 | + pub_date=date.today(), |
| 67 | + reporter=cls.reporter, |
| 68 | + editor=cls.editor |
| 69 | + ) |
| 70 | + cls.other_article = ArticleModel.objects.create( |
| 71 | + headline='Lex Luthor is SO Rich', |
| 72 | + pub_date=date.today(), |
| 73 | + reporter=cls.reporter, |
| 74 | + editor=cls.editor |
| 75 | + ) |
| 76 | + cls.editor.pets.add(cls.reporter) |
| 77 | + |
| 78 | + def test_select_related(self): |
| 79 | + query = """query GetArticle($articleId: ID!){ |
| 80 | + article(id: $articleId) { |
| 81 | + headline |
| 82 | + reporter { |
| 83 | + email |
| 84 | + } |
| 85 | + editor { |
| 86 | + email |
| 87 | + } |
| 88 | + } |
| 89 | + }""" |
| 90 | + |
| 91 | + variables = {'articleId': str(self.article.id)} |
| 92 | + |
| 93 | + with CaptureQueriesContext(connection) as query_context: |
| 94 | + results = schema.execute(query, variable_values=variables) |
| 95 | + |
| 96 | + returned_article = results.data['article'] |
| 97 | + assert returned_article['headline'] == self.article.headline |
| 98 | + assert returned_article['reporter']['email'] == self.reporter.email |
| 99 | + assert returned_article['editor']['email'] == self.editor.email |
| 100 | + |
| 101 | + self.assertEqual(len(query_context.captured_queries), 1) |
| 102 | + |
| 103 | + def test_prefetch_related(self): |
| 104 | + query = """query { |
| 105 | + articles { |
| 106 | + edges { |
| 107 | + node { |
| 108 | + headline |
| 109 | + editor { |
| 110 | + email |
| 111 | + pets { |
| 112 | + email |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + }""" |
| 119 | + |
| 120 | + with CaptureQueriesContext(connection) as query_context: |
| 121 | + results = schema.execute(query) |
| 122 | + |
| 123 | + returned_articles = results.data['articles']['edges'] |
| 124 | + assert len(returned_articles) == 2 |
| 125 | + |
| 126 | + self.assertEqual(len(query_context.captured_queries), 4) |
0 commit comments