示例#1
0
    def test_trivia_fetch_question_valid(self):
        question_json = {
            "response_code":
            0,
            "results": [{
                "category": "cat1",
                "type": "multiple",
                "difficulty": "easy",
                "question": "What is the first letter of the alphabet?",
                "correct_answer": "A",
                "incorrect_answers": ["D", "C", "B"]
            }]
        }

        with patch("trivia.requests.get") as mock_get:
            mock_get.return_value.ok = True
            mock_get.return_value.json = lambda: question_json

            question = Trivia.fetch_question()
            self.assertEqual(question.category, "cat1")
            self.assertEqual(question.type, "multiple")
            self.assertEqual(question.difficulty, "easy")
            self.assertEqual(question.question,
                             "What is the first letter of the alphabet?")
            self.assertEqual(question.correct_answer, "A")
            self.assertListEqual(question.answers, ["A", "B", "C", "D"])
示例#2
0
    def execute_get_new_question(self):
        category = None
        difficulty = None

        if len(self.command_arguments) > 1:
            category = self.command_arguments[1]
            if category.lower() == "help":
                self.handle_normal_query(Trivia.get_help_text())
                return
                
            if len(self.command_arguments) > 2:
                difficulty = self.command_arguments[2]

        try:
            self.trivia = Trivia.fetch_question(category, difficulty)
        except BotError as bot_err:
            print(f"TriviaCommand Error (Checked):\n\t{bot_err}")
            self.handle_normal_query(str(bot_err))
        except Exception as err:
            print(f"TriviaCommand Error (Unchecked):\n\t{err}")
            self.handle_normal_query(TRIVIA_BASE_ERROR_MESSAGE)
        else:
            self.handle_normal_query(self.trivia.formatted_response())