import pytest from api.utils import get_object from assignments.models import Assignment from books.factories import ChapterFactory, ContentBlockFactory, ModuleFactory from books.models.contentblock import ContentBlock from core.logger import get_logger logger = get_logger(__name__) pytestmark = pytest.mark.django_db add_mutation = """ mutation AddContentBlock($input: AddContentBlockInput!) { addContentBlock(input: $input) { newContentBlock { id } errors clientMutationId } } """ update_mutation = """ mutation MutateContentBlock($input: MutateContentBlockInput!) { mutateContentBlock(input: $input) { contentBlock { id contents title } } } """ def create_block_input(content_type, value): return { "contents": { "type": content_type, "value": value, }, "title": "some title", "type": "NORMAL", } @pytest.mark.usefixtures("create_users") class TestCreateCustomContentBlock: def _add_content_block(self, client, input): module = ModuleFactory() chapter = ChapterFactory(parent=module) content_block = ContentBlockFactory(parent=chapter, module=module) after = content_block.graphql_id logger.info(after) result = client.execute( add_mutation, variables={ "input": { "contentBlock": input, "after": after, } }, ) assert result.errors is None assert result.data.get("addContentBlock", None).get("errors", None) is None assert ContentBlock.objects.count() == 2 new_content_block_data = result.data.get("addContentBlock").get( "newContentBlock" ) new_block_id = new_content_block_data.get("id") new_content_block = get_object(ContentBlock, new_block_id) return new_content_block def _update_content_block(self, client, input): update_result = client.execute(update_mutation, variables=input) assert update_result.errors is None assert ContentBlock.objects.count() == 2 updated_content_block_id = ( update_result.data.get("mutateContentBlock").get("contentBlock").get("id") ) updated_content_block = get_object(ContentBlock, updated_content_block_id) return updated_content_block def test_add_custom_content_block(self, teacher, get_client): content_block_title = "A new Custom Content Block" content_block_input = { "contents": [ { "contents": [ { "type": "text_block", "value": {"text": "
Some nested text
"}, } ], "type": "content_list_item", } ], "title": content_block_title, "type": "NORMAL", } client = get_client(teacher) new_content_block = self._add_content_block( client=client, input=content_block_input ) assert new_content_block.title == content_block_title content_list_content = new_content_block.contents.raw_data[0] logger.debug(content_list_content) content_list_id = content_list_content.get("id") assert content_list_id is not None text_content = content_list_content.get("value")[0] text_id = text_content.get("id") assert text_id is not None update_input = { "input": { "contentBlock": { "contents": [ { "contents": [ { "type": "text_block", "id": text_id, "value": {"text": "Some nested text
"}, }, { "type": "text_block", "value": {"text": "Second nested text
"}, }, ], "id": content_list_id, "type": "content_list_item", } ], "title": content_block_title, "type": "NORMAL", }, "id": new_content_block.graphql_id, } } updated_content_block = self._update_content_block(client, update_input) updated_content_list_content = updated_content_block.contents.raw_data[0] updated_content_list_id = updated_content_list_content.get("id") assert content_list_id == updated_content_list_id updated_text_content = updated_content_list_content.get("value")[0] updated_text_id = updated_text_content.get("id") assert text_id == updated_text_id added_text_content = updated_content_list_content.get("value")[1] assert added_text_content.get("id") is not None assert ( added_text_content.get("value").get("text") == "Second nested text
" ) def test_add_text_block_should_get_id(self, teacher, get_client): client = get_client(teacher) content_block_title = "A new Custom Content Block" content_block_input = { "contents": [ { "value": {"text": "Some text"}, "type": "text_block", } ], "title": content_block_title, "type": "NORMAL", } new_content_block = self._add_content_block( client=client, input=content_block_input ) assert new_content_block.title == content_block_title text_block = new_content_block.contents.raw_data[0] text_id = text_block.get("id") assert text_id is not None update_input = { "input": { "contentBlock": { "contents": [ { "id": text_id, "type": "text_block", "value": {"text": "ein text
"}, } ], "title": content_block_title, "type": "NORMAL", }, "id": new_content_block.graphql_id, } } updated_content_block = self._update_content_block(client, update_input) updated_text = updated_content_block.contents.raw_data[0] assert updated_text.get("id") == text_id def test_add_text_block(self, teacher, get_client): content_type = "text_block" value = {"text": "some text
"} client = get_client(teacher) input = create_block_input(content_type=content_type, value=value) new_content_block = self._add_content_block(client=client, input=input) text_block = new_content_block.contents.raw_data[0] assert text_block.get("id") is not None assert text_block.get("value").get("text") == value.get("text") def test_add_assignment(self, teacher, get_client): content_type = "assignment" value = {"title": "Assignment title", "assignment": "Assignment text"} client = get_client(teacher) input = create_block_input(content_type=content_type, value=value) new_content_block = self._add_content_block(client=client, input=input) assignment_block = new_content_block.contents.raw_data[0] logger.debug(assignment_block) assert assignment_block.get("id") is not None assert assignment_block.get("value").get("assignment_id") is not None assert Assignment.objects.count() == 1 assignment = Assignment.objects.get( pk=assignment_block.get("value").get("assignment_id") ) assert assignment.title == value.get("title") assert assignment.assignment == value.get("assignment") def test_add_image_block(self, teacher, get_client): content_type = "image_url_block" value = {"url": "https://hep-verlag.ch/some-image"} client = get_client(teacher) input = create_block_input(content_type=content_type, value=value) new_content_block = self._add_content_block(client=client, input=input) image_block = new_content_block.contents.raw_data[0] assert image_block.get("id") is not None assert image_block.get("value").get("url") == value.get("url") def test_add_link_block(self, teacher, get_client): content_type = "link_block" value = {"text": "link of the text", "url": "https://hep-verlag.ch/some-image"} client = get_client(teacher) input = create_block_input(content_type=content_type, value=value) new_content_block = self._add_content_block(client=client, input=input) link_block = new_content_block.contents.raw_data[0] assert link_block.get("id") is not None assert link_block.get("value").get("text") == value.get("text") assert link_block.get("value").get("url") == value.get("url") def test_add_video_block(self, teacher, get_client): content_type = "video_block" value = {"url": "https://hep-verlag.ch/some-video"} client = get_client(teacher) input = create_block_input(content_type=content_type, value=value) new_content_block = self._add_content_block(client=client, input=input) video_block = new_content_block.contents.raw_data[0] assert video_block.get("id") is not None assert video_block.get("value").get("url") == value.get("url") def test_add_document_block(self, teacher, get_client): content_type = "document_block" value = {"url": "https://hep-verlag.ch/some-document"} client = get_client(teacher) input = create_block_input(content_type=content_type, value=value) new_content_block = self._add_content_block(client=client, input=input) document_block = new_content_block.contents.raw_data[0] assert document_block.get("id") is not None assert document_block.get("value").get("url") == value.get("url") def test_add_subtitle(self, teacher, get_client): content_type = "subtitle" value = {"text": "some subtitle"} client = get_client(teacher) input = create_block_input(content_type=content_type, value=value) new_content_block = self._add_content_block(client=client, input=input) subtitle_block = new_content_block.contents.raw_data[0] assert subtitle_block.get("id") is not None assert subtitle_block.get("value").get("text") == value.get("text") # def test_add_readonly(self): # content_type = "readonly" # value = {"text": "some subtitle"} # client = get_client(teacher) # input = create_block_input(content_type=content_type, value=value) # new_content_block = self._add_content_block(client=client, input=input) # subtitle_block = new_content_block.contents.raw_data[0] # assert subtitle_block.get("id") is not None # assert subtitle_block.get("value").get("text") == value.get("text")