mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-13 13:16:49 +03:00
Improved django GraphQL view testing
This commit is contained in:
parent
5b415a1de6
commit
d47f1d544e
|
@ -24,13 +24,16 @@ class GraphQLView(View):
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def response_errors(self, *errors):
|
||||||
|
return JsonResponse({
|
||||||
|
"errors": [{
|
||||||
|
"message": str(e)
|
||||||
|
} for e in errors]
|
||||||
|
})
|
||||||
|
|
||||||
def execute_query(self, request, query):
|
def execute_query(self, request, query):
|
||||||
if not query:
|
if not query:
|
||||||
data = {
|
return self.response_errors(Exception("Must provide query string."))
|
||||||
"errors": [{
|
|
||||||
"message": "Must provide query string."
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
result = self.schema.execute(query, root=object())
|
result = self.schema.execute(query, root=object())
|
||||||
|
@ -38,9 +41,7 @@ class GraphQLView(View):
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
raise e
|
raise e
|
||||||
data = {
|
return self.response_errors(e)
|
||||||
"errors": [{"message": str(e)}]
|
|
||||||
}
|
|
||||||
|
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
|
@ -48,11 +49,19 @@ class GraphQLView(View):
|
||||||
query = request.GET.get('query')
|
query = request.GET.get('query')
|
||||||
return self.execute_query(request, query or '')
|
return self.execute_query(request, query or '')
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_content_type(request):
|
||||||
|
meta = request.META
|
||||||
|
return meta.get('CONTENT_TYPE', meta.get('HTTP_CONTENT_TYPE', ''))
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
if request.body:
|
content_type = self.get_content_type(request)
|
||||||
received_json_data = json.loads(request.body)
|
if content_type == 'application/json':
|
||||||
query = received_json_data.get('query')
|
try:
|
||||||
|
received_json_data = json.loads(request.body)
|
||||||
|
query = received_json_data.get('query')
|
||||||
|
except ValueError, e:
|
||||||
|
return self.response_errors(ValueError("Malformed json body in the post data"))
|
||||||
else:
|
else:
|
||||||
query = request.POST.get('query') or request.GET.get('query')
|
query = request.POST.get('query') or request.GET.get('query')
|
||||||
raise Exception(query)
|
|
||||||
return self.execute_query(request, query or '')
|
return self.execute_query(request, query or '')
|
||||||
|
|
34
tests/contrib_django/test_urls.py
Normal file
34
tests/contrib_django/test_urls.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
from django.conf.urls import url
|
||||||
|
|
||||||
|
from graphene.contrib.django.views import GraphQLView
|
||||||
|
|
||||||
|
from graphene import Schema
|
||||||
|
from graphene.contrib.django.types import (
|
||||||
|
DjangoNode,
|
||||||
|
DjangoInterface
|
||||||
|
)
|
||||||
|
|
||||||
|
from .models import Reporter, Article
|
||||||
|
|
||||||
|
|
||||||
|
class Character(DjangoNode):
|
||||||
|
class Meta:
|
||||||
|
model = Reporter
|
||||||
|
|
||||||
|
def get_node(self, id):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Human(DjangoNode):
|
||||||
|
class Meta:
|
||||||
|
model = Article
|
||||||
|
|
||||||
|
def get_node(self, id):
|
||||||
|
pass
|
||||||
|
|
||||||
|
schema = Schema(query=Human)
|
||||||
|
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
url(r'^graphql', GraphQLView.as_view(schema=schema)),
|
||||||
|
]
|
94
tests/contrib_django/test_views.py
Normal file
94
tests/contrib_django/test_views.py
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
import json
|
||||||
|
import pytest
|
||||||
|
from py.test import raises
|
||||||
|
from collections import namedtuple
|
||||||
|
from pytest import raises
|
||||||
|
from graphene.core.fields import (
|
||||||
|
Field,
|
||||||
|
StringField,
|
||||||
|
)
|
||||||
|
from graphql.core.type import (
|
||||||
|
GraphQLObjectType,
|
||||||
|
GraphQLInterfaceType
|
||||||
|
)
|
||||||
|
|
||||||
|
from graphene import Schema
|
||||||
|
from graphene.contrib.django.types import (
|
||||||
|
DjangoNode,
|
||||||
|
DjangoInterface
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def format_response(response):
|
||||||
|
return json.loads(response.content)
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_get_no_query(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
response = client.get('/graphql')
|
||||||
|
json_response = format_response(response)
|
||||||
|
assert json_response == {'errors': [{'message': 'Must provide query string.'}]}
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_no_query(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
response = client.post('/graphql', {})
|
||||||
|
print response.content
|
||||||
|
json_response = format_response(response)
|
||||||
|
assert json_response == {'errors': [{'message': 'Must provide query string.'}]}
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_malformed_json(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
response = client.post('/graphql', 'MALFORMED', 'application/json')
|
||||||
|
json_response = format_response(response)
|
||||||
|
assert json_response == {'errors': [{'message': 'Malformed json body in the post data'}]}
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_empty_query(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
response = client.post('/graphql', json.dumps({'query': ''}), 'application/json')
|
||||||
|
json_response = format_response(response)
|
||||||
|
assert json_response == {'errors': [{'message': 'Must provide query string.'}]}
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_bad_query(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
response = client.post('/graphql', json.dumps({'query': '{ MALFORMED'}), 'application/json')
|
||||||
|
json_response = format_response(response)
|
||||||
|
assert 'errors' in json_response
|
||||||
|
assert len(json_response['errors']) == 1
|
||||||
|
assert 'Syntax Error GraphQL' in json_response['errors'][0]['message']
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_get_good_query(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
response = client.get('/graphql', {'query': '{ headline }'})
|
||||||
|
json_response = format_response(response)
|
||||||
|
expected_json = {
|
||||||
|
'data': {
|
||||||
|
'headline': None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert json_response == expected_json
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_good_query(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
response = client.post('/graphql', json.dumps({'query': '{ headline }'}), 'application/json')
|
||||||
|
json_response = format_response(response)
|
||||||
|
expected_json = {
|
||||||
|
'data': {
|
||||||
|
'headline': None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert json_response == expected_json
|
||||||
|
|
||||||
|
|
||||||
|
# def test_client_get_bad_query(settings, client):
|
||||||
|
# settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
||||||
|
# response = client.get('/graphql')
|
||||||
|
# json_response = format_response(response)
|
||||||
|
# assert json_response == {'errors': [{'message': 'Must provide query string.'}]}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user