Exemplo n.º 1
0
class ConceptsTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='******', password='******')
        self.client.login(username='******', password='******')
        self.course = Course(title='test_title', addedBy=self.user)
        self.course.save()
        self.unit = Unit(title='test unit title', addedBy=self.user)
        self.unit.save()
        self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user)
        self.course_unit.save()
        self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR)
        self.role.save()
        self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user)
        self.lesson = Lesson(title='New York Test Lesson', text='brr', addedBy=self.user)
        self.lesson.save_root(self.concept)
        self.unit_lesson = UnitLesson(unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id)
        self.unit_lesson.save()

    def test_unit_concepts_search(self):
        self.lesson.concept = self.concept
        self.lesson.save()
        response = self.client.get(
            reverse('ct:unit_concepts', kwargs={'course_id': self.course.id, 'unit_id': self.unit.id}),
            {'search': 'New York'},
            follow=True
        )
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'ct/concepts.html')
        self.assertIn('actionTarget', response.context)
        cset_dict = {i[0]: i[1] for i in response.context['cset']}
        self.assertIn('New York Test Lesson', cset_dict)
        self.assertIn('New York', cset_dict)
        self.assertIn('The New York Times Company', cset_dict)
Exemplo n.º 2
0
class ConceptsTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='******', password='******')
        self.client.login(username='******', password='******')
        self.course = Course(title='test_title', addedBy=self.user)
        self.course.save()
        self.unit = Unit(title='test unit title', addedBy=self.user)
        self.unit.save()
        self.course_unit = CourseUnit(course=self.course, unit=self.unit, order=0, addedBy=self.user)
        self.course_unit.save()
        self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR)
        self.role.save()
        self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user)
        self.lesson = Lesson(title='New York Test Lesson', text='brr', addedBy=self.user)
        self.lesson.save_root(self.concept)
        self.unit_lesson = UnitLesson(unit=self.unit, lesson=self.lesson, addedBy=self.user, treeID=self.lesson.id)
        self.unit_lesson.save()

    def test_unit_concepts_search(self):
        self.lesson.concept = self.concept
        self.lesson.save()
        response = self.client.get(
            reverse('ct:unit_concepts', kwargs={'course_id': self.course.id, 'unit_id': self.unit.id}),
            {'search': 'New York'},
            follow=True
        )
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'ct/concepts.html')
        self.assertIn('actionTarget', response.context)
        cset_dict = {i[0]: i[1] for i in response.context['cset']}
        self.assertIn('New York Test Lesson', cset_dict)
        self.assertIn('New York', cset_dict)
        self.assertIn('The New York Times Company', cset_dict)
Exemplo n.º 3
0
def lesson_question_parametrized(request, user, concept, temp_image):
    instance = Lesson(
        title='ugh', text='brr', addedBy=user,
        kind=Lesson.ORCT_QUESTION, sub_kind=request.param,
        concept=concept, attachment=temp_image
    )
    instance.save_root(concept)
    return instance
Exemplo n.º 4
0
def lesson_question(user, concept):
    instance = Lesson(title='ugh',
                      text='brr',
                      addedBy=user,
                      kind=Lesson.ORCT_QUESTION,
                      concept=concept)
    instance.save_root(concept)
    return instance
Exemplo n.º 5
0
def lesson_question_canvas(user, concept, base64_gif_image):
    instance = Lesson(
        title='ugh', text='brr', addedBy=user,
        kind=Lesson.ORCT_QUESTION, sub_kind=Lesson.CANVAS,
        concept=concept, attachment=base64_to_file('data:image/gif;base64,{}'.format(base64_gif_image)),
    )
    instance.save_root(concept)
    return instance
Exemplo n.º 6
0
def lesson_question_parametrized(request, user, concept, image_file):
    instance = Lesson(title='ugh',
                      text='brr',
                      addedBy=user,
                      kind=Lesson.ORCT_QUESTION,
                      sub_kind=request.param,
                      concept=concept,
                      attachment=image_file)
    instance.save_root(concept)
    return instance
Exemplo n.º 7
0
def lesson_question_canvas(user, concept, base64_gif_image):
    instance = Lesson(
        title='ugh',
        text='brr',
        addedBy=user,
        kind=Lesson.ORCT_QUESTION,
        sub_kind=Lesson.CANVAS,
        concept=concept,
        attachment=base64_to_file(
            'data:image/gif;base64,{}'.format(base64_gif_image)),
    )
    instance.save_root(concept)
    return instance
Exemplo n.º 8
0
class TagsTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='******', password='******')
        self.course = Course(title='test_title', addedBy=self.user)
        self.course.save()
        self.concept = Concept(title='test title', addedBy=self.user)
        self.concept.save()
        self.lesson = Lesson(title='ugh',
                             text='brr',
                             addedBy=self.user,
                             kind=Lesson.ORCT_QUESTION)
        self.lesson.save_root()
        self.lesson.add_concept_link(self.concept, ConceptLink.TESTS,
                                     self.user)
        self.unit = Unit(title='test unit title', addedBy=self.user)
        self.unit.save()
        self.unit_lesson = UnitLesson(unit=self.unit,
                                      addedBy=self.user,
                                      treeID=42,
                                      lesson=self.lesson)
        self.unit_lesson.save()
        self.response = Response(course=self.course,
                                 lesson=self.lesson,
                                 author=self.user,
                                 unitLesson=self.unit_lesson,
                                 confidence=Response.GUESS,
                                 title='test title',
                                 text='test text')
        self.response.save()
        self.context = {
            'actionTarget': '/ct/courses/1/units/1/',
            'ul': self.unit_lesson,
            'test_text': 'This is a test text',
            'r': self.response
        }

    def render_template(self, string, context=None):
        context = context or {}
        context = Context(context)
        return Template(string).render(context)

    @unpack
    @data(
        ('{{ test_text | md2html }}', '<p>This is a test text</p>\n'),
        ('{{ actionTarget | get_object_url:ul }}',
         '/ct/courses/1/units/1/lessons/1/'),
        ('{{ actionTarget | get_home_url:ul }}',
         '/ct/courses/1/units/1/lessons/1/'),
        ('{{ actionTarget | get_thread_url:r }}',
         '/ct/courses/1/units/1/lessons/1/faq/1/'),
        ('{{ actionTarget | get_tasks_url:ul }}',
         '/ct/courses/1/units/1/lessons/1/tasks/'),
        ('{{ actionTarget | get_dummy_navbar }}',
         '<li><a href="/ct/courses/1/">Course</a></li>'),
    )
    def test_all_filters(self, template_variable, expected_result):
        rendered = self.render_template('{% load ct_extras %}' +
                                        template_variable,
                                        context=self.context)
        self.assertEqual(rendered, expected_result)

    @patch('ct.templatetags.ct_extras.pypandoc')
    def test_md2html_pandoc_exception(self, pypandoc):
        pypandoc.convert.side_effect = StandardError
        rendered = self.render_template(
            '{% load ct_extras %}'
            '{{ test_text | md2html }}',
            context=self.context)
        self.assertEqual(rendered, self.context['test_text'])

    @data('get_base_url', 'get_path_type')
    def test_get_base_url_exception(self, helper):
        with self.assertRaises(ValueError):
            getattr(inspect.getmodule(self),
                    helper).__call__('/ct/courses/1/units/1/lessons/1/',
                                     baseToken='non_existent_token')

    def test_get_object_url_exception(self):
        self.context[
            'ul'] = self.unit  # Unit object does not have get_url method
        rendered = self.render_template(
            '{% load ct_extras %}'
            '{{ actionTarget | get_object_url:ul }}',
            context=self.context)
        self.assertEqual(rendered, '/ct/courses/1/units/1/unit/1/teach/')

    @patch('ct.templatetags.ct_extras.timezone')
    def test_display_datetime(self, timezone_patched):
        saved_time = timezone.now()
        timezone_patched.now.return_value = saved_time
        context = {'dt': saved_time - timedelta(1)}
        rendered = self.render_template(
            '{% load ct_extras %}'
            '{{ dt|display_datetime }}',
            context=context)
        self.assertEqual(rendered, '1 day ago')

    def test_find_audio(self):
        result = find_audio('test tag head .. audio:: test tag tail \n', 4)
        self.assertEqual(result, (14, 39, 'test tag tail'))

    def test_audio_html(self):
        """
        Function should return string for embeding audio into html.
        """
        result = audio_html('audio.mp3')
        self.assertEqual(
            result,
            '''<audio controls><source src="audio.ogg" type="audio/ogg"><source src="audio.mp3" '''
            '''type="audio/mpeg">no support for audio!</audio>''')

    def test_video_html(self):
        """
        Function should return string for embeding youtube or vimeo link.
        """
        result = video_html('youtube:test_video_path')
        self.assertIn('test_video_path', result)
        self.assertIn('src="https://www.youtube.com/embed/', result)

        result = video_html('vimeo:test_video_path')
        self.assertIn('test_video_path', result)
        self.assertIn('src="https://player.vimeo.com/video/', result)

    def test_video_html_with_exception(self):
        """
        Test exception handling.
        """
        result = video_html('youtube')
        self.assertEqual(result, 'ERROR: bad video source: youtube')

        result = video_html('some_new_cdn:test_video_path')
        self.assertEqual(result, 'ERROR: unknown video sourceDB: some_new_cdn')

    def test_add_replace_temporary_markers(self):
        """
        Test add_temporary_markers and replace_temporary_markers in tandem.
        """
        result = add_temporary_markers(
            'test tag head .. audio:: test tag tail \n', find_audio)
        self.assertEqual(
            result,
            ('test tag head mArKeR:0:\n', [('mArKeR:0:', 'test tag tail')]))

        result = replace_temporary_markers(result[0], audio_html, result[1])
        self.assertEqual(
            result,
            '''test tag head <audio controls><source src="test tag tail.ogg" type="audio/ogg">'''
            '''<source src="test tag tail.mp3" type="audio/mpeg">no support for audio!</audio>\n'''
        )
