Fix unit tests

This commit is contained in:
Ramon Wenger 2021-05-05 19:47:06 +02:00
parent 70e7dc39a9
commit 45f99385d3
14 changed files with 106 additions and 198 deletions

View File

@ -56,8 +56,6 @@ class CustomAssignmentTestCase(TestCase):
edges { edges {
node { node {
contentBlocks { contentBlocks {
edges {
node {
contents contents
} }
} }
@ -65,8 +63,6 @@ class CustomAssignmentTestCase(TestCase):
} }
} }
} }
}
}
''' '''
result = self.client.execute(query, variables={ result = self.client.execute(query, variables={
@ -78,8 +74,7 @@ class CustomAssignmentTestCase(TestCase):
@staticmethod @staticmethod
def get_first_contents(result): def get_first_contents(result):
return result.get('data').get('module').get('chapters').get('edges')[0].get('node').get('contentBlocks').get( return result.get('data').get('module').get('chapters').get('edges')[0].get('node').get('contentBlocks')[0].get('contents')
'edges')[0].get('node').get('contents')
def test_module_query(self): def test_module_query(self):
result = self.query_module() result = self.query_module()

View File

@ -2,6 +2,6 @@ import graphene
from graphene import relay from graphene import relay
from graphene_django.filter import DjangoFilterConnectionField from graphene_django.filter import DjangoFilterConnectionField
class ChapterInterface(relay.Node): class ChapterInterface(graphene.Interface):
description = graphene.String() description = graphene.String()
title = graphene.String() title = graphene.String()

View File

@ -4,7 +4,7 @@ from graphene import relay
from api.graphene_wagtail import GenericStreamFieldType from api.graphene_wagtail import GenericStreamFieldType
class ContentBlockInterface(relay.Node): class ContentBlockInterface(graphene.Interface):
title = graphene.String() title = graphene.String()
contents = GenericStreamFieldType() contents = GenericStreamFieldType()
type = graphene.String() type = graphene.String()

View File

@ -2,11 +2,8 @@ import graphene
from django.db.models import Q from django.db.models import Q
from graphene import relay from graphene import relay
from graphene_django import DjangoObjectType from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from graphql_relay import to_global_id
from books.models import Chapter, ContentBlock from books.models import Chapter, ContentBlock
from books.models.snapshot import ChapterSnapshot
from books.schema.interfaces import ChapterInterface from books.schema.interfaces import ChapterInterface
from notes.models import ChapterBookmark from notes.models import ChapterBookmark
from notes.schema import ChapterBookmarkNode from notes.schema import ChapterBookmarkNode
@ -15,6 +12,8 @@ from notes.schema import ChapterBookmarkNode
class ChapterNode(DjangoObjectType): class ChapterNode(DjangoObjectType):
bookmark = graphene.Field(ChapterBookmarkNode) bookmark = graphene.Field(ChapterBookmarkNode)
content_blocks = graphene.List('books.schema.nodes.ContentBlockNode') content_blocks = graphene.List('books.schema.nodes.ContentBlockNode')
title_hidden_for = graphene.List('users.schema.SchoolClassNode')
description_hidden_for = graphene.List('users.schema.SchoolClassNode')
class Meta: class Meta:
model = Chapter model = Chapter
@ -24,7 +23,7 @@ class ChapterNode(DjangoObjectType):
filter_fields = [ filter_fields = [
'slug', 'title', 'slug', 'title',
] ]
interfaces = (ChapterInterface,) interfaces = (relay.Node, ChapterInterface)
def resolve_content_blocks(self, info, **kwargs): def resolve_content_blocks(self, info, **kwargs):
user = info.context.user user = info.context.user
@ -51,52 +50,10 @@ class ChapterNode(DjangoObjectType):
chapter=self chapter=self
).first() ).first()
@staticmethod
class ChapterInSnapshotNode(DjangoObjectType): def resolve_title_hidden_for(parent: Chapter, info, **kwargs):
title_hidden = graphene.Boolean() return parent.title_hidden_for.all()
description_hidden = graphene.Boolean()
title = graphene.String()
description = graphene.String()
id = graphene.ID(required=True)
class Meta:
model = ChapterSnapshot
only_fields = '__all__'
filter_fields = [
'id',
]
interfaces = (ChapterInterface,)
@staticmethod @staticmethod
def resolve_title_hidden(parent, info): def resolve_description_hidden_for(parent: Chapter, info, **kwargs):
return parent.title_hidden return parent.description_hidden_for.all()
@staticmethod
def resolve_description_hidden(parent, info):
return parent.description_hidden
@staticmethod
def resolve_title(parent, info):
return parent.chapter.title
@staticmethod
def resolve_description(parent, info):
return parent.chapter.description
@staticmethod
def resolve_content_blocks(parent, info, **kwargs):
snapshot = parent.snapshot
user_created = Q(user_created=True)
hidden_for_snapshot = Q(hidden_for_snapshots=snapshot)
custom_hidden = Q(contentblocksnapshot__hidden=True)
qs = ContentBlock.get_by_parent(parent.chapter) \
.exclude(user_created) \
.exclude(hidden_for_snapshot) \
.exclude(custom_hidden)
return qs
def resolve_id(self, *args):
return to_global_id('SnapshotChapterNode', self.chapter.pk)

View File

@ -1,10 +1,12 @@
import graphene import graphene
from graphene import relay
from graphene_django import DjangoObjectType from graphene_django import DjangoObjectType
from books.models import ContentBlock from books.models import ContentBlock
from books.schema.interfaces.contentblock import ContentBlockInterface from books.schema.interfaces.contentblock import ContentBlockInterface
from books.utils import are_solutions_enabled_for from books.utils import are_solutions_enabled_for
from core.logger import get_logger from core.logger import get_logger
from core.mixins import HiddenAndVisibleForMixin
from notes.models import ContentBlockBookmark from notes.models import ContentBlockBookmark
from notes.schema import ContentBlockBookmarkNode from notes.schema import ContentBlockBookmarkNode
from rooms.models import ModuleRoomSlug from rooms.models import ModuleRoomSlug
@ -34,11 +36,9 @@ def is_solution_and_hidden_for_user(type, user, module):
return type == 'solution' and not (are_solutions_enabled_for(user, module) or user.is_teacher()) return type == 'solution' and not (are_solutions_enabled_for(user, module) or user.is_teacher())
class ContentBlockNode(DjangoObjectType): class ContentBlockNode(DjangoObjectType, HiddenAndVisibleForMixin):
mine = graphene.Boolean() mine = graphene.Boolean()
bookmarks = graphene.List(ContentBlockBookmarkNode) bookmarks = graphene.List(ContentBlockBookmarkNode)
hidden_for = graphene.List('users.schema.SchoolClassNode')
visible_for = graphene.List('users.schema.SchoolClassNode')
class Meta: class Meta:
model = ContentBlock model = ContentBlock
@ -48,7 +48,7 @@ class ContentBlockNode(DjangoObjectType):
filter_fields = [ filter_fields = [
'slug', 'title', 'slug', 'title',
] ]
interfaces = (ContentBlockInterface,) interfaces = (relay.Node, ContentBlockInterface,)
convert_choices_to_enum = False convert_choices_to_enum = False
def resolve_mine(parent, info, **kwargs): def resolve_mine(parent, info, **kwargs):
@ -78,12 +78,6 @@ class ContentBlockNode(DjangoObjectType):
content_block=self content_block=self
) )
def resolve_hidden_for(parent, info, **kwargs):
return parent.hidden_for.all()
def resolve_visible_for(parent, info, **kwargs):
return parent.visible_for.all()
def process_module_room_slug_block(content): def process_module_room_slug_block(content):
if content['type'] == 'module_room_slug': if content['type'] == 'module_room_slug':

