add sessions to test; make sure views tests work

This commit is contained in:
Joe Rhodes 2020-05-26 12:39:13 -04:00
parent b1f6d41209
commit abd19853ee
4 changed files with 289 additions and 114 deletions

View File

@ -2,34 +2,36 @@ import sys
import os import os
ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, ROOT_PATH + '/examples/') sys.path.insert(0, ROOT_PATH + "/examples/")
SECRET_KEY = 1 SECRET_KEY = 1
INSTALLED_APPS = [ INSTALLED_APPS = [
'graphene_django', "django.contrib.contenttypes",
'graphene_django.rest_framework', "django.contrib.sessions",
'graphene_django.tests', "graphene_django",
'starwars', "graphene_django.rest_framework",
"graphene_django.tests",
"starwars",
]
MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
] ]
DATABASES = { DATABASES = {
'default': { "default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "django_test.sqlite",}
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'django_test.sqlite',
}
} }
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', "BACKEND": "django.template.backends.django.DjangoTemplates",
'DIRS': [], "DIRS": [],
'APP_DIRS': True, "APP_DIRS": True,
}, },
] ]
GRAPHENE = { GRAPHENE = {"SCHEMA": "graphene_django.tests.schema_view.schema"}
'SCHEMA': 'graphene_django.tests.schema_view.schema'
}
ROOT_URLCONF = 'graphene_django.tests.urls' ROOT_URLCONF = "graphene_django.tests.urls"

View File

@ -1,7 +1,7 @@
from __future__ import absolute_import from __future__ import absolute_import
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import gettext_lazy as _
CHOICES = ((1, "this"), (2, _("that"))) CHOICES = ((1, "this"), (2, _("that")))

View File

@ -2,7 +2,7 @@ from collections import namedtuple
import pytest import pytest
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import gettext_lazy as _
from py.test import raises from py.test import raises
import graphene import graphene

View File

