def load_questions_xml(filename): """Load questions from the given XML file.""" q_bank = parse(filename).getElementsByTagName("question") for question in q_bank: summary_node = question.getElementsByTagName("summary")[0] summary = (summary_node.childNodes[0].data).strip() desc_node = question.getElementsByTagName("description")[0] description = (desc_node.childNodes[0].data).strip() type_node = question.getElementsByTagName("type")[0] type = (type_node.childNodes[0].data).strip() points_node = question.getElementsByTagName("points")[0] points = float((points_node.childNodes[0].data).strip()) \ if points_node else 1.0 test_node = question.getElementsByTagName("test")[0] test = decode_html((test_node.childNodes[0].data).strip()) opt_node = question.getElementsByTagName("options")[0] opt = decode_html((opt_node.childNodes[0].data).strip()) new_question = Question(summary=summary, description=description, points=points, options=opt, type=type, test=test) new_question.save()
def save(self, commit=True): summary = self.cleaned_data.get("summary") description = self.cleaned_data.get("description") points = self.cleaned_data.get("points") test = self.cleaned_data.get("test") options = self.cleaned_data.get("options") language = self.cleaned_data.get("language") type = self.cleaned_data.get("type") active = self.cleaned_data.get("active") snippet = self.cleaned_data.get("snippet") new_question = Question() new_question.summary = summary new_question.description = description new_question.points = points new_question.test = test new_question.options = options new_question.language = language new_question.type = type new_question.active = active new_question.snippet = snippet new_question = super(QuestionForm, self).save(commit=False) if commit: new_question.save() return new_question
class QuestionTestCases(unittest.TestCase): def setUp(self): # Single question details # self.question = Question(summary='Demo question', language='Python', # type='Code', active=True, # description='Write a function', points=1.0, # test='Test Cases', snippet='def myfunc()') self.question = Question(summary='Demo question', language='Python', type='Code', active=True, description='Write a function', points=1.0, snippet='def myfunc()') self.question.save() self.question.tags.add('python', 'function') self.testcase = TestCase(question=self.question, func_name='def myfunc', kw_args='a=10,b=11', pos_args='12,13', expected_answer='15') answer_data = {"user_answer": "demo_answer", "test_parameter": [{"func_name": "def myfunc", "expected_answer": "15", "test_id": self.testcase.id, "pos_args": ["12", "13"], "kw_args": {"a": "10", "b": "11"} }], "id": self.question.id, "language": "Python"} self.answer_data_json = json.dumps(answer_data) self.user_answer = "demo_answer" def test_question(self): """ Test question """ self.assertEqual(self.question.summary, 'Demo question') self.assertEqual(self.question.language, 'Python') self.assertEqual(self.question.type, 'Code') self.assertFalse(self.question.options) self.assertEqual(self.question.description, 'Write a function') self.assertEqual(self.question.points, 1.0) self.assertTrue(self.question.active) self.assertEqual(self.question.snippet, 'def myfunc()') tag_list = [] for tag in self.question.tags.all(): tag_list.append(tag.name) self.assertEqual(tag_list, ['python', 'function']) def test_consolidate_answer_data(self): """ Test consolidate_answer_data function """ result = self.question.consolidate_answer_data([self.testcase], self.user_answer) self.assertEqual(result, self.answer_data_json)
class TestCaseTestCases(unittest.TestCase): def setUp(self): self.question = Question(summary='Demo question', language='Python', type='Code', active=True, description='Write a function', points=1.0, snippet='def myfunc()') self.question.save() self.testcase = TestCase(question=self.question, func_name='def myfunc', kw_args='a=10,b=11', pos_args='12,13', expected_answer='15') def test_testcase(self): """ Test question """ self.assertEqual(self.testcase.question, self.question) self.assertEqual(self.testcase.func_name, 'def myfunc') self.assertEqual(self.testcase.kw_args, 'a=10,b=11') self.assertEqual(self.testcase.pos_args, '12,13') self.assertEqual(self.testcase.expected_answer, '15')
def save(self): summary = self.cleaned_data["summary"] description = self.cleaned_data["description"] points = self.cleaned_data['points'] test = self.cleaned_data["test"] options = self.cleaned_data['options'] language = self.cleaned_data['language'] type = self.cleaned_data["type"] active = self.cleaned_data["active"] snippet = self.cleaned_data["snippet"] new_question = Question() new_question.summary = summary new_question.description = description new_question.points = points new_question.test = test new_question.options = options new_question.language = language new_question.type = type new_question.active = active new_question.snippet = snippet new_question.save()