1
|
|
|
# -*- coding:utf-8 -*- |
2
|
|
|
""" |
3
|
|
|
Test for button_maker() function |
4
|
|
|
""" |
5
|
|
|
import os |
6
|
|
|
import sys |
7
|
|
|
|
8
|
|
|
sys.path.insert(0, "%s/../" % os.path.dirname(os.path.abspath(__file__))) |
9
|
|
|
|
10
|
|
|
import pytest |
11
|
|
|
from telebot.types import InlineKeyboardButton |
12
|
|
|
from keyboa.keyboards import button_maker |
13
|
|
|
|
14
|
|
|
ACCEPTABLE_BUTTON_SOURCE_TYPES = ( |
15
|
|
|
2, "a", "2", {2: "a", }, {"a": 2, }, |
16
|
|
|
(2, "a"), ("a", 2), ("a", None), |
17
|
|
|
) |
18
|
|
|
UNACCEPTABLE_BUTTON_SOURCE_TYPES = ( |
19
|
|
|
{2, "a"}, {"a", 2}, [2, "a"], |
20
|
|
|
["a", 2], (None, 2), (None, None), None, |
21
|
|
|
) |
22
|
|
|
COMBO_BUTTON_DATA = ( |
23
|
|
|
{"button_text": "button_callback_data"}, |
24
|
|
|
("button_text", "button_callback_data"), |
25
|
|
|
) |
26
|
|
|
UNACCEPTABLE_BUTTON_TEXTS = [ |
27
|
|
|
[None, "button_callback_data"], |
28
|
|
|
(None, "button_callback_data"), |
29
|
|
|
[{"test_data": "test_data"}, "button_callback_data"]] |
30
|
|
|
|
31
|
|
|
STRING_INT = ["12345", 12345] |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
@pytest.mark.parametrize("button_data", ACCEPTABLE_BUTTON_SOURCE_TYPES) |
35
|
|
|
def test_acceptable_button_source_types(button_data): |
36
|
|
|
""" |
37
|
|
|
|
38
|
|
|
:param button_data: |
39
|
|
|
:return: |
40
|
|
|
""" |
41
|
|
|
assert isinstance(button_maker( |
42
|
|
|
button_data=button_data, |
43
|
|
|
copy_text_to_callback=True), InlineKeyboardButton) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
@pytest.mark.parametrize("button_data", UNACCEPTABLE_BUTTON_SOURCE_TYPES) |
47
|
|
|
def test_unacceptable_button_source_types(button_data): |
48
|
|
|
""" |
49
|
|
|
|
50
|
|
|
:param button_data: |
51
|
|
|
:return: |
52
|
|
|
""" |
53
|
|
|
with pytest.raises(Exception) as _: |
54
|
|
|
button_maker(button_data=button_data) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_unacceptable_front_marker_type(): |
58
|
|
|
""" |
59
|
|
|
|
60
|
|
|
:return: |
61
|
|
|
""" |
62
|
|
|
with pytest.raises(Exception) as _: |
63
|
|
|
button_maker(button_data="button_text", front_marker={1, 2, 3, }) |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def test_unacceptable_back_marker_type(): |
67
|
|
|
""" |
68
|
|
|
|
69
|
|
|
:return: |
70
|
|
|
""" |
71
|
|
|
with pytest.raises(Exception) as _: |
72
|
|
|
button_maker(button_data="button_text", back_marker={1, 2, 3, }) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_unacceptable_callback_data_type(): |
76
|
|
|
""" |
77
|
|
|
|
78
|
|
|
:return: |
79
|
|
|
""" |
80
|
|
|
with pytest.raises(Exception) as _: |
81
|
|
|
button_maker(button_data=["button_text", {1, 2, 3, }]) |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
@pytest.mark.parametrize( |
85
|
|
|
"button_data", UNACCEPTABLE_BUTTON_TEXTS) |
86
|
|
|
def test_unacceptable_text_type(button_data): |
87
|
|
|
""" |
88
|
|
|
|
89
|
|
|
:param button_data: |
90
|
|
|
:return: |
91
|
|
|
""" |
92
|
|
|
with pytest.raises(Exception) as _: |
93
|
|
|
button_maker(button_data=button_data) |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
@pytest.mark.parametrize("button_data", COMBO_BUTTON_DATA) |
97
|
|
|
def test_create_button_from_dict_tuple_list(button_data): |
98
|
|
|
""" |
99
|
|
|
|
100
|
|
|
:param button_data: |
101
|
|
|
:return: |
102
|
|
|
""" |
103
|
|
|
button = button_maker( |
104
|
|
|
button_data=button_data, |
105
|
|
|
front_marker="front_", |
106
|
|
|
back_marker="_back", |
107
|
|
|
) |
108
|
|
|
assert isinstance(button, InlineKeyboardButton) |
109
|
|
|
assert button.text == "button_text" |
110
|
|
|
assert button.callback_data == "front_button_callback_data_back" |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
@pytest.mark.parametrize("button_data", STRING_INT) |
114
|
|
|
def test_create_button_from_int_or_str_with_copy_option(button_data): |
115
|
|
|
""" |
116
|
|
|
|
117
|
|
|
:param button_data: |
118
|
|
|
:return: |
119
|
|
|
""" |
120
|
|
|
button = button_maker( |
121
|
|
|
button_data=button_data, |
122
|
|
|
front_marker="front_", |
123
|
|
|
back_marker="_back", |
124
|
|
|
copy_text_to_callback=True |
125
|
|
|
) |
126
|
|
|
assert isinstance(button, InlineKeyboardButton) |
127
|
|
|
assert button.text == "12345" |
128
|
|
|
assert button.callback_data == "front_12345_back" |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
@pytest.mark.parametrize("button_data", STRING_INT) |
132
|
|
|
def test_create_button_from_int_or_str_without_copy_option(button_data): |
133
|
|
|
""" |
134
|
|
|
|
135
|
|
|
:param button_data: |
136
|
|
|
:return: |
137
|
|
|
""" |
138
|
|
|
button = button_maker( |
139
|
|
|
button_data=button_data, |
140
|
|
|
front_marker="front_", |
141
|
|
|
copy_text_to_callback=False, |
142
|
|
|
) |
143
|
|
|
assert isinstance(button, InlineKeyboardButton) |
144
|
|
|
assert button.text == "12345" |
145
|
|
|
assert button.callback_data == "front_" |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
@pytest.mark.parametrize("button_data", STRING_INT) |
149
|
|
|
def test_create_button_from_int_or_str_without_callback(button_data): |
150
|
|
|
""" |
151
|
|
|
|
152
|
|
|
:param button_data: |
153
|
|
|
:return: |
154
|
|
|
""" |
155
|
|
|
with pytest.raises(Exception) as _: |
156
|
|
|
button_maker( |
157
|
|
|
button_data=button_data, |
158
|
|
|
copy_text_to_callback=False, |
159
|
|
|
) |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def test_create_button_from_button(): |
163
|
|
|
""" |
164
|
|
|
|
165
|
|
|
:return: |
166
|
|
|
""" |
167
|
|
|
test_button = button_maker( |
168
|
|
|
button_data="button_text", |
169
|
|
|
front_marker="front_", |
170
|
|
|
back_marker="_back", |
171
|
|
|
copy_text_to_callback=True, |
172
|
|
|
) |
173
|
|
|
button = button_maker(button_data=test_button) |
174
|
|
|
assert button == test_button |
175
|
|
|
assert button is test_button |
176
|
|
|
assert isinstance(button, InlineKeyboardButton) |
177
|
|
|
assert button.text == "button_text" |
178
|
|
|
assert button.callback_data == "front_button_text_back" |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
def test_empty_text(): |
182
|
|
|
""" |
183
|
|
|
|
184
|
|
|
:return: |
185
|
|
|
""" |
186
|
|
|
with pytest.raises(Exception) as _: |
187
|
|
|
button_maker(button_data=("", "button_callback_data")) |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
def test_empty_callback_data(): |
191
|
|
|
""" |
192
|
|
|
:return: |
193
|
|
|
""" |
194
|
|
|
with pytest.raises(Exception) as _: |
195
|
|
|
button_maker(button_data=("button_text", ""), copy_text_to_callback=False) |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
def test_big_callback_data(): |
199
|
|
|
""" |
200
|
|
|
:return: |
201
|
|
|
""" |
202
|
|
|
with pytest.raises(Exception) as _: |
203
|
|
|
button_maker(button_data=("button_text", "limit"*13), copy_text_to_callback=False) |
204
|
|
|
|