Add a unit test file for highlight mutations

Contains a first test for creation
This commit is contained in:
Ramon Wenger 2024-02-14 23:05:46 +01:00
parent 7cf192f132
commit 238be6cf4f
1 changed files with 109 additions and 0 deletions

View File

@ -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