コード例 #1
0
    def test_equality(self):
        a = WebAppInfo(self.url)
        b = WebAppInfo(self.url)
        c = WebAppInfo("")
        d = WebAppInfo("not_url")

        assert a == b
        assert hash(a) == hash(b)
        assert a is not b

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)
コード例 #2
0
    def test_equality(self):
        a = InlineKeyboardButton("text", callback_data="data")
        b = InlineKeyboardButton("text", callback_data="data")
        c = InlineKeyboardButton("texts", callback_data="data")
        d = InlineKeyboardButton("text", callback_data="info")
        e = InlineKeyboardButton("text", url="http://google.com")
        f = InlineKeyboardButton("text", web_app=WebAppInfo(url="https://ptb.org"))
        g = LoginUrl("http://google.com")

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)

        assert a != f
        assert hash(a) != hash(f)

        assert a != g
        assert hash(a) != hash(g)
コード例 #3
0
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Send a message with a button that opens a the web app."""
    await update.message.reply_text(
        "Please press the button below to choose a color via the WebApp.",
        reply_markup=ReplyKeyboardMarkup.from_button(
            KeyboardButton(
                text="Open the color picker!",
                web_app=WebAppInfo(
                    url="https://python-telegram-bot.org/static/webappbot"),
            )),
    )
コード例 #4
0
    def test_equality(self):
        a = KeyboardButton("test", request_contact=True)
        b = KeyboardButton("test", request_contact=True)
        c = KeyboardButton("Test", request_location=True)
        d = KeyboardButton("Test", web_app=WebAppInfo(url="https://ptb.org"))
        e = InlineKeyboardButton("test", callback_data="test")

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)
コード例 #5
0
    def test_equality(self, menu_button, bot):
        a = MenuButton("base_type")
        b = MenuButton("base_type")
        c = menu_button
        d = deepcopy(menu_button)
        e = Dice(4, "emoji")

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)

        assert c == d
        assert hash(c) == hash(d)

        assert c != e
        assert hash(c) != hash(e)

        if hasattr(c, "web_app"):
            json_dict = c.to_dict()
            json_dict["web_app"] = WebAppInfo(
                "https://foo.bar/web_app").to_dict()
            f = c.__class__.de_json(json_dict, bot)

            assert c != f
            assert hash(c) != hash(f)

        if hasattr(c, "text"):
            json_dict = c.to_dict()
            json_dict["text"] = "other text"
            g = c.__class__.de_json(json_dict, bot)

            assert c != g
            assert hash(c) != hash(g)
コード例 #6
0
class TestKeyboardButton:
    text = "text"
    request_location = True
    request_contact = True
    request_poll = KeyboardButtonPollType("quiz")
    web_app = WebAppInfo(url="https://example.com")

    def test_slot_behaviour(self, keyboard_button, mro_slots):
        inst = keyboard_button
        for attr in inst.__slots__:
            assert getattr(inst, attr,
                           "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(
            mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, keyboard_button):
        assert keyboard_button.text == self.text
        assert keyboard_button.request_location == self.request_location
        assert keyboard_button.request_contact == self.request_contact
        assert keyboard_button.request_poll == self.request_poll
        assert keyboard_button.web_app == self.web_app

    def test_to_dict(self, keyboard_button):
        keyboard_button_dict = keyboard_button.to_dict()

        assert isinstance(keyboard_button_dict, dict)
        assert keyboard_button_dict["text"] == keyboard_button.text
        assert keyboard_button_dict[
            "request_location"] == keyboard_button.request_location
        assert keyboard_button_dict[
            "request_contact"] == keyboard_button.request_contact
        assert keyboard_button_dict[
            "request_poll"] == keyboard_button.request_poll.to_dict()
        assert keyboard_button_dict[
            "web_app"] == keyboard_button.web_app.to_dict()

    def test_de_json(self, bot):
        json_dict = {
            "text": self.text,
            "request_location": self.request_location,
            "request_contact": self.request_contact,
            "request_poll": self.request_poll.to_dict(),
            "web_app": self.web_app.to_dict(),
        }

        inline_keyboard_button = KeyboardButton.de_json(json_dict, None)
        assert inline_keyboard_button.text == self.text
        assert inline_keyboard_button.request_location == self.request_location
        assert inline_keyboard_button.request_contact == self.request_contact
        assert inline_keyboard_button.request_poll == self.request_poll
        assert inline_keyboard_button.web_app == self.web_app

        none = KeyboardButton.de_json({}, None)
        assert none is None

    def test_equality(self):
        a = KeyboardButton("test", request_contact=True)
        b = KeyboardButton("test", request_contact=True)
        c = KeyboardButton("Test", request_location=True)
        d = KeyboardButton("Test", web_app=WebAppInfo(url="https://ptb.org"))
        e = InlineKeyboardButton("test", callback_data="test")

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)
コード例 #7
0
class TestMenuButton:
    text = "button_text"
    web_app = WebAppInfo(url="https://python-telegram-bot.org/web_app")

    def test_slot_behaviour(self, menu_button, mro_slots):
        for attr in menu_button.__slots__:
            assert getattr(menu_button, attr,
                           "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(menu_button)) == len(set(
            mro_slots(menu_button))), "duplicate slot"

    def test_de_json(self, bot, scope_class_and_type):
        cls = scope_class_and_type[0]
        type_ = scope_class_and_type[1]

        assert cls.de_json({}, bot) is None
        assert cls.de_json(None, bot) is None

        json_dict = {
            "type": type_,
            "text": self.text,
            "web_app": self.web_app.to_dict()
        }
        menu_button = MenuButton.de_json(json_dict, bot)

        assert isinstance(menu_button, MenuButton)
        assert type(menu_button) is cls
        assert menu_button.type == type_
        if "web_app" in cls.__slots__:
            assert menu_button.web_app == self.web_app
        if "text" in cls.__slots__:
            assert menu_button.text == self.text

    def test_de_json_invalid_type(self, bot):
        json_dict = {
            "type": "invalid",
            "text": self.text,
            "web_app": self.web_app.to_dict()
        }
        menu_button = MenuButton.de_json(json_dict, bot)

        assert type(menu_button) is MenuButton
        assert menu_button.type == "invalid"

    def test_de_json_subclass(self, scope_class, bot):
        """This makes sure that e.g. MenuButtonDefault(data) never returns a
        MenuButtonChat instance."""
        json_dict = {
            "type": "invalid",
            "text": self.text,
            "web_app": self.web_app.to_dict()
        }
        assert type(scope_class.de_json(json_dict, bot)) is scope_class

    def test_to_dict(self, menu_button):
        menu_button_dict = menu_button.to_dict()

        assert isinstance(menu_button_dict, dict)
        assert menu_button_dict["type"] == menu_button.type
        if hasattr(menu_button, "web_app"):
            assert menu_button_dict["web_app"] == menu_button.web_app.to_dict()
        if hasattr(menu_button, "text"):
            assert menu_button_dict["text"] == menu_button.text

    def test_equality(self, menu_button, bot):
        a = MenuButton("base_type")
        b = MenuButton("base_type")
        c = menu_button
        d = deepcopy(menu_button)
        e = Dice(4, "emoji")

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)

        assert c == d
        assert hash(c) == hash(d)

        assert c != e
        assert hash(c) != hash(e)

        if hasattr(c, "web_app"):
            json_dict = c.to_dict()
            json_dict["web_app"] = WebAppInfo(
                "https://foo.bar/web_app").to_dict()
            f = c.__class__.de_json(json_dict, bot)

            assert c != f
            assert hash(c) != hash(f)

        if hasattr(c, "text"):
            json_dict = c.to_dict()
            json_dict["text"] = "other text"
            g = c.__class__.de_json(json_dict, bot)

            assert c != g
            assert hash(c) != hash(g)
コード例 #8
0
class TestInlineKeyboardButton:
    text = "text"
    url = "url"
    callback_data = "callback data"
    switch_inline_query = "switch_inline_query"
    switch_inline_query_current_chat = "switch_inline_query_current_chat"
    callback_game = CallbackGame()
    pay = True
    login_url = LoginUrl("http://google.com")
    web_app = WebAppInfo(url="https://example.com")

    def test_slot_behaviour(self, inline_keyboard_button, mro_slots):
        inst = inline_keyboard_button
        for attr in inst.__slots__:
            assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"

    def test_expected_values(self, inline_keyboard_button):
        assert inline_keyboard_button.text == self.text
        assert inline_keyboard_button.url == self.url
        assert inline_keyboard_button.callback_data == self.callback_data
        assert inline_keyboard_button.switch_inline_query == self.switch_inline_query
        assert (
            inline_keyboard_button.switch_inline_query_current_chat
            == self.switch_inline_query_current_chat
        )
        assert isinstance(inline_keyboard_button.callback_game, CallbackGame)
        assert inline_keyboard_button.pay == self.pay
        assert inline_keyboard_button.login_url == self.login_url
        assert inline_keyboard_button.web_app == self.web_app

    def test_to_dict(self, inline_keyboard_button):
        inline_keyboard_button_dict = inline_keyboard_button.to_dict()

        assert isinstance(inline_keyboard_button_dict, dict)
        assert inline_keyboard_button_dict["text"] == inline_keyboard_button.text
        assert inline_keyboard_button_dict["url"] == inline_keyboard_button.url
        assert inline_keyboard_button_dict["callback_data"] == inline_keyboard_button.callback_data
        assert (
            inline_keyboard_button_dict["switch_inline_query"]
            == inline_keyboard_button.switch_inline_query
        )
        assert (
            inline_keyboard_button_dict["switch_inline_query_current_chat"]
            == inline_keyboard_button.switch_inline_query_current_chat
        )
        assert (
            inline_keyboard_button_dict["callback_game"]
            == inline_keyboard_button.callback_game.to_dict()
        )
        assert inline_keyboard_button_dict["pay"] == inline_keyboard_button.pay
        assert (
            inline_keyboard_button_dict["login_url"] == inline_keyboard_button.login_url.to_dict()
        )  # NOQA: E127
        assert inline_keyboard_button_dict["web_app"] == inline_keyboard_button.web_app.to_dict()

    def test_de_json(self, bot):
        json_dict = {
            "text": self.text,
            "url": self.url,
            "callback_data": self.callback_data,
            "switch_inline_query": self.switch_inline_query,
            "switch_inline_query_current_chat": self.switch_inline_query_current_chat,
            "callback_game": self.callback_game.to_dict(),
            "web_app": self.web_app.to_dict(),
            "login_url": self.login_url.to_dict(),
            "pay": self.pay,
        }

        inline_keyboard_button = InlineKeyboardButton.de_json(json_dict, None)
        assert inline_keyboard_button.text == self.text
        assert inline_keyboard_button.url == self.url
        assert inline_keyboard_button.callback_data == self.callback_data
        assert inline_keyboard_button.switch_inline_query == self.switch_inline_query
        assert (
            inline_keyboard_button.switch_inline_query_current_chat
            == self.switch_inline_query_current_chat
        )
        # CallbackGame has empty _id_attrs, so just test if the class is created.
        assert isinstance(inline_keyboard_button.callback_game, CallbackGame)
        assert inline_keyboard_button.pay == self.pay
        assert inline_keyboard_button.login_url == self.login_url
        assert inline_keyboard_button.web_app == self.web_app

        none = InlineKeyboardButton.de_json({}, bot)
        assert none is None

    def test_equality(self):
        a = InlineKeyboardButton("text", callback_data="data")
        b = InlineKeyboardButton("text", callback_data="data")
        c = InlineKeyboardButton("texts", callback_data="data")
        d = InlineKeyboardButton("text", callback_data="info")
        e = InlineKeyboardButton("text", url="http://google.com")
        f = InlineKeyboardButton("text", web_app=WebAppInfo(url="https://ptb.org"))
        g = LoginUrl("http://google.com")

        assert a == b
        assert hash(a) == hash(b)

        assert a != c
        assert hash(a) != hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)

        assert a != f
        assert hash(a) != hash(f)

        assert a != g
        assert hash(a) != hash(g)

    @pytest.mark.parametrize("callback_data", ["foo", 1, ("da", "ta"), object()])
    def test_update_callback_data(self, callback_data):
        button = InlineKeyboardButton(text="test", callback_data="data")
        button_b = InlineKeyboardButton(text="test", callback_data="data")

        assert button == button_b
        assert hash(button) == hash(button_b)

        button.update_callback_data(callback_data)
        assert button.callback_data is callback_data
        assert button != button_b
        assert hash(button) != hash(button_b)

        button_b.update_callback_data(callback_data)
        assert button_b.callback_data is callback_data
        assert button == button_b
        assert hash(button) == hash(button_b)

        button.update_callback_data({})
        assert button.callback_data == {}
        with pytest.raises(TypeError, match="unhashable"):
            hash(button)
コード例 #9
0
    def test_de_json(self, bot):
        json_dict = {"url": self.url}
        web_app_info = WebAppInfo.de_json(json_dict, bot)

        assert web_app_info.url == self.url
コード例 #10
0
def web_app_info():
    return WebAppInfo(url=TestWebAppInfo.url)