From d433fe75da28d7051b96f2fbdc5a01d5b5c159b1 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 9 Oct 2017 15:12:09 +0530 Subject: Fix bug that prevents students from revisiting AnswerPaper with only one question --- yaksh/models.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 787daa6..e5285ba 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1215,10 +1215,7 @@ class AnswerPaper(models.Model): self.questions_answered.add(question_id) self.questions_unanswered.remove(question_id) - next_question = self.next_question(question_id) - if next_question and next_question.id == int(question_id): - return None - return next_question + return self.next_question(question_id) def next_question(self, question_id): """ -- cgit From 8939dfcefc151ba41c7cb0a2ed32effab9120b0e Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 11 Oct 2017 15:56:44 +0530 Subject: Add tests to verify the change --- yaksh/models.py | 5 ++-- yaksh/test_models.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 6 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index e5285ba..6dc8ce2 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1223,8 +1223,9 @@ class AnswerPaper(models.Model): available question. """ if self.questions_order: - all_questions = [int(q_id) - for q_id in self.questions_order.split(',')] + all_questions = [ + int(q_id) for q_id in self.questions_order.split(',') + ] else: all_questions = list(self.questions.all().values_list( 'id', flat=True)) diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 00506cd..3ba7a71 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -507,12 +507,19 @@ class AnswerPaperTestCases(unittest.TestCase): self.quiz = Quiz.objects.get(description='demo quiz 1') self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() + self.quiz2 = Quiz.objects.get(description='demo quiz 2') + self.qtn_paper_with_single_question = QuestionPaper( + quiz=self.quiz2, total_marks=3 + ) + self.qtn_paper_with_single_question.save() + self.questions = Question.objects.all()[0:3] self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) self.question1 = self.questions[0] self.question2 = self.questions[1] self.question3 = self.questions[2] + self.question4 = Question.objects.all()[3] # create answerpaper self.answerpaper = AnswerPaper(user=self.user, @@ -555,6 +562,32 @@ class AnswerPaperTestCases(unittest.TestCase): ) self.answerpaper.answers.add(self.answer1) + # create an answerpaper with only one question + self.answerpaper_single_question = AnswerPaper(user=self.user, + question_paper=self.question_paper, + start_time=self.start_time, + end_time=self.end_time, + user_ip=self.ip + ) + self.attempted_papers = AnswerPaper.objects.filter( + question_paper=self.question_paper, + user=self.user + ) + self.qtn_paper_with_single_question.fixed_questions.add(self.question4) + already_attempted = self.attempted_papers.count() + self.answerpaper_single_question.attempt_number = already_attempted + 1 + self.answerpaper_single_question.save() + self.answerpaper_single_question.questions.add(self.question4) + self.answerpaper_single_question.save() + # answers for the Answer Paper + self.single_answer = Answer(question=self.question4, + answer="Demo answer", + correct=True, marks=1, + error=json.dumps([]) + ) + self.single_answer.save() + self.answerpaper.answers.add(self.single_answer) + self.question1.language = 'python' self.question1.test_case_type = 'standardtestcase' self.question1.summary = "Question1" @@ -614,6 +647,39 @@ class AnswerPaperTestCases(unittest.TestCase): self.user2, self.ip, 1 ) + def test_returned_question_is_not_none(self): + # Test add_completed_question and next_question + # When all questions are answered + + current_question = self.answerpaper_single_question.add_completed_question( + self.question1.id + ) + + # Then + self.assertEqual(self.answerpaper_single_question.questions_left(), 0) + self.assertIsNotNone(current_question) + self.assertEqual(current_question.summary, "Q4") + + # When + next_question = self.answerpaper_single_question.next_question( + self.question1.id + ) + + # Then + self.assertEqual(self.answerpaper_single_question.questions_left(), 0) + self.assertIsNotNone(next_question) + self.assertEqual(next_question.summary, "Q4") + + # When + current_question = self.answerpaper_single_question.get_current_question( + self.answerpaper_single_question.questions.all() + ) + + # Then + self.assertEqual(self.answerpaper_single_question.questions_left(), 0) + self.assertIsNotNone(current_question) + self.assertEqual(current_question.summary, "Q4") + def test_validate_and_regrade_mcc_correct_answer(self): # Given mcc_answer = [str(self.mcc_based_testcase.id)] @@ -744,6 +810,7 @@ class AnswerPaperTestCases(unittest.TestCase): # Test completed_question() method of Answer Paper question = self.answerpaper.add_completed_question(self.question1.id) + self.assertIsNotNone(question) self.assertEqual(self.answerpaper.questions_left(), 2) # Test next_question() method of Answer Paper @@ -775,7 +842,6 @@ class AnswerPaperTestCases(unittest.TestCase): # Then self.assertTrue(next_question_id is not None) - self.assertEqual(next_question_id.summary, "Question1") # Given, last question in the list @@ -809,11 +875,12 @@ class AnswerPaperTestCases(unittest.TestCase): # When all questions are answered current_question = self.answerpaper.add_completed_question( - self.question2.id - ) + self.question2.id + ) # Then self.assertEqual(self.answerpaper.questions_left(), 1) + self.assertIsNotNone(current_question) self.assertEqual(current_question.summary, "Question3") # When @@ -823,6 +890,7 @@ class AnswerPaperTestCases(unittest.TestCase): # Then self.assertEqual(self.answerpaper.questions_left(), 0) + self.assertIsNotNone(current_question) self.assertTrue(current_question == self.answerpaper.questions.all()[0]) # When @@ -856,7 +924,7 @@ class AnswerPaperTestCases(unittest.TestCase): first_answer_obj = first_answer['answer'] self.assertEqual(first_answer_obj.answer, 'Demo answer') self.assertTrue(first_answer_obj.correct) - self.assertEqual(len(answered), 2) + self.assertEqual(len(answered), 3) def test_is_answer_correct(self): self.assertTrue(self.answerpaper.is_answer_correct(self.questions[0])) -- cgit From 90402ae9b0f2d4038099e9ea4e39f832063dd0dd Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 18 Oct 2017 15:40:58 +0530 Subject: - Fix and add test cases - Fix god-mode/user-mode url pattern --- yaksh/models.py | 11 ++++++----- yaksh/test_models.py | 24 +++++++++++++++++------- yaksh/urls.py | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) (limited to 'yaksh') diff --git a/yaksh/models.py b/yaksh/models.py index 6dc8ce2..de794c4 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1196,11 +1196,12 @@ class AnswerPaper(models.Model): def get_current_question(self, questions): if self.questions_order: - question_id = int(self.questions_order.split(',')[0]) - question = questions.get(id=question_id) - else: - question = questions.first() - return question + available_question_ids = questions.values_list('id', flat=True) + ordered_question_ids = [int(q) for q in self.questions_order.split(',')] + for qid in ordered_question_ids: + if qid in available_question_ids: + return questions.get(id=qid) + return questions.first() def questions_left(self): """Returns the number of questions left.""" diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 3ba7a71..f7662ad 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -513,13 +513,14 @@ class AnswerPaperTestCases(unittest.TestCase): ) self.qtn_paper_with_single_question.save() - self.questions = Question.objects.all()[0:3] + all_questions = Question.objects.all() + self.questions = all_questions[0:3] self.start_time = timezone.now() self.end_time = self.start_time + timedelta(minutes=20) - self.question1 = self.questions[0] - self.question2 = self.questions[1] - self.question3 = self.questions[2] - self.question4 = Question.objects.all()[3] + self.question1 = all_questions[0] + self.question2 = all_questions[1] + self.question3 = all_questions[2] + self.question4 = all_questions[3] # create answerpaper self.answerpaper = AnswerPaper(user=self.user, @@ -578,6 +579,7 @@ class AnswerPaperTestCases(unittest.TestCase): self.answerpaper_single_question.attempt_number = already_attempted + 1 self.answerpaper_single_question.save() self.answerpaper_single_question.questions.add(self.question4) + self.answerpaper_single_question.questions_unanswered.add(self.question4) self.answerpaper_single_question.save() # answers for the Answer Paper self.single_answer = Answer(question=self.question4, @@ -651,18 +653,26 @@ class AnswerPaperTestCases(unittest.TestCase): # Test add_completed_question and next_question # When all questions are answered + # Before questions are answered + self.assertEqual(self.answerpaper_single_question.questions_left(), 1) + current_question = self.answerpaper_single_question.add_completed_question( - self.question1.id + self.question4.id ) + # Then + self.assertEqual( + self.answerpaper_single_question.questions_answered.all()[0], + self.question4 + ) self.assertEqual(self.answerpaper_single_question.questions_left(), 0) self.assertIsNotNone(current_question) self.assertEqual(current_question.summary, "Q4") # When next_question = self.answerpaper_single_question.next_question( - self.question1.id + self.question4.id ) # Then diff --git a/yaksh/urls.py b/yaksh/urls.py index 2e25bee..4c5593a 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -100,7 +100,7 @@ urlpatterns = [ views.regrade, name='regrade'), url(r'^manage/regrade/paper/(?P\d+)/(?P\d+)/$', views.regrade, name='regrade'), - url(r'^manage/(?P[\w\-]+)/(?P\d+)/$', views.test_quiz), + url(r'^manage/(?Pgodmode|usermode)/(?P\d+)/$', views.test_quiz), url(r'^manage/create_demo_course/$', views.create_demo_course), url(r'^manage/courses/download_course_csv/(?P\d+)/$', views.download_course_csv, name="download_course_csv"), -- cgit