From 238be6cf4fc38ad33f7a24f21c23f0f1ee8f1e69 Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Wed, 14 Feb 2024 23:05:46 +0100 Subject: [PATCH] Add a unit test file for highlight mutations Contains a first test for creation --- server/notes/tests/test_highlights.py | 109 ++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 server/notes/tests/test_highlights.py diff --git a/server/notes/tests/test_highlights.py b/server/notes/tests/test_highlights.py new file mode 100644 index 00000000..ddf79556 --- /dev/null +++ b/server/notes/tests/test_highlights.py @@ -0,0 +1,109 @@ +import pytest +from books.factories import ContentBlockFactory, ModuleFactory +from books.models.contentblock import ContentBlock +from graphql_relay import to_global_id + +pytestmark = pytest.mark.django_db + +mutation = """ + fragment HighlightParts on HighlightNode { + id + contentIndex + paragraphIndex + selectionLength + contentUuid + startPosition + color + note { + text + } + text + page { + ... on Node { + id + } + } + } + + + mutation AddHighlight($input: AddHighlightInput!) { + addHighlight(input: $input) { + __typename + highlight { + ...HighlightParts + } + } + } +""" + +content_block_query = """ +fragment HighlightLegacyParts on HighlightNode { + id + contentIndex + paragraphIndex + selectionLength + contentUuid + startPosition + color + note { + text + } + text + page { + ... on Node { + id + } + } +} + + + query ContentBlockQuery($id: ID!) { + contentBlock(id: $id) { + id + highlights { + ...HighlightLegacyParts + } + } + } +""" + + +@pytest.mark.usefixtures("create_users") +class TestAddHighlight: + def test_add_highlight(self, teacher, get_client): + module = ModuleFactory() + content_block = ContentBlockFactory(module=module) + client = get_client(teacher) + # content_block = ContentBlock.objects.create(slug="slug", title="title") + cid = to_global_id("ContentBlockNode", content_block.id) + result = client.execute( + mutation, + variables={ + "input": { + "highlight": { + "page": cid, + "contentIndex": 0, + "contentUuid": "427ee0b7-aee0-424e-8cb6-35ac12a8fb66", + "paragraphIndex": 0, + "text": "Hallo", + "startPosition": 0, + "selectionLength": 10, + "color": "alpha", + } + } + }, + ) + assert result.errors is None + + highlight = result.data.get("addHighlight").get("highlight") + assert highlight.get("text") == "Hallo" + assert highlight.get("page").get("id") == cid + + client = get_client(teacher) + result = client.execute(content_block_query, variables={"id": cid}) + assert result.errors is None + content_block = result.data.get("contentBlock") + assert content_block.get("id") == cid + print(content_block.get("highlights")[0]) + assert content_block.get("highlights")[0].get("color") == "alpha" + assert content_block.get("highlights")[0].get("page").get("id") == cid