Add first unit test

This commit is contained in:
Ramon Wenger 2021-04-07 10:58:52 +02:00
parent 728ac73821
commit be7e280ebc
1 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,93 @@
from django.test import TestCase, RequestFactory
from graphene.test import Client
from graphql_relay import to_global_id
from api.schema import schema
from books.factories import ModuleFactory, ChapterFactory, ContentBlockFactory
from users.models import User, SchoolClass
from users.services import create_users
MODULE_QUERY = """
query ModulesQuery($slug: String!) {
module(slug: $slug) {
id
title
chapters {
edges {
node {
id
contentBlocks {
edges {
node {
id
visibleFor {
edges {
node {
name
}
}
}
hiddenFor {
edges {
node {
name
}
}
}
}
}
}
}
}
}
}
}
"""
class CreateSnapshotTestCase(TestCase):
def setUp(self):
create_users()
skillbox_class = SchoolClass.objects.get(name='skillbox')
second_class = SchoolClass.objects.get(name='second_class')
# teacher will create snapshot
self.teacher = User.objects.get(username='teacher')
self.module = ModuleFactory(slug='some-module')
# module M has a chapter
self.chapter = ChapterFactory(parent=self.module, slug='some-chapter')
# chapter has some content blocks a, b, c
self.a = ContentBlockFactory(parent=self.chapter, module=self.module, slug='cb-a')
self.b = ContentBlockFactory(parent=self.chapter, module=self.module, slug='cb-b')
# content block c is user created
self.c = ContentBlockFactory(parent=self.chapter, owner=self.teacher, user_created=True, module=self.module, slug='cb-c')
# content block a and c are visible to school class X
self.b.hidden_for.add(skillbox_class)
self.c.visible_for.add(skillbox_class)
request = RequestFactory().get('/')
request.user = self.teacher
self.client = Client(schema=schema, context_value=request)
# we make a snapshot S of the module M
# snapshot S looks like module M for school class X
def test_visible_and_hidden(self):
result = self.client.execute(MODULE_QUERY, variables={
'slug': self.module.slug
})
self.assertIsNone(result.get('errors'))
module = result.get('data').get('module')
chapter = module.get('chapters').get('edges')[0]['node']
self.assertIsNotNone(chapter)
content_blocks = [edge['node'] for edge in chapter.get('contentBlocks').get('edges')]
content_block_ids = [node['id'] for node in content_blocks]
self.assertTrue(to_global_id('ContentBlockNode', self.a.id) in content_block_ids)
self.assertTrue(to_global_id('ContentBlockNode', self.b.id) in content_block_ids)
self.assertTrue(to_global_id('ContentBlockNode', self.c.id) in content_block_ids)
b = [node for node in content_blocks if node['id'] == to_global_id('ContentBlockNode', self.b.id)][0]
c = [node for node in content_blocks if node['id'] == to_global_id('ContentBlockNode', self.c.id)][0]
self.assertTrue('skillbox' in [edge['node']['name'] for edge in b.get('hiddenFor').get('edges')])
self.assertTrue('skillbox' in [edge['node']['name'] for edge in c.get('visibleFor').get('edges')])