mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-12 09:12:18 +03:00
Document using graphql_query in a pytest fixture
Also add a test
This commit is contained in:
parent
79cb43e01f
commit
091772da73
|
@ -1,6 +1,9 @@
|
|||
Testing API calls with django
|
||||
=============================
|
||||
|
||||
Using unittest
|
||||
--------------
|
||||
|
||||
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/"`.
|
||||
|
@ -78,3 +81,38 @@ Usage:
|
|||
|
||||
# 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
|
|
@ -71,3 +71,17 @@ def test_graphql_query_case_op_name(post_mock):
|
|||
"operationName",
|
||||
"QueryName",
|
||||
) 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"}}
|
||||
|
|
Loading…
Reference in New Issue
Block a user