Skip to content

Commit

Permalink
#10 Created tests for _PresetMessageResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
OrBin committed Apr 10, 2019
1 parent 41ee4d7 commit 163661f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
10 changes: 5 additions & 5 deletions gramhopper/responses/preset_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, preset_response: Union[str, List[str]]):
self.preset_responses = preset_response

@abc.abstractmethod
def respond(self, bot: Bot, update: Update, response_payload: dict) -> None:
def respond(self, bot: Bot, update: Update, response_payload: dict):
pass

def get_response_text(self):
Expand All @@ -44,20 +44,20 @@ def __init__(self, preset_response: Union[str, Document]):
"""
self.preset_response = preset_response

def respond(self, bot: Bot, update: Update, response_payload: dict) -> None:
def respond(self, bot: Bot, update: Update, response_payload: dict):
ResponseHelper.document(bot, update, self.preset_response)


class _PresetMessageResponse(_PresetTextResponse):
"""A preset response in which the response method is a normal message"""

def respond(self, bot: Bot, update: Update, response_payload: dict) -> None:
ResponseHelper.message(bot, update, self.get_response_text())
def respond(self, bot: Bot, update: Update, response_payload: dict):
return ResponseHelper.message(bot, update, self.get_response_text())


class _PresetReplyResponse(_PresetTextResponse):
"""A preset response in which the response method is a reply to the triggering message"""
def respond(self, bot: Bot, update: Update, response_payload: dict) -> None:
def respond(self, bot: Bot, update: Update, response_payload: dict):
ResponseHelper.reply(bot, update, self.get_response_text())


Expand Down
10 changes: 5 additions & 5 deletions gramhopper/responses/response_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
class ResponseHelper:
@staticmethod
def reply(bot: Bot, update: Update, response: Any):
bot.send_message(chat_id=update.effective_chat.id,
text=response,
reply_to_message_id=update.message.message_id)
return bot.send_message(chat_id=update.effective_chat.id,
text=response,
reply_to_message_id=update.message.message_id)

@staticmethod
def message(bot: Bot, update: Update, response: Any):
bot.send_message(chat_id=update.effective_chat.id, text=response)
return bot.send_message(chat_id=update.effective_chat.id, text=response)

@staticmethod
def document(bot: Bot, update: Update, response: Any):
bot.send_document(update.effective_chat.id, response)
return bot.send_document(update.effective_chat.id, response)
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def bot() -> Bot:
return Bot(_get_bot_parameter('token'))

@fixture(scope='module')
def bot() -> str:
def chat_id() -> str:
"""
Returns a chat id from 'CHAT_ID' environment variable if exists,
or the default one from PUBLIC_TEST_BOT_PARAMETERS otherwise.
Expand Down
Empty file added tests/responses/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions tests/responses/test_preset_responses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
from ...gramhopper.responses.preset_responses import _PresetTextResponse, \
_PresetDocumentResponse, _PresetMessageResponse, _PresetReplyResponse


@pytest.mark.usefixtures('bot')
@pytest.mark.usefixtures('chat_id')
class TestPresetMessageResponse:

SINGLE_PRESET_TEXT = 'one'
MULTIPLE_PRESET_TEXTS = ['two', 'three']

@pytest.mark.usefixtures('generate_new_update')
def test_single_preset_message(self, bot, chat_id, generate_new_update):
response = _PresetMessageResponse(self.SINGLE_PRESET_TEXT)

update = generate_new_update(chat_id=chat_id)
possible_values_for_payload = [None, {}, {'key': 'value'}]

for payload in possible_values_for_payload:
message = response.respond(bot, update, payload)
assert message
assert message.text == self.SINGLE_PRESET_TEXT

@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_messages(self, bot, chat_id, generate_new_update):
response = _PresetMessageResponse(self.MULTIPLE_PRESET_TEXTS)

update = generate_new_update(chat_id=chat_id)
possible_values_for_payload = [None, {}, {'key': 'value'}]

for payload in possible_values_for_payload:
message = response.respond(bot, update, payload)
assert message
assert message.text in self.MULTIPLE_PRESET_TEXTS

0 comments on commit 163661f

Please sign in to comment.