View File

@ -4,8 +4,6 @@ from graphene import relay, ObjectType
from graphene_django import DjangoObjectType from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField from graphene_django.filter import DjangoFilterConnectionField
from . import ChapterInSnapshotNode
from books.models.snapshot import Snapshot from books.models.snapshot import Snapshot
from ..interfaces import ModuleInterface, ChapterInterface from ..interfaces import ModuleInterface, ChapterInterface
from ..interfaces.contentblock import ContentBlockInterface from ..interfaces.contentblock import ContentBlockInterface
@ -47,14 +45,14 @@ class SnapshotChapter:
class SnapshotContentBlockNode(ObjectType): class SnapshotContentBlockNode(ObjectType):
class Meta: class Meta:
interfaces = (ContentBlockInterface,) interfaces = (relay.Node, ContentBlockInterface,)
hidden = graphene.Boolean() hidden = graphene.Boolean()
class SnapshotChapterNode(ObjectType): class SnapshotChapterNode(ObjectType):
class Meta: class Meta:
interfaces = (ChapterInterface,) interfaces = (relay.Node, ChapterInterface,)
content_blocks = graphene.List(SnapshotContentBlockNode) content_blocks = graphene.List(SnapshotContentBlockNode)
description_hidden = graphene.Boolean() description_hidden = graphene.Boolean()

