Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trivia bot: cleanup. #436

Merged
merged 3 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions tools/run-mypy
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ force_include = [
"zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py",
"zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py",
"zulip_bots/zulip_bots/bots/tictactoe/test_tictactoe.py",
"zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py",
"zulip_bots/zulip_bots/bots/trivia_quiz/test_trivia_quiz.py",
"zulip_bots/zulip_bots/bots/game_handler_bot/game_handler_bot.py",
"zulip_bots/zulip_bots/bots/game_handler_bot/test_game_handler_bot.py",
"zulip_bots/zulip_bots/bots/trello/trello.py",
Expand Down
18 changes: 18 additions & 0 deletions zulip_bots/zulip_bots/bots/trivia_quiz/test_trivia_quiz.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import html

from unittest.mock import patch
from typing import Optional
Expand All @@ -9,8 +10,13 @@
read_bot_fixture_data,
)

from zulip_bots.request_test_lib import (
mock_request_exception,
)

from zulip_bots.bots.trivia_quiz.trivia_quiz import (
get_quiz_from_payload,
fix_quotes,
)

class TestTriviaQuizBot(BotTestCase, DefaultTests):
Expand Down Expand Up @@ -40,6 +46,18 @@ def test_bot_new_question(self) -> None:
def test_question_not_available(self) -> None:
self._test('new', 'Uh-Oh! Trivia service is down.', 'test_status_code')

with mock_request_exception():
self.verify_reply('new', 'Uh-Oh! Trivia service is down.')

def test_fix_quotes(self) -> None:
self.assertEqual(fix_quotes('test & test'), html.unescape('test & test'))
print('f')
with patch('html.unescape') as mock_html_unescape:
mock_html_unescape.side_effect = Exception
with self.assertRaises(Exception) as exception:
fix_quotes('test')
self.assertEqual(str(exception.exception), "Please use python3.4 or later for this bot.")

def test_invalid_answer(self) -> None:
invalid_replies = ['answer A',
'answer A Q10',
Expand Down
28 changes: 13 additions & 15 deletions zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
from zulip_bots.lib import Any

from typing import Optional, Any, Dict
from typing import Optional, Any, Dict, Tuple

class NotAvailableException(Exception):
pass
Expand Down Expand Up @@ -38,7 +38,7 @@ def handle_message(self, message: Dict[str, Any], bot_handler: Any) -> None:
return
try:
quiz_payload = get_quiz_from_id(quiz_id, bot_handler)
except KeyError:
except (KeyError, TypeError):
bot_response = 'Invalid quiz id'
bot_handler.send_reply(message, bot_response)
return
Expand All @@ -63,7 +63,7 @@ def start_new_quiz(message: Dict[str, Any], bot_handler: Any) -> None:
bot_handler.storage.put(quiz_id, json.dumps(quiz))
bot_handler.send_reply(message, bot_response, widget_content)

def parse_answer(query):
def parse_answer(query: str) -> Tuple[str, str]:
m = re.match('answer\s+(Q...)\s+(.)', query)
if not m:
raise InvalidAnswerException()
Expand All @@ -75,12 +75,12 @@ def parse_answer(query):

return (quiz_id, answer)

def get_trivia_quiz() -> str:
def get_trivia_quiz() -> Dict[str, Any]:
payload = get_trivia_payload()
quiz = get_quiz_from_payload(payload)
return quiz

def get_trivia_payload() -> str:
def get_trivia_payload() -> Dict[str, Any]:

url = 'https://opentdb.com/api.php?amount=1&type=multiple'

Expand All @@ -96,7 +96,7 @@ def get_trivia_payload() -> str:
payload = data.json()
return payload

def fix_quotes(s):
def fix_quotes(s: str) -> Optional[str]:
# opentdb is nice enough to escape HTML for us, but
# we are sending this to code that does that already :)
#
Expand All @@ -107,7 +107,7 @@ def fix_quotes(s):
except Exception:
raise Exception('Please use python3.4 or later for this bot.')

def get_quiz_from_payload(payload):
def get_quiz_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
result = payload['results'][0]
question = result['question']
letters = ['A', 'B', 'C', 'D']
Expand All @@ -129,27 +129,25 @@ def get_quiz_from_payload(payload):
)
return quiz

def generate_quiz_id(storage) -> str:
def generate_quiz_id(storage: Any) -> str:
try:
quiz_num = storage.get('quiz_id')
except KeyError:
quiz_num = 0
except TypeError:
except (KeyError, TypeError):
quiz_num = 0
quiz_num += 1
quiz_num = quiz_num % (1000)
storage.put('quiz_id', quiz_num)
quiz_id = 'Q%03d' % (quiz_num,)
return quiz_id

def format_quiz_for_widget(quiz_id, quiz):
def format_quiz_for_widget(quiz_id: str, quiz: Dict[str, Any]) -> str:
widget_type = 'zform'
question = quiz['question']
answers = quiz['answers']

heading = quiz_id + ': ' + question

def get_choice(letter):
def get_choice(letter: str) -> Dict[str, str]:
answer = answers[letter]
reply = 'answer ' + quiz_id + ' ' + letter

Expand All @@ -175,7 +173,7 @@ def get_choice(letter):
payload = json.dumps(widget_content)
return payload

def format_quiz_for_markdown(quiz_id, quiz):
def format_quiz_for_markdown(quiz_id: str, quiz: Dict[str, Any]) -> str:
question = quiz['question']
answers = quiz['answers']
answer_list = '\n'.join([
Expand All @@ -198,7 +196,7 @@ def format_quiz_for_markdown(quiz_id, quiz):
)
return content

def grade_question(quiz, answer):
def grade_question(quiz: Dict[str, Any], answer: str) -> Tuple[bool, str]:
correct = (answer == quiz['correct_letter'])

if correct:
Expand Down