Refactor helper method from test

This commit is contained in:
Ramon Wenger 2018-09-10 12:54:31 +02:00
parent 9a2bc91a20
commit 370fd54b98
3 changed files with 34 additions and 11 deletions

View File

@ -1,9 +1,13 @@
"""Script defined to create helper functions for graphql schema."""
# https://medium.com/@jamesvaresamuel/mutation-and-query-in-graphql-using-python-django-part-2-79d9852a1092
import io
import os
from graphene_django.filter import DjangoFilterConnectionField
from graphql_relay.node.node import from_global_id
"""Script defined to create helper functions for graphql schema."""
# https://medium.com/@jamesvaresamuel/mutation-and-query-in-graphql-using-python-django-part-2-79d9852a1092
def get_object(object_name, relay_id, otherwise=None):
try:
return object_name.objects.get(pk=from_global_id(relay_id)[1])
@ -31,3 +35,21 @@ def get_errors(e):
messages = ['; '.join(m) for m in e.message_dict.values()]
errors = [i for pair in zip(fields, messages) for i in pair]
return errors
def get_graphql_query(filename):
from django.conf import settings
with io.open(os.path.join(settings.GRAPHQL_QUERIES_DIR, filename)) as f:
query = f.read()
return query
def get_graphql_mutation(filename):
from django.conf import settings
with io.open(os.path.join(settings.GRAPHQL_MUTATIONS_DIR, filename)) as f:
mutation = f.read()
return mutation

View File

@ -7,6 +7,7 @@ from graphene.test import Client
from graphql_relay import to_global_id
from api.schema import schema
from api.utils import get_graphql_mutation
from book.factories import ContentBlockFactory
from book.models import ContentBlock
@ -21,8 +22,8 @@ class NewContentBlockMutationTest(TestCase):
def test_add_new_content_block(self):
self.assertEqual(ContentBlock.objects.count(), 1)
client = Client(schema=schema)
with io.open(os.path.join(settings.GRAPHQL_MUTATIONS_DIR, 'addContentBlock.gql')) as f:
mutation = f.read()
mutation = get_graphql_mutation('addContentBlock.gql')
executed = client.execute(mutation, variables={
'input': {

View File

@ -43,6 +43,13 @@ if not DEBUG:
# Application definition
INSTALLED_APPS = [
'core',
'api',
'user',
'book',
'objectives',
'rooms',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
@ -71,13 +78,6 @@ INSTALLED_APPS = [
'graphene_django',
'django_extensions',
'compressor',
'core',
'api',
'user',
'book.apps.BookConfig',
'objectives',
'rooms',
]
if DEBUG: