diff options
Diffstat (limited to 'yaksh/tests.py')
-rw-r--r-- | yaksh/tests.py | 77 |
1 files changed, 59 insertions, 18 deletions
diff --git a/yaksh/tests.py b/yaksh/tests.py index d3ff4fc..58b8518 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -27,7 +27,7 @@ def setUpModule(): # create 20 questions for i in range(1, 21): - Question.objects.create(summary='Q%d' % (i), points=1, type='code') + Question.objects.create(summary='Q%d' % (i), points=1, type='code', user=user) # create a quiz quiz = Quiz.objects.create(start_date_time=datetime(2015, 10, 9, 10, 8, 15, 0), @@ -72,13 +72,22 @@ class ProfileTestCases(unittest.TestCase): class QuestionTestCases(unittest.TestCase): def setUp(self): # Single question details - self.question = Question(summary='Demo question', language='Python', + self.user1 = User.objects.get(pk=1) + self.user2 = User.objects.get(pk=2) + self.question1 = 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, + snippet='def myfunc()', user=self.user1) + self.question1.save() + + self.question2 = Question(summary='Demo Json', language='python', + type='code', active=True, + description='factorial of a no', points=2.0, + snippet='def fact()', user=self.user2) + self.question2.save() + + self.question1.tags.add('python', 'function') + self.testcase = TestCase(question=self.question1, func_name='def myfunc', kw_args='a=10,b=11', pos_args='12,13', expected_answer='15') answer_data = { "test": "", @@ -90,41 +99,73 @@ class QuestionTestCases(unittest.TestCase): "kw_args": {"a": "10", "b": "11"} }], - "id": self.question.id, + "id": self.question1.id, "ref_code_path": "", } self.answer_data_json = json.dumps(answer_data) self.user_answer = "demo_answer" + questions_data = [{"snippet": "def fact()", "active": True, "points": 1.0, + "ref_code_path": "", "description": "factorial of a no", + "language": "Python", "test": "", "type": "Code", + "options": "", "summary": "Json Demo"}] + self.json_questions_data = json.dumps(questions_data) 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()') + self.assertEqual(self.question1.summary, 'Demo question') + self.assertEqual(self.question1.language, 'Python') + self.assertEqual(self.question1.type, 'Code') + self.assertFalse(self.question1.options) + self.assertEqual(self.question1.description, 'Write a function') + self.assertEqual(self.question1.points, 1.0) + self.assertTrue(self.question1.active) + self.assertEqual(self.question1.snippet, 'def myfunc()') tag_list = [] - for tag in self.question.tags.all(): + for tag in self.question1.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], + result = self.question1.consolidate_answer_data([self.testcase], self.user_answer) self.assertEqual(result, self.answer_data_json) + def test_dump_questions_into_json(self): + """ Test dump questions into json """ + question = Question() + question_id = ['24'] + questions = json.loads(question.dump_into_json(question_id, self.user1)) + for q in questions: + self.assertEqual(self.question2.summary, q['summary']) + self.assertEqual(self.question2.language, q['language']) + self.assertEqual(self.question2.type, q['type']) + self.assertEqual(self.question2.description, q['description']) + self.assertEqual(self.question2.points, q['points']) + self.assertTrue(self.question2.active) + self.assertEqual(self.question2.snippet, q['snippet']) + + def test_load_questions_from_json(self): + """ Test load questions into database from json """ + question = Question() + result = question.load_from_json(self.json_questions_data, self.user1) + question_data = Question.objects.get(pk=27) + self.assertEqual(question_data.summary, 'Json Demo') + self.assertEqual(question_data.language, 'Python') + self.assertEqual(question_data.type, 'Code') + self.assertEqual(question_data.description, 'factorial of a no') + self.assertEqual(question_data.points, 1.0) + self.assertTrue(question_data.active) + self.assertEqual(question_data.snippet, 'def fact()') ############################################################################### class TestCaseTestCases(unittest.TestCase): def setUp(self): + self.user = User.objects.get(pk=1) self.question = Question(summary='Demo question', language='Python', type='Code', active=True, description='Write a function', points=1.0, - snippet='def myfunc()') + snippet='def myfunc()', user=self.user) self.question.save() self.testcase = TestCase(question=self.question, func_name='def myfunc', kw_args='a=10,b=11', |