View File

@ -4,10 +4,9 @@ from graphene_django.filter import DjangoFilterConnectionField
from api.utils import get_object from api.utils import get_object
from core.logger import get_logger from core.logger import get_logger
from ..models import Book, Topic, Module, Chapter, Snapshot
from .nodes import ContentBlockNode, ChapterNode, ModuleNode, TopicNode, SnapshotNode
from .connections import TopicConnection, ModuleConnection from .connections import TopicConnection, ModuleConnection
from .nodes import ContentBlockNode, ChapterNode, ModuleNode, TopicNode, SnapshotNode
from ..models import Book, Topic, Module, Chapter, Snapshot
logger = get_logger(__name__) logger = get_logger(__name__)

View File

@ -3,8 +3,8 @@ from graphene.test import Client
from graphql_relay import to_global_id from graphql_relay import to_global_id
from api.schema import schema from api.schema import schema
from books.models import ContentBlock, Chapter
from books.factories import ModuleFactory from books.factories import ModuleFactory
from books.models import ContentBlock, Chapter
from users.factories import SchoolClassFactory from users.factories import SchoolClassFactory
from users.models import User from users.models import User
from users.services import create_users from users.services import create_users
@ -29,7 +29,6 @@ class ContentBlocksVisibleForTeachers(TestCase):
user_content_block.visible_for.add(school_class) user_content_block.visible_for.add(school_class)
user_content_block.save() user_content_block.save()
teacher_request = RequestFactory().get('/') teacher_request = RequestFactory().get('/')
teacher_request.user = teacher teacher_request.user = teacher
self.teacher_client = Client(schema=schema, context_value=teacher_request) self.teacher_client = Client(schema=schema, context_value=teacher_request)
@ -49,7 +48,7 @@ class ContentBlocksVisibleForTeachers(TestCase):
'id': self.chapter 'id': self.chapter
}) })
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks').get('edges')), 2) self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks')), 2)
def test_teacher_created_content_block(self): def test_teacher_created_content_block(self):
self.assertEqual(ContentBlock.objects.count(), 2) self.assertEqual(ContentBlock.objects.count(), 2)
@ -58,14 +57,10 @@ class ContentBlocksVisibleForTeachers(TestCase):
query ChapterQuery($id: ID!) { query ChapterQuery($id: ID!) {
chapter(id: $id) { chapter(id: $id) {
contentBlocks { contentBlocks {
edges {
node {
id id
} }
} }
} }
}
}
""" """
self._assert_school_classes(query, self.student_client) self._assert_school_classes(query, self.student_client)

View File

@ -10,8 +10,6 @@ query ContentBlockQuery($slug: String!) {
node { node {
id id
contentBlocks { contentBlocks {
edges {
node {
id id
title title
type type
@ -21,8 +19,6 @@ query ContentBlockQuery($slug: String!) {
} }
} }
} }
}
}
""" """
@ -46,7 +42,7 @@ class ContentBlockTestCase(SkillboxTestCase):
}) })
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
module = result.get('data').get('module') module = result.get('data').get('module')
content_block = module['chapters']['edges'][0]['node']['contentBlocks']['edges'][0]['node'] content_block = module['chapters']['edges'][0]['node']['contentBlocks'][0]
self.assertEqual(content_block['title'], 'Title') self.assertEqual(content_block['title'], 'Title')
self.assertIsNotNone(content_block['type']) self.assertIsNotNone(content_block['type'])

View File

