81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
from django.test import TestCase
|
|
from graphene.test import Client
|
|
from graphql_relay import to_global_id
|
|
|
|
from api.schema import schema
|
|
from api.utils import get_graphql_mutation, get_object
|
|
from book.factories import ContentBlockFactory
|
|
from book.models import ContentBlock
|
|
|
|
|
|
class NewContentBlockMutationTest(TestCase):
|
|
def setUp(self):
|
|
content_block = ContentBlockFactory()
|
|
self.sibling_id = to_global_id('ContentBlock', content_block.pk)
|
|
|
|
def test_add_new_content_block(self):
|
|
self.assertEqual(ContentBlock.objects.count(), 1)
|
|
client = Client(schema=schema)
|
|
|
|
mutation = get_graphql_mutation('addContentBlock.gql')
|
|
|
|
title = "Hello World"
|
|
|
|
result = client.execute(mutation, variables={
|
|
'input': {
|
|
"contentBlock": {
|
|
"title": title,
|
|
"contents": [
|
|
{
|
|
"type": "text_block",
|
|
"text": "Hello there"
|
|
}
|
|
]
|
|
},
|
|
"after": self.sibling_id
|
|
}
|
|
})
|
|
self.assertIsNone(result.get('errors'))
|
|
self.assertEqual(ContentBlock.objects.count(), 2)
|
|
|
|
new_content_block = result.get('data').get('addContentBlock').get('newContentBlock')
|
|
self.assertEqual(new_content_block.get('title'), title)
|
|
self.assertEqual(len(new_content_block['contents']), 1)
|
|
|
|
id = new_content_block.get('id')
|
|
content_block_page = get_object(ContentBlock, id)
|
|
|
|
self.assertEqual(content_block_page.title, title)
|
|
|
|
def test_addNewContentBlock_withImageUrlBlock(self):
|
|
self.assertEqual(ContentBlock.objects.count(), 1)
|
|
client = Client(schema=schema)
|
|
|
|
mutation = get_graphql_mutation('addContentBlock.gql')
|
|
|
|
title = "Hello World"
|
|
|
|
result = client.execute(mutation, variables={
|
|
'input': {
|
|
"contentBlock": {
|
|
"title": title,
|
|
"contents": [
|
|
{
|
|
"type": "image_url_block",
|
|
'value': {
|
|
"url": "/test.png"
|
|
}
|
|
}
|
|
]
|
|
},
|
|
"after": self.sibling_id
|
|
}
|
|
})
|
|
self.assertIsNone(result.get('errors'))
|
|
self.assertEqual(ContentBlock.objects.count(), 2)
|
|
|
|
new_content_block = result.get('data').get('addContentBlock').get('newContentBlock')
|
|
self.assertEqual(new_content_block.get('title'), title)
|
|
self.assertEqual(len(new_content_block['contents']), 1)
|
|
self.assertEqual(new_content_block['contents'], [{'type': 'image_url_block', 'value': {'url': '/test.png'}}])
|