def test_is_done(self, mock_stdout, *_): q1, q2, q3, q4 = ( Question("What?"), Question("What?"), Question("What?", required=False), Question("What?", required=False), ) q1.pre_ask = q2.pre_ask = ( lambda q: self.assertFalse(q.quiz.is_done) if q.attempt == 1 else self.assertTrue(q.quiz.is_done) ) q3.pre_ask = q4.pre_ask = lambda q: self.assertFalse(q.quiz.is_done) quiz = Quiz(questions=[q1, q2, q3, q4]) quiz.start() mock_stdout.assert_has_calls( [ call( "\nYou now have answered all the required questions on" " this test. You may finish, but There are still some" " optional questions left (3, 4).\n" ), call("\n[Ready, some optional questions left (4).]\n"), call( "\nYou now have answered all the questions on this test." " You may finish or revise your questions if you want.\n" ), call("\n[Completed, waiting for finish command.]\n"), ] )
def test_finish(self, *_): question = Question("Hello?", required=False) quiz = Quiz(questions=[question]) quiz.start() self.assertIsNone(question.answer) self.assertEqual(1, question.attempt)
def test_done_ready_api(self): q1, q2, q3 = ( Question("What?"), Question("What?", required=False), Question("What?"), ) quiz = Quiz(questions=[q1, q2, q3]) self.assertFalse(quiz.is_ready) self.assertFalse(quiz.is_done) q1.answer = q3.answer = "A" self.assertFalse(quiz.is_ready) self.assertFalse(quiz.is_done) quiz.inquiries = 2 self.assertTrue(quiz.is_ready) self.assertFalse(quiz.is_done) q2.answer = "A" self.assertTrue(quiz.is_ready) self.assertTrue(quiz.is_done) q1.answer = None self.assertFalse(quiz.is_ready) self.assertFalse(quiz.is_done)
def test_quiz_inquiries_get_incremented(self, *_): questions = [Question("What?"), Question("Hello?")] quiz = Quiz(questions=questions) quiz.start() self.assertEqual(4, quiz.inquiries) self.assertEqual(sum(q.attempt for q in questions), quiz.inquiries)
def test_answers(self, mock_stdout, *_): q1, q2, q3, q4 = ( Question("What?"), Question("How?"), Question("When?", required=False), MultipleChoiceQuestion("Which?", choices=["This"]), ) quiz = Quiz( questions=[q1, q2, q3, q4], scheme=Scheme(commands=[Answers, Finish]), ) quiz.start() mock_stdout.assert_has_calls( [ ANY, call( "\nCurrent answers:\n" "1. What? -> [Me]\n" "2. How? -> [Handsome]\n" "~3. When? -> [No answer]\n" "4. Which? -> [a) This]\n" ), ANY, ] )
def test_is_ready(self, mock_stdout, *_): q1, q2, q3, q4 = ( Question("What?"), Question("What?"), Question("What?", required=False), Question("What?", required=False), ) q1.pre_ask = lambda q: self.assertFalse(q.quiz.is_ready) q2.pre_ask = lambda q: self.assertFalse(q.quiz.is_ready) q3.pre_ask = lambda q: self.assertTrue(q.quiz.is_ready) q4.pre_ask = lambda q: self.assertTrue(q.quiz.is_ready) quiz = Quiz(questions=[q1, q2, q3, q4]) quiz.start() mock_stdout.assert_has_calls( [ call( "\nYou now have answered all the required questions on" " this test. You may finish, but There are still some" " optional questions left (3, 4).\n" ), call("\n[Ready, some optional questions left (4).]\n"), ] )
def test_jump(self, mock_stdout, *_): q1, q2, q3, q4 = ( Question("What?"), MultipleChoiceQuestion("What?", choices=["a"], required=False), MultipleChoiceQuestion("What?", choices=["c"], required=False), Question("What?", required=False), ) quiz = Quiz( questions=[q1, q2, q3, q4], scheme=Scheme(commands=[Jump, Finish]) ) quiz.start() mock_stdout.assert_has_calls( [ call("Jumped to question 4."), call("Jumped to question 1."), call("Jumped to question 4."), call("Jumped to question 1."), ANY, ] ) self.assertEqual(3, q1.attempt) self.assertEqual(1, q2.attempt) self.assertEqual(2, q4.attempt) self.assertEqual(0, q3.attempt) self.assertEqual("Answer", q1.answer)
def test_finish_error_on_uncompleted(self, mock_stdout, *_): q1, q2, q3 = ( Question("Hello?"), Question("World?"), Question("Hey?", required=False), ) quiz = Quiz(questions=[q1, q2, q3]) quiz.start() mock_stdout.assert_has_calls( [ call( "There are still some required questions to answer: (1, 2)" ), ANY, ] ) self.assertEqual("Answer", q1.answer) self.assertEqual("Cat", q2.answer) self.assertIsNone(q3.answer) self.assertEqual(2, q1.attempt) self.assertEqual(1, q2.attempt) self.assertEqual(1, q3.attempt)
def test_quiz_index_corresponds_question_sequence(self, *_): question = Question("What?") question1 = Question("Hello?") question.pre_ask = question1.pre_ask = lambda q: self.assertEqual( q.sequence, q.quiz.index ) quiz = Quiz(questions=[question, question1]) quiz.start()
def test_update_scheme_force(self): my_scheme = Scheme(commands=[Skip]) a, b = Question("a"), Question("b", scheme=my_scheme) quiz = Quiz(questions=[a], scheme=my_scheme) quiz.questions.append(b) quiz.update(force_scheme=True) self.assertEqual([my_scheme, my_scheme], b.mounted_schemes)
def test_start(self, *_): q = Question("Hello?", required=False) q1 = Question("Hello?", required=False) quiz = Quiz(questions=[q, q1]) quiz.start() self.assertEqual(1, quiz.inquiries) self.assertEqual(1, q.attempt) self.assertEqual(0, q1.attempt)
def test_update_scheme_do_not_force(self): # Same scheme is not applied twice if force_scheme=False my_scheme = Scheme(commands=[Skip]) a, b = Question("a"), Question("b", scheme=my_scheme) quiz = Quiz(questions=[a], scheme=my_scheme) quiz.questions.append(b) quiz.update() self.assertEqual([my_scheme], b.mounted_schemes)
def test_ready_done_verbosity_changes(self, *_): question = Question("What?", required=False) quiz = Quiz(questions=[question]) self.assertFalse(hasattr(quiz, "_ready_verbose")) self.assertFalse(hasattr(quiz, "_done_verbose")) quiz.start() self.assertFalse(quiz._ready_verbose) self.assertFalse(quiz._done_verbose)
def test_jump_method(self): q0, q1, q2, q3 = ( MultipleChoiceQuestion("What?", choices=["a"]), MultipleChoiceQuestion("What?", choices=["b"]), MultipleChoiceQuestion("What?", choices=["c"]), MultipleChoiceQuestion("What?", choices=["d"]), ) quiz = Quiz(questions=[q0, q1, q2, q3]) self.assertEqual(q0, quiz.jump(0)) self.assertEqual(q1, quiz.jump(1)) self.assertEqual(q2, quiz.jump(2)) self.assertEqual(q3, quiz.jump(3))
def test_next_previous(self, *_): q1, q2, q3 = ( Question("What?"), Question("What? 2"), Question("What? 3"), ) quiz = Quiz( questions=[q1, q2, q3], scheme=Scheme(commands=[Next, Previous, Finish], required=False), ) quiz.start() self.assertEqual(17, q1.attempt) self.assertEqual(16, q2.attempt) self.assertEqual(0, q3.attempt)
def test_jump_no_arg_or_invalid(self, mock_stdout, *_): question = Question("What?", commands=[Jump, Finish]) quiz = Quiz(questions=[question]) quiz.start() mock_stdout.assert_has_calls( [ call("Please specify a question number to jump."), call("Question number needs to be a positive integer."), call("Question number needs to be a positive integer."), call("Can't jump to question 52, no such question."), ANY, ] ) self.assertEqual("Ans", question.answer)
def test_update(self): a, b = Question("a"), Question("b") quiz = Quiz(questions=[a]) quiz.questions.append(b) self.assertIsNone(b.quiz) self.assertEqual([a, b], quiz.required_questions) self.assertEqual(0, b.sequence) quiz.update() self.assertEqual(quiz, b.quiz) self.assertEqual(1, b.sequence) # update() should add Quiz's scheme. (default scheme, in this case) self.assertEqual([Scheme(commands=[Finish])], b.mounted_schemes)
def test_required_questions_and_min_inquiries(self): q = Question("Hello?") q1 = Question("Hello?", required=False) q2 = Question("Hello?") q3 = Question("Hello?") q4 = Question("Hello?", required=False) quiz = Quiz(questions=[q, q1, q2, q3, q4]) self.assertEqual([q, q2, q3], quiz.required_questions) self.assertEqual(3, quiz.min_inquiries)
def test_question_prompt_changes_with_quiz(self): question = Question("What?") question2 = Question("Hello?") Quiz(questions=[question, question2]) self.assertEqual( "* Question 1/2. [No answer]\nWhat?", question.get_prompt() ) question.update_scheme(Scheme(suffix="Suf", prefix="Pre")) self.assertEqual( "* Question 1/2. [No answer]\nPreWhat?Suf", question.get_prompt() )
def test_quiz_sets_question_sequence_and_self(self): question = Question("What?") question1 = MultipleChoiceQuestion("What?", choices=["A"]) self.assertEqual(0, question.sequence) self.assertEqual(0, question1.sequence) self.assertIsNone(question.quiz) self.assertIsNone(question1.quiz) quiz = Quiz(questions=[question, question1]) self.assertEqual(0, question.sequence) self.assertEqual(1, question1.sequence) self.assertEqual(quiz, question.quiz) self.assertEqual(quiz, question1.quiz)
def test_update_manually(self): a, b = Question("A"), Question("B") quiz = Quiz(questions=[a]) quiz.questions.append(b) self.assertIsNone(b.quiz) self.assertEqual([a, b], quiz.required_questions) self.assertEqual(0, b.sequence) b.quiz = quiz # Manual assignment, instead of quiz.update() self.assertEqual(quiz, b.quiz) self.assertEqual(1, b.sequence) self.assertEqual([], b.mounted_schemes)
def test_question_pre(self): question = Question("What?") Quiz(questions=[question]) self.assertEqual( "* Question 1/1. [No answer]\n", question.get_question_pre() ) question.answer = "Good answer" self.assertEqual( "* Question 1/1. [Good answer]\n", question.get_question_pre() ) question.answer = Option(value="A", expression="Best answer.") self.assertEqual( "* Question 1/1. [A) Best answer.]\n", question.get_question_pre() )
def test_empty_scheme_removes_finish_command(self): question = Question("What?") Quiz(questions=[question], scheme=Scheme()) self.assertEqual([], question.commands)
def test_default_scheme_has_finish_command(self): question = Question("What?") Quiz(questions=[question]) self.assertEqual([Finish], question.commands)
def test_update_scheme_gets_called(self, mock_update_scheme): my_scheme = Scheme(command_delimiter="**") Quiz(questions=[Question("What?")], scheme=my_scheme) mock_update_scheme.assert_called_with(my_scheme, force=False)
range(11, 17): "Mild mood disturbance", range(17, 21): "Borderline clinical depression", range(21, 31): "Moderate depression", range(31, 41): "Severe depression", range(41, 64): "Extreme depression", } with open("data.json") as file: question_data = json.loads(file.read()) quiz = Quiz( questions=[ MultipleChoiceQuestion("Choose one", choices=choices) for choices in question_data ], scheme=Scheme( style="number_fromzero", display="vertical", commands=[Next, Previous, Jump, Finish, Quit, Help], ), ) print(warning) quiz.start() result = sum(int(question.answer.value) for question in quiz.questions) correspond = next(description for (scale, description) in levels.items() if result in scale) print("Your answers added up to %d, which corresponds to: '%s'." %