Skip to content

Added GraphQLTransactionTestCase #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@ Usage:
# Add some more asserts if you like
...


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
}
}
}
''',
op_name='myMutation',
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
...

Using pytest
------------

Expand Down
2 changes: 1 addition & 1 deletion graphene_django/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ def func(*args, **kwargs):


def test_pytest_fixture_usage(client_query):
response = graphql_query("query { test }")
response = client_query("query { test }")
content = json.loads(response.content)
assert content == {"data": {"test": "Hello World"}}
12 changes: 10 additions & 2 deletions graphene_django/utils/testing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import warnings

from django.test import Client, TestCase
from django.test import Client, TestCase, TransactionTestCase

DEFAULT_GRAPHQL_URL = "/graphql/"

Expand Down Expand Up @@ -61,7 +61,7 @@ def graphql_query(
return resp


class GraphQLTestCase(TestCase):
class GraphQLTestMixin(object):
"""
Based on: https://www.sam.today/blog/testing-graphql-with-graphene-django/
"""
Expand Down Expand Up @@ -139,3 +139,11 @@ def assertResponseHasErrors(self, resp, msg=None):
"""
content = json.loads(resp.content)
self.assertIn("errors", list(content.keys()), msg or content)


class GraphQLTestCase(GraphQLTestMixin, TestCase):
pass


class GraphQLTransactionTestCase(GraphQLTestMixin, TransactionTestCase):
pass