Document using graphql_query in a pytest fixture

Also add a test
This commit is contained in:
Nikolai R Kristiansen 2020-07-31 15:57:42 +02:00
parent 79cb43e01f
commit 091772da73
No known key found for this signature in database
GPG Key ID: 1A24E7511E55EF87
2 changed files with 52 additions and 0 deletions

View File

@ -1,6 +1,9 @@
Testing API calls with django Testing API calls with django
============================= =============================
Using unittest
--------------
If you want to unittest your API calls derive your test case from the class `GraphQLTestCase`. If you want to unittest your API calls derive your test case from the class `GraphQLTestCase`.
Your endpoint is set through the `GRAPHQL_URL` attribute on `GraphQLTestCase`. The default endpoint is `GRAPHQL_URL = "/graphql/"`. Your endpoint is set through the `GRAPHQL_URL` attribute on `GraphQLTestCase`. The default endpoint is `GRAPHQL_URL = "/graphql/"`.
@ -78,3 +81,38 @@ Usage:
# Add some more asserts if you like # Add some more asserts if you like
... ...
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`.
import pytest
from graphene_django.utils.testing import graphql_query
@pytest.fixture
def client_query(client)
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):
response = graphql_query(
'''
query {
myModel {
id
name
}
}
''',
op_name='myModel'
)
content = json.loads(response.content)
assert 'errors' not in content

View File

@ -71,3 +71,17 @@ def test_graphql_query_case_op_name(post_mock):
"operationName", "operationName",
"QueryName", "QueryName",
) in body.items(), "Field 'operationName' is not present in the final request." ) in body.items(), "Field 'operationName' is not present in the final request."
@pytest.fixture
def client_query(client):
def func(*args, **kwargs):
return graphql_query(*args, **kwargs, client=client)
return func
def test_pytest_fixture_usage(client_query):
response = graphql_query("query { test }")
content = json.loads(response.content)
assert content == {"data": {"test": "Hello World"}}