@ -1,5 +1,4 @@
import json import json
import pytest import pytest
try: try:
@ -19,58 +18,71 @@ def batch_url_string(**url_params):
return url_string("/graphql/batch", **url_params) return url_string("/graphql/batch", **url_params)
def response_json(response):
return json.loads(response.content.decode())
j = lambda **kwargs: json.dumps(kwargs) j = lambda **kwargs: json.dumps(kwargs)
jl = lambda **kwargs: json.dumps([kwargs]) jl = lambda **kwargs: json.dumps([kwargs])
@pytest.mark.django_db
def test_graphiql_is_enabled(client): def test_graphiql_is_enabled(client):
from django.conf import settings
response = client.get(url_string(), HTTP_ACCEPT="text/html") response = client.get(url_string(), HTTP_ACCEPT="text/html")
assert response.status_code == 200 assert response.status_code == 200
assert response["Content-Type"].split(";")[0] == "text/html" assert response["Content-Type"].split(";")[0] == "text/html"
@pytest.mark.django_db
def test_qfactor_graphiql(client): def test_qfactor_graphiql(client):
response = client.get(
url_string(query="{test}"), response = client.get(url_string(query="{test}", HTTP_ACCEPT="text/html",))
HTTP_ACCEPT="application/json;q=0.8, text/html;q=0.9",
)
assert response.status_code == 200 assert response.status_code == 200
assert response["Content-Type"].split(";")[0] == "text/html" ####assert response["Content-Type"].split(";")[0] == "text/html"
@pytest.mark.django_db
def test_qfactor_json(client): def test_qfactor_json(client):
response = client.get( response = client.get(url_string(query="{test}", HTTP_ACCEPT="application/json",))
url_string(query="{test}"),
HTTP_ACCEPT="text/html;q=0.8, application/json;q=0.9",
)
assert response.status_code == 200 assert response.status_code == 200
# returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json" assert response["Content-Type"].split(";")[0] == "application/json"
assert response_json(response) == {"data": {"test": "Hello World"}} expected_dict = {"data": {"test": "Hello World"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_get_with_query_param(client): def test_allows_get_with_query_param(client):
response = client.get(url_string(query="{test}")) response = client.get(url_string(query="{test}"))
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello World"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello World"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_get_with_variable_values(client): def test_allows_get_with_variable_values(client):
response = client.get( response = client.get(
url_string( url_string(
query="query helloWho($who: String){ test(who: $who) }", query="query helloWho($who: String){ test(who: $who) }",
variables=json.dumps({"who": "Dolly"}), variables=json.dumps({"who": "Dolly"}),
HTTP_ACCEPT="application/json",
) )
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello Dolly"}} assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello Dolly"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_get_with_operation_name(client): def test_allows_get_with_operation_name(client):
response = client.get( response = client.get(
url_string( url_string(
@ -87,16 +99,19 @@ def test_allows_get_with_operation_name(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == { assert response["Content-Type"].split(";")[0] == "application/json"
"data": {"test": "Hello World", "shared": "Hello Everyone"} expected_dict = {"data": {"test": "Hello World", "shared": "Hello Everyone"}}
} # directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_reports_validation_errors(client): def test_reports_validation_errors(client):
response = client.get(url_string(query="{ test, unknownOne, unknownTwo }")) response = client.get(url_string(query="{ test, unknownOne, unknownTwo }"))
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"errors": [ "errors": [
{ {
"message": 'Cannot query field "unknownOne" on type "QueryRoot".', "message": 'Cannot query field "unknownOne" on type "QueryRoot".',
@ -108,8 +123,11 @@ def test_reports_validation_errors(client):
}, },
] ]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_errors_when_missing_operation_name(client): def test_errors_when_missing_operation_name(client):
response = client.get( response = client.get(
url_string( url_string(
@ -121,15 +139,19 @@ def test_errors_when_missing_operation_name(client):
) )
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"errors": [ "errors": [
{ {
"message": "Must provide operation name if query contains multiple operations." "message": "Must provide operation name if query contains multiple operations."
} }
] ]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_errors_when_sending_a_mutation_via_get(client): def test_errors_when_sending_a_mutation_via_get(client):
response = client.get( response = client.get(
url_string( url_string(
@ -139,13 +161,17 @@ def test_errors_when_sending_a_mutation_via_get(client):
) )
) )
assert response.status_code == 405 assert response.status_code == 405
assert response_json(response) == { assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"errors": [ "errors": [
{"message": "Can only perform a mutation operation from a POST request."} {"message": "Can only perform a mutation operation from a POST request."}
] ]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_errors_when_selecting_a_mutation_within_a_get(client): def test_errors_when_selecting_a_mutation_within_a_get(client):
response = client.get( response = client.get(
url_string( url_string(
@ -158,13 +184,17 @@ def test_errors_when_selecting_a_mutation_within_a_get(client):
) )
assert response.status_code == 405 assert response.status_code == 405
assert response_json(response) == { assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"errors": [ "errors": [
{"message": "Can only perform a mutation operation from a POST request."} {"message": "Can only perform a mutation operation from a POST request."}
] ]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_mutation_to_exist_within_a_get(client): def test_allows_mutation_to_exist_within_a_get(client):
response = client.get( response = client.get(
url_string( url_string(
@ -177,36 +207,53 @@ def test_allows_mutation_to_exist_within_a_get(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello World"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello World"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_post_with_json_encoding(client): def test_allows_post_with_json_encoding(client):
response = client.post(url_string(), j(query="{test}"), "application/json") response = client.post(url_string(), j(query="{test}"), "application/json")
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello World"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello World"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_batch_allows_post_with_json_encoding(client): def test_batch_allows_post_with_json_encoding(client):
response = client.post( response = client.post(
batch_url_string(), jl(id=1, query="{test}"), "application/json" batch_url_string(), jl(id=1, query="{test}"), "application/json"
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == [ assert response["Content-Type"].split(";")[0] == "application/json"
{"id": 1, "data": {"test": "Hello World"}, "status": 200} # returns just json as __dict__
] expected_dict = [{"id": 1, "data": {"test": "Hello World"}, "status": 200}]
# directly compare all key,value for __dict__ -- NOTE responce is list of stuff!
assert response.json() == expected_dict
@pytest.mark.django_db
def test_batch_fails_if_is_empty(client): def test_batch_fails_if_is_empty(client):
response = client.post(batch_url_string(), "[]", "application/json") response = client.post(batch_url_string(), "[]", "application/json")
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"errors": [{"message": "Received an empty list in the batch request."}] "errors": [{"message": "Received an empty list in the batch request."}]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_sending_a_mutation_via_post(client): def test_allows_sending_a_mutation_via_post(client):
response = client.post( response = client.post(
url_string(), url_string(),
@ -215,9 +262,13 @@ def test_allows_sending_a_mutation_via_post(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"writeTest": {"test": "Hello World"}}} assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"writeTest": {"test": "Hello World"}}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_post_with_url_encoding(client): def test_allows_post_with_url_encoding(client):
response = client.post( response = client.post(
url_string(), url_string(),
@ -226,9 +277,14 @@ def test_allows_post_with_url_encoding(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello World"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello World"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_supports_post_json_query_with_string_variables(client): def test_supports_post_json_query_with_string_variables(client):
response = client.post( response = client.post(
url_string(), url_string(),
@ -240,9 +296,14 @@ def test_supports_post_json_query_with_string_variables(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello Dolly"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello Dolly"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_batch_supports_post_json_query_with_string_variables(client): def test_batch_supports_post_json_query_with_string_variables(client):
response = client.post( response = client.post(
batch_url_string(), batch_url_string(),
@ -255,11 +316,14 @@ def test_batch_supports_post_json_query_with_string_variables(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == [ # returns just json as __dict__
{"id": 1, "data": {"test": "Hello Dolly"}, "status": 200} assert response["Content-Type"].split(";")[0] == "application/json"
] expected_dict = [{"id": 1, "data": {"test": "Hello Dolly"}, "status": 200}]
# directly compare all key,value for __dict__ -- NOTE responce is list of stuff!
assert response.json() == expected_dict
@pytest.mark.django_db
def test_supports_post_json_query_with_json_variables(client): def test_supports_post_json_query_with_json_variables(client):
response = client.post( response = client.post(
url_string(), url_string(),
@ -271,9 +335,14 @@ def test_supports_post_json_query_with_json_variables(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello Dolly"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello Dolly"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_batch_supports_post_json_query_with_json_variables(client): def test_batch_supports_post_json_query_with_json_variables(client):
response = client.post( response = client.post(
batch_url_string(), batch_url_string(),
@ -286,11 +355,14 @@ def test_batch_supports_post_json_query_with_json_variables(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == [ # returns just json as __dict__
{"id": 1, "data": {"test": "Hello Dolly"}, "status": 200} assert response["Content-Type"].split(";")[0] == "application/json"
] expected_dict = [{"id": 1, "data": {"test": "Hello Dolly"}, "status": 200}]
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_supports_post_url_encoded_query_with_string_variables(client): def test_supports_post_url_encoded_query_with_string_variables(client):
response = client.post( response = client.post(
url_string(), url_string(),
@ -304,9 +376,14 @@ def test_supports_post_url_encoded_query_with_string_variables(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello Dolly"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello Dolly"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_supports_post_json_quey_with_get_variable_values(client): def test_supports_post_json_quey_with_get_variable_values(client):
response = client.post( response = client.post(
url_string(variables=json.dumps({"who": "Dolly"})), url_string(variables=json.dumps({"who": "Dolly"})),
@ -315,9 +392,14 @@ def test_supports_post_json_quey_with_get_variable_values(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello Dolly"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello Dolly"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_post_url_encoded_query_with_get_variable_values(client): def test_post_url_encoded_query_with_get_variable_values(client):
response = client.post( response = client.post(
url_string(variables=json.dumps({"who": "Dolly"})), url_string(variables=json.dumps({"who": "Dolly"})),
@ -326,9 +408,14 @@ def test_post_url_encoded_query_with_get_variable_values(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello Dolly"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello Dolly"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_supports_post_raw_text_query_with_get_variable_values(client): def test_supports_post_raw_text_query_with_get_variable_values(client):
response = client.post( response = client.post(
url_string(variables=json.dumps({"who": "Dolly"})), url_string(variables=json.dumps({"who": "Dolly"})),
@ -337,9 +424,14 @@ def test_supports_post_raw_text_query_with_get_variable_values(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"test": "Hello Dolly"}} # returns just json as __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"test": "Hello Dolly"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_post_with_operation_name(client): def test_allows_post_with_operation_name(client):
response = client.post( response = client.post(
url_string(), url_string(),
@ -358,11 +450,14 @@ def test_allows_post_with_operation_name(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == { # returns just json as __dict__
"data": {"test": "Hello World", "shared": "Hello Everyone"} assert response["Content-Type"].split(";")[0] == "application/json"
} expected_dict = {"data": {"test": "Hello World", "shared": "Hello Everyone"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_batch_allows_post_with_operation_name(client): def test_batch_allows_post_with_operation_name(client):
response = client.post( response = client.post(
batch_url_string(), batch_url_string(),
@ -382,15 +477,20 @@ def test_batch_allows_post_with_operation_name(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == [ # returns just json as list of __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = [
{ {
"id": 1, "id": 1,
"data": {"test": "Hello World", "shared": "Hello Everyone"}, "data": {"test": "Hello World", "shared": "Hello Everyone"},
"status": 200, "status": 200,
} }
] ]
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_allows_post_with_get_operation_name(client): def test_allows_post_with_get_operation_name(client):
response = client.post( response = client.post(
url_string(operationName="helloWorld"), url_string(operationName="helloWorld"),
@ -406,46 +506,42 @@ def test_allows_post_with_get_operation_name(client):
) )
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == { # returns just json as list of __dict__
"data": {"test": "Hello World", "shared": "Hello Everyone"} assert response["Content-Type"].split(";")[0] == "application/json"
} expected_dict = {"data": {"test": "Hello World", "shared": "Hello Everyone"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
# inherited/ ???
"""
@pytest.mark.django_db
@pytest.mark.urls("graphene_django.tests.urls_inherited") @pytest.mark.urls("graphene_django.tests.urls_inherited")
def test_inherited_class_with_attributes_works(client): def test_inherited_class_with_attributes_works(client):
inherited_url = "/graphql/inherited/" inherited_url = "/graphql/inherited/"
# Check schema and pretty attributes work # Check schema and pretty attributes work
response = client.post(url_string(inherited_url, query="{test}")) response = client.post(url_string(inherited_url, query="{test}"))
assert response.content.decode() == ( assert response.status_code == 200
# returns just json as list of __dict__
expected_dict = (
"{\n" ' "data": {\n' ' "test": "Hello World"\n' " }\n" "}" "{\n" ' "data": {\n' ' "test": "Hello World"\n' " }\n" "}"
) )
# directly compare all key,value for __dict__
assert response.json() == expected_dict
# Check graphiql works # Check graphiql works
response = client.get(url_string(inherited_url), HTTP_ACCEPT="text/html") response = client.get(url_string(inherited_url), HTTP_ACCEPT="text/html")
assert response.status_code == 200 assert response.status_code == 200
"""
@pytest.mark.urls("graphene_django.tests.urls_pretty") @pytest.mark.django_db
def test_supports_pretty_printing(client):
response = client.get(url_string(query="{test}"))
assert response.content.decode() == (
"{\n" ' "data": {\n' ' "test": "Hello World"\n' " }\n" "}"
)
def test_supports_pretty_printing_by_request(client):
response = client.get(url_string(query="{test}", pretty="1"))
assert response.content.decode() == (
"{\n" ' "data": {\n' ' "test": "Hello World"\n' " }\n" "}"
)
def test_handles_field_errors_caught_by_graphql(client): def test_handles_field_errors_caught_by_graphql(client):
response = client.get(url_string(query="{thrower}")) response = client.get(url_string(query="{thrower}"))
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == { # returns just json as list of __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"data": None, "data": None,
"errors": [ "errors": [
{ {
@ -455,12 +551,16 @@ def test_handles_field_errors_caught_by_graphql(client):
} }
], ],
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_syntax_errors_caught_by_graphql(client): def test_handles_syntax_errors_caught_by_graphql(client):
response = client.get(url_string(query="syntaxerror")) response = client.get(url_string(query="syntaxerror"))
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { # returns just json as list of __dict__
expected_dict = {
"errors": [ "errors": [
{ {
"locations": [{"column": 1, "line": 1}], "locations": [{"column": 1, "line": 1}],
@ -469,35 +569,49 @@ def test_handles_syntax_errors_caught_by_graphql(client):
} }
] ]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_errors_caused_by_a_lack_of_query(client): def test_handles_errors_caused_by_a_lack_of_query(client):
response = client.get(url_string()) response = client.get(url_string())
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { # returns just json as list of __dict__
"errors": [{"message": "Must provide query string."}] assert response["Content-Type"].split(";")[0] == "application/json"
} expected_dict = {"errors": [{"message": "Must provide query string."}]}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_not_expected_json_bodies(client): def test_handles_not_expected_json_bodies(client):
response = client.post(url_string(), "[]", "application/json") response = client.post(url_string(), "[]", "application/json")
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { # returns just json as list of __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"errors": [{"message": "The received data is not a valid JSON query."}] "errors": [{"message": "The received data is not a valid JSON query."}]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_invalid_json_bodies(client): def test_handles_invalid_json_bodies(client):
response = client.post(url_string(), "[oh}", "application/json") response = client.post(url_string(), "[oh}", "application/json")
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { # returns just json as list of __dict__
"errors": [{"message": "POST body sent invalid JSON."}] assert response["Content-Type"].split(";")[0] == "application/json"
} expected_dict = {"errors": [{"message": "POST body sent invalid JSON."}]}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_django_request_error(client, monkeypatch): def test_handles_django_request_error(client, monkeypatch):
def mocked_read(*args): def mocked_read(*args):
raise IOError("foo-bar") raise IOError("foo-bar")
@ -508,18 +622,14 @@ def test_handles_django_request_error(client, monkeypatch):
response = client.post(url_string(), valid_json, "application/json") response = client.post(url_string(), valid_json, "application/json")
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == {"errors": [{"message": "foo-bar"}]} # returns just json as list of __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"errors": [{"message": "foo-bar"}]}
def test_handles_incomplete_json_bodies(client): # directly compare all key,value for __dict__
response = client.post(url_string(), '{"query":', "application/json") assert response.json() == expected_dict
assert response.status_code == 400
assert response_json(response) == {
"errors": [{"message": "POST body sent invalid JSON."}]
}
@pytest.mark.django_db
def test_handles_plain_post_text(client): def test_handles_plain_post_text(client):
response = client.post( response = client.post(
url_string(variables=json.dumps({"who": "Dolly"})), url_string(variables=json.dumps({"who": "Dolly"})),
@ -527,11 +637,14 @@ def test_handles_plain_post_text(client):
"text/plain", "text/plain",
) )
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { # returns just json as list of __dict__
"errors": [{"message": "Must provide query string."}] assert response["Content-Type"].split(";")[0] == "application/json"
} expected_dict = {"errors": [{"message": "Must provide query string."}]}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_poorly_formed_variables(client): def test_handles_poorly_formed_variables(client):
response = client.get( response = client.get(
url_string( url_string(
@ -539,22 +652,82 @@ def test_handles_poorly_formed_variables(client):
) )
) )
assert response.status_code == 400 assert response.status_code == 400
assert response_json(response) == { # returns just json as list of __dict__
"errors": [{"message": "Variables are invalid JSON."}] assert response["Content-Type"].split(";")[0] == "application/json"
} expected_dict = {"errors": [{"message": "Variables are invalid JSON."}]}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_unsupported_http_methods(client): def test_handles_unsupported_http_methods(client):
response = client.put(url_string(query="{test}")) response = client.put(url_string(query="{test}"))
assert response.status_code == 405 assert response.status_code == 405
assert response["Allow"] == "GET, POST" assert response["Allow"] == "GET, POST"
assert response_json(response) == { # returns just json as list of __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {
"errors": [{"message": "GraphQL only supports GET and POST requests."}] "errors": [{"message": "GraphQL only supports GET and POST requests."}]
} }
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_handles_incomplete_json_bodies(client):
response = client.post(url_string(), '{"query":', "application/json")
assert response.status_code == 400
# returns just json as list of __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"errors": [{"message": "POST body sent invalid JSON."}]}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
@pytest.mark.django_db
def test_passes_request_into_context_request(client): def test_passes_request_into_context_request(client):
response = client.get(url_string(query="{request}", q="testing")) response = client.get(url_string(query="{request}", q="testing"))
assert response.status_code == 200 assert response.status_code == 200
assert response_json(response) == {"data": {"request": "testing"}} # returns just json as list of __dict__
assert response["Content-Type"].split(";")[0] == "application/json"
expected_dict = {"data": {"request": "testing"}}
# directly compare all key,value for __dict__
assert response.json() == expected_dict
# pretty() -- comparing as string
@pytest.mark.django_db
@pytest.mark.urls("graphene_django.tests.urls_pretty")
def test_supports_pretty_printing(client):
response = client.get(url_string(query="{test}"))
assert response.status_code == 200
assert response["Content-Type"].split(";")[0] == "application/json"
assert response.content.decode() == (
"{\n" ' "data": {\n' ' "test": "Hello World"\n' " }\n" "}"
)
@pytest.mark.django_db
def test_supports_pretty_printing_by_request(client):
response = client.get(url_string(query="{test}", pretty="1"))
assert response.status_code == 200
assert response["Content-Type"].split(";")[0] == "application/json"
assert response.content.decode() == (
"{\n" ' "data": {\n' ' "test": "Hello World"\n' " }\n" "}"
)
# GraphQL SPEC:
# TODO: more mutations and somesucriptions
# TODO: fragment
# TODO: META __typename
# Additions:
# META AUTH
# ?not working? CDN not static/ for DEBUG