Exemplo n.º 9
0
class TestCourseletViewHistoryTab(TestCase):

    def setUp(self):
        self.user = User.objects.create_user(username='******', password='******')
        self.client.login(username='******', password='******')

        self.course = Course(title='test_title', addedBy=self.user)
        self.course.save()
        self.unit = Unit(title='test unit title', addedBy=self.user)
        self.unit.save()
        self.course_unit = CourseUnit(
            course=self.course,
            unit=self.unit,
            order=0,
            addedBy=self.user
        )
        self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1)
        self.course_unit.save()

        self.enroll = EnrollUnitCode(courseUnit=self.course_unit)
        self.enroll.save()

        self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR)
        self.role.save()

        self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED)
        self.student_role.save()

        self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user)

        self.lesson = Lesson(
            title='New York Test Lesson',
            text='brr',
            addedBy=self.user,
            kind=Lesson.ORCT_QUESTION
        )
        self.lesson.save_root(self.concept)

        self.unit_lesson = UnitLesson(
            unit=self.unit,
            lesson=self.lesson,
            addedBy=self.user,
            treeID=self.lesson.id,
            order=0
        )
        self.unit_lesson.save()

        self.unit_lesson_answer = UnitLesson(
            parent=self.unit_lesson,
            unit=self.unit,
            lesson=self.lesson,
            addedBy=self.user,
            treeID=self.lesson.id,
            kind=UnitLesson.ANSWERS
        )
        self.unit_lesson_answer.save()

        self.user = User.objects.create_user(username='******', password='******')

        call_command('fsm_deploy')

    def test_click_on_courslet_creates_new_chat(self):
        # test that there's no history yet
        response = self.client.get(
            reverse('lms:course_view', kwargs={'course_id': self.course.id})
        )
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(list(response.context['courslets']))

        self.assertEqual(response.status_code, 200)

        chats_count_1 = Chat.objects.all().count()

        # firstly call to chat:init_chat_api function with enroll_key and chat_id=0
        response = self.client.get(
            reverse(
                'chat:init_chat_api',
                kwargs={
                    'enroll_key': self.enroll.enrollCode,
                    'chat_id': 0
                }
            ),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        json_content = json.loads(response.content)
        chat_id = json_content['id']

        response = self.client.get(
            reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id})
        )
        self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id)
        self.assertEqual(response.status_code, 200)
        chats_count_2 = Chat.objects.count()

        self.assertNotEqual(chats_count_2, chats_count_1)

        response = self.client.get(
            reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id})
        )
        chats_count_3 = Chat.objects.count()

        response = self.client.get(
            reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id})
        )
        chats_count_4 = Chat.objects.count()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(chats_count_4, chats_count_2)
        self.assertEqual(chats_count_3, chats_count_2)

        self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id)

        chat = Chat.objects.all().first()
        # get chat and set state to None it means that courslet finished.
        chat.state = None
        chat.save()

        response = self.client.get(
            reverse('lms:course_view', kwargs={'course_id': self.course.id})
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Chat.objects.count(), chats_count_2)
        self.assertEqual(len(list(response.context['courslets'])), 1)

    def test_courslet_history(self):
        enroll_code = EnrollUnitCode.get_code(self.course_unit)

        response = self.client.get(
            reverse(
                'chat:init_chat_api',
                kwargs={
                    'enroll_key': self.enroll.enrollCode,
                    'chat_id': 0
                }
            ),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        json_content = json.loads(response.content)
        chat_id = json_content['id']

        response = self.client.get(
            reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True
        )

        response = self.client.get(
            reverse('chat:history'), {'chat_id': chat_id}, follow=True
        )
        json_content = json.loads(response.content)

        next_url = json_content['input']['url']

        answer = 'My Answer'
        response = self.client.put(
            next_url,
            data=json.dumps({"text": answer, "chat_id": chat_id}),
            content_type='application/json',
            follow=True
        )

        json_content = json.loads(response.content)
        next_url = json_content['input']['url']

        response = self.client.get(
            next_url, {'chat_id': chat_id}, follow=True
        )

        json_content = json.loads(response.content)
        next_url = json_content['input']['url']

        self.assertIsNotNone(json_content['input']['options'])
        self.assertEqual(len(json_content['addMessages']), 2)

        # emulate chat finished - set state to None

        Chat.objects.filter(id=chat_id).update(state=None)

        response = self.client.get(
            reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True
        )
        response = self.client.get(
            reverse('chat:history'), {'chat_id': chat_id}, follow=True
        )
        json_content = json.loads(response.content)

        self.assertIsNone(json_content['input']['options'])
        self.assertEqual(len(json_content['addMessages']), 4)
Exemplo n.º 10
0
class TestCourseletViewHistoryTab(TestCase):

    def setUp(self):
        self.user = User.objects.create_user(username='******', password='******')
        self.client.login(username='******', password='******')

        self.course = Course(title='test_title', addedBy=self.user)
        self.course.save()
        self.unit = Unit(title='test unit title', addedBy=self.user)
        self.unit.save()
        self.course_unit = CourseUnit(
            course=self.course,
            unit=self.unit,
            order=0,
            addedBy=self.user
        )
        self.course_unit.releaseTime = timezone.now() - datetime.timedelta(days=1)
        self.course_unit.save()

        self.enroll = EnrollUnitCode(courseUnit=self.course_unit)
        self.enroll.save()

        self.role = Role(course=self.course, user=self.user, role=Role.INSTRUCTOR)
        self.role.save()

        self.student_role = Role(course=self.course, user=self.user, role=Role.ENROLLED)
        self.student_role.save()

        self.concept = Concept.new_concept('bad', 'idea', self.unit, self.user)

        self.lesson = Lesson(
            title='New York Test Lesson',
            text='brr',
            addedBy=self.user,
            kind=Lesson.ORCT_QUESTION
        )
        self.lesson.save_root(self.concept)

        self.unit_lesson = UnitLesson(
            unit=self.unit,
            lesson=self.lesson,
            addedBy=self.user,
            treeID=self.lesson.id,
            order=0
        )
        self.unit_lesson.save()

        self.unit_lesson_answer = UnitLesson(
            parent=self.unit_lesson,
            unit=self.unit,
            lesson=self.lesson,
            addedBy=self.user,
            treeID=self.lesson.id,
            kind=UnitLesson.ANSWERS
        )
        self.unit_lesson_answer.save()

        self.user = User.objects.create_user(username='******', password='******')

        call_command('fsm_deploy')

    def test_click_on_courslet_creates_new_chat(self):
        # test that there's no history yet
        response = self.client.get(
            reverse('lms:course_view', kwargs={'course_id': self.course.id})
        )
        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(list(response.context['courslets']))

        self.assertEqual(response.status_code, 200)

        chats_count_1 = Chat.objects.all().count()

        # firstly call to chat:init_chat_api function with enroll_key and chat_id=0
        response = self.client.get(
            reverse(
                'chat:init_chat_api',
                kwargs={
                    'enroll_key': self.enroll.enrollCode,
                    'chat_id': 0
                }
            ),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        json_content = json.loads(response.content)
        chat_id = json_content['id']

        response = self.client.get(
            reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id})
        )
        self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id)
        self.assertEqual(response.status_code, 200)
        chats_count_2 = Chat.objects.count()

        self.assertNotEqual(chats_count_2, chats_count_1)

        response = self.client.get(
            reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id})
        )
        chats_count_3 = Chat.objects.count()

        response = self.client.get(
            reverse('chat:chat_enroll', kwargs={'enroll_key': self.enroll.enrollCode, 'chat_id': chat_id})
        )
        chats_count_4 = Chat.objects.count()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(chats_count_4, chats_count_2)
        self.assertEqual(chats_count_3, chats_count_2)

        self.assertEqual(response.context['chat'].id, Chat.objects.all().first().id)

        chat = Chat.objects.all().first()
        # get chat and set state to None it means that courslet finished.
        chat.state = None
        chat.save()

        response = self.client.get(
            reverse('lms:course_view', kwargs={'course_id': self.course.id})
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Chat.objects.count(), chats_count_2)
        self.assertEqual(len(list(response.context['courslets'])), 1)

    def test_courslet_history(self):
        enroll_code = EnrollUnitCode.get_code(self.course_unit)

        response = self.client.get(
            reverse(
                'chat:init_chat_api',
                kwargs={
                    'enroll_key': self.enroll.enrollCode,
                    'chat_id': 0
                }
            ),
            HTTP_X_REQUESTED_WITH='XMLHttpRequest'
        )
        json_content = json.loads(response.content)
        chat_id = json_content['id']

        response = self.client.get(
            reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True
        )

        response = self.client.get(
            reverse('chat:history'), {'chat_id': chat_id}, follow=True
        )
        json_content = json.loads(response.content)

        next_url = json_content['input']['url']

        answer = 'My Answer'
        response = self.client.put(
            next_url,
            data=json.dumps({"text": answer, "chat_id": chat_id}),
            content_type='application/json',
            follow=True
        )

        json_content = json.loads(response.content)
        next_url = json_content['input']['url']

        response = self.client.get(
            next_url, {'chat_id': chat_id}, follow=True
        )

        json_content = json.loads(response.content)
        next_url = json_content['input']['url']

        self.assertIsNotNone(json_content['input']['options'])
        self.assertEquals(len(json_content['addMessages']), 2)

        # emulate chat finished - set state to None

        Chat.objects.filter(id=chat_id).update(state=None)

        response = self.client.get(
            reverse('chat:chat_enroll', args=(enroll_code, chat_id)), follow=True
        )
        response = self.client.get(
            reverse('chat:history'), {'chat_id': chat_id}, follow=True
        )
        json_content = json.loads(response.content)

        self.assertIsNone(json_content['input']['options'])
        self.assertEquals(len(json_content['addMessages']), 4)
