mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-10 19:57:15 +03:00
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
This commit is contained in:
parent
2d4ca0ac7b
commit
5cee41407c
|
@ -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
|
||||
------------
|
||||
|
||||
|
|
|
@ -83,6 +83,6 @@ def client_query(client):
|
|||
|
||||
|
||||
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"}}
|
||||
|
|
|
@ -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/"
|
||||
|
||||
|
@ -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/
|
||||
"""
|
||||
|
@ -143,3 +143,11 @@ class GraphQLTestCase(TestCase):
|
|||
"""
|
||||
content = json.loads(resp.content)
|
||||
self.assertIn("errors", list(content.keys()), msg or content)
|
||||
|
||||
|
||||
class GraphQLTestCase(GraphQLTestMixin, TestCase):
|
||||
pass
|
||||
|
||||
|
||||
class GraphQLTransactionTestCase(GraphQLTestMixin, TransactionTestCase):
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue
Block a user