Skip to content

Commit

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

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


class _PresetMessageResponse(_PresetTextResponse):
Expand All @@ -58,7 +58,7 @@ def respond(self, bot: Bot, update: Update, response_payload: dict):
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):
ResponseHelper.reply(bot, update, self.get_response_text())
return ResponseHelper.reply(bot, update, self.get_response_text())


class PresetResponses(DictEnum):
Expand Down
124 changes: 111 additions & 13 deletions tests/responses/test_preset_responses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest
from flaky import flaky
from ...gramhopper.responses.preset_responses import _PresetTextResponse, \
_PresetDocumentResponse, _PresetMessageResponse, _PresetReplyResponse

FLAKY_MAX_RUNS = 5
FLAKY_MIN_PASSES = 1

@pytest.mark.usefixtures('bot')
@pytest.mark.usefixtures('chat_id')
Expand All @@ -10,26 +13,121 @@ 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):
def _test_single_preset_message(self, bot, chat_id, generate_new_update, payload):
response = _PresetMessageResponse(self.SINGLE_PRESET_TEXT)
update = generate_new_update(chat_id=chat_id)

message = response.respond(bot, update, payload)
assert message
assert message.text == self.SINGLE_PRESET_TEXT

def _test_multiple_preset_messages(self, bot, chat_id, generate_new_update, payload):
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 == self.SINGLE_PRESET_TEXT
message = response.respond(bot, update, payload)
assert message
assert message.text in self.MULTIPLE_PRESET_TEXTS

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_messages(self, bot, chat_id, generate_new_update):
response = _PresetMessageResponse(self.MULTIPLE_PRESET_TEXTS)
def test_single_preset_message_with_none_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_message(bot, chat_id, generate_new_update, None)

update = generate_new_update(chat_id=chat_id)
@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_single_preset_message_with_empty_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_message(bot, chat_id, generate_new_update, {})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_single_preset_message_with_some_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_message(bot, chat_id, generate_new_update, {'key': 'value'})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_messages_with_none_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_message(bot, chat_id, generate_new_update, None)

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_messages_with_empty_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_message(bot, chat_id, generate_new_update, {})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_messages_with_some_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_message(bot, chat_id, generate_new_update, {'key': 'value'})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_messages(self, bot, chat_id, generate_new_update):
possible_values_for_payload = [None, {}, {'key': 'value'}]
for payload in possible_values_for_payload:
self._test_multiple_preset_messages(bot, chat_id, generate_new_update, payload)


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

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

def _test_single_preset_reply(self, bot, chat_id, generate_new_update, payload):
response = _PresetReplyResponse(self.SINGLE_PRESET_TEXT)
message_to_reply_to = bot.send_message(chat_id=chat_id, text='Message to reply to')
update = generate_new_update(chat_id=chat_id, message_id=message_to_reply_to.message_id)

message = response.respond(bot, update, payload)
assert message
assert message.text == self.SINGLE_PRESET_TEXT
assert message.reply_to_message.message_id == message_to_reply_to.message_id

def _test_multiple_preset_replies(self, bot, chat_id, generate_new_update, payload):
response = _PresetReplyResponse(self.MULTIPLE_PRESET_TEXTS)
message_to_reply_to = bot.send_message(chat_id=chat_id, text='Message to reply to')
update = generate_new_update(chat_id=chat_id, message_id=message_to_reply_to.message_id)

message = response.respond(bot, update, payload)
assert message
assert message.text in self.MULTIPLE_PRESET_TEXTS
assert message.reply_to_message.message_id == message_to_reply_to.message_id

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_single_preset_reply_with_none_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_reply(bot, chat_id, generate_new_update, None)


@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_single_preset_reply_with_empty_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_reply(bot, chat_id, generate_new_update, {})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_single_preset_reply_with_some_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_reply(bot, chat_id, generate_new_update, {'key': 'value'})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_replies_with_none_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_reply(bot, chat_id, generate_new_update, None)

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_replies_with_empty_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_reply(bot, chat_id, generate_new_update, {})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_replies_with_some_payload(self, bot, chat_id, generate_new_update):
self._test_single_preset_reply(bot, chat_id, generate_new_update, {'key': 'value'})

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES)
@pytest.mark.usefixtures('generate_new_update')
def test_multiple_preset_replies(self, bot, chat_id, generate_new_update):
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
self._test_multiple_preset_replies(bot, chat_id, generate_new_update, payload)

0 comments on commit 6ab15da

Please sign in to comment.