Exemplo n.º 11
0
class FSMTests(OurTestCase):
    """
    Tests for FSM stack.
    """
    def setUp(self):
        self.user = User.objects.create_user(
            username='******', email='jacob@_', password='******'
        )
        # have to login or Django self.client.session storage won't work
        self.client.login(username='******', password='******')
        self.course = Course(
            title='Great Course', description='the bestest', addedBy=self.user
        )
        self.course.save()
        self.unit = Unit(title='My Courselet', addedBy=self.user)
        self.unit.save()
        self.lesson = Lesson(
            title='Big Deal', text='very interesting info', addedBy=self.user
        )
        self.lesson.save_root()
        self.unitLesson = UnitLesson.create_from_lesson(
            self.lesson, self.unit, order='APPEND'
        )
        self.ulQ = create_question_unit(self.user)
        self.ulQ2 = create_question_unit(
            self.user, 'Pretest', 'Scary Question', 'Tell me something.'
        )
        self.json_mixin = JSONBlobMixin()
        self.fsmDict = dict(name='test', title='try this')
        self.nodeDict = dict(
            START=dict(title='start here', path='ct:home', funcName='fsm.fsm_plugin.testme.START'),
            MID=dict(title='in the middle', path='ct:about', doLogging=True),
            END=dict(title='end here', path='ct:home')
        )
        self.edgeDict = (
            dict(name='next', fromNode='START', toNode='END', title='go go go'),
            dict(name='select_Lesson', fromNode='MID', toNode='MID', title='go go go'),
        )

    def test_load(self):
        """
        Check loading an FSM graph, and replacing it.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        self.assertEqual(f.fsmnode_set.count(), 3)
        self.assertEqual(f.startNode.name, 'START')
        self.assertEqual(f.startNode.outgoing.count(), 1)
        e = f.startNode.outgoing.all()[0]
        self.assertEqual(e.name, 'next')
        self.assertEqual(e.toNode.name, 'END')
        f2 = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')  # replace
        self.assertEqual(FSM.objects.get(pk=f.pk).name, 'testOLD')  # renamed
        self.assertNotEqual(f.startNode, f2.startNode)
        self.assertEqual(f.startNode.name, f2.startNode.name)

    def test_json_blob(self):
        """
        Check roundtrip dump/load via json blob data.
        """
        name, pk = self.json_mixin.dump_json_id(self.unit)
        label, obj = self.json_mixin.load_json_id(name, pk)
        self.assertEqual(self.unit, obj)
        self.assertEqual(label, 'Unit')

    def test_json_blob2(self):
        """
        Check roundtrip dump/load via named json blob data.
        """
        name, pk = self.json_mixin.dump_json_id(self.unit, 'fruity')
        self.assertEqual(name, 'fruity_Unit_id')
        label, obj = self.json_mixin.load_json_id(name, pk)
        self.assertEqual(self.unit, obj)
        self.assertEqual(label, 'fruity')

    def test_json_blob3(self):
        """
        Check roundtrip dump/load via json blob string.
        """
        s = self.json_mixin.dump_json_id_dict(dict(fruity=self.unit))
        d = self.json_mixin.load_json_id_dict(s)
        self.assertEqual(d.items(), [('fruity', self.unit)])

    def test_json_blob4(self):
        """
        Check roundtrip dump/load via db storage.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        d = f.startNode.load_json_data()
        self.assertEqual(d, {})
        d['fruity'] = self.unit
        d['anumber'] = 3
        d['astring'] = 'jeff'
        f.startNode.save_json_data(d)
        node = FSMNode.objects.get(pk=f.startNode.pk)
        d2 = node.load_json_data()
        self.assertEqual(d2, {'fruity': self.unit, 'anumber': 3,
                              'astring': 'jeff'})

    def test_start(self):
        """
        Check basic startup of new FSM instance.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        self.do_start(f)

    def test_start2(self):
        """
        Check basic startup of new FSM instance using FSMSpecification.
        """
        from fsm.fsm_plugin.testme import get_specs
        spec = get_specs()[0]
        f = spec.save_graph('jacob')
        self.assertTrue(f.startNode.doLogging)
        self.assertFalse(f.get_node('MID').doLogging)
        fsmStack = self.do_start(f)
        # test filter_input() plugin functionality
        edge = fsmStack.state.fsmNode.outgoing.get(name='next')
        self.assertTrue(edge.filter_input('the right stuff'))
        self.assertFalse(edge.filter_input('the WRONG stuff'))
        # test get_help() plugin functionality
        request = FakeRequest(self.user, path='/ct/about/')
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, 'here here!')
        request = FakeRequest(self.user, path='/ct/courses/1/')
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, 'there there')
        request = FakeRequest(self.user)
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, None)

    def test_start3(self):
        """
        Check that FSMState saves unitLesson, select_ data, and logging.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        fsmStack = self.do_start(f, unitLesson=self.unitLesson)
        self.assertEqual(fsmStack.state.unitLesson, self.unitLesson)
        request = FakeRequest(self.user, method='GET')
        # check logging on the MID node
        fsmStack.event(request, None)  # send a render event to log
        self.assertEqual(fsmStack.state.activity.fsmName, 'test')
        ae = fsmStack.state.activityEvent
        self.assertEqual(ae.nodeName, 'MID')
        self.assertEqual(ae.unitLesson, self.unitLesson)
        self.assertIsNotNone(ae.startTime)
        self.assertIsNone(ae.endTime)
        request = FakeRequest(self.user)
        # now try a select_ event
        fsmStack.event(request, 'select_Lesson', lesson=self.lesson)
        self.assertEqual(fsmStack.state.get_data_attr('lesson'), self.lesson)
        # check that exit from MID node was logged
        self.assertIsNotNone(ae.endTime)
        self.assertTrue(ae.endTime > ae.startTime)
        self.assertEqual(ae.exitEvent, 'select_Lesson')
        self.assertIsNone(fsmStack.state.activityEvent)

    def do_start(self, f, **kwargs):
        """
        Run tests of basic startup of new FSM instance.
        """
        fsmData = dict(unit=self.unit, foo='bar')
        request = FakeRequest(self.user)
        fsmStack = FSMStack(request)
        self.assertIsNone(fsmStack.state)
        try:
            result = fsmStack.push(request, 'invalid', stateData=fsmData, **kwargs)
        except FSM.DoesNotExist:
            pass
        else:
            raise AssertionError('failed to catch bad FSM query')
        result = fsmStack.push(request, 'test', stateData=fsmData, **kwargs)
        self.assertEqual(request.session['fsmID'], fsmStack.state.pk)
        self.assertEqual(fsmStack.state.load_json_data(), fsmData)
        self.assertEqual(fsmStack.state.fsmNode.name, 'MID')
        self.assertEqual(fsmStack.state.fsmNode.path, 'ct:about')
        self.assertEqual(fsmStack.state.fsmNode.get_path(
            fsmStack.state, request
        ), '/ct/about/')
        self.assertEqual(result, '/ct/about/')
        return fsmStack

    def test_trivial_plugin(self):
        """
        Check trivial plugin import and call.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        request = FakeRequest(self.user)
        fsmStack = FSMStack(request)
        fsmStack.state = FSMState(user=self.user, fsmNode=f.startNode)
        self.assertEqual(f.startNode.event(fsmStack, request, 'start'),
                         '/ct/about/')
        self.assertEqual(f.startNode.get_path(fsmStack.state, request),
                         '/ct/some/where/else/')

    def test_bad_funcName(self):
        """
        Check that FSM.save_graph() catches bad plugin funcName.
        """
        nodeDictBad = dict(
            START=dict(title='start here', path='ct:home', funcName='fsm.fsm_plugin.testme.invalid')
        )
        try:
            FSM.save_graph(self.fsmDict, nodeDictBad, (), 'jacob')
        except AttributeError:
            pass
        else:
            raise AssertionError('FSM.save_graph() failed to catch bad plugin funcName')

    def test_bad_fsmID(self):
        """
        Make sure FSMStack silently handles bad fsmID.
        """
        request = FakeRequest(self.user, dict(fsmID=99))
        fsmStack = FSMStack(request)
        self.assertEqual(request.session, {})
        self.assertIsNone(fsmStack.state)

    def test_randomtrial(self):
        """
        Basic randomized trial.
        """
        self.assertEqual(self.ulQ.order, 0)
        from ct.fsm_plugin.lessonseq import get_specs
        f = get_specs()[0].save_graph(self.user.username)  # load FSM spec
        from ct.fsm_plugin.randomtrial import get_specs
        f = get_specs()[0].save_graph(self.user.username)  # load FSM spec
        self.assertEqual(ActivityLog.objects.count(), 0)
        fsmData = dict(testFSM='lessonseq', treatmentFSM='lessonseq',
                       treatment1=self.ulQ.unit, treatment2=self.ulQ.unit,
                       testUnit=self.ulQ2.unit, course=self.course)
        request, fsmStack, result = self.get_fsm_request(
            'randomtrial', fsmData, dict(trialName='test')
        )
        self.assertEqual(self.client.session['fsmID'], fsmStack.state.pk)
        self.assertEqual(result, '/fsm/nodes/%d/' % f.startNode.pk)
        self.assertEqual(ActivityLog.objects.count(), 1)
        # rt FSM start page
        response = self.client.get(result)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'answer a few preliminary questions')
        url = '/ct/courses/%d/units/%d/lessons/%d/ask/' \
              % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk)
        self.check_post_get(result, dict(fsmtask='next'), url, 'Scary Question')
        # pretest Q
        postdata = dict(text='i dunno', confidence=Response.GUESS)
        url = self.check_post_get(url, postdata, '/assess/', 'write an answer')
        # pretest assess
        assessPOST = dict(selfeval=Response.CORRECT, status=DONE_STATUS, liked='')
        url2 = '/ct/courses/%d/units/%d/lessons/%d/ask/' \
               % (self.course.pk, self.ulQ.unit.pk, self.ulQ.pk)
        self.check_post_get(url, assessPOST, url2, 'your quest')
        # treatment Q
        postdata = dict(text='i like rats', confidence=Response.GUESS)
        url = self.check_post_get(url2, postdata, '/assess/', 'write an answer')
        # treatment assess
        url2 = '/ct/courses/%d/units/%d/lessons/%d/ask/' \
               % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk)
        self.check_post_get(url, assessPOST, url2, 'Scary Question')
        # posttest Q
        postdata = dict(text='i still dunno', confidence=Response.GUESS)
        url = self.check_post_get(url2, postdata, '/assess/', 'write an answer')
        # posttest assess
        url2 = '/ct/courses/%d/units/%d/tasks/' \
               % (self.course.pk, self.ulQ.unit.pk)
        self.check_post_get(url, assessPOST, url2, 'Next Step to work on')
        self.assertEqual(Response.objects.filter(activity__isnull=False,
                                                 confidence=Response.GUESS).
                         count(), 3)  # check responses logged to RT activity

    def test_slideshow(self):
        """
        Basic slide show FSM.
        """
        from ct.fsm_plugin.slideshow import get_specs
        get_specs()[0].save_graph(self.user.username)  # load FSM spec
        fsmData = dict(unit=self.ulQ2.unit, course=self.course)
        request, fsmStack, result = self.get_fsm_request('slideshow', fsmData)
        self.assertEqual(result, '/ct/courses/%d/units/%d/lessons/%d/read/'
              % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk))
        # start page = question
        response = self.client.get(result)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Scary Question')
        # answer page
        answer = self.ulQ2.get_answers()[0]
        url = '/ct/courses/%d/units/%d/lessons/%d/read/' \
              % (self.course.pk, self.ulQ2.unit.pk, answer.pk)
        self.check_post_get(result, dict(fsmtask='next'), url, 'an answer')
        # end of slide show should dump us on concepts page
        url2 = '/ct/courses/%d/units/%d/concepts/' % (self.course.pk, self.ulQ2.unit.pk)
        self.check_post_get(url, dict(fsmtask='next'), url2, 'Pretest')

    def get_fsm_request(self, fsmName, stateData, startArgs=None, **kwargs):
        """
        Create request, fsmStack and start specified FSM.
        """
        startArgs = startArgs or {}
        request = FakeRequest(self.user)
        request.session = self.client.session
        fsmStack = FSMStack(request)
        result = fsmStack.push(request, fsmName, stateData, startArgs, **kwargs)
        request.session.save()
        return request, fsmStack, result
Exemplo n.º 12
0
def lesson_question(user, concept):
    instance = Lesson(
        title='ugh', text='brr', addedBy=user, kind=Lesson.ORCT_QUESTION, concept=concept
    )
    instance.save_root(concept)
    return instance
Exemplo n.º 13
0
class FSMTests(OurTestCase):
    """
    Tests for FSM stack.
    """

    def setUp(self):
        self.user = User.objects.create_user(username="******", email="jacob@_", password="******")
        # have to login or Django self.client.session storage won't work
        self.client.login(username="******", password="******")
        self.course = Course(title="Great Course", description="the bestest", addedBy=self.user)
        self.course.save()
        self.unit = Unit(title="My Courselet", addedBy=self.user)
        self.unit.save()
        self.lesson = Lesson(title="Big Deal", text="very interesting info", addedBy=self.user)
        self.lesson.save_root()
        self.unitLesson = UnitLesson.create_from_lesson(self.lesson, self.unit, order="APPEND")
        self.ulQ = create_question_unit(self.user)
        self.ulQ2 = create_question_unit(self.user, "Pretest", "Scary Question", "Tell me something.")
        self.json_mixin = JSONBlobMixin()
        self.fsmDict = dict(name="test", title="try this")
        self.nodeDict = dict(
            START=dict(title="start here", path="ct:home", funcName="fsm.fsm_plugin.testme.START"),
            MID=dict(title="in the middle", path="ct:about", doLogging=True),
            END=dict(title="end here", path="ct:home"),
        )
        self.edgeDict = (
            dict(name="next", fromNode="START", toNode="END", title="go go go"),
            dict(name="select_Lesson", fromNode="MID", toNode="MID", title="go go go"),
        )

    def test_load(self):
        """
        Check loading an FSM graph, and replacing it.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, "jacob")
        self.assertEqual(f.fsmnode_set.count(), 3)
        self.assertEqual(f.startNode.name, "START")
        self.assertEqual(f.startNode.outgoing.count(), 1)
        e = f.startNode.outgoing.all()[0]
        self.assertEqual(e.name, "next")
        self.assertEqual(e.toNode.name, "END")
        f2 = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, "jacob")  # replace
        self.assertEqual(FSM.objects.get(pk=f.pk).name, "testOLD")  # renamed
        self.assertNotEqual(f.startNode, f2.startNode)
        self.assertEqual(f.startNode.name, f2.startNode.name)

    def test_json_blob(self):
        """
        Check roundtrip dump/load via json blob data.
        """
        name, pk = self.json_mixin.dump_json_id(self.unit)
        label, obj = self.json_mixin.load_json_id(name, pk)
        self.assertEqual(self.unit, obj)
        self.assertEqual(label, "Unit")

    def test_json_blob2(self):
        """
        Check roundtrip dump/load via named json blob data.
        """
        name, pk = self.json_mixin.dump_json_id(self.unit, "fruity")
        self.assertEqual(name, "fruity_Unit_id")
        label, obj = self.json_mixin.load_json_id(name, pk)
        self.assertEqual(self.unit, obj)
        self.assertEqual(label, "fruity")

    def test_json_blob3(self):
        """
        Check roundtrip dump/load via json blob string.
        """
        s = self.json_mixin.dump_json_id_dict(dict(fruity=self.unit))
        d = self.json_mixin.load_json_id_dict(s)
        self.assertEqual(d.items(), [("fruity", self.unit)])

    def test_json_blob4(self):
        """
        Check roundtrip dump/load via db storage.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, "jacob")
        d = f.startNode.load_json_data()
        self.assertEqual(d, {})
        d["fruity"] = self.unit
        d["anumber"] = 3
        d["astring"] = "jeff"
        f.startNode.save_json_data(d)
        node = FSMNode.objects.get(pk=f.startNode.pk)
        d2 = node.load_json_data()
        self.assertEqual(d2, {"fruity": self.unit, "anumber": 3, "astring": "jeff"})

    def test_start(self):
        """
        Check basic startup of new FSM instance.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, "jacob")
        self.do_start(f)

    def test_start2(self):
        """
        Check basic startup of new FSM instance using FSMSpecification.
        """
        from fsm.fsm_plugin.testme import get_specs

        spec = get_specs()[0]
        f = spec.save_graph("jacob")
        self.assertTrue(f.startNode.doLogging)
        self.assertFalse(f.get_node("MID").doLogging)
        fsmStack = self.do_start(f)
        # test filter_input() plugin functionality
        edge = fsmStack.state.fsmNode.outgoing.get(name="next")
        self.assertTrue(edge.filter_input("the right stuff"))
        self.assertFalse(edge.filter_input("the WRONG stuff"))
        # test get_help() plugin functionality
        request = FakeRequest(self.user, path="/ct/about/")
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, "here here!")
        request = FakeRequest(self.user, path="/ct/courses/1/")
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, "there there")
        request = FakeRequest(self.user)
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, None)

    def test_start3(self):
        """
        Check that FSMState saves unitLesson, select_ data, and logging.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, "jacob")
        fsmStack = self.do_start(f, unitLesson=self.unitLesson)
        self.assertEqual(fsmStack.state.unitLesson, self.unitLesson)
        request = FakeRequest(self.user, method="GET")
        # check logging on the MID node
        fsmStack.event(request, None)  # send a render event to log
        self.assertEqual(fsmStack.state.activity.fsmName, "test")
        ae = fsmStack.state.activityEvent
        self.assertEqual(ae.nodeName, "MID")
        self.assertEqual(ae.unitLesson, self.unitLesson)
        self.assertIsNotNone(ae.startTime)
        self.assertIsNone(ae.endTime)
        request = FakeRequest(self.user)
        # now try a select_ event
        fsmStack.event(request, "select_Lesson", lesson=self.lesson)
        self.assertEqual(fsmStack.state.get_data_attr("lesson"), self.lesson)
        # check that exit from MID node was logged
        self.assertIsNotNone(ae.endTime)
        self.assertTrue(ae.endTime > ae.startTime)
        self.assertEqual(ae.exitEvent, "select_Lesson")
        self.assertIsNone(fsmStack.state.activityEvent)

    def do_start(self, f, **kwargs):
        """
        Run tests of basic startup of new FSM instance.
        """
        fsmData = dict(unit=self.unit, foo="bar")
        request = FakeRequest(self.user)
        fsmStack = FSMStack(request)
        self.assertIsNone(fsmStack.state)
        try:
            result = fsmStack.push(request, "invalid", stateData=fsmData, **kwargs)
        except FSM.DoesNotExist:
            pass
        else:
            raise AssertionError("failed to catch bad FSM query")
        result = fsmStack.push(request, "test", stateData=fsmData, **kwargs)
        self.assertEqual(request.session["fsmID"], fsmStack.state.pk)
        self.assertEqual(fsmStack.state.load_json_data(), fsmData)
        self.assertEqual(fsmStack.state.fsmNode.name, "MID")
        self.assertEqual(fsmStack.state.fsmNode.path, "ct:about")
        self.assertEqual(fsmStack.state.fsmNode.get_path(fsmStack.state, request), "/ct/about/")
        self.assertEqual(result, "/ct/about/")
        return fsmStack

    def test_trivial_plugin(self):
        """
        Check trivial plugin import and call.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, "jacob")
        request = FakeRequest(self.user)
        fsmStack = FSMStack(request)
        fsmStack.state = FSMState(user=self.user, fsmNode=f.startNode)
        self.assertEqual(f.startNode.event(fsmStack, request, "start"), "/ct/about/")
        self.assertEqual(f.startNode.get_path(fsmStack.state, request), "/ct/some/where/else/")

    def test_bad_funcName(self):
        """
        Check that FSM.save_graph() catches bad plugin funcName.
        """
        nodeDictBad = dict(START=dict(title="start here", path="ct:home", funcName="fsm.fsm_plugin.testme.invalid"))
        try:
            FSM.save_graph(self.fsmDict, nodeDictBad, (), "jacob")
        except AttributeError:
            pass
        else:
            raise AssertionError("FSM.save_graph() failed to catch bad plugin funcName")

    def test_bad_fsmID(self):
        """
        Make sure FSMStack silently handles bad fsmID.
        """
        request = FakeRequest(self.user, dict(fsmID=99))
        fsmStack = FSMStack(request)
        self.assertEqual(request.session, {})
        self.assertIsNone(fsmStack.state)

    def test_randomtrial(self):
        """
        Basic randomized trial.
        """
        self.assertEqual(self.ulQ.order, 0)
        from ct.fsm_plugin.lessonseq import get_specs

        f = get_specs()[0].save_graph(self.user.username)  # load FSM spec
        from ct.fsm_plugin.randomtrial import get_specs

        f = get_specs()[0].save_graph(self.user.username)  # load FSM spec
        self.assertEqual(ActivityLog.objects.count(), 0)
        fsmData = dict(
            testFSM="lessonseq",
            treatmentFSM="lessonseq",
            treatment1=self.ulQ.unit,
            treatment2=self.ulQ.unit,
            testUnit=self.ulQ2.unit,
            course=self.course,
        )
        request, fsmStack, result = self.get_fsm_request("randomtrial", fsmData, dict(trialName="test"))
        self.assertEqual(self.client.session["fsmID"], fsmStack.state.pk)
        self.assertEqual(result, "/fsm/nodes/%d/" % f.startNode.pk)
        self.assertEqual(ActivityLog.objects.count(), 1)
        # rt FSM start page
        response = self.client.get(result)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "answer a few preliminary questions")
        url = "/ct/courses/%d/units/%d/lessons/%d/ask/" % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk)
        self.check_post_get(result, dict(fsmtask="next"), url, "Scary Question")
        # pretest Q
        postdata = dict(text="i dunno", confidence=Response.GUESS)
        url = self.check_post_get(url, postdata, "/assess/", "write an answer")
        # pretest assess
        assessPOST = dict(selfeval=Response.CORRECT, status=DONE_STATUS, liked="")
        url2 = "/ct/courses/%d/units/%d/lessons/%d/ask/" % (self.course.pk, self.ulQ.unit.pk, self.ulQ.pk)
        self.check_post_get(url, assessPOST, url2, "your quest")
        # treatment Q
        postdata = dict(text="i like rats", confidence=Response.GUESS)
        url = self.check_post_get(url2, postdata, "/assess/", "write an answer")
        # treatment assess
        url2 = "/ct/courses/%d/units/%d/lessons/%d/ask/" % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk)
        self.check_post_get(url, assessPOST, url2, "Scary Question")
        # posttest Q
        postdata = dict(text="i still dunno", confidence=Response.GUESS)
        url = self.check_post_get(url2, postdata, "/assess/", "write an answer")
        # posttest assess
        url2 = "/ct/courses/%d/units/%d/tasks/" % (self.course.pk, self.ulQ.unit.pk)
        self.check_post_get(url, assessPOST, url2, "Next Step to work on")
        self.assertEqual(
            Response.objects.filter(activity__isnull=False, confidence=Response.GUESS).count(), 3
        )  # check responses logged to RT activity

    def test_slideshow(self):
        """
        Basic slide show FSM.
        """
        from ct.fsm_plugin.slideshow import get_specs

        get_specs()[0].save_graph(self.user.username)  # load FSM spec
        fsmData = dict(unit=self.ulQ2.unit, course=self.course)
        request, fsmStack, result = self.get_fsm_request("slideshow", fsmData)
        self.assertEqual(
            result, "/ct/courses/%d/units/%d/lessons/%d/read/" % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk)
        )
        # start page = question
        response = self.client.get(result)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Scary Question")
        # answer page
        answer = self.ulQ2.get_answers()[0]
        url = "/ct/courses/%d/units/%d/lessons/%d/read/" % (self.course.pk, self.ulQ2.unit.pk, answer.pk)
        self.check_post_get(result, dict(fsmtask="next"), url, "an answer")
        # end of slide show should dump us on concepts page
        url2 = "/ct/courses/%d/units/%d/concepts/" % (self.course.pk, self.ulQ2.unit.pk)
        self.check_post_get(url, dict(fsmtask="next"), url2, "Pretest")

    def get_fsm_request(self, fsmName, stateData, startArgs=None, **kwargs):
        """
        Create request, fsmStack and start specified FSM.
        """
        startArgs = startArgs or {}
        request = FakeRequest(self.user)
        request.session = self.client.session
        fsmStack = FSMStack(request)
        result = fsmStack.push(request, fsmName, stateData, startArgs, **kwargs)
        request.session.save()
        return request, fsmStack, result
