Example #1
0
    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)
Example #2
0
    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))
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
 def test_no_options_provided(self, *_):
     with self.assertRaisesRegex(
         ValueError,
         "MultipleChoiceQuestion should"
         " at least have one member in 'options' or 'choices' attributes.",
     ):
         MultipleChoiceQuestion("What?")
Example #6
0
    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,
            ]
        )
Example #7
0
    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)
Example #8
0
 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="?")
Example #9
0
    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)
Example #10
0
    def test_display_horizontal(self, mock_stdin):
        question = MultipleChoiceQuestion(
            "What?", choices=["A", "B", "C", "D"], display="horizontal"
        )

        question.ask()
        mock_stdin.assert_called_with(
            "What?\na) A  b) B  c) C  d) D\nYour answer: "
        )
Example #11
0
    def test_display_vertical(self, mock_stdin):
        question = MultipleChoiceQuestion(
            "What?",
            choices=["A", "B", "C", "D"],
            display="vertical",
            option_indicator="-)",
        )

        question.ask()
        mock_stdin.assert_called_with(
            "What?\na-)A\nb-)B\nc-)C\nd-)D\nYour answer: "
        )
Example #12
0
    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(),
        )
Example #13
0
    def test_display_invalid(self):
        question = MultipleChoiceQuestion(
            "What?", choices=["A"], display="Cat"
        )

        with self.assertRaisesRegex(
            NotImplementedError,
            re.escape(
                "There is no such display 'Cat'. Built-in displays are:"
                " (vertical, horizontal). You may create this display by"
                " implementing get_Cat_display method."
            ),
        ):
            question.get_prompt()
Example #14
0
    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(),
        )
Example #15
0
    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)
Example #16
0
 def test_str(self):
     question = MultipleChoiceQuestion("What?", choices=["A"])
     self.assertEqual("<MultipleChoiceQuestion: What?>", str(question))
Example #17
0
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()
Example #18
0
    def test_display_none(self, mock_stdin):
        question = MultipleChoiceQuestion("What?", choices=["A"], display=None)
        question.ask()

        mock_stdin.assert_called_with("What?")
        self.assertIsNone(question.get_display())