skillbox/server/assignments/tests/test_custom_assignments.py

108 lines
3.2 KiB
Python

import json
from django.test import TestCase, RequestFactory
from graphene.test import Client
from api import schema
from api.schema import schema
from assignments.factories import AssignmentFactory
from assignments.models import Assignment
from books.factories import ModuleFactory
from books.models import ContentBlock, Chapter
from core.factories import UserFactory
from users.models import User
from users.services import create_users
class CustomAssignmentTestCase(TestCase):
def setUp(self):
create_users()
self.teacher = User.objects.get(username='teacher')
self.student1 = User.objects.get(username='student1')
self.assignment = AssignmentFactory(
owner=self.teacher,
user_created=True
)
self.module = ModuleFactory()
chapter = Chapter(slug='some-slug', title='chapter')
self.module.add_child(instance=chapter)
self.content_block = ContentBlock(
slug='slug',
title='title'
)
self.admin = UserFactory(
username='test',
is_staff=True,
is_superuser=True,
first_name='Nicol',
last_name='Bolas'
)
chapter.specific.add_child(instance=self.content_block)
request = RequestFactory().get('/')
request.user = self.student1
self.client = Client(schema=schema, context_value=request)
def query_module(self):
query = '''
query Module($slug: String!){
module(slug: $slug) {
id
chapters {
edges {
node {
contentBlocks {
edges {
node {
contents
}
}
}
}
}
}
}
}
'''
result = self.client.execute(query, variables={
'slug': self.module.slug
})
self.assertIsNone(result.get('errors'))
return result
@staticmethod
def get_first_contents(result):
return result.get('data').get('module').get('chapters').get('edges')[0].get('node').get('contentBlocks').get(
'edges')[0].get('node').get('contents')
def test_module_query(self):
result = self.query_module()
contents = self.get_first_contents(result)
self.assertIsNotNone(contents)
def test_global_assignment(self):
title = 'Assignment'
assignment = Assignment.objects.create(
title=title,
assignment='Assignment text',
owner=self.admin,
module=self.module
)
self.content_block.contents = json.dumps([
{
'type': 'assignment',
'value': {
'assignment_id': assignment.id
}}
])
self.content_block.save()
result = self.query_module()
contents = self.get_first_contents(result)
self.assertEqual(contents[0].get('value').get('title'), title)