Created
August 23, 2019 14:18
-
-
Save Glyphack/1beb602a996646f67b2b7acab958bb23 to your computer and use it in GitHub Desktop.
functions to test graphql api with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import AnonymousUser | |
from django.test import RequestFactory | |
from snapshottest.django import TestCase | |
from graphene.test import Client | |
from hackernews.schema import schema | |
class APITestCase(TestCase): | |
def setUp(self): | |
super().setUp() | |
self.client = Client(schema) | |
def snapshot_graphql_request( | |
self, | |
request_string, | |
context=None, | |
variables=None | |
): | |
if context is None: | |
context = {} | |
graphql_response = self.client.execute( | |
request_string, | |
variables=variables, | |
context=self.generate_context(**context) | |
) | |
self.assertMatchSnapshot(graphql_response) | |
def generate_context(self, user=None, files=None): | |
request = RequestFactory() | |
context_value = request.get('/graphql/') | |
context_value.user = user or AnonymousUser() | |
self.__set_context_files(context_value, files) | |
return context_value | |
@staticmethod | |
def __set_context_files(context, files): | |
if isinstance(files, dict): | |
for name, file in files.items(): | |
context.FILES[name] = file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment