def set_assessment_content(self, unit, assessment_content, errors=None): """Updates the content of an assessment.""" if errors is None: errors = [] path = self._app_context.fs.impl.physical_to_logical( self.get_assessment_filename(unit.unit_id)) root_name = 'assessment' try: content, noverify_text = verify.convert_javascript_to_python( assessment_content, root_name) assessment = verify.evaluate_python_expression_from_text( content, root_name, verify.Assessment().scope, noverify_text) except Exception: # pylint: disable-msg=broad-except errors.append('Unable to parse %s:\n%s' % ( root_name, str(sys.exc_info()[1]))) return verifier = verify.Verifier() try: verifier.verify_assessment_instance(assessment, path) except verify.SchemaException: errors.append('Error validating %s\n' % root_name) return fs = self.app_context.fs fs.put( path, vfs.string_to_stream(assessment_content), is_draft=not unit.now_available)
def verify_assessment(assessment_text): """Parses and semantically verifies assessment.""" assessment = AssessmentParser13.parse_string_in_scope( assessment_text, verify.Assessment().scope, 'assessment') assert assessment verifier = verify.Verifier() verifier.verify_assessment_instance(assessment, 'test')
def test_assessment_ast(): """Test a mix of various activities using legacy and new parser.""" # pylint: disable=anomalous-backslash-in-string assessment_text = ( """assessment = { preamble: '<p>This is text.</p>', questionsList: [ {'questionHTML': '<p>This is text.</p>', choices: ["A and B", "D and B", correct("A and C"), "C and D", "I don't know"] }, {"questionHTML": '<p>This is text.</p>', choices: [correct("True"), "False", "I don't know"], choiceScores: [0, 0.5, 1.0], weight: 3 }, {questionHTML: '<p>This is text.</p>', correctAnswerString: 'sunrise' }, {questionHTML: '<p>This is text.</p>', correctAnswerRegex: regex("/354\s*[+]\s*651/") } ], assessmentName: 'Pre', checkAnswers: false } """) # pylint: enable=anomalous-backslash-in-string verify_assessment(assessment_text) scope = verify.Assessment().scope current_ast = AssessmentParser13.parse_string_in_scope( assessment_text, scope, 'assessment') expected_ast = verify.legacy_eval_python_expression_for_test( assessment_text, scope, 'assessment') same = ( len(current_ast.get('assessment')) == 4 and len(current_ast.get('assessment').get('questionsList')) == 4 and current_ast.get('assessment') == expected_ast.get('assessment') and current_ast == expected_ast) if not same: import pprint pprint.pprint(current_ast.get('assessment')) pprint.pprint(expected_ast.get('assessment')) assert same