Add unit test for module and chapter highlights

This commit is contained in:
Ramon Wenger 2024-02-21 21:53:59 +01:00
parent 15e173221a
commit 9e536e0224
1 changed files with 117 additions and 7 deletions

View File

@ -3,9 +3,13 @@ from books.factories import ContentBlockFactory, ModuleFactory
from books.models.contentblock import ContentBlock from books.models.contentblock import ContentBlock
from graphql_relay import to_global_id from graphql_relay import to_global_id
from core.logger import get_logger
pytestmark = pytest.mark.django_db pytestmark = pytest.mark.django_db
mutation = """ logger = get_logger(__name__)
highlight_fragment = """
fragment HighlightParts on HighlightNode { fragment HighlightParts on HighlightNode {
id id
contentIndex contentIndex
@ -22,10 +26,18 @@ mutation = """
... on Node { ... on Node {
id id
} }
... on ModuleNode {
slug
id
}
} }
} }
"""
add_highlight_mutation = (
highlight_fragment
+ """
mutation AddHighlight($input: AddHighlightInput!) { mutation AddHighlight($input: AddHighlightInput!) {
addHighlight(input: $input) { addHighlight(input: $input) {
__typename __typename
@ -35,8 +47,22 @@ mutation = """
} }
} }
""" """
)
add_content_highlight_mutation = (
highlight_fragment
+ """
mutation AddContentHighlight($input: AddContentHighlightInput!) {
addContentHighlight(input: $input) {
__typename
highlight {
...HighlightParts
}
}
}
"""
)
content_block_query = """ fragment = """
fragment HighlightLegacyParts on HighlightNode { fragment HighlightLegacyParts on HighlightNode {
id id
contentIndex contentIndex
@ -53,10 +79,17 @@ fragment HighlightLegacyParts on HighlightNode {
... on Node { ... on Node {
id id
} }
... on ModuleNode {
slug
id
} }
} }
}
"""
content_block_query = (
fragment
+ """
query ContentBlockQuery($id: ID!) { query ContentBlockQuery($id: ID!) {
contentBlock(id: $id) { contentBlock(id: $id) {
id id
@ -66,18 +99,33 @@ fragment HighlightLegacyParts on HighlightNode {
} }
} }
""" """
)
module_query = (
fragment
+ """
query ModuleQuery($id: ID!) {
module(id: $id) {
id
highlights {
...HighlightLegacyParts
}
}
}
"""
)
@pytest.mark.usefixtures("create_users") @pytest.mark.usefixtures("create_users")
class TestAddHighlight: class TestAddHighlight:
def test_add_highlight(self, teacher, get_client): def test_add_highlight_in_content_block(self, teacher, get_client):
module = ModuleFactory() module = ModuleFactory()
content_block = ContentBlockFactory(module=module) content_block = ContentBlockFactory(module=module)
client = get_client(teacher) client = get_client(teacher)
# content_block = ContentBlock.objects.create(slug="slug", title="title") # content_block = ContentBlock.objects.create(slug="slug", title="title")
cid = to_global_id("ContentBlockNode", content_block.id) cid = to_global_id("ContentBlockNode", content_block.id)
result = client.execute( result = client.execute(
mutation, add_content_highlight_mutation,
variables={ variables={
"input": { "input": {
"highlight": { "highlight": {
@ -95,7 +143,7 @@ class TestAddHighlight:
) )
assert result.errors is None assert result.errors is None
highlight = result.data.get("addHighlight").get("highlight") highlight = result.data.get("addContentHighlight").get("highlight")
assert highlight.get("text") == "Hallo" assert highlight.get("text") == "Hallo"
assert highlight.get("page").get("id") == cid assert highlight.get("page").get("id") == cid
@ -104,6 +152,68 @@ class TestAddHighlight:
assert result.errors is None assert result.errors is None
content_block = result.data.get("contentBlock") content_block = result.data.get("contentBlock")
assert content_block.get("id") == cid 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("color") == "alpha"
assert content_block.get("highlights")[0].get("page").get("id") == cid 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