trivia_quiz: Remove Python < 3.4 compatibility code.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
8b3c50206c
commit
158480fe2c
2 changed files with 3 additions and 28 deletions
|
@ -1,4 +1,3 @@
|
||||||
import html
|
|
||||||
import json
|
import json
|
||||||
from typing import Any, Dict, Optional, Tuple
|
from typing import Any, Dict, Optional, Tuple
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
@ -6,7 +5,6 @@ from unittest.mock import patch
|
||||||
from typing_extensions import override
|
from typing_extensions import override
|
||||||
|
|
||||||
from zulip_bots.bots.trivia_quiz.trivia_quiz import (
|
from zulip_bots.bots.trivia_quiz.trivia_quiz import (
|
||||||
fix_quotes,
|
|
||||||
get_quiz_from_id,
|
get_quiz_from_id,
|
||||||
get_quiz_from_payload,
|
get_quiz_from_payload,
|
||||||
handle_answer,
|
handle_answer,
|
||||||
|
@ -57,17 +55,6 @@ class TestTriviaQuizBot(BotTestCase, DefaultTests):
|
||||||
with mock_request_exception():
|
with mock_request_exception():
|
||||||
self.verify_reply("new", "Uh-Oh! Trivia service is down.")
|
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:
|
def test_invalid_answer(self) -> None:
|
||||||
invalid_replies = ["answer A", "answer A Q10", "answer Q001 K", "answer 001 A"]
|
invalid_replies = ["answer A", "answer A Q10", "answer Q001 K", "answer 001 A"]
|
||||||
for reply in invalid_replies:
|
for reply in invalid_replies:
|
||||||
|
|
|
@ -2,7 +2,7 @@ import html
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
from typing import Any, Dict, Optional, Tuple
|
from typing import Any, Dict, Tuple
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@ -107,18 +107,6 @@ def get_trivia_payload() -> Dict[str, Any]:
|
||||||
return payload
|
return payload
|
||||||
|
|
||||||
|
|
||||||
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 :)
|
|
||||||
#
|
|
||||||
# Meanwhile Python took until version 3.4 to have a
|
|
||||||
# simple html.unescape function.
|
|
||||||
try:
|
|
||||||
return html.unescape(s)
|
|
||||||
except Exception:
|
|
||||||
raise Exception("Please use python3.4 or later for this bot.")
|
|
||||||
|
|
||||||
|
|
||||||
def get_quiz_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
|
def get_quiz_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
result = payload["results"][0]
|
result = payload["results"][0]
|
||||||
question = result["question"]
|
question = result["question"]
|
||||||
|
@ -129,9 +117,9 @@ def get_quiz_from_payload(payload: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
answers[correct_letter] = result["correct_answer"]
|
answers[correct_letter] = result["correct_answer"]
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
answers[letters[i + 1]] = result["incorrect_answers"][i]
|
answers[letters[i + 1]] = result["incorrect_answers"][i]
|
||||||
answers = {letter: fix_quotes(answer) for letter, answer in answers.items()}
|
answers = {letter: html.unescape(answer) for letter, answer in answers.items()}
|
||||||
quiz: Dict[str, Any] = dict(
|
quiz: Dict[str, Any] = dict(
|
||||||
question=fix_quotes(question),
|
question=html.unescape(question),
|
||||||
answers=answers,
|
answers=answers,
|
||||||
answered_options=[],
|
answered_options=[],
|
||||||
pending=True,
|
pending=True,
|
||||||
|
|
Loading…
Add table
Reference in a new issue