@ -20,48 +20,38 @@ fragment SchoolClassFragment on SchoolClassNode {
} }
""" """
EDGES_FRAGMENT = SCHOOL_CLASS_FRAGMENT + """ CONTENT_BLOCK_QUERY = SCHOOL_CLASS_FRAGMENT + """
fragment SchoolClassNodeFragment on SchoolClassNodeConnection { query ContentBlockQuery($id: ID!) {
edges { contentBlock(id: $id) {
node { hiddenFor {
...SchoolClassFragment
}
visibleFor {
...SchoolClassFragment ...SchoolClassFragment
} }
} }
} }
""" """
CONTENT_BLOCK_QUERY = EDGES_FRAGMENT + """ CHAPTER_QUERY = SCHOOL_CLASS_FRAGMENT + """
query ContentBlockQuery($id: ID!) {
contentBlock(id: $id) {
hiddenFor {
...SchoolClassNodeFragment
}
visibleFor {
...SchoolClassNodeFragment
}
}
}
"""
CHAPTER_QUERY = EDGES_FRAGMENT + """
query ChapterQuery($id: ID!) { query ChapterQuery($id: ID!) {
chapter(id: $id) { chapter(id: $id) {
id id
titleHiddenFor { titleHiddenFor {
...SchoolClassNodeFragment ...SchoolClassFragment
} }
descriptionHiddenFor { descriptionHiddenFor {
...SchoolClassNodeFragment ...SchoolClassFragment
} }
} }
} }
""" """
OBJECTIVE_GROUP_QUERY = EDGES_FRAGMENT + """ OBJECTIVE_GROUP_QUERY = SCHOOL_CLASS_FRAGMENT + """
query ObjectiveGroupQuery($id: ID!) { query ObjectiveGroupQuery($id: ID!) {
objectiveGroup(id: $id) { objectiveGroup(id: $id) {
hiddenFor { hiddenFor {
...SchoolClassNodeFragment ...SchoolClassFragment
} }
objectives { objectives {
edges { edges {
@ -69,10 +59,10 @@ query ObjectiveGroupQuery($id: ID!) {
id id
text text
hiddenFor { hiddenFor {
...SchoolClassNodeFragment ...SchoolClassFragment
} }
visibleFor { visibleFor {
...SchoolClassNodeFragment ...SchoolClassFragment
} }
} }
} }
@ -193,61 +183,61 @@ class CopyVisibilityForClassesTestCase(TestCase):
def _test_in_sync(self): def _test_in_sync(self):
# the hidden block is hidden for both now # the hidden block is hidden for both now
hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.hidden_content_block) hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.hidden_content_block)
hidden_for = hidden_result.get('data').get('contentBlock').get('hiddenFor').get('edges') hidden_for = hidden_result.get('data').get('contentBlock').get('hiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], hidden_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], hidden_for))
# the other hidden block is hidden for no one now # the other hidden block is hidden for no one now
other_hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, other_hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client,
self.other_hidden_content_block) self.other_hidden_content_block)
hidden_for = other_hidden_result.get('data').get('contentBlock').get('hiddenFor').get('edges') hidden_for = other_hidden_result.get('data').get('contentBlock').get('hiddenFor')
self.assertEqual(len(hidden_for), 0) self.assertEqual(len(hidden_for), 0)
# the default block is still hidden for no one # the default block is still hidden for no one
default_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.default_content_block) default_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.default_content_block)
hidden_for = default_result.get('data').get('contentBlock').get('hiddenFor').get('edges') hidden_for = default_result.get('data').get('contentBlock').get('hiddenFor')
self.assertEqual(len(hidden_for), 0) self.assertEqual(len(hidden_for), 0)
# the custom block is visible for both # the custom block is visible for both
custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.custom_content_block) custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.custom_content_block)
visible_for = custom_result.get('data').get('contentBlock').get('visibleFor').get('edges') visible_for = custom_result.get('data').get('contentBlock').get('visibleFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], visible_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], visible_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], visible_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], visible_for))
# the other custom block is visible for no one # the other custom block is visible for no one
other_custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, other_custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client,
self.other_custom_content_block) self.other_custom_content_block)
visible_for = other_custom_result.get('data').get('contentBlock').get('visibleFor').get('edges') visible_for = other_custom_result.get('data').get('contentBlock').get('visibleFor')
self.assertEqual(len(visible_for), 0) self.assertEqual(len(visible_for), 0)
def test_hidden_for_and_visible_for_set_correctly(self): def test_hidden_for_and_visible_for_set_correctly(self):
self.assertEqual(ContentBlock.objects.count(), 5) self.assertEqual(ContentBlock.objects.count(), 5)
hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.hidden_content_block) hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.hidden_content_block)
hidden_for = hidden_result.get('data').get('contentBlock').get('hiddenFor').get('edges') hidden_for = hidden_result.get('data').get('contentBlock').get('hiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], hidden_for))
self.assertFalse(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertFalse(SYNC_CLASS_NAME in map(lambda x: x['name'], hidden_for))
other_hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, other_hidden_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client,
self.other_hidden_content_block) self.other_hidden_content_block)
hidden_for = other_hidden_result.get('data').get('contentBlock').get('hiddenFor').get('edges') hidden_for = other_hidden_result.get('data').get('contentBlock').get('hiddenFor')
self.assertFalse(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertFalse(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], hidden_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], hidden_for))
default_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.default_content_block) default_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.default_content_block)
hidden_for = default_result.get('data').get('contentBlock').get('hiddenFor').get('edges') hidden_for = default_result.get('data').get('contentBlock').get('hiddenFor')
self.assertEqual(len(hidden_for), 0) self.assertEqual(len(hidden_for), 0)
custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.custom_content_block) custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, self.custom_content_block)
visible_for = custom_result.get('data').get('contentBlock').get('visibleFor').get('edges') visible_for = custom_result.get('data').get('contentBlock').get('visibleFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], visible_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], visible_for))
self.assertFalse(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], visible_for)) self.assertFalse(SYNC_CLASS_NAME in map(lambda x: x['name'], visible_for))
other_custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client, other_custom_result = self._get_result(CONTENT_BLOCK_QUERY, self.teacher_client,
self.other_custom_content_block) self.other_custom_content_block)
visible_for = other_custom_result.get('data').get('contentBlock').get('visibleFor').get('edges') visible_for = other_custom_result.get('data').get('contentBlock').get('visibleFor')
self.assertFalse(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], visible_for)) self.assertFalse(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], visible_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], visible_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], visible_for))
def test_syncs_correctly(self): def test_syncs_correctly(self):
self.module.sync_from_school_class(self.template_school_class, self.school_class_to_be_synced) self.module.sync_from_school_class(self.template_school_class, self.school_class_to_be_synced)
@ -269,24 +259,24 @@ class CopyVisibilityForClassesTestCase(TestCase):
result = self.student1_client.execute(query, variables=variables) result = self.student1_client.execute(query, variables=variables)
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
chapter = result.get('data').get('chapter') chapter = result.get('data').get('chapter')
title_hidden_for = chapter.get('titleHiddenFor').get('edges') title_hidden_for = chapter.get('titleHiddenFor')
description_hidden_for = chapter.get('descriptionHiddenFor').get('edges') description_hidden_for = chapter.get('descriptionHiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], title_hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], title_hidden_for))
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], description_hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], description_hidden_for))
self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['node']['name'], title_hidden_for)) self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['name'], title_hidden_for))
self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['node']['name'], description_hidden_for)) self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['name'], description_hidden_for))
self._execute_sync() self._execute_sync()
result = self.student1_client.execute(query, variables=variables) result = self.student1_client.execute(query, variables=variables)
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
chapter = result.get('data').get('chapter') chapter = result.get('data').get('chapter')
title_hidden_for = chapter.get('titleHiddenFor').get('edges') title_hidden_for = chapter.get('titleHiddenFor')
description_hidden_for = chapter.get('descriptionHiddenFor').get('edges') description_hidden_for = chapter.get('descriptionHiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], title_hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], title_hidden_for))
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], description_hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], description_hidden_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], title_hidden_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], title_hidden_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], description_hidden_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], description_hidden_for))
def _objective_group_query(self): def _objective_group_query(self):
query = OBJECTIVE_GROUP_QUERY query = OBJECTIVE_GROUP_QUERY
@ -303,29 +293,29 @@ class CopyVisibilityForClassesTestCase(TestCase):
def test_objective_group_visibility(self): def test_objective_group_visibility(self):
query, variables = self._objective_group_query() query, variables = self._objective_group_query()
objective_group = self._get_objective_group(self.student1_client, query, variables) objective_group = self._get_objective_group(self.student1_client, query, variables)
hidden_for = objective_group.get('hiddenFor').get('edges') hidden_for = objective_group.get('hiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], hidden_for))
self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['name'], hidden_for))
self._execute_sync() self._execute_sync()
objective_group = self._get_objective_group(self.student1_client, query, variables) objective_group = self._get_objective_group(self.student1_client, query, variables)
hidden_for = objective_group.get('hiddenFor').get('edges') hidden_for = objective_group.get('hiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], hidden_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], hidden_for))
def test_objective_visibility(self): def test_objective_visibility(self):
query, variables = self._objective_group_query() query, variables = self._objective_group_query()
objective_group = self._get_objective_group(self.student2_client, query, variables) objective_group = self._get_objective_group(self.student2_client, query, variables)
objective = objective_group.get('objectives').get('edges')[0]['node'] objective = objective_group.get('objectives')['edges'][0]['node']
hidden_for = objective.get('hiddenFor').get('edges') hidden_for = objective.get('hiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], hidden_for))
self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(SYNC_CLASS_NAME not in map(lambda x: x['name'], hidden_for))
self._execute_sync() self._execute_sync()
objective_group = self._get_objective_group(self.student2_client, query, variables) objective_group = self._get_objective_group(self.student2_client, query, variables)
objective = objective_group.get('objectives').get('edges')[0]['node'] objective = objective_group.get('objectives')['edges'][0]['node']
hidden_for = objective.get('hiddenFor').get('edges') hidden_for = objective.get('hiddenFor')
self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(TEMPLATE_CLASS_NAME in map(lambda x: x['name'], hidden_for))
self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['node']['name'], hidden_for)) self.assertTrue(SYNC_CLASS_NAME in map(lambda x: x['name'], hidden_for))

