From f276e468904b3ec0d5cde2cd1573d9622a692eb3 Mon Sep 17 00:00:00 2001 From: Rhea Parekh Date: Mon, 4 Jun 2018 02:55:32 +0530 Subject: [PATCH] bots: Add tests for trivia_quiz bot --- .../fixtures/test_new_question.json | 23 ++++++ .../fixtures/test_status_code.json | 14 ++++ .../bots/trivia_quiz/test_trivia_quiz.py | 71 +++++++++++++++++++ .../bots/trivia_quiz/trivia_quiz.py | 5 +- 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_new_question.json create mode 100644 zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_status_code.json create mode 100644 zulip_bots/zulip_bots/bots/trivia_quiz/test_trivia_quiz.py diff --git a/zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_new_question.json b/zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_new_question.json new file mode 100644 index 0000000..c5678b5 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_new_question.json @@ -0,0 +1,23 @@ +{ + "request":{ + "api_url":"https://opentdb.com/api.php?amount=1&type=multiple" + }, + "response":{ + "response_code":0, + "results":[ + { + "category":"Animals", + "type":"multiple", + "difficulty":"easy", + "question":"Which class of animals are newts members of?", + "correct_answer":"Amphibian", + "incorrect_answers":["Fish","Reptiles","Mammals"] + } + ] + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_status_code.json b/zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_status_code.json new file mode 100644 index 0000000..ae4eef7 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/trivia_quiz/fixtures/test_status_code.json @@ -0,0 +1,14 @@ +{ + "request": { + "api_url":"https://opentdb.com/api.php?amount=1&type=multiple" + }, + "response": { + "data": { + "status_code":404 + } + }, + "response-headers":{ + "status":404, + "content-type":"text/html" + } +} diff --git a/zulip_bots/zulip_bots/bots/trivia_quiz/test_trivia_quiz.py b/zulip_bots/zulip_bots/bots/trivia_quiz/test_trivia_quiz.py new file mode 100644 index 0000000..1e8f547 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/trivia_quiz/test_trivia_quiz.py @@ -0,0 +1,71 @@ +import json + +from unittest.mock import patch +from typing import Optional + +from zulip_bots.test_lib import ( + BotTestCase, + read_bot_fixture_data, +) + +from zulip_bots.bots.trivia_quiz.trivia_quiz import ( + get_quiz_from_payload, +) + +class TestTriviaQuizBot(BotTestCase): + bot_name = "trivia_quiz" # type: str + + new_question_response = '\nQ: Which class of animals are newts members of?\n\n' + \ + '* **A** Amphibian\n' + \ + '* **B** Fish\n' + \ + '* **C** Reptiles\n' + \ + '* **D** Mammals\n' + \ + '**reply**: answer Q001 ' + + def _test(self, message: str, response: str, fixture: Optional[str]=None) -> None: + if fixture: + with self.mock_http_conversation(fixture): + self.verify_reply(message, response) + else: + self.verify_reply(message, response) + + def test_bot_responds_to_empty_message(self) -> None: + self._test('', 'type "new" for a new question') + + def test_bot_new_question(self) -> None: + with patch('random.shuffle'): + self._test('new', self.new_question_response, 'test_new_question') + + def test_question_not_available(self) -> None: + self._test('new', 'Uh-Oh! Trivia service is down.', 'test_status_code') + + def test_invalid_answer(self) -> None: + invalid_replies = ['answer A', + 'answer A Q10', + 'answer Q001 K', + 'answer 001 A'] + for reply in invalid_replies: + self._test(reply, 'Invalid answer format') + + def test_invalid_quiz_id(self) -> None: + self._test('answer Q100 A', 'Invalid quiz id') + + def test_answers(self) -> None: + quiz_payload = read_bot_fixture_data('trivia_quiz', 'test_new_question')['response'] + with patch('random.shuffle'): + quiz = get_quiz_from_payload(quiz_payload) + + self.assertEqual(quiz['question'], 'Which class of animals are newts members of?') + self.assertEqual(quiz['correct_letter'], 'A') + self.assertEqual(quiz['answers']['D'], 'Mammals') + + # test incorrect answer + with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id', + return_value=json.dumps(quiz)): + self._test('answer Q001 B', '**WRONG!** B is not correct :disappointed:') + + # test correct answer + with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.get_quiz_from_id', + return_value=json.dumps(quiz)): + with patch('zulip_bots.bots.trivia_quiz.trivia_quiz.start_new_quiz') as mock_new_quiz: + self._test('answer Q001 A', '**CORRECT!** Amphibian :tada:') diff --git a/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py b/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py index e9364a0..0baf039 100644 --- a/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py +++ b/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py @@ -37,7 +37,7 @@ class TriviaQuizHandler: bot_handler.send_reply(message, bot_response) return try: - quiz_payload = bot_handler.storage.get(quiz_id) + quiz_payload = get_quiz_from_id(quiz_id, bot_handler) except KeyError: bot_response = 'Invalid quiz id' bot_handler.send_reply(message, bot_response) @@ -52,6 +52,9 @@ class TriviaQuizHandler: bot_response = 'type "new" for a new question' bot_handler.send_reply(message, bot_response) +def get_quiz_from_id(quiz_id: str, bot_handler: Any) -> str: + return bot_handler.storage.get(quiz_id) + def start_new_quiz(message: Dict[str, Any], bot_handler: Any) -> None: quiz = get_trivia_quiz() quiz_id = generate_quiz_id(bot_handler.storage)