2019-03-29 13:51:40 +03:00
|
|
|
Testing API calls with django
|
|
|
|
=============================
|
|
|
|
|
2020-08-05 22:24:16 +03:00
|
|
|
Using unittest
|
|
|
|
--------------
|
|
|
|
|
2019-03-29 13:51:40 +03:00
|
|
|
If you want to unittest your API calls derive your test case from the class `GraphQLTestCase`.
|
|
|
|
|
2020-04-06 11:58:55 +03:00
|
|
|
Your endpoint is set through the `GRAPHQL_URL` attribute on `GraphQLTestCase`. The default endpoint is `GRAPHQL_URL = "/graphql/"`.
|
|
|
|
|
2019-03-29 13:51:40 +03:00
|
|
|
Usage:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
2019-05-09 00:45:28 +03:00
|
|
|
from graphene_django.utils.testing import GraphQLTestCase
|
2019-03-29 13:51:40 +03:00
|
|
|
|
|
|
|
class MyFancyTestCase(GraphQLTestCase):
|
|
|
|
def test_some_query(self):
|
|
|
|
response = self.query(
|
|
|
|
'''
|
|
|
|
query {
|
|
|
|
myModel {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
''',
|
2022-03-03 16:58:48 +03:00
|
|
|
operation_name='myModel'
|
2019-03-29 13:51:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
content = json.loads(response.content)
|
|
|
|
|
|
|
|
# This validates the status code and if you get errors
|
|
|
|
self.assertResponseNoErrors(response)
|
|
|
|
|
|
|
|
# Add some more asserts if you like
|
|
|
|
...
|
|
|
|
|
2019-09-07 21:49:29 +03:00
|
|
|
def test_query_with_variables(self):
|
|
|
|
response = self.query(
|
|
|
|
'''
|
|
|
|
query myModel($id: Int!){
|
|
|
|
myModel(id: $id) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
''',
|
2022-03-03 16:58:48 +03:00
|
|
|
operation_name='myModel',
|
2019-09-07 21:49:29 +03:00
|
|
|
variables={'id': 1}
|
|
|
|
)
|
|
|
|
|
|
|
|
content = json.loads(response.content)
|
|
|
|
|
|
|
|
# This validates the status code and if you get errors
|
|
|
|
self.assertResponseNoErrors(response)
|
|
|
|
|
|
|
|
# Add some more asserts if you like
|
|
|
|
...
|
|
|
|
|
2019-03-29 13:51:40 +03:00
|
|
|
def test_some_mutation(self):
|
|
|
|
response = self.query(
|
|
|
|
'''
|
|
|
|
mutation myMutation($input: MyMutationInput!) {
|
|
|
|
myMutation(input: $input) {
|
|
|
|
my-model {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
''',
|
2022-03-03 16:58:48 +03:00
|
|
|
operation_name='myMutation',
|
2019-03-29 13:51:40 +03:00
|
|
|
input_data={'my_field': 'foo', 'other_field': 'bar'}
|
|
|
|
)
|
|
|
|
|
|
|
|
# This validates the status code and if you get errors
|
|
|
|
self.assertResponseNoErrors(response)
|
|
|
|
|
|
|
|
# Add some more asserts if you like
|
2021-02-23 07:24:02 +03:00
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
For testing mutations that are executed within a transaction you should subclass `GraphQLTransactionTestCase`
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from graphene_django.utils.testing import GraphQLTransactionTestCase
|
|
|
|
|
|
|
|
class MyFancyTransactionTestCase(GraphQLTransactionTestCase):
|
|
|
|
|
|
|
|
def test_some_mutation_that_executes_within_a_transaction(self):
|
|
|
|
response = self.query(
|
|
|
|
'''
|
|
|
|
mutation myMutation($input: MyMutationInput!) {
|
|
|
|
myMutation(input: $input) {
|
|
|
|
my-model {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
''',
|
2022-03-03 16:58:48 +03:00
|
|
|
operation_name='myMutation',
|
2021-02-23 07:24:02 +03:00
|
|
|
input_data={'my_field': 'foo', 'other_field': 'bar'}
|
|
|
|
)
|
|
|
|
|
|
|
|
# This validates the status code and if you get errors
|
|
|
|
self.assertResponseNoErrors(response)
|
|
|
|
|
|
|
|
# Add some more asserts if you like
|
2019-03-29 13:51:40 +03:00
|
|
|
...
|
2020-08-05 22:24:16 +03:00
|
|
|
|
|
|
|
Using pytest
|
|
|
|
------------
|
|
|
|
|
|
|
|
To use pytest define a simple fixture using the query helper below
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
# Create a fixture using the graphql_query helper and `client` fixture from `pytest-django`.
|
2020-12-31 02:37:57 +03:00
|
|
|
import json
|
2020-08-05 22:24:16 +03:00
|
|
|
import pytest
|
|
|
|
from graphene_django.utils.testing import graphql_query
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-12-31 02:37:57 +03:00
|
|
|
def client_query(client):
|
2020-08-05 22:24:16 +03:00
|
|
|
def func(*args, **kwargs):
|
|
|
|
return graphql_query(*args, **kwargs, client=client)
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
# Test you query using the client_query fixture
|
|
|
|
def test_some_query(client_query):
|
2020-12-31 02:37:57 +03:00
|
|
|
response = client_query(
|
2020-08-05 22:24:16 +03:00
|
|
|
'''
|
|
|
|
query {
|
|
|
|
myModel {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
''',
|
2022-03-03 16:58:48 +03:00
|
|
|
operation_name='myModel'
|
2020-08-05 22:24:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
content = json.loads(response.content)
|
2020-12-31 02:37:57 +03:00
|
|
|
assert 'errors' not in content
|