View File

@ -1,4 +1,4 @@
from django.test import TestCase, RequestFactory from django.test import RequestFactory
from graphene.test import Client from graphene.test import Client
from graphql_relay import to_global_id, from_global_id from graphql_relay import to_global_id, from_global_id
@ -7,7 +7,6 @@ from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory
from books.models import Snapshot, ChapterSnapshot from books.models import Snapshot, ChapterSnapshot
from core.tests.base_test import SkillboxTestCase from core.tests.base_test import SkillboxTestCase
from users.models import User, SchoolClass from users.models import User, SchoolClass
from users.services import create_users
MODULE_QUERY = """ MODULE_QUERY = """
query ModulesQuery($slug: String!) { query ModulesQuery($slug: String!) {
@ -19,20 +18,12 @@ query ModulesQuery($slug: String!) {
node { node {
id id
contentBlocks { contentBlocks {
edges {
node {
id id
title title
visibleFor { visibleFor {
edges {
node {
name name
} }
}
}
hiddenFor { hiddenFor {
edges {
node {
name name
} }
} }
@ -41,10 +32,6 @@ query ModulesQuery($slug: String!) {
} }
} }
} }
}
}
}
}
""" """
CREATE_SNAPSHOT_MUTATION = """ CREATE_SNAPSHOT_MUTATION = """
@ -150,7 +137,7 @@ class CreateSnapshotTestCase(SkillboxTestCase):
module = result.get('data').get('module') module = result.get('data').get('module')
chapter = edges_to_array(module.get('chapters'))[0] chapter = edges_to_array(module.get('chapters'))[0]
self.assertIsNotNone(chapter) self.assertIsNotNone(chapter)
content_blocks = edges_to_array(chapter.get('contentBlocks')) content_blocks = chapter.get('contentBlocks')
content_block_titles = [node['title'] for node in content_blocks] content_block_titles = [node['title'] for node in content_blocks]
self.assertTrue(self.title_visible in content_block_titles) self.assertTrue(self.title_visible in content_block_titles)
self.assertTrue(self.title_hidden in content_block_titles) self.assertTrue(self.title_hidden in content_block_titles)
@ -162,11 +149,11 @@ class CreateSnapshotTestCase(SkillboxTestCase):
# check if hidden node is hidden for this school class # check if hidden node is hidden for this school class
self.assertTrue( self.assertTrue(
school_class_name in [school_class['name'] for school_class in school_class_name in [school_class['name'] for school_class in
edges_to_array(hidden_node.get('hiddenFor'))]) hidden_node.get('hiddenFor')])
# check if the custom node is visible for this school class # check if the custom node is visible for this school class
self.assertTrue( self.assertTrue(
school_class_name in [school_class['name'] for school_class in school_class_name in [school_class['name'] for school_class in
edges_to_array(custom_node.get('visibleFor'))]) custom_node.get('visibleFor')])
def test_setup(self): def test_setup(self):
# make sure everything is setup correctly # make sure everything is setup correctly

