2016-09-18 02:29:00 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
import graphene
|
|
|
|
from graphene.relay import Node
|
|
|
|
from graphene_django import DjangoConnectionField, DjangoObjectType
|
|
|
|
|
|
|
|
from ...tests.models import Reporter
|
|
|
|
from ..middleware import DjangoDebugMiddleware
|
|
|
|
from ..types import DjangoDebug
|
|
|
|
|
|
|
|
|
|
|
|
class context(object):
|
|
|
|
pass
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
# from examples.starwars_django.models import Character
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_query_field():
|
2018-07-20 02:51:33 +03:00
|
|
|
r1 = Reporter(last_name="ABA")
|
2016-09-18 02:29:00 +03:00
|
|
|
r1.save()
|
2018-07-20 02:51:33 +03:00
|
|
|
r2 = Reporter(last_name="Griffin")
|
2016-09-18 02:29:00 +03:00
|
|
|
r2.save()
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
reporter = graphene.Field(ReporterType)
|
2019-05-20 12:48:28 +03:00
|
|
|
debug = graphene.Field(DjangoDebug, name="_debug")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_reporter(self, info, **args):
|
2016-09-18 02:29:00 +03:00
|
|
|
return Reporter.objects.first()
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterQuery {
|
|
|
|
reporter {
|
|
|
|
lastName
|
|
|
|
}
|
2019-05-20 12:48:28 +03:00
|
|
|
_debug {
|
2016-09-18 02:29:00 +03:00
|
|
|
sql {
|
|
|
|
rawSql
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-09-18 02:29:00 +03:00
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"reporter": {"lastName": "ABA"},
|
2019-06-09 22:41:04 +03:00
|
|
|
"_debug": {"sql": [{"rawSql": str(Reporter.objects.order_by("pk")[:1].query)}]},
|
2016-09-18 02:29:00 +03:00
|
|
|
}
|
2016-09-20 10:08:14 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
result = schema.execute(
|
|
|
|
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_query_list():
|
2018-07-20 02:51:33 +03:00
|
|
|
r1 = Reporter(last_name="ABA")
|
2016-09-18 02:29:00 +03:00
|
|
|
r1.save()
|
2018-07-20 02:51:33 +03:00
|
|
|
r2 = Reporter(last_name="Griffin")
|
2016-09-18 02:29:00 +03:00
|
|
|
r2.save()
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = graphene.List(ReporterType)
|
2019-05-20 12:48:28 +03:00
|
|
|
debug = graphene.Field(DjangoDebug, name="_debug")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_all_reporters(self, info, **args):
|
2016-09-18 02:29:00 +03:00
|
|
|
return Reporter.objects.all()
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterQuery {
|
|
|
|
allReporters {
|
|
|
|
lastName
|
|
|
|
}
|
2019-05-20 12:48:28 +03:00
|
|
|
_debug {
|
2016-09-18 02:29:00 +03:00
|
|
|
sql {
|
|
|
|
rawSql
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
2016-09-18 02:29:00 +03:00
|
|
|
expected = {
|
2018-07-20 02:51:33 +03:00
|
|
|
"allReporters": [{"lastName": "ABA"}, {"lastName": "Griffin"}],
|
2019-05-20 12:48:28 +03:00
|
|
|
"_debug": {"sql": [{"rawSql": str(Reporter.objects.all().query)}]},
|
2016-09-18 02:29:00 +03:00
|
|
|
}
|
2016-09-20 10:08:14 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
result = schema.execute(
|
|
|
|
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert not result.errors
|
|
|
|
assert result.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_query_connection():
|
2018-07-20 02:51:33 +03:00
|
|
|
r1 = Reporter(last_name="ABA")
|
2016-09-18 02:29:00 +03:00
|
|
|
r1.save()
|
2018-07-20 02:51:33 +03:00
|
|
|
r2 = Reporter(last_name="Griffin")
|
2016-09-18 02:29:00 +03:00
|
|
|
r2.save()
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
all_reporters = DjangoConnectionField(ReporterType)
|
2019-05-20 12:48:28 +03:00
|
|
|
debug = graphene.Field(DjangoDebug, name="_debug")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_all_reporters(self, info, **args):
|
2016-09-18 02:29:00 +03:00
|
|
|
return Reporter.objects.all()
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterQuery {
|
|
|
|
allReporters(first:1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
lastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 12:48:28 +03:00
|
|
|
_debug {
|
2016-09-18 02:29:00 +03:00
|
|
|
sql {
|
|
|
|
rawSql
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
|
|
|
expected = {"allReporters": {"edges": [{"node": {"lastName": "ABA"}}]}}
|
2016-09-20 10:08:14 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
result = schema.execute(
|
|
|
|
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert not result.errors
|
2018-07-20 02:51:33 +03:00
|
|
|
assert result.data["allReporters"] == expected["allReporters"]
|
2019-05-20 12:48:28 +03:00
|
|
|
assert "COUNT" in result.data["_debug"]["sql"][0]["rawSql"]
|
2016-09-18 02:29:00 +03:00
|
|
|
query = str(Reporter.objects.all()[:1].query)
|
2019-05-20 12:48:28 +03:00
|
|
|
assert result.data["_debug"]["sql"][1]["rawSql"] == query
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_should_query_connectionfilter():
|
|
|
|
from ...filter import DjangoFilterConnectionField
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
r1 = Reporter(last_name="ABA")
|
2016-09-18 02:29:00 +03:00
|
|
|
r1.save()
|
2018-07-20 02:51:33 +03:00
|
|
|
r2 = Reporter(last_name="Griffin")
|
2016-09-18 02:29:00 +03:00
|
|
|
r2.save()
|
|
|
|
|
|
|
|
class ReporterType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Reporter
|
2018-07-20 02:51:33 +03:00
|
|
|
interfaces = (Node,)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
2018-07-20 02:51:33 +03:00
|
|
|
all_reporters = DjangoFilterConnectionField(ReporterType, fields=["last_name"])
|
2016-09-18 02:29:00 +03:00
|
|
|
s = graphene.String(resolver=lambda *_: "S")
|
2019-05-20 12:48:28 +03:00
|
|
|
debug = graphene.Field(DjangoDebug, name="_debug")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_all_reporters(self, info, **args):
|
2016-09-18 02:29:00 +03:00
|
|
|
return Reporter.objects.all()
|
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
query = """
|
2016-09-18 02:29:00 +03:00
|
|
|
query ReporterQuery {
|
|
|
|
allReporters(first:1) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
lastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 12:48:28 +03:00
|
|
|
_debug {
|
2016-09-18 02:29:00 +03:00
|
|
|
sql {
|
|
|
|
rawSql
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 02:51:33 +03:00
|
|
|
"""
|
|
|
|
expected = {"allReporters": {"edges": [{"node": {"lastName": "ABA"}}]}}
|
2016-09-20 10:08:14 +03:00
|
|
|
schema = graphene.Schema(query=Query)
|
2018-07-20 02:51:33 +03:00
|
|
|
result = schema.execute(
|
|
|
|
query, context_value=context(), middleware=[DjangoDebugMiddleware()]
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
assert not result.errors
|
2018-07-20 02:51:33 +03:00
|
|
|
assert result.data["allReporters"] == expected["allReporters"]
|
2019-05-20 12:48:28 +03:00
|
|
|
assert "COUNT" in result.data["_debug"]["sql"][0]["rawSql"]
|
2016-09-18 02:29:00 +03:00
|
|
|
query = str(Reporter.objects.all()[:1].query)
|
2019-05-20 12:48:28 +03:00
|
|
|
assert result.data["_debug"]["sql"][1]["rawSql"] == query
|