From 3c85ab6520dc9d917bf8c87c00baf7107be5da2e Mon Sep 17 00:00:00 2001 From: Ramon Wenger Date: Tue, 25 May 2021 15:46:43 +0200 Subject: [PATCH] Fix custom content blocks in snapshot preview --- server/books/schema/nodes/snapshot.py | 2 +- server/books/tests/queries.py | 131 +++++++++++++++++ server/books/tests/test_snapshots.py | 193 ++++++-------------------- 3 files changed, 173 insertions(+), 153 deletions(-) create mode 100644 server/books/tests/queries.py diff --git a/server/books/schema/nodes/snapshot.py b/server/books/schema/nodes/snapshot.py index a856c030..be3c0b88 100644 --- a/server/books/schema/nodes/snapshot.py +++ b/server/books/schema/nodes/snapshot.py @@ -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( diff --git a/server/books/tests/queries.py b/server/books/tests/queries.py new file mode 100644 index 00000000..f5bdb338 --- /dev/null +++ b/server/books/tests/queries.py @@ -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 + } + } +} +""" diff --git a/server/books/tests/test_snapshots.py b/server/books/tests/test_snapshots.py index ca08cf3d..e66f40f8 100644 --- a/server/books/tests/test_snapshots.py +++ b/server/books/tests/test_snapshots.py @@ -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)