View File

@ -37,28 +37,24 @@ class OwnContentTestCase(TestCase):
id id
title title
contentBlocks { contentBlocks {
edges {
node {
id id
title title
} }
} }
} }
}
}
""" """
result = self.client.execute(chapterQuery, variables={ result = self.client.execute(chapterQuery, variables={
"id": self.chapter_id "id": self.chapter_id
}) })
self.assertIsNone(result.get('errors')) self.assertIsNone(result.get('errors'))
self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks').get('edges')), 1) self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks')), 1)
custom_content_block = ContentBlock(title='own', slug='own', user_created=True, owner=self.user) custom_content_block = ContentBlock(title='own', slug='own', user_created=True, owner=self.user)
self.chapter.specific.add_child(instance=custom_content_block) self.chapter.specific.add_child(instance=custom_content_block)
result = self.client.execute(chapterQuery, variables={ result = self.client.execute(chapterQuery, variables={
"id": self.chapter_id "id": self.chapter_id
}) })
self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks').get('edges')), 2) self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks')), 2)
for school_class in self.user.school_classes.all(): for school_class in self.user.school_classes.all():
custom_content_block.visible_for.add(school_class) custom_content_block.visible_for.add(school_class)
@ -66,5 +62,5 @@ class OwnContentTestCase(TestCase):
result = self.client.execute(chapterQuery, variables={ result = self.client.execute(chapterQuery, variables={
"id": self.chapter_id "id": self.chapter_id
}) })
self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks').get('edges')), 2) self.assertEqual(len(result.get('data').get('chapter').get('contentBlocks')), 2)

