Skip to content

Commit 30d8c09

Browse files
author
Frederic Massart
committed
Slightly increasing test coverage to massage coverall
1 parent 8458a3f commit 30d8c09

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

telegrambot/test/testcases.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def _test_message_ok(self, action, update=None, number=1):
8484
self.assertBotResponse(mock_send, action)
8585
self.assertEqual(number, Update.objects.count())
8686
self.assertUpdate(Update.objects.get(update_id=update.update_id), update)
87-
87+
8888
def _test_message_no_handler(self, action, update=None, number=1):
8989
if not update:
9090
update = self.update
@@ -96,4 +96,13 @@ def _test_message_no_handler(self, action, update=None, number=1):
9696
self.assertEqual(response.status_code, status.HTTP_200_OK)
9797
self.assertEqual(0, mock_send.call_count)
9898
self.assertEqual(number, Update.objects.count())
99-
self.assertUpdate(Update.objects.get(update_id=update.update_id), update)
99+
self.assertUpdate(Update.objects.get(update_id=update.update_id), update)
100+
101+
def _test_no_response(self, action, update=None):
102+
if not update:
103+
update = self.update
104+
with mock.patch("telegram.bot.Bot.sendMessage", callable=mock.MagicMock()) as mock_send:
105+
update.message.text = action['in']
106+
response = self.client.post(self.webhook_url, update.to_json(), **self.kwargs)
107+
self.assertEqual(response.status_code, status.HTTP_200_OK)
108+
self.assertEqual(0, mock_send.call_count)

tests/bot_handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from tests.commands_views import StartView, AuthorCommandView, AuthorInverseListView, AuthorCommandQueryView, \
2-
UnknownView, AuthorName, MessageView
2+
UnknownView, AuthorName, MessageView, MissingTemplateView
33
from telegrambot.handlers import command, unknown_command, regex, message
44
from telegrambot.bot_views.decorators import login_required
55
from telegrambot.bot_views.generic import SendMessageCommandView, EchoCommandView, HelloWorldCommandView
@@ -13,6 +13,7 @@
1313
command('author', AuthorCommandView.as_command_view()),
1414
command('hello', HelloWorldCommandView.as_command_view()),
1515
command('how_are_you', SendMessageCommandView.as_command_view(message='Good, thanks!')),
16+
command('missing', MissingTemplateView.as_command_view()),
1617
regex(r'^Echo', EchoCommandView.as_command_view()),
1718
unknown_command(UnknownView.as_command_view()),
1819
message(MessageView.as_command_view())

tests/commands_views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ class AuthorName(DetailCommandView):
4848

4949
def get_slug(self, **kwargs):
5050
return kwargs.get('name', None)
51+
52+
class MissingTemplateView(TemplateCommandView):
53+
template_text = "i/dont/exist.txt"

tests/test_telegrambot.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ class TestBotCommands(testcases.BaseTestBot):
128128
'text': 'Good, thanks!'}
129129
}
130130

131+
template_missing = {'in': '/missing',
132+
'out': None
133+
}
134+
131135
def test_start(self):
132136
self._test_message_ok(self.start)
133137

@@ -177,6 +181,9 @@ def test_hello_world(self):
177181
def test_message(self):
178182
self._test_message_ok(self.message)
179183

184+
def test_missing_template(self):
185+
self._test_no_response(self.template_missing)
186+
180187

181188
class TestBotMessage(testcases.BaseTestBot):
182189

0 commit comments

Comments
 (0)