Add tests and required changes

This commit is contained in:
MrYacha 2020-05-30 16:50:58 +03:00
parent 34cac79a14
commit f5f16f2618
No known key found for this signature in database
GPG key ID: B041BFFC2429D520
2 changed files with 61 additions and 5 deletions

View file

@ -18,16 +18,18 @@ class InlineKeyboardMarkup(MutableTelegramObject):
Source: https://core.telegram.org/bots/api#inlinekeyboardmarkup
"""
row_width: int = 3
inline_keyboard: List[List[InlineKeyboardButton]] = []
inline_keyboard: List[List[InlineKeyboardButton]] = list()
"""Array of button rows, each represented by an Array of InlineKeyboardButton objects"""
row_width: int = 3
"""Wight of row. New buttons will fit this limit."""
def add(self, *args: InlineKeyboardButton) -> InlineKeyboardMarkup:
"""
Add buttons
:param args:
:return: self
:rtype: :obj:`types.InlineKeyboardMarkup`
"""
row = []
for index, button in enumerate(args, start=1):
@ -42,9 +44,9 @@ class InlineKeyboardMarkup(MutableTelegramObject):
def row(self, *args: InlineKeyboardButton) -> InlineKeyboardMarkup:
"""
Add row
:param args:
:return: self
:rtype: :obj:`types.InlineKeyboardMarkup`
"""
btn_array = []
for button in args:
@ -55,9 +57,9 @@ class InlineKeyboardMarkup(MutableTelegramObject):
def insert(self, button: InlineKeyboardButton) -> InlineKeyboardMarkup:
"""
Insert button to last row
:param button:
:return: self
:rtype: :obj:`types.InlineKeyboardMarkup`
"""
if self.inline_keyboard and len(self.inline_keyboard[-1]) < self.row_width:
self.inline_keyboard[-1].append(button)

View file

@ -0,0 +1,54 @@
import pytest
from aiogram.api.types import InlineKeyboardButton, InlineKeyboardMarkup
test_row_width = [1, 2, 3, 10]
test_inline_keyboard = [
[],
[
[
InlineKeyboardButton(text="foo", url="https://google.com"),
InlineKeyboardButton(text="foo", url="https://google.com"),
]
],
]
class TestInlineKeyboardMarkup:
@pytest.mark.parametrize("row_width", test_row_width)
def test_keyboard_markup_add(self, row_width):
kwargs = dict(row_width=row_width, inline_keyboard=[])
button = InlineKeyboardButton(text="foo", url="https://google.com")
keyboard_markup = InlineKeyboardMarkup(**kwargs)
api_method = keyboard_markup.add(button)
assert button in api_method.inline_keyboard[0]
@pytest.mark.parametrize("row_width", test_row_width)
def test_keyboard_markup_row(self, row_width):
kwargs = dict(row_width=row_width, inline_keyboard=[])
buttons = [
InlineKeyboardButton(text="foo", url="https://google.com"),
InlineKeyboardButton(text="bar", callback_data="test"),
]
keyboard_markup = InlineKeyboardMarkup(**kwargs)
api_method = keyboard_markup.row(*buttons)
assert buttons == api_method.inline_keyboard[0]
@pytest.mark.parametrize("row_width", test_row_width)
@pytest.mark.parametrize("inline_keyboard", test_inline_keyboard)
def test_keyboard_markup_insert(self, row_width, inline_keyboard):
kwargs = dict(row_width=row_width, inline_keyboard=inline_keyboard)
button = InlineKeyboardButton(text="foo", url="https://google.com")
keyboard_markup = InlineKeyboardMarkup(**kwargs)
api_method = keyboard_markup.insert(button)
assert button in api_method.inline_keyboard[0]