|
| 1 | +import json |
| 2 | +import html |
| 3 | + |
| 4 | +from unittest.mock import patch |
| 5 | +from typing import Optional, Tuple, Any, Dict |
| 6 | + |
| 7 | +from zulip_bots.test_lib import ( |
| 8 | + BotTestCase, |
| 9 | + DefaultTests, |
| 10 | + read_bot_fixture_data, |
| 11 | + StubBotHandler, |
| 12 | +) |
| 13 | + |
| 14 | +from zulip_bots.request_test_lib import ( |
| 15 | + mock_request_exception |
| 16 | +) |
| 17 | + |
| 18 | +from zulip_bots.bots.trivia_quiz_game.controller import ( |
| 19 | + TriviaQuizGameModel, |
| 20 | + NotAvailableException |
| 21 | +) |
| 22 | + |
| 23 | +from zulip_bots.bots.trivia_quiz_game.trivia_quiz_game import ( |
| 24 | + TriviaQuizGameMessageHandler |
| 25 | +) |
| 26 | + |
| 27 | +from zulip_bots.game_handler import BadMoveException |
| 28 | + |
| 29 | +class TestTriviaQuizGameBot(BotTestCase, DefaultTests): |
| 30 | + bot_name = 'trivia_quiz_game' # type: str |
| 31 | + |
| 32 | + new_question_response = '\nQ: Which class of animals are newts members of?\n\n' + \ |
| 33 | + '* **A** Amphibian\n' + \ |
| 34 | + '* **B** Fish\n' + \ |
| 35 | + '* **C** Reptiles\n' + \ |
| 36 | + '* **D** Mammals\n' + \ |
| 37 | + '**reply**: <letter>' |
| 38 | + |
| 39 | + test_question = { |
| 40 | + 'question': 'Question 1?', |
| 41 | + 'answers': { |
| 42 | + 'A': 'Correct', |
| 43 | + 'B': 'Incorrect 1', |
| 44 | + 'C': 'Incorrect 2', |
| 45 | + 'D': 'Incorrect 3' |
| 46 | + }, |
| 47 | + 'correct_letter': 'A' |
| 48 | + } |
| 49 | + |
| 50 | + test_question_message_content = ''' |
| 51 | +Q: Question 1? |
| 52 | +
|
| 53 | +* **A** Correct |
| 54 | +* **B** Incorrect 1 |
| 55 | +* **C** Incorrect 2 |
| 56 | +* **D** Incorrect 3 |
| 57 | +**reply**: <letter>''' |
| 58 | + |
| 59 | + test_question_message_widget = '{"widget_type": "zform", "extra_data": {"type": "choices", "heading": "Question 1?", "choices": [{"type": "multiple_choice", "short_name": "A", "long_name": "Correct", "reply": "A"}, {"type": "multiple_choice", "short_name": "B", "long_name": "Incorrect 1", "reply": "B"}, {"type": "multiple_choice", "short_name": "C", "long_name": "Incorrect 2", "reply": "C"}, {"type": "multiple_choice", "short_name": "D", "long_name": "Incorrect 3", "reply": "D"}]}}' |
| 60 | + |
| 61 | + def test_question_not_available(self) -> None: |
| 62 | + with self.mock_http_conversation('test_new_question'): |
| 63 | + model = TriviaQuizGameModel() |
| 64 | + # Exception |
| 65 | + with self.assertRaises(NotAvailableException): |
| 66 | + with mock_request_exception(): |
| 67 | + model.get_trivia_quiz() |
| 68 | + # non-ok status code |
| 69 | + with self.assertRaises(NotAvailableException): |
| 70 | + with self.mock_http_conversation("test_status_code"): |
| 71 | + model.get_trivia_quiz() |
| 72 | + |
| 73 | + def test_validate_move(self) -> None: |
| 74 | + with self.mock_http_conversation('test_new_question'): |
| 75 | + model = TriviaQuizGameModel() |
| 76 | + valid_moves = [ |
| 77 | + 'A', |
| 78 | + 'B', |
| 79 | + 'C', |
| 80 | + 'D' |
| 81 | + ] |
| 82 | + invalid_moves = [ |
| 83 | + 'AA', |
| 84 | + '1' |
| 85 | + ] |
| 86 | + for valid_move in valid_moves: |
| 87 | + self.assertTrue(model.validate_move(valid_move)) |
| 88 | + for invalid_move in invalid_moves: |
| 89 | + self.assertFalse(model.validate_move(invalid_move)) |
| 90 | + |
| 91 | + def test_make_move(self) -> None: |
| 92 | + with self.mock_http_conversation('test_new_question'): |
| 93 | + model = TriviaQuizGameModel() |
| 94 | + model.current_board = self.test_question |
| 95 | + model.scores = { |
| 96 | + 0: 0, |
| 97 | + 1: 1 |
| 98 | + } |
| 99 | + # Invalid move should raise BadMoveException |
| 100 | + with self.assertRaises(BadMoveException): |
| 101 | + model.make_move('AA', 0) |
| 102 | + # Correct move should: |
| 103 | + with self.mock_http_conversation('test_new_question'): |
| 104 | + with patch('random.shuffle'): |
| 105 | + move_data = model.make_move('A', 0) |
| 106 | + # Increment score |
| 107 | + self.assertEqual(model.scores[0], 1) |
| 108 | + # Change question |
| 109 | + self.assertEqual(model.current_board, read_bot_fixture_data("trivia_quiz_game", "test_new_question_dict")) |
| 110 | + # Move data correct should be true |
| 111 | + self.assertTrue(move_data['correct']) |
| 112 | + # Move data score should be the same as model.scores[player_number] |
| 113 | + self.assertEqual(move_data['score'], 1) |
| 114 | + # Incorrect move should: |
| 115 | + with self.mock_http_conversation('test_new_question'): |
| 116 | + model.current_board = self.test_question |
| 117 | + move_data = model.make_move('B', 1) |
| 118 | + # Decrement score |
| 119 | + self.assertEqual(model.scores[1], 0) |
| 120 | + # Move data correct should be false |
| 121 | + self.assertFalse(move_data['correct']) |
| 122 | + |
| 123 | + def test_determine_game_over(self) -> None: |
| 124 | + with self.mock_http_conversation('test_new_question'): |
| 125 | + model = TriviaQuizGameModel() |
| 126 | + model.scores = { |
| 127 | + 0: 0, |
| 128 | + 1: 5, |
| 129 | + 2: 1 |
| 130 | + } |
| 131 | + self.assertEqual(model.determine_game_over(["Test 0", "Test 1", "Test 2"]), "Test 1") |
| 132 | + model.scores = { |
| 133 | + 0: 0, |
| 134 | + 1: 4, |
| 135 | + 2: 1 |
| 136 | + } |
| 137 | + self.assertIsNone(model.determine_game_over(["Test 0", "Test 1", "Test 2"])) |
| 138 | + |
| 139 | + def test_message_handler_parse_board(self) -> None: |
| 140 | + message_handler = TriviaQuizGameMessageHandler() |
| 141 | + board_message_content, board_message_widget = message_handler.parse_board(self.test_question) |
| 142 | + self.assertEqual(board_message_content, self.test_question_message_content) |
| 143 | + self.assertEqual(json.loads(board_message_widget), json.loads(self.test_question_message_widget)) |
| 144 | + |
| 145 | + def test_message_handler_alert_move_message(self) -> None: |
| 146 | + message_handler = TriviaQuizGameMessageHandler() |
| 147 | + correct_responses = [ |
| 148 | + (("Test User", "A", {'correct': True, 'score': 5}), ":tada: Correct Test User (5 points)!"), |
| 149 | + (("Test User", "B", {'correct': False, 'score': 1, 'correct_letter': "B"}), ":disappointed: Incorrect Test User (1 points). The correct answer was **B**") |
| 150 | + ] |
| 151 | + for args, response in correct_responses: |
| 152 | + self.assertEqual(message_handler.alert_move_message(*args), response) |
| 153 | + |
| 154 | + def test_message_handler_get_player_color(self) -> None: |
| 155 | + message_handler = TriviaQuizGameMessageHandler() |
| 156 | + self.assertIsNone(message_handler.get_player_color(0)) |
0 commit comments