def test_assessments_with_tracks_not_settable_as_pre_post(self):
        self.assessment_one.labels = str(self.track_one_id)
        self.assessment_two.labels = str(self.track_one_id)
        self.course.save()
        unit_rest_handler = unit_lesson_editor.UnitRESTHandler()
        unit_rest_handler.app_context = self.course.app_context

        with common_utils.Namespace(NAMESPACE):
            errors = []
            unit_rest_handler.apply_updates(
                self.unit_no_lessons, {
                    'title': self.unit_no_lessons.title,
                    'now_available': self.unit_no_lessons.now_available,
                    'label_groups': [],
                    'pre_assessment': self.assessment_one.unit_id,
                    'post_assessment': self.assessment_two.unit_id,
                    'show_contents_on_one_page': False,
                    'manual_progress': False,
                    'description': None,
                    'unit_header': None,
                    'unit_footer': None,
                }, errors)
            self.assertEquals([
                'Assessment "Assessment One" has track labels, so it '
                'cannot be used as a pre/post unit element',
                'Assessment "Assessment Two" has track labels, so it '
                'cannot be used as a pre/post unit element'
            ], errors)
    def test_old_assessment_assignment(self):
        new_course_context = actions.simple_add_course('new_course',
                                                       ADMIN_EMAIL,
                                                       'My New Course')
        new_course = courses.Course(None, new_course_context)
        new_course.import_from(
            sites.get_all_courses(rules_text='course:/:/')[0])
        new_course.save()

        unit_rest_handler = unit_lesson_editor.UnitRESTHandler()
        unit_rest_handler.app_context = new_course_context

        # Use REST handler function to save pre/post handlers on one unit.
        errors = []
        unit = new_course.get_units_of_type(verify.UNIT_TYPE_UNIT)[0]
        assessment = new_course.get_units_of_type(
            verify.UNIT_TYPE_ASSESSMENT)[0]
        unit_rest_handler.apply_updates(
            unit, {
                'title': unit.title,
                'now_available': unit.now_available,
                'label_groups': [],
                'pre_assessment': assessment.unit_id,
                'post_assessment': -1,
                'show_contents_on_one_page': False,
                'manual_progress': False,
                'description': None,
                'unit_header': None,
                'unit_footer': None,
            }, errors)
        assert not errors
    def test_old_assessment_availability(self):
        new_course_context = actions.simple_add_course('new_course',
                                                       ADMIN_EMAIL,
                                                       'My New Course')
        new_course = courses.Course(None, new_course_context)
        new_course.import_from(
            sites.get_all_courses(rules_text='course:/:/')[0])
        new_course.save()

        # Prove that there are at least some assessments in this course.
        assessments = new_course.get_units_of_type(verify.UNIT_TYPE_ASSESSMENT)
        self.assertIsNotNone(assessments[0])

        # Get the first Unit
        unit = new_course.get_units_of_type(verify.UNIT_TYPE_UNIT)[0]

        unit_rest_handler = unit_lesson_editor.UnitRESTHandler()
        schema = unit_rest_handler.get_annotations_dict(
            new_course, unit.unit_id)

        # Verify that there are 4 valid choices for pre- or post-asssments
        # for this unit
        choices = self._get_selection_choices(
            schema, ['properties', 'pre_assessment', '_inputex'])
        self.assertEquals(5, len(choices))
        self.assertEquals(-1, choices['-- None --'])

        choices = self._get_selection_choices(
            schema, ['properties', 'post_assessment', '_inputex'])
        self.assertEquals(5, len(choices))
        self.assertEquals(-1, choices['-- None --'])
    def test_new_assessment_availability(self):
        unit_rest_handler = unit_lesson_editor.UnitRESTHandler()

        schema = unit_rest_handler.get_annotations_dict(
            self.course, self.unit_no_lessons.unit_id)
        choices = self._get_selection_choices(
            schema, ['properties', 'pre_assessment', '_inputex'])
        self.assertEquals(
            {
                '-- None --': -1,
                self.assessment_one.title: self.assessment_one.unit_id,
                self.assessment_two.title: self.assessment_two.unit_id
            }, choices)

        choices = self._get_selection_choices(
            schema, ['properties', 'post_assessment', '_inputex'])
        self.assertEquals(
            {
                '-- None --': -1,
                self.assessment_one.title: self.assessment_one.unit_id,
                self.assessment_two.title: self.assessment_two.unit_id
            }, choices)
    def test_rest_unit_assignment(self):
        unit_rest_handler = unit_lesson_editor.UnitRESTHandler()
        unit_rest_handler.app_context = self.course.app_context
        # Use REST handler function to save pre/post handlers on one unit.
        errors = []
        unit_rest_handler.apply_updates(
            self.unit_no_lessons, {
                'title': self.unit_no_lessons.title,
                'now_available': self.unit_no_lessons.now_available,
                'label_groups': [],
                'pre_assessment': self.assessment_one.unit_id,
                'post_assessment': self.assessment_two.unit_id,
                'show_contents_on_one_page': False,
                'manual_progress': False,
                'description': None,
                'unit_header': None,
                'unit_footer': None,
            }, errors)
        self.assertEquals([], errors)
        self.assertEquals(self.unit_no_lessons.pre_assessment,
                          self.assessment_one.unit_id)
        self.assertEquals(self.unit_no_lessons.post_assessment,
                          self.assessment_two.unit_id)
        self.course.save()

        # Verify that the assessments are no longer available for choosing
        # on the other unit.
        schema = unit_rest_handler.get_annotations_dict(
            self.course, self.unit_one_lesson.unit_id)
        choices = self._get_selection_choices(
            schema, ['properties', 'pre_assessment', '_inputex'])
        self.assertEquals({'-- None --': -1}, choices)

        # Verify that they are available for choosing on the unit where
        # they are assigned.
        schema = unit_rest_handler.get_annotations_dict(
            self.course, self.unit_no_lessons.unit_id)
        choices = self._get_selection_choices(
            schema, ['properties', 'pre_assessment', '_inputex'])
        self.assertEquals(
            {
                '-- None --': -1,
                self.assessment_one.title: self.assessment_one.unit_id,
                self.assessment_two.title: self.assessment_two.unit_id
            }, choices)

        # Verify that attempting to set pre/post assessments that
        # are already in use fails.
        errors = []
        unit_rest_handler.apply_updates(
            self.unit_one_lesson, {
                'title': self.unit_one_lesson.title,
                'now_available': self.unit_one_lesson.now_available,
                'label_groups': [],
                'pre_assessment': self.assessment_one.unit_id,
                'post_assessment': self.assessment_two.unit_id,
                'show_contents_on_one_page': False,
                'manual_progress': False,
                'description': None,
                'unit_header': None,
                'unit_footer': None,
            }, errors)
        self.assertEquals([
            'Assessment "Assessment One" is already '
            'asssociated to unit "No Lessons"',
            'Assessment "Assessment Two" is already '
            'asssociated to unit "No Lessons"'
        ], errors)
        self.assertEquals(self.unit_one_lesson.pre_assessment, None)
        self.assertEquals(self.unit_one_lesson.post_assessment, None)
        self.course.save()

        # Verify that swapping the order of pre/post assessments on the
        # unit that already has them is fine.
        errors = []
        unit_rest_handler.apply_updates(
            self.unit_no_lessons, {
                'title': self.unit_no_lessons.title,
                'now_available': self.unit_no_lessons.now_available,
                'label_groups': [],
                'pre_assessment': self.assessment_two.unit_id,
                'post_assessment': self.assessment_one.unit_id,
                'show_contents_on_one_page': False,
                'manual_progress': False,
                'description': None,
                'unit_header': None,
                'unit_footer': None,
            }, errors)
        self.assertEquals([], errors)
        self.assertEquals(self.unit_no_lessons.pre_assessment,
                          self.assessment_two.unit_id)
        self.assertEquals(self.unit_no_lessons.post_assessment,
                          self.assessment_one.unit_id)
        self.course.save()

        # Verify that using the same assessment as both pre and post fails.
        errors = []
        unit_rest_handler.apply_updates(
            self.unit_no_lessons, {
                'title': self.unit_no_lessons.title,
                'now_available': self.unit_no_lessons.now_available,
                'label_groups': [],
                'pre_assessment': self.assessment_one.unit_id,
                'post_assessment': self.assessment_one.unit_id,
                'show_contents_on_one_page': False,
                'manual_progress': False,
                'description': None,
                'unit_header': None,
                'unit_footer': None,
            }, errors)
        self.assertEquals([
            'The same assessment cannot be used as both the pre '
            'and post assessment of a unit.'
        ], errors)
        self.assertEquals(self.unit_no_lessons.pre_assessment,
                          self.assessment_one.unit_id)
        self.assertEquals(self.unit_no_lessons.post_assessment, None)
        self.course.save()