Exemplo n.º 14
0
class FSMTests(OurTestCase):
    """
    Tests for FSM stack.
    """
    def setUp(self):
        self.user = User.objects.create_user(
            username='******', email='jacob@_', password='******'
        )
        # have to login or Django self.client.session storage won't work
        self.client.login(username='******', password='******')
        self.course = Course(
            title='Great Course', description='the bestest', addedBy=self.user
        )
        self.course.save()
        self.unit = Unit(title='My Courselet', addedBy=self.user)
        self.unit.save()
        self.lesson = Lesson(
            title='Big Deal', text='very interesting info', addedBy=self.user
        )
        self.lesson.save_root()
        self.unitLesson = UnitLesson.create_from_lesson(
            self.lesson, self.unit, order='APPEND'
        )
        self.ulQ = create_question_unit(self.user)
        self.ulQ2 = create_question_unit(
            self.user, 'Pretest', 'Scary Question', 'Tell me something.'
        )
        self.json_mixin = JSONBlobMixin()
        self.fsmDict = dict(name='test', title='try this')
        self.nodeDict = dict(
            START=dict(title='start here', path='ct:home', funcName='fsm.fsm_plugin.testme.START'),
            MID=dict(title='in the middle', path='ct:about', doLogging=True),
            END=dict(title='end here', path='ct:home')
        )
        self.edgeDict = (
            dict(name='next', fromNode='START', toNode='END', title='go go go'),
            dict(name='select_Lesson', fromNode='MID', toNode='MID', title='go go go'),
        )

    def test_load(self):
        """
        Check loading an FSM graph, and replacing it.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        self.assertEqual(f.fsmnode_set.count(), 3)
        self.assertEqual(f.startNode.name, 'START')
        self.assertEqual(f.startNode.outgoing.count(), 1)
        e = f.startNode.outgoing.all()[0]
        self.assertEqual(e.name, 'next')
        self.assertEqual(e.toNode.name, 'END')
        f2 = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')  # replace
        self.assertEqual(FSM.objects.get(pk=f.pk).name, 'testOLD')  # renamed
        self.assertNotEqual(f.startNode, f2.startNode)
        self.assertEqual(f.startNode.name, f2.startNode.name)

    def test_json_blob(self):
        """
        Check roundtrip dump/load via json blob data.
        """
        name, pk = self.json_mixin.dump_json_id(self.unit)
        label, obj = self.json_mixin.load_json_id(name, pk)
        self.assertEqual(self.unit, obj)
        self.assertEqual(label, 'Unit')

    def test_json_blob2(self):
        """
        Check roundtrip dump/load via named json blob data.
        """
        name, pk = self.json_mixin.dump_json_id(self.unit, 'fruity')
        self.assertEqual(name, 'fruity_Unit_id')
        label, obj = self.json_mixin.load_json_id(name, pk)
        self.assertEqual(self.unit, obj)
        self.assertEqual(label, 'fruity')

    def test_json_blob3(self):
        """
        Check roundtrip dump/load via json blob string.
        """
        s = self.json_mixin.dump_json_id_dict(dict(fruity=self.unit))
        d = self.json_mixin.load_json_id_dict(s)
        self.assertEqual(list(d.items()), [('fruity', self.unit)])

    def test_json_blob4(self):
        """
        Check roundtrip dump/load via db storage.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        d = f.startNode.load_json_data()
        self.assertEqual(d, {})
        d['fruity'] = self.unit
        d['anumber'] = 3
        d['astring'] = 'jeff'
        f.startNode.save_json_data(d)
        node = FSMNode.objects.get(pk=f.startNode.pk)
        d2 = node.load_json_data()
        self.assertEqual(d2, {'fruity': self.unit, 'anumber': 3,
                              'astring': 'jeff'})

    def test_start(self):
        """
        Check basic startup of new FSM instance.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        self.do_start(f)

    def test_start2(self):
        """
        Check basic startup of new FSM instance using FSMSpecification.
        """
        from fsm.fsm_plugin.testme import get_specs
        spec = get_specs()[0]
        f = spec.save_graph('jacob')
        self.assertTrue(f.startNode.doLogging)
        self.assertFalse(f.get_node('MID').doLogging)
        fsmStack = self.do_start(f)
        # test filter_input() plugin functionality
        edge = fsmStack.state.fsmNode.outgoing.get(name='next')
        self.assertTrue(edge.filter_input('the right stuff'))
        self.assertFalse(edge.filter_input('the WRONG stuff'))
        # test get_help() plugin functionality
        request = FakeRequest(self.user, path='/ct/about/')
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, 'here here!')
        request = FakeRequest(self.user, path='/ct/courses/1/')
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, 'there there')
        request = FakeRequest(self.user)
        msg = fsmStack.state.fsmNode.get_help(fsmStack.state, request)
        self.assertEqual(msg, None)

    def test_start3(self):
        """
        Check that FSMState saves unitLesson, select_ data, and logging.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        fsmStack = self.do_start(f, unitLesson=self.unitLesson)
        self.assertEqual(fsmStack.state.unitLesson, self.unitLesson)
        request = FakeRequest(self.user, method='GET')
        # check logging on the MID node
        fsmStack.event(request, None)  # send a render event to log
        self.assertEqual(fsmStack.state.activity.fsmName, 'test')
        ae = fsmStack.state.activityEvent
        self.assertEqual(ae.nodeName, 'MID')
        self.assertEqual(ae.unitLesson, self.unitLesson)
        self.assertIsNotNone(ae.startTime)
        self.assertIsNone(ae.endTime)
        request = FakeRequest(self.user)
        # now try a select_ event
        fsmStack.event(request, 'select_Lesson', lesson=self.lesson)
        self.assertEqual(fsmStack.state.get_data_attr('lesson'), self.lesson)
        # check that exit from MID node was logged
        self.assertIsNotNone(ae.endTime)
        self.assertTrue(ae.endTime > ae.startTime)
        self.assertEqual(ae.exitEvent, 'select_Lesson')
        self.assertIsNone(fsmStack.state.activityEvent)

    def do_start(self, f, **kwargs):
        """
        Run tests of basic startup of new FSM instance.
        """
        fsmData = dict(unit=self.unit, foo='bar')
        request = FakeRequest(self.user)
        fsmStack = FSMStack(request)
        self.assertIsNone(fsmStack.state)
        try:
            result = fsmStack.push(request, 'invalid', stateData=fsmData, **kwargs)
        except FSM.DoesNotExist:
            pass
        else:
            raise AssertionError('failed to catch bad FSM query')
        result = fsmStack.push(request, 'test', stateData=fsmData, **kwargs)
        self.assertEqual(request.session['fsmID'], fsmStack.state.pk)
        self.assertEqual(fsmStack.state.load_json_data(), fsmData)
        self.assertEqual(fsmStack.state.fsmNode.name, 'MID')
        self.assertEqual(fsmStack.state.fsmNode.path, 'ct:about')
        self.assertEqual(fsmStack.state.fsmNode.get_path(
            fsmStack.state, request
        ), '/ct/about/')
        self.assertEqual(result, '/ct/about/')
        return fsmStack

    def test_trivial_plugin(self):
        """
        Check trivial plugin import and call.
        """
        f = FSM.save_graph(self.fsmDict, self.nodeDict, self.edgeDict, 'jacob')
        request = FakeRequest(self.user)
        fsmStack = FSMStack(request)
        fsmStack.state = FSMState(user=self.user, fsmNode=f.startNode)
        self.assertEqual(f.startNode.event(fsmStack, request, 'start'),
                         '/ct/about/')
        self.assertEqual(f.startNode.get_path(fsmStack.state, request),
                         '/ct/some/where/else/')

    def test_bad_funcName(self):
        """
        Check that FSM.save_graph() catches bad plugin funcName.
        """
        nodeDictBad = dict(
            START=dict(title='start here', path='ct:home', funcName='fsm.fsm_plugin.testme.invalid')
        )
        try:
            FSM.save_graph(self.fsmDict, nodeDictBad, (), 'jacob')
        except AttributeError:
            pass
        else:
            raise AssertionError('FSM.save_graph() failed to catch bad plugin funcName')

    def test_bad_fsmID(self):
        """
        Make sure FSMStack silently handles bad fsmID.
        """
        request = FakeRequest(self.user, dict(fsmID=99))
        fsmStack = FSMStack(request)
        self.assertEqual(request.session, {})
        self.assertIsNone(fsmStack.state)

    def test_randomtrial(self):
        """
        Basic randomized trial.
        """
        self.assertEqual(self.ulQ.order, 0)
        from ct.fsm_plugin.lessonseq import get_specs
        f = get_specs()[0].save_graph(self.user.username)  # load FSM spec
        from ct.fsm_plugin.randomtrial import get_specs
        f = get_specs()[0].save_graph(self.user.username)  # load FSM spec
        self.assertEqual(ActivityLog.objects.count(), 0)
        fsmData = dict(testFSM='lessonseq', treatmentFSM='lessonseq',
                       treatment1=self.ulQ.unit, treatment2=self.ulQ.unit,
                       testUnit=self.ulQ2.unit, course=self.course)
        request, fsmStack, result = self.get_fsm_request(
            'randomtrial', fsmData, dict(trialName='test')
        )
        self.assertEqual(self.client.session['fsmID'], fsmStack.state.pk)
        self.assertEqual(result, '/fsm/nodes/%d/' % f.startNode.pk)
        self.assertEqual(ActivityLog.objects.count(), 1)
        # rt FSM start page
        response = self.client.get(result)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'answer a few preliminary questions')
        url = '/ct/courses/%d/units/%d/lessons/%d/ask/' \
              % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk)
        self.check_post_get(result, dict(fsmtask='next'), url, 'Scary Question')
        # pretest Q
        postdata = dict(text='i dunno', confidence=Response.GUESS)
        url = self.check_post_get(url, postdata, '/assess/', 'write an answer')
        # pretest assess
        assessPOST = dict(selfeval=Response.CORRECT, status=DONE_STATUS, liked='')
        url2 = '/ct/courses/%d/units/%d/lessons/%d/ask/' \
               % (self.course.pk, self.ulQ.unit.pk, self.ulQ.pk)
        self.check_post_get(url, assessPOST, url2, 'your quest')
        # treatment Q
        postdata = dict(text='i like rats', confidence=Response.GUESS)
        url = self.check_post_get(url2, postdata, '/assess/', 'write an answer')
        # treatment assess
        url2 = '/ct/courses/%d/units/%d/lessons/%d/ask/' \
               % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk)
        self.check_post_get(url, assessPOST, url2, 'Scary Question')
        # posttest Q
        postdata = dict(text='i still dunno', confidence=Response.GUESS)
        url = self.check_post_get(url2, postdata, '/assess/', 'write an answer')
        # posttest assess
        url2 = '/ct/courses/%d/units/%d/tasks/' \
               % (self.course.pk, self.ulQ.unit.pk)
        self.check_post_get(url, assessPOST, url2, 'Next Step to work on')
        self.assertEqual(Response.objects.filter(activity__isnull=False,
                                                 confidence=Response.GUESS).
                         count(), 3)  # check responses logged to RT activity

    def test_slideshow(self):
        """
        Basic slide show FSM.
        """
        from ct.fsm_plugin.slideshow import get_specs
        get_specs()[0].save_graph(self.user.username)  # load FSM spec
        fsmData = dict(unit=self.ulQ2.unit, course=self.course)
        request, fsmStack, result = self.get_fsm_request('slideshow', fsmData)
        self.assertEqual(result, '/ct/courses/%d/units/%d/lessons/%d/read/'
              % (self.course.pk, self.ulQ2.unit.pk, self.ulQ2.pk))
        # start page = question
        response = self.client.get(result)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Scary Question')
        # answer page
        answer = self.ulQ2.get_answers()[0]
        url = '/ct/courses/%d/units/%d/lessons/%d/read/' \
              % (self.course.pk, self.ulQ2.unit.pk, answer.pk)
        self.check_post_get(result, dict(fsmtask='next'), url, 'an answer')
        # end of slide show should dump us on concepts page
        url2 = '/ct/courses/%d/units/%d/concepts/' % (self.course.pk, self.ulQ2.unit.pk)
        self.check_post_get(url, dict(fsmtask='next'), url2, 'Pretest')

    def get_fsm_request(self, fsmName, stateData, startArgs=None, **kwargs):
        """
        Create request, fsmStack and start specified FSM.
        """
        startArgs = startArgs or {}
        request = FakeRequest(self.user)
        request.session = self.client.session
        fsmStack = FSMStack(request)
        result = fsmStack.push(request, fsmName, stateData, startArgs, **kwargs)
        request.session.save()
        return request, fsmStack, result
