skillbox/server/notes/tests/test_highlights.py

263 lines
7.3 KiB
Python

import pytest
from books.factories import ChapterFactory, ContentBlockFactory, ModuleFactory
from books.models.contentblock import ContentBlock
from graphql_relay import to_global_id
from core.logger import get_logger
pytestmark = pytest.mark.django_db
logger = get_logger(__name__)
highlight_fragment = """
fragment HighlightParts on HighlightNode {
id
contentIndex
paragraphIndex
selectionLength
contentUuid
startPosition
color
note {
text
}
text
page {
... on Node {
id
}
... on ModuleNode {
slug
id
}
}
}
"""
add_highlight_mutation = (
highlight_fragment
+ """
mutation AddHighlight($input: AddHighlightInput!) {
addHighlight(input: $input) {
__typename
highlight {
...HighlightParts
}
}
}
"""
)
add_content_highlight_mutation = (
highlight_fragment
+ """
mutation AddContentHighlight($input: AddContentHighlightInput!) {
addContentHighlight(input: $input) {
__typename
highlight {
...HighlightParts
}
}
}
"""
)
fragment = """
fragment HighlightLegacyParts on HighlightNode {
id
contentIndex
paragraphIndex
selectionLength
contentUuid
startPosition
color
note {
text
}
text
page {
... on Node {
id
}
... on ModuleNode {
slug
id
}
}
}
"""
content_block_query = (
fragment
+ """
query ContentBlockQuery($id: ID!) {
contentBlock(id: $id) {
id
highlights {
...HighlightLegacyParts
}
}
}
"""
)
module_query = (
fragment
+ """
query ModuleQuery($id: ID!) {
module(id: $id) {
id
highlights {
...HighlightLegacyParts
}
chapters {
id
highlights {
...HighlightLegacyParts
}
}
}
}
"""
)
@pytest.mark.usefixtures("create_users")
class TestAddHighlight:
def test_add_highlight_in_content_block(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(
add_content_highlight_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("addContentHighlight").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
assert content_block.get("highlights")[0].get("color") == "alpha"
assert content_block.get("highlights")[0].get("page").get("id") == cid
def test_add_highlight_in_content_block_without_uuid(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(
add_content_highlight_mutation,
variables={
"input": {
"highlight": {
"page": cid,
"contentIndex": 0,
"paragraphIndex": 0,
"text": "Hallo",
"startPosition": 0,
"selectionLength": 10,
"color": "alpha",
}
}
},
)
assert result.errors is not None
def test_add_highlight_in_instrument(self, teacher, get_client):
assert 1 == 0
def test_add_highlight_in_module(self, teacher, get_client):
module = ModuleFactory()
client = get_client(teacher)
mid = to_global_id("ModuleNode", module.id)
result = client.execute(
add_highlight_mutation,
variables={
"input": {
"highlight": {
"page": mid,
"paragraphIndex": 0,
"text": "Hallo",
"startPosition": 0,
"selectionLength": 10,
"color": "alpha",
}
}
},
)
assert result.errors is None
highlight = result.data.get("addHighlight").get("highlight")
logger.debug(highlight)
logger.debug(mid)
assert highlight.get("text") == "Hallo"
assert highlight.get("page").get("id") == mid
client = get_client(teacher)
result = client.execute(module_query, variables={"id": mid})
assert result.errors is None
module = result.data.get("module")
logger.debug(module)
assert module.get("id") == mid
assert module.get("highlights")[0].get("color") == "alpha"
assert module.get("highlights")[0].get("page").get("id") == mid
def test_add_highlight_in_chapter(self, teacher, get_client):
module = ModuleFactory()
chapter = ChapterFactory(parent=module)
client = get_client(teacher)
mid = to_global_id("ModuleNode", module.id)
cid = to_global_id("ChapterNode", chapter.id)
result = client.execute(
add_highlight_mutation,
variables={
"input": {
"highlight": {
"page": cid,
"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(module_query, variables={"id": mid})
assert result.errors is None
module = result.data.get("module")
logger.debug(module)
chapter = module.get("chapters")[0]
assert chapter.get("id") == cid
assert chapter.get("highlights")[0].get("color") == "alpha"
assert chapter.get("highlights")[0].get("page").get("id") == cid