graphene/tests/contrib_django/test_views.py

90 lines
3.1 KiB
Python
Raw Normal View History

2015-10-03 23:27:19 +03:00
import json
def format_response(response):
2015-10-06 08:59:23 +03:00
return json.loads(response.content.decode())
2015-10-03 23:27:19 +03:00
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.'}]}
2015-10-03 23:27:19 +03:00
def test_client_post_no_query(settings, client):
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
response = client.post('/graphql', {})
json_response = format_response(response)
assert json_response == {'errors': [
{'message': 'Must provide query string.'}]}
2015-10-03 23:27:19 +03:00
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'}]}
2015-10-03 23:27:19 +03:00
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')
2015-10-03 23:27:19 +03:00
json_response = format_response(response)
assert json_response == {'errors': [
{'message': 'Must provide query string.'}]}
2015-10-03 23:27:19 +03:00
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')
2015-10-03 23:27:19 +03:00
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
2015-10-11 00:53:46 +03:00
def test_client_get_good_query_with_raise(settings, client):
settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
response = client.get('/graphql', {'query': '{ raises }'})
json_response = format_response(response)
assert json_response['errors'][0][
'message'] == 'This field should raise exception'
2015-10-11 00:53:46 +03:00
assert json_response['data']['raises'] is None
2015-10-03 23:27:19 +03:00
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')
2015-10-03 23:27:19 +03:00
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.'}]}