def test_creating_a_quiz__only_uses_published_questions(self): create_questions(1) q = Question.objects.all()[0] q.published = 0 q.save() with self.assertRaises(ValueError): fixed_quiz.create_quiz(1)
def test_creating_a_quiz_with_many_questions__has_correct_number_of_random_questions( self): create_questions(30) fixed_quiz.create_quiz(20) self.assertEqual(20, Quiz.objects.get(pk=1).questions.count()) self.assertLooksKindOfRandom( [q.id for q in Quiz.objects.get(pk=1).questions.all()])
def test_when_a_session_exists_with_a_different_key__the_old_state_is_deleted(self): create_questions(20) key1 = fixed_quiz.create_quiz(fixed_quiz.nof_questions_in_quiz) key2 = fixed_quiz.create_quiz(fixed_quiz.nof_questions_in_quiz) quiz1 = Quiz.objects.get(key=key1) quiz2 = Quiz.objects.get(key=key2) response = self.answer_correctly(key1, quiz1.get_ordered_questions()[0].pk) self.assert_status_string_with(response, answered=1, points=1) response = self.client.get(reverse("quiz:quiz", args=(key2,))) self.assert_status_string_with(response, answered=0, points=0)
def test_when_a_session_exists_with_a_different_key__the_old_state_is_deleted( self): create_questions(20) key1 = fixed_quiz.create_quiz(fixed_quiz.nof_questions_in_quiz) key2 = fixed_quiz.create_quiz(fixed_quiz.nof_questions_in_quiz) quiz1 = Quiz.objects.get(key=key1) quiz2 = Quiz.objects.get(key=key2) response = self.answer_correctly(key1, quiz1.get_ordered_questions()[0].pk) self.assert_status_string_with(response, answered=1, points=1) response = self.client.get(reverse('quiz:quiz', args=(key2, ))) self.assert_status_string_with(response, answered=0, points=0)
def test_incorrectly_answering_a_question__redisplays_it_with_a_message_and_an_option_to_skip_it( self): create_questions(1) key = fixed_quiz.create_quiz(1) response = self.answer_incorrectly(key) self.assertContains(response, 'Incorrect!') self.assertEqual(1, UsersAnswer.objects.count())
def test_creating_a_quiz__only_uses_published_questions(self): create_questions(4) q0 = Question.objects.all()[0] q1 = Question.objects.all()[1] q2 = Question.objects.all()[2] q3 = Question.objects.all()[3] q0.state = 'NEW' q1.state = 'RET' q2.state = 'REF' q3.state = 'ACC' q0.save() q1.save() q2.save() q3.save() with self.assertRaises(ValueError): fixed_quiz.create_quiz(1)
def test_skipping_the_last_question__takes_you_to_the_summary(self): create_questions(1) key = fixed_quiz.create_quiz(1) response = self.skip(key) self.assertContains(response, "All right!") self.assert_result_string_with(response, 0) self.assertEqual(0, UsersAnswer.objects.count()) self.assertNotContains(response, "Correct")
def test_when_viewing_a_question__it_gets_timestamped(self): create_questions(fixed_quiz.nof_questions_in_quiz) key = fixed_quiz.create_quiz() response = self.client.get(reverse('quiz:quiz', args=(key, ))) pk = get_question_pk(response.content) self.assertLess( (datetime.datetime.now() - Question.objects.get(pk=pk).last_viewed).total_seconds(), 10)
def test_skipping_the_last_question__takes_you_to_the_summary(self): create_questions(1) key = fixed_quiz.create_quiz(1) response = self.skip(key) self.assertContains(response, 'All right!') self.assert_result_string_with(response, 0) self.assertEqual(0, UsersAnswer.objects.count()) self.assertNotContains(response, 'Correct')
def test_correctly_answering_the_last_question__takes_you_to_the_summary(self): create_questions(1) key = fixed_quiz.create_quiz(1) response = self.answer_correctly(key, 1) self.assertContains(response, "All right!") self.assert_result_string_with(response, 1) self.assertEqual(1, UsersAnswer.objects.count()) explanation = Quiz.objects.get(key=key).get_ordered_questions()[0].explanation self.assertContains(response, explanation)
def test_skipping_a_question__takes_you_to_the_next_question_but_gives_you_no_points(self): create_questions(fixed_quiz.nof_questions_in_quiz) key = fixed_quiz.create_quiz() quiz = Quiz.objects.get(key=key) response = self.answer_correctly(key, quiz.get_ordered_questions()[0].pk) self.assert_status_string_with(response, answered=1, points=1) users_answers_before_skipping = UsersAnswer.objects.count() response = self.skip(key) self.assert_status_string_with(response, answered=2, points=1) self.assertEqual(users_answers_before_skipping, UsersAnswer.objects.count())
def test_correctly_answering_the_last_question__takes_you_to_the_summary( self): create_questions(1) key = fixed_quiz.create_quiz(1) response = self.answer_correctly(key, 1) self.assertContains(response, 'All right!') self.assert_result_string_with(response, 1) self.assertEqual(1, UsersAnswer.objects.count()) explanation = Quiz.objects.get( key=key).get_ordered_questions()[0].explanation self.assertContains(response, explanation)
def test_correctly_answering_a_question_which_is_not_the_last__congratulates_you_and_takes_you_to_the_next_question( self): create_questions(fixed_quiz.nof_questions_in_quiz) key = fixed_quiz.create_quiz() response = self.client.get(reverse('quiz:quiz', args=(key, ))) pk = get_question_pk(response.content) response = self.answer_correctly(key, pk) self.assertContains(response, 'Correct!') explanation = Question.objects.get(pk=pk).explanation self.assertContains(response, explanation) self.assert_status_string_with(response, 1, 1) self.assertEqual(1, UsersAnswer.objects.count())
def test_correctly_answering_a_question_which_is_not_the_last__congratulates_you_and_takes_you_to_the_next_question( self ): create_questions(fixed_quiz.nof_questions_in_quiz) key = fixed_quiz.create_quiz() response = self.client.get(reverse("quiz:quiz", args=(key,))) pk = get_question_pk(response.content) response = self.answer_correctly(key, pk) self.assertContains(response, "Correct!") explanation = Question.objects.get(pk=pk).explanation self.assertContains(response, explanation) self.assert_status_string_with(response, 1, 1) self.assertEqual(1, UsersAnswer.objects.count())
def test_skipping_a_question__takes_you_to_the_next_question_but_gives_you_no_points( self): create_questions(fixed_quiz.nof_questions_in_quiz) key = fixed_quiz.create_quiz() quiz = Quiz.objects.get(key=key) response = self.answer_correctly(key, quiz.get_ordered_questions()[0].pk) self.assert_status_string_with(response, answered=1, points=1) users_answers_before_skipping = UsersAnswer.objects.count() response = self.skip(key) self.assert_status_string_with(response, answered=2, points=1) self.assertEqual(users_answers_before_skipping, UsersAnswer.objects.count())
def test_asking_for_a_hint__displays_the_hint_but_takes_away_half_a_point(self): create_questions(fixed_quiz.nof_questions_in_quiz) key = fixed_quiz.create_quiz() quiz = Quiz.objects.get(key=key) response = self.client.get(reverse("quiz:quiz", args=(key,))) self.assertContains(response, ">a hint<") self.assertContains(response, "Hint", count=0) response = self.client.get(reverse("quiz:quiz", args=(key,)), {"hint": "1"}) self.assertContains(response, ">a hint<", count=0) self.assertContains(response, "Hint") response = self.answer_correctly(key, quiz.get_ordered_questions()[0].pk) self.assert_status_string_with(response, answered=1, points=0.5)
def test_asking_for_a_hint__displays_the_hint_but_takes_away_half_a_point( self): create_questions(fixed_quiz.nof_questions_in_quiz) key = fixed_quiz.create_quiz() quiz = Quiz.objects.get(key=key) response = self.client.get(reverse('quiz:quiz', args=(key, ))) self.assertContains(response, '>a hint<') self.assertContains(response, 'Hint', count=0) response = self.client.get(reverse('quiz:quiz', args=(key, )), {'hint': '1'}) self.assertContains(response, '>a hint<', count=0) self.assertContains(response, 'Hint') response = self.answer_correctly(key, quiz.get_ordered_questions()[0].pk) self.assert_status_string_with(response, answered=1, points=0.5)
def test_creating_a_quiz__gives_it_a_random_key(self): create_questions(10) key1 = fixed_quiz.create_quiz(1) key2 = fixed_quiz.create_quiz(2) self.assertNotEqual(key1, key2) self.assertEqual([key1, key2], [q.key for q in Quiz.objects.all()])
def test_creating_a_quiz_with_many_questions__has_correct_number_of_random_questions(self): create_questions(30) fixed_quiz.create_quiz(20) self.assertEqual(20, Quiz.objects.get(pk=1).questions.count()) self.assertLooksKindOfRandom([q.id for q in Quiz.objects.get(pk=1).questions.all()])
def set_up(self, nof_questions=10): create_questions(nof_questions) key = create_quiz(nof_questions) self.quiz = Quiz.objects.get(key=key) self.in_progress = QuizInProgress({}, self.quiz)
def test_creating_a_quiz__has_questions_in_random_order(self): create_questions(10) fixed_quiz.create_quiz(10) pks = [q.pk for q in Quiz.objects.get(pk=1).get_ordered_questions()] self.assertNotEqual(sorted(pks), pks)
def set_up(self, nof_questions=10): create_questions(nof_questions) self.key = create_quiz(nof_questions) self.quiz = Quiz.objects.get(key=self.key)
def test_incorrectly_answering_a_question__redisplays_it_with_a_message_and_an_option_to_skip_it(self): create_questions(1) key = fixed_quiz.create_quiz(1) response = self.answer_incorrectly(key) self.assertContains(response, "Incorrect!") self.assertEqual(1, UsersAnswer.objects.count())
def start(request): clear_quiz_in_progress(request.session) key = fixed_quiz.create_quiz() return HttpResponseRedirect('/q/%s' % key)