diff --git a/docs/testing.rst b/docs/testing.rst index c24f7c448..65b6f64a4 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -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 ------------ diff --git a/graphene_django/tests/test_utils.py b/graphene_django/tests/test_utils.py index d895f46bf..adad00efa 100644 --- a/graphene_django/tests/test_utils.py +++ b/graphene_django/tests/test_utils.py @@ -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"}} diff --git a/graphene_django/utils/testing.py b/graphene_django/utils/testing.py index 584a08bca..bb55d42c8 100644 --- a/graphene_django/utils/testing.py +++ b/graphene_django/utils/testing.py @@ -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/" @@ -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/ """ @@ -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