Exemplo n.º 15
0
class TagsTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username="******", password="******")
        self.course = Course(title="test_title", addedBy=self.user)
        self.course.save()
        self.concept = Concept(title="test title", addedBy=self.user)
        self.concept.save()
        self.lesson = Lesson(title="ugh", text="brr", addedBy=self.user, kind=Lesson.ORCT_QUESTION)
        self.lesson.save_root()
        self.lesson.add_concept_link(self.concept, ConceptLink.TESTS, self.user)
        self.unit = Unit(title="test unit title", addedBy=self.user)
        self.unit.save()
        self.unit_lesson = UnitLesson(unit=self.unit, addedBy=self.user, treeID=42, lesson=self.lesson)
        self.unit_lesson.save()
        self.response = Response(
            course=self.course,
            lesson=self.lesson,
            author=self.user,
            unitLesson=self.unit_lesson,
            confidence=Response.GUESS,
            title="test title",
            text="test text",
        )
        self.response.save()
        self.context = {
            "actionTarget": "/ct/courses/1/units/1/",
            "ul": self.unit_lesson,
            "test_text": "This is a test text",
            "r": self.response,
        }

    def render_template(self, string, context=None):
        context = context or {}
        context = Context(context)
        return Template(string).render(context)

    @unpack
    @data(
        ("{{ test_text | md2html }}", "<p>This is a test text</p>\n"),
        ("{{ actionTarget | get_object_url:ul }}", "/ct/courses/1/units/1/lessons/1/"),
        ("{{ actionTarget | get_home_url:ul }}", "/ct/courses/1/units/1/lessons/1/"),
        ("{{ actionTarget | get_thread_url:r }}", "/ct/courses/1/units/1/lessons/1/faq/1/"),
        ("{{ actionTarget | get_tasks_url:ul }}", "/ct/courses/1/units/1/lessons/1/tasks/"),
        ("{{ actionTarget | get_dummy_navbar }}", '<li><a href="/ct/courses/1/">Course</a></li>'),
    )
    def test_all_filters(self, template_variable, expected_result):
        rendered = self.render_template("{% load ct_extras %}" + template_variable, context=self.context)
        self.assertEqual(rendered, expected_result)

    @patch("ct.templatetags.ct_extras.pypandoc")
    def test_md2html_pandoc_exception(self, pypandoc):
        pypandoc.convert.side_effect = StandardError
        rendered = self.render_template("{% load ct_extras %}" "{{ test_text | md2html }}", context=self.context)
        self.assertEqual(rendered, self.context["test_text"])

    @data("get_base_url", "get_path_type")
    def test_get_base_url_exception(self, helper):
        with self.assertRaises(ValueError):
            getattr(inspect.getmodule(self), helper).__call__(
                "/ct/courses/1/units/1/lessons/1/", baseToken="non_existent_token"
            )

    def test_get_object_url_exception(self):
        self.context["ul"] = self.unit  # Unit object does not have get_url method
        rendered = self.render_template(
            "{% load ct_extras %}" "{{ actionTarget | get_object_url:ul }}", context=self.context
        )
        self.assertEqual(rendered, "/ct/courses/1/units/1/unit/1/teach/")

    @patch("ct.templatetags.ct_extras.timezone")
    def test_display_datetime(self, timezone_patched):
        saved_time = timezone.now()
        timezone_patched.now.return_value = saved_time
        context = {"dt": saved_time - timedelta(1)}
        rendered = self.render_template("{% load ct_extras %}" "{{ dt|display_datetime }}", context=context)
        self.assertEqual(rendered, "1 day ago")

    def test_find_audio(self):
        result = find_audio("test tag head .. audio:: test tag tail \n", 4)
        self.assertEqual(result, (14, 39, "test tag tail"))

    def test_audio_html(self):
        """
        Function should return string for embeding audio into html.
        """
        result = audio_html("audio.mp3")
        self.assertEqual(
            result,
            """<audio controls><source src="audio.ogg" type="audio/ogg"><source src="audio.mp3" """
            """type="audio/mpeg">no support for audio!</audio>""",
        )

    def test_video_html(self):
        """
        Function should return string for embeding youtube or vimeo link.
        """
        result = video_html("youtube:test_video_path")
        self.assertIn("test_video_path", result)
        self.assertIn('src="https://www.youtube.com/embed/', result)

        result = video_html("vimeo:test_video_path")
        self.assertIn("test_video_path", result)
        self.assertIn('src="https://player.vimeo.com/video/', result)

    def test_video_html_with_exception(self):
        """
        Test exception handling.
        """
        result = video_html("youtube")
        self.assertEqual(result, "ERROR: bad video source: youtube")

        result = video_html("some_new_cdn:test_video_path")
        self.assertEqual(result, "ERROR: unknown video sourceDB: some_new_cdn")

    def test_add_replace_temporary_markers(self):
        """
        Test add_temporary_markers and replace_temporary_markers in tandem.
        """
        result = add_temporary_markers("test tag head .. audio:: test tag tail \n", find_audio)
        self.assertEqual(result, ("test tag head mArKeR:0:\n", [("mArKeR:0:", "test tag tail")]))

        result = replace_temporary_markers(result[0], audio_html, result[1])
        self.assertEqual(
            result,
            """test tag head <audio controls><source src="test tag tail.ogg" type="audio/ogg">"""
            """<source src="test tag tail.mp3" type="audio/mpeg">no support for audio!</audio>\n""",
        )