mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 17:52:19 +03:00
Test manual optimization
This commit is contained in:
parent
34e6a90df8
commit
582958c642
|
@ -3,22 +3,15 @@ from django.db import connection
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.utils import CaptureQueriesContext
|
from django.test.utils import CaptureQueriesContext
|
||||||
import graphene
|
import graphene
|
||||||
import pytest
|
|
||||||
|
|
||||||
from .. import registry
|
from ..fields import DjangoConnectionField, DjangoListField
|
||||||
from ..fields import DjangoConnectionField
|
|
||||||
from ..optimization import optimize_queryset
|
from ..optimization import optimize_queryset
|
||||||
from ..types import DjangoObjectType
|
from ..types import DjangoObjectType
|
||||||
from .models import (
|
from .models import (
|
||||||
Article as ArticleModel,
|
Article as ArticleModel,
|
||||||
Reporter as ReporterModel,
|
Reporter as ReporterModel
|
||||||
Pet as PetModel
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pytestmark = pytest.mark.django_db
|
|
||||||
|
|
||||||
registry.reset_global_registry()
|
|
||||||
|
|
||||||
|
|
||||||
class Article(DjangoObjectType):
|
class Article(DjangoObjectType):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -27,24 +20,36 @@ class Article(DjangoObjectType):
|
||||||
|
|
||||||
|
|
||||||
class Reporter(DjangoObjectType):
|
class Reporter(DjangoObjectType):
|
||||||
|
favorite_pet = graphene.Field(lambda: Reporter)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ReporterModel
|
model = ReporterModel
|
||||||
|
#interfaces = (graphene.relay.Node,)
|
||||||
|
optimizations = {
|
||||||
|
'favorite_pet': {
|
||||||
|
'prefetch': ['pets']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def resolve_favorite_pet(self, *args):
|
||||||
class Pet(DjangoObjectType):
|
for pet in self.pets.all():
|
||||||
class Meta:
|
if pet.last_name == 'Kent':
|
||||||
model = PetModel
|
return pet
|
||||||
|
|
||||||
|
|
||||||
class RootQuery(graphene.ObjectType):
|
class RootQuery(graphene.ObjectType):
|
||||||
article = graphene.Field(Article, id=graphene.ID())
|
article = graphene.Field(Article, id=graphene.ID())
|
||||||
articles = DjangoConnectionField(Article)
|
articles = DjangoConnectionField(Article)
|
||||||
|
reporters = DjangoListField(Reporter)
|
||||||
|
|
||||||
def resolve_article(self, args, context, info):
|
def resolve_article(self, args, context, info):
|
||||||
qs = ArticleModel.objects
|
qs = ArticleModel.objects
|
||||||
qs = optimize_queryset(ArticleModel, qs, info.field_asts[0])
|
qs = optimize_queryset(ArticleModel, qs, info.field_asts[0])
|
||||||
return qs.get(**args)
|
return qs.get(**args)
|
||||||
|
|
||||||
|
def resolve_reporters(self, args, context, info):
|
||||||
|
return ReporterModel.objects
|
||||||
|
|
||||||
|
|
||||||
schema = graphene.Schema(query=RootQuery)
|
schema = graphene.Schema(query=RootQuery)
|
||||||
|
|
||||||
|
@ -124,3 +129,25 @@ class TestOptimization(TestCase):
|
||||||
assert len(returned_articles) == 2
|
assert len(returned_articles) == 2
|
||||||
|
|
||||||
self.assertEqual(len(query_context.captured_queries), 4)
|
self.assertEqual(len(query_context.captured_queries), 4)
|
||||||
|
|
||||||
|
def test_manual(self):
|
||||||
|
query = """query {
|
||||||
|
reporters {
|
||||||
|
email
|
||||||
|
favoritePet {
|
||||||
|
email
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
with CaptureQueriesContext(connection) as query_context:
|
||||||
|
results = schema.execute(query)
|
||||||
|
|
||||||
|
returned_reporters = results.data['reporters']
|
||||||
|
assert len(returned_reporters) == 2
|
||||||
|
|
||||||
|
returned_editor = [reporter for reporter in returned_reporters
|
||||||
|
if reporter['email'] == self.editor.email][0]
|
||||||
|
assert returned_editor['favoritePet']['email'] == self.reporter.email
|
||||||
|
|
||||||
|
self.assertEqual(len(query_context.captured_queries), 2)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user