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_style(self, style, sample): question = MultipleChoiceQuestion( "What?", style=style, choices=["???" for _ in range(len(sample))] ) for option, value in zip(question.get_options(), sample): self.assertEqual(value, option.value)
def test_choices_converted_to_options(self): choices = ["Hello", "World"] question = MultipleChoiceQuestion("What?", choices=choices) self.assertEqual(len(choices), len(question.options)) for option, expression in zip(question.get_options(), choices): self.assertEqual(expression, option.expression)
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_choices_combined_with_options(self): question = MultipleChoiceQuestion( "What?", choices=["Hello", "World"], options=[Option(value="Hello", expression="World")], ) self.assertEqual( [ Option(value="a", expression="Hello"), Option(value="b", expression="World"), Option(value="Hello", expression="World"), ], question.get_options(), )
def test_no_options_provided(self, *_): with self.assertRaisesRegex( ValueError, "MultipleChoiceQuestion should" " at least have one member in 'options' or 'choices' attributes.", ): MultipleChoiceQuestion("What?")
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_invalid_style_iterator(self): with self.assertRaisesRegex( ValueError, re.escape( "Unknown style or invalid style iterator. Built-in styles are:" " (letter, letter_uppercase, number, number_fromzero)" ), ): MultipleChoiceQuestion("What?", choices=["A"], style="?")
def test_default_instance_attribute_values(self): question = MultipleChoiceQuestion( "What?", options=[Option(value="Hello", expression="World")] ) self.assertEqual([], question.choices) self.assertEqual("horizontal", question.display) self.assertEqual("letter", question.style) self.assertIsNone(question.style_iterator)
def test_choices_update_options_with_scheme(self): scheme = Scheme(choices=["Cat", "Dog", "Fish"]) question = MultipleChoiceQuestion( "What?", choices=["Chicken"], scheme=scheme ) self.assertEqual( [ Option(value="a", expression="Chicken"), Option(value="b", expression="Cat"), Option(value="c", expression="Dog"), Option(value="d", expression="Fish"), ], question.get_options(), ) # External update new_scheme = Scheme(choices=["Cow"]) question.update_scheme(new_scheme) self.assertEqual( [ Option(value="a", expression="Chicken"), Option(value="b", expression="Cat"), Option(value="c", expression="Dog"), Option(value="d", expression="Fish"), Option(value="e", expression="Cow"), ], question.get_options(), ) self.assertEqual([], question.primitive_options)
def test_choices_combined_with_options_with_scheme(self): scheme = Scheme(options=[Option(value="Cat", expression="Meow")]) question = MultipleChoiceQuestion( "What?", choices=["Hello", "World"], options=[Option(value="Hello", expression="World")], scheme=scheme, ) self.assertEqual( [ Option(value="a", expression="Hello"), Option(value="b", expression="World"), Option(value="Hello", expression="World"), Option(value="Cat", expression="Meow"), ], question.get_options(), ) self.assertEqual( [ Option(value="Hello", expression="World"), Option(value="Cat", expression="Meow"), ], question.primitive_options, ) # External update new_scheme = Scheme(options=[Option(value="Dog", expression="Bark")]) question.update_scheme(new_scheme) self.assertEqual( [ Option(value="Hello", expression="World"), Option(value="Cat", expression="Meow"), Option(value="Dog", expression="Bark"), ], question.primitive_options, ) self.assertEqual( [ Option(value="a", expression="Hello"), Option(value="b", expression="World"), Option(value="Hello", expression="World"), Option(value="Cat", expression="Meow"), Option(value="Dog", expression="Bark"), ], question.get_options(), )
def test_str(self): question = MultipleChoiceQuestion("What?", choices=["A"]) self.assertEqual("<MultipleChoiceQuestion: What?>", str(question))
levels = { range(0, 11): "These ups and downs are considered normal", 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()