Skip to content

Commit 5cee414

Browse files
authored
Added GraphQLTransactionTestCase (#1099)
* Added GraphQLTransactionTestCase - Adds support for testing code that is executed within a transaction Reference: https://docs.djangoproject.com/en/3.1/topics/testing/tools/#django.test.TransactionTestCase ``` For instance, you cannot test that a block of code is executing within a transaction, as is required when using select_for_update(). In those cases, you should use TransactionTestCase. ``` * Update testing.py * Update testing.py * Fixed formatting. * Updated docs. * Updated test. * Update testing.rst
1 parent 2d4ca0a commit 5cee414

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

docs/testing.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,41 @@ Usage:
8282
# Add some more asserts if you like
8383
...
8484
85+
86+
For testing mutations that are executed within a transaction you should subclass `GraphQLTransactionTestCase`
87+
88+
Usage:
89+
90+
.. code:: python
91+
92+
import json
93+
94+
from graphene_django.utils.testing import GraphQLTransactionTestCase
95+
96+
class MyFancyTransactionTestCase(GraphQLTransactionTestCase):
97+
98+
def test_some_mutation_that_executes_within_a_transaction(self):
99+
response = self.query(
100+
'''
101+
mutation myMutation($input: MyMutationInput!) {
102+
myMutation(input: $input) {
103+
my-model {
104+
id
105+
name
106+
}
107+
}
108+
}
109+
''',
110+
op_name='myMutation',
111+
input_data={'my_field': 'foo', 'other_field': 'bar'}
112+
)
113+
114+
# This validates the status code and if you get errors
115+
self.assertResponseNoErrors(response)
116+
117+
# Add some more asserts if you like
118+
...
119+
85120
Using pytest
86121
------------
87122

graphene_django/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,6 @@ def func(*args, **kwargs):
8383

8484

8585
def test_pytest_fixture_usage(client_query):
86-
response = graphql_query("query { test }")
86+
response = client_query("query { test }")
8787
content = json.loads(response.content)
8888
assert content == {"data": {"test": "Hello World"}}

graphene_django/utils/testing.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import warnings
33

4-
from django.test import Client, TestCase
4+
from django.test import Client, TestCase, TransactionTestCase
55

66
DEFAULT_GRAPHQL_URL = "/graphql/"
77

@@ -63,7 +63,7 @@ def graphql_query(
6363
return resp
6464

6565

66-
class GraphQLTestCase(TestCase):
66+
class GraphQLTestMixin(object):
6767
"""
6868
Based on: https://www.sam.today/blog/testing-graphql-with-graphene-django/
6969
"""
@@ -143,3 +143,11 @@ def assertResponseHasErrors(self, resp, msg=None):
143143
"""
144144
content = json.loads(resp.content)
145145
self.assertIn("errors", list(content.keys()), msg or content)
146+
147+
148+
class GraphQLTestCase(GraphQLTestMixin, TestCase):
149+
pass
150+
151+
152+
class GraphQLTransactionTestCase(GraphQLTestMixin, TransactionTestCase):
153+
pass

0 commit comments

Comments
 (0)