16
16
#
17
17
# You should have received a copy of the GNU Lesser Public License
18
18
# along with this program. If not, see [http://www.gnu.org/licenses/].
19
- import logging
20
19
import time
21
20
import datetime as dtm
22
21
from pathlib import Path
49
48
Chat ,
50
49
)
51
50
from telegram .constants import MAX_INLINE_QUERY_RESULTS
51
+ from telegram .ext import Bot as ExtBot
52
52
from telegram .error import BadRequest , InvalidToken , NetworkError , RetryAfter
53
+ from telegram .ext .utils .callbackdatacache import InvalidCallbackData
53
54
from telegram .utils .helpers import (
54
55
from_timestamp ,
55
56
escape_markdown ,
@@ -103,6 +104,10 @@ def inline_results():
103
104
104
105
105
106
class TestBot :
107
+ """
108
+ Most are executed on tg.ext.Bot, as that class only extends the functionality of tg.bot
109
+ """
110
+
106
111
@pytest .mark .parametrize (
107
112
'token' ,
108
113
argvalues = [
@@ -124,7 +129,7 @@ def test_invalid_token(self, token):
124
129
[(True , 1024 , True ), (False , 1024 , False ), (0 , 0 , True ), (None , None , True )],
125
130
)
126
131
def test_callback_data_maxsize (self , bot , acd_in , maxsize , acd ):
127
- bot = Bot (bot .token , arbitrary_callback_data = acd_in )
132
+ bot = ExtBot (bot .token , arbitrary_callback_data = acd_in )
128
133
assert bot .arbitrary_callback_data == acd
129
134
assert bot .callback_data .maxsize == maxsize
130
135
@@ -1110,7 +1115,7 @@ def test_get_updates(self, bot):
1110
1115
if updates :
1111
1116
assert isinstance (updates [0 ], Update )
1112
1117
1113
- def test_get_updates_invalid_callback_data (self , bot , monkeypatch , caplog ):
1118
+ def test_get_updates_invalid_callback_data (self , bot , monkeypatch ):
1114
1119
def post (* args , ** kwargs ):
1115
1120
return [
1116
1121
Update (
@@ -1135,15 +1140,11 @@ def post(*args, **kwargs):
1135
1140
try :
1136
1141
monkeypatch .setattr (bot .request , 'post' , post )
1137
1142
bot .delete_webhook () # make sure there is no webhook set if webhook tests failed
1138
- with caplog .at_level (logging .DEBUG ):
1139
- updates = bot .get_updates (timeout = 1 )
1143
+ updates = bot .get_updates (timeout = 1 )
1140
1144
1141
- assert any (
1142
- "Skipping CallbackQuery with invalid data: {'update_id': 17" in record .getMessage ()
1143
- for record in caplog .records
1144
- )
1145
1145
assert isinstance (updates , list )
1146
- assert len (updates ) == 0
1146
+ assert len (updates ) == 1
1147
+ assert isinstance (updates [0 ].callback_query .data , InvalidCallbackData )
1147
1148
1148
1149
finally :
1149
1150
# Reset b/c bots scope is session
@@ -1881,10 +1882,15 @@ def test_replace_callback_data_send_message(self, bot, chat_id):
1881
1882
1882
1883
assert inline_keyboard [0 ][1 ] == no_replace_button
1883
1884
assert inline_keyboard [0 ][0 ] != replace_button
1884
- assert bot .callback_data .pop (inline_keyboard [0 ][0 ].callback_data ) == 'replace_test'
1885
+ keyboard , button = (
1886
+ inline_keyboard [0 ][0 ].callback_data [:32 ],
1887
+ inline_keyboard [0 ][0 ].callback_data [32 :],
1888
+ )
1889
+ assert bot .callback_data ._keyboard_data [keyboard ].button_data [button ] == 'replace_test'
1885
1890
finally :
1886
1891
bot .arbitrary_callback_data = False
1887
- bot .callback_data .clear ()
1892
+ bot .callback_data .clear_callback_data ()
1893
+ bot .callback_data .clear_callback_queries ()
1888
1894
1889
1895
def test_replace_callback_data_stop_poll (self , bot , chat_id ):
1890
1896
poll_message = bot .send_poll (chat_id = chat_id , question = 'test' , options = ['1' , '2' ])
@@ -1907,10 +1913,15 @@ def test_replace_callback_data_stop_poll(self, bot, chat_id):
1907
1913
1908
1914
assert inline_keyboard [0 ][1 ] == no_replace_button
1909
1915
assert inline_keyboard [0 ][0 ] != replace_button
1910
- assert bot .callback_data .pop (inline_keyboard [0 ][0 ].callback_data ) == 'replace_test'
1916
+ keyboard , button = (
1917
+ inline_keyboard [0 ][0 ].callback_data [:32 ],
1918
+ inline_keyboard [0 ][0 ].callback_data [32 :],
1919
+ )
1920
+ assert bot .callback_data ._keyboard_data [keyboard ].button_data [button ] == 'replace_test'
1911
1921
finally :
1912
1922
bot .arbitrary_callback_data = False
1913
- bot .callback_data .clear ()
1923
+ bot .callback_data .clear_callback_data ()
1924
+ bot .callback_data .clear_callback_queries ()
1914
1925
1915
1926
def test_replace_callback_data_copy_message (self , bot , chat_id ):
1916
1927
original_message = bot .send_message (chat_id = chat_id , text = 'original' )
@@ -1932,13 +1943,15 @@ def test_replace_callback_data_copy_message(self, bot, chat_id):
1932
1943
)
1933
1944
message = helper_message .reply_to_message
1934
1945
inline_keyboard = message .reply_markup .inline_keyboard
1935
-
1936
- assert inline_keyboard [0 ][1 ] == no_replace_button
1937
- assert inline_keyboard [0 ][0 ] != replace_button
1938
- assert bot .callback_data .pop (inline_keyboard [0 ][0 ].callback_data ) == 'replace_test'
1946
+ keyboard , button = (
1947
+ inline_keyboard [0 ][0 ].callback_data [:32 ],
1948
+ inline_keyboard [0 ][0 ].callback_data [32 :],
1949
+ )
1950
+ assert bot .callback_data ._keyboard_data [keyboard ].button_data [button ] == 'replace_test'
1939
1951
finally :
1940
1952
bot .arbitrary_callback_data = False
1941
- bot .callback_data .clear ()
1953
+ bot .callback_data .clear_callback_data ()
1954
+ bot .callback_data .clear_callback_queries ()
1942
1955
1943
1956
# TODO: Needs improvement. We need incoming inline query to test answer.
1944
1957
def test_replace_callback_data_answer_inline_query (self , monkeypatch , bot , chat_id ):
@@ -1954,8 +1967,12 @@ def make_assertion(
1954
1967
).inline_keyboard
1955
1968
assertion_1 = inline_keyboard [0 ][1 ] == no_replace_button
1956
1969
assertion_2 = inline_keyboard [0 ][0 ] != replace_button
1970
+ keyboard , button = (
1971
+ inline_keyboard [0 ][0 ].callback_data [:32 ],
1972
+ inline_keyboard [0 ][0 ].callback_data [32 :],
1973
+ )
1957
1974
assertion_3 = (
1958
- bot .callback_data .pop ( inline_keyboard [ 0 ][ 0 ]. callback_data ) == 'replace_test'
1975
+ bot .callback_data ._keyboard_data [ keyboard ]. button_data [ button ] == 'replace_test'
1959
1976
)
1960
1977
return assertion_1 and assertion_2 and assertion_3
1961
1978
@@ -1984,4 +2001,5 @@ def make_assertion(
1984
2001
1985
2002
finally :
1986
2003
bot .arbitrary_callback_data = False
1987
- bot .callback_data .clear ()
2004
+ bot .callback_data .clear_callback_data ()
2005
+ bot .callback_data .clear_callback_queries ()
0 commit comments