Fix custom content blocks in snapshot preview

This commit is contained in:
Ramon Wenger 2021-05-25 15:46:43 +02:00
parent a415604610
commit 3c85ab6520
3 changed files with 173 additions and 153 deletions

View File

@ -31,7 +31,7 @@ class SnapshotChapter:
self.description_hidden = description_hidden
self.content_blocks = []
base_qs = ContentBlock.get_by_parent(chapter)
without_owner = Q(owner__isnull=True)
without_owner = Q(Q(owner__isnull=True) & Q(contentblocksnapshot__snapshot__isnull=True))
this_snapshot = Q(contentblocksnapshot__snapshot=snapshot)
self.content_blocks = [
SnapshotContentBlock(

View File

@ -0,0 +1,131 @@
MODULE_QUERY = """
query ModulesQuery($slug: String, $id: ID) {
module(slug: $slug, id: $id) {
id
title
objectiveGroups {
objectives {
id
text
hiddenFor {
name
}
visibleFor {
name
}
}
}
chapters {
id
contentBlocks {
id
title
visibleFor {
name
}
hiddenFor {
name
}
}
}
}
}
"""
CREATE_SNAPSHOT_MUTATION = """
mutation CreateSnapshot($input: CreateSnapshotInput!) {
createSnapshot(input: $input) {
snapshot {
id
created
creator
objectiveGroups {
objectives {
text
hidden
}
}
chapters {
id
descriptionHidden
titleHidden
title
description
contentBlocks {
id
title
hidden
}
}
}
success
}
}
"""
APPLY_SNAPSHOT_MUTATION = """
mutation ApplySnapshot($input: ApplySnapshotInput!) {
applySnapshot(input: $input) {
success
}
}
"""
SNAPSHOT_MODULE_QUERY = """
query SnapshotDetail($id: ID!) {
snapshot(id: $id) {
id
changes {
newContentBlocks
newObjectives
hiddenContentBlocks
hiddenObjectives
}
objectiveGroups {
title
id
hidden
objectives {
hidden
id
text
}
}
chapters {
id
description
title
titleHidden
descriptionHidden
contentBlocks {
id
title
hidden
}
}
}
}
"""
SHARE_SNAPSHOT_MUTATION = """
mutation ShareSnapshot($input: ShareSnapshotInput!) {
shareSnapshot(input: $input) {
success
snapshot {
shared
}
}
}
"""
MODULE_SNAPSHOTS_QUERY = """
query SnapshotQuery($slug: String!) {
module(slug: $slug) {
snapshots {
id
title
created
creator
}
}
}
"""

View File

@ -6,143 +6,13 @@ from api.schema import schema
from api.utils import get_object
from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory
from books.models import Snapshot, ChapterSnapshot
from books.tests.queries import MODULE_QUERY, SNAPSHOT_MODULE_QUERY, CREATE_SNAPSHOT_MUTATION, APPLY_SNAPSHOT_MUTATION, \
MODULE_SNAPSHOTS_QUERY, SHARE_SNAPSHOT_MUTATION
from core.tests.base_test import SkillboxTestCase
from objectives.factories import ObjectiveGroupFactory, ObjectiveFactory
from users.factories import SchoolClassFactory
from users.models import User, SchoolClass
MODULE_QUERY = """
query ModulesQuery($slug: String, $id: ID) {
module(slug: $slug, id: $id) {
id
title
objectiveGroups {
objectives {
id
text
hiddenFor {
name
}
visibleFor {
name
}
}
}
chapters {
id
contentBlocks {
id
title
visibleFor {
name
}
hiddenFor {
name
}
}
}
}
}
"""
CREATE_SNAPSHOT_MUTATION = """
mutation CreateSnapshot($input: CreateSnapshotInput!) {
createSnapshot(input: $input) {
snapshot {
id
created
creator
objectiveGroups {
objectives {
text
hidden
}
}
chapters {
id
descriptionHidden
titleHidden
title
description
contentBlocks {
id
title
hidden
}
}
}
success
}
}
"""
APPLY_SNAPSHOT_MUTATION = """
mutation ApplySnapshot($input: ApplySnapshotInput!) {
applySnapshot(input: $input) {
success
}
}
"""
SNAPSHOT_MODULE_QUERY = """
query SnapshotDetail($id: ID!) {
snapshot(id: $id) {
id
changes {
newContentBlocks
newObjectives
hiddenContentBlocks
hiddenObjectives
}
objectiveGroups {
title
id
hidden
objectives {
hidden
id
text
}
}
chapters {
id
description
title
titleHidden
descriptionHidden
contentBlocks {
id
title
hidden
}
}
}
}
"""
SHARE_SNAPSHOT_MUTATION = """
mutation ShareSnapshot($input: ShareSnapshotInput!) {
shareSnapshot(input: $input) {
success
snapshot {
shared
}
}
}
"""
MODULE_SNAPSHOTS_QUERY = """
query SnapshotQuery($slug: String!) {
module(slug: $slug) {
snapshots {
id
title
created
creator
}
}
}
"""
class CreateSnapshotTestCase(SkillboxTestCase):
def _test_objective(self, objective, text, hidden):
@ -251,17 +121,19 @@ class CreateSnapshotTestCase(SkillboxTestCase):
school_class_name in [school_class['name'] for school_class in
custom_objective.get('visibleFor')])
def _compare_content_blocks(self, content_blocks):
self.assertEqual(len(content_blocks), 4)
first, second, third, fourth = content_blocks
self._test_content_block(first, self.title_visible, False)
self._test_content_block(second, self.title_hidden, True)
self._test_content_block(third, self.title_custom, False)
self._test_content_block(fourth, self.title_custom_hidden, True)
def test_setup(self):
# make sure everything is setup correctly
self._test_module_visibility(self.client, 'skillbox')
def test_create_snapshot(self):
result = self.client.execute(CREATE_SNAPSHOT_MUTATION, variables={
'input': {
'module': self.slug,
'selectedClass': to_global_id('SchoolClassNode', self.skillbox_class.pk),
}
})
def _test_create_snapshot(self, result, num_snapshots=1):
self.assertIsNone(result.get('errors'))
snapshot = result.get('data').get('createSnapshot').get('snapshot')
chapter = snapshot.get('chapters')[0]
@ -275,13 +147,8 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.assertEqual(int(chapter_id), self.chapter.id)
content_blocks = chapter['contentBlocks']
self.assertEqual(len(content_blocks), 4)
visible, hidden, custom, custom_hidden = content_blocks
self._test_content_block(visible, self.title_visible, False)
self._test_content_block(hidden, self.title_hidden, True)
self._test_content_block(custom, self.title_custom, False)
self._test_content_block(custom_hidden, self.title_custom_hidden, True)
self.assertEqual(ChapterSnapshot.objects.count(), 2)
self._compare_content_blocks(content_blocks)
self.assertEqual(ChapterSnapshot.objects.count(), 2 * num_snapshots)
visible, hidden, custom, custom_hidden = snapshot['objectiveGroups'][0]['objectives']
@ -294,6 +161,32 @@ class CreateSnapshotTestCase(SkillboxTestCase):
snapshot = get_object(Snapshot, id)
self.assertEqual(snapshot.objective_groups.count(), 2)
def test_create_snapshot(self):
result = self.client.execute(CREATE_SNAPSHOT_MUTATION, variables={
'input': {
'module': self.slug,
'selectedClass': to_global_id('SchoolClassNode', self.skillbox_class.pk),
}
})
self._test_create_snapshot(result)
def test_create_two_snapshots(self):
self.client.execute(CREATE_SNAPSHOT_MUTATION, variables={
'input': {
'module': self.slug,
'selectedClass': to_global_id('SchoolClassNode', self.skillbox_class.pk),
}
})
result = self.client.execute(CREATE_SNAPSHOT_MUTATION, variables={
'input': {
'module': self.slug,
'selectedClass': to_global_id('SchoolClassNode', self.skillbox_class.pk),
}
})
self._test_create_snapshot(result, num_snapshots=2)
def test_apply_snapshot(self):
self.snapshot = Snapshot.objects.create_snapshot(module=self.module, school_class=self.skillbox_class,
user=self.teacher)
@ -327,12 +220,8 @@ class CreateSnapshotTestCase(SkillboxTestCase):
self.assertEqual(len(chapters), 2)
chapter = chapters[0]
content_blocks = chapter.get('contentBlocks')
self.assertEqual(len(content_blocks), 4)
first, second, third, fourth = content_blocks
self._test_content_block(first, self.title_visible, False)
self._test_content_block(second, self.title_hidden, True)
self._test_content_block(third, self.title_custom, False)
self._test_content_block(fourth, self.title_custom_hidden, True)
self._compare_content_blocks(content_blocks)
objective_groups = snapshot['objectiveGroups']
self.assertEqual(len(objective_groups), 2)