View File

@ -372,8 +372,8 @@ GRAPHENE = {
# http://docs.wagtail.io/en/v2.1/advanced_topics/settings.html?highlight=urls # http://docs.wagtail.io/en/v2.1/advanced_topics/settings.html?highlight=urls
WAGTAIL_SITE_NAME = 'skillbox' WAGTAIL_SITE_NAME = 'skillbox'
GRAPHQL_QUERIES_DIR = os.path.join(BASE_DIR, '..', 'client', 'src', 'graphql', 'gql') GRAPHQL_QUERIES_DIR = os.path.join(BASE_DIR, '..', 'client', 'src', 'graphql', 'gql', 'queries')
GRAPHQL_MUTATIONS_DIR = os.path.join(GRAPHQL_QUERIES_DIR, 'mutations') GRAPHQL_MUTATIONS_DIR = os.path.join(GRAPHQL_QUERIES_DIR, '../mutations')
# Sendgrid Config # Sendgrid Config

View File

@ -4,10 +4,11 @@ from graphene import relay
from graphene_django import DjangoObjectType from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField from graphene_django.filter import DjangoFilterConnectionField
from core.mixins import HiddenAndVisibleForMixin, HiddenForMixin
from objectives.models import ObjectiveGroup, Objective from objectives.models import ObjectiveGroup, Objective
class ObjectiveGroupNode(DjangoObjectType): class ObjectiveGroupNode(DjangoObjectType, HiddenForMixin):
pk = graphene.Int() pk = graphene.Int()
display_title = graphene.String() display_title = graphene.String()
@ -36,7 +37,7 @@ class ObjectiveGroupNode(DjangoObjectType):
return self.objectives.filter(objectives_from_publisher | objectives_from_teacher) return self.objectives.filter(objectives_from_publisher | objectives_from_teacher)
class ObjectiveNode(DjangoObjectType): class ObjectiveNode(DjangoObjectType, HiddenAndVisibleForMixin):
pk = graphene.Int() pk = graphene.Int()
user_created = graphene.Boolean() user_created = graphene.Boolean()
mine = graphene.Boolean() mine = graphene.Boolean()