def test_set_charset_already(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() bot.show_about(context) context.answer.assert_called_with(context.lang.get.return_value) context.lang.get.assert_called_with("help", "about", version=version)
def test_generate_message_too_long(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() bot.generate(context, Format.TEXT, "ita2", "hello" * 1000) context.answer.assert_called_with(context.lang.get.return_value) context.lang.get.assert_called_with("encoding", "too_long", columns="1024")
def test_generate_some_unsupported(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() bot.generate(context, Format.DEFAULT, "ita2", "пhello") context.answer.assert_called_with(context.lang.__getitem__.return_value) context.lang.__getitem__.assert_called_with( ("encoding", "some_unsupported"))
def test_clearing_format_on_text(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.data = ChatData() context.message = "hello, world" bot.text(context) context.save.assert_called_with(format=Format.DEFAULT)
def main(ctx, api_key, mongo): if mongo is None: store = InMemoryStore() else: store = MongoStore(mongo) bot = KeyPunchBot(api_key, store) bot.initialize() ctx.obj = bot
def test_cancel_nothing(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.data = ChatData() bot.cancel_format(context) context.answer.assert_called_with(context.lang.__getitem__.return_value) context.lang.__getitem__.assert_called_with(("cancel", "fail"))
def test_generate_message_too_many_pages(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() bot.generate(context, Format.DEFAULT, "ebcdic", "hello\n" * 7) context.answer.assert_called_with(context.lang.get.return_value) context.lang.get.assert_called_with("encoding", "too_many_pages", pages="5")
def test_generate_images(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() bot.generate(context, Format.DEFAULT, "ebcdic", "hello\nworld") assert context.send_photo.call_count == 2 for call in context.send_photo.call_args_list: img = Image.open(call[0][0]) assert img.format == "PNG"
def test_cancel_done(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.data = ChatData(output_format=Format.PNG) bot.cancel_format(context) context.answer.assert_called_with(context.lang.__getitem__.return_value) context.lang.__getitem__.assert_called_with(("cancel", "done")) context.save.assert_called_with(format=Format.DEFAULT)
def test_setting_format_argument(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.data = ChatData() context.message = "hello, world" bot.set_format(context, Format.PNG) context.send_file.assert_called() context.save.assert_not_called()
def test_set_charset_already(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.data.charset = "mtk2" bot.select_character_set(context, "mtk2") context.answer.assert_called_with(context.lang.get.return_value) context.save.assert_not_called() context.lang.get.assert_called_with("set_charset", "already", encoding="MTK-2")
def test_generatign_files(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() bot.generate(context, Format.JPEG, "ebcdic", "hello\nworld") assert context.send_file.call_count == 2 for filename, call in zip(["card-1.jpg", "card-2.jpg"], context.send_file.call_args_list): img = Image.open(call[0][0]) assert img.format == "JPEG" assert call[0][1] == filename
def test_setting_format_no_argument_enc(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.message = "" context.is_group_chat = False bot.set_format(context, Format.DEFAULT) context.save.assert_not_called() context.send_file.assert_not_called() context.lang.get.assert_called_with("format", "group_prompt", format="enc")
def test_set_charset_switch(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.data.charset = "mtk2" bot.select_character_set(context, "ascii") context.answer.assert_called_with(context.lang.get.return_value) context.save.assert_called_with(charset="ascii") context.lang.get.assert_called_with("set_charset", "selected", encoding="ASCII", kind=["set_charset", "punchcard"])
def test_setting_format_no_argument_group(): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.message = "" context.is_group_chat = True bot.set_format(context, Format.PNG) context.save.assert_not_called() context.send_file.assert_not_called() context.answer.assert_called_with(context.lang.get.return_value) context.lang.get.assert_called_with("format", "group_prompt", format="png")
def test_change_show_text(current_show: bool, set_show: bool, message: str, should_update: bool): with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.data.show_text = current_show context.lang.__getitem__.side_effect = lambda x: '.'.join(x) bot.set_text_visible(context, set_show) context.answer.assert_called_with(message) if should_update: context.save.assert_called_with(show_text=set_show) else: context.save.assert_not_called()
def test_unknown_command(num_results: int, answer: str): language = StringsLanguage( None, { "unknown": { "command": "unknown.{suggestion}", "suggestion": { "text": " perhaps {suggestion}?", "command_double": "{a} or {b}", "separator": ", ", "separator_last": ", or " } } }) with patch("keypunch_bot.bot.Updater"): bot = KeyPunchBot("", MagicMock()) context = MagicMock() context.lang = language with patch("keypunch_bot.keypunchbot.compute_suggestions") as mock: mock.return_value = list(string.ascii_lowercase[:num_results]) bot.unknown_command(context) context.answer.assert_called_with(answer)