Skip to content

Commit

Permalink
Added GraphQLTransactionTestCase (#1099)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jackton1 committed Feb 23, 2021
1 parent 2d4ca0a commit 5cee414
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
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 @@ -63,7 +63,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 @@ -143,3 +143,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

0 comments on commit 5cee414

Please sign in to comment.