Skip to content

Commit

Permalink
#10 Parametrized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OrBin committed Apr 13, 2019
1 parent 06574b8 commit 9fa5079
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
51 changes: 19 additions & 32 deletions tests/responses/test_preset_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
FLAKY_MAX_RUNS = 6
FLAKY_MIN_PASSES = 1
RESPONSE_PAYLOADS = [None, {}, {'key': 'value'}]
SINGLE_PRESET_TEXT = 'one'
MULTIPLE_PRESET_TEXTS = ['two', 'three']
PAYLOADS_PRESET_RESPONSES_MATRIX = itertools.product(RESPONSE_PAYLOADS,
[SINGLE_PRESET_TEXT, MULTIPLE_PRESET_TEXTS])


def delay_rerun(*args): # pylint: disable=unused-argument
Expand All @@ -24,31 +20,32 @@ def delay_rerun(*args): # pylint: disable=unused-argument
def is_acceptable(str_to_check: str, acceptable_string_or_strings: Union[str, List[str]]):
if isinstance(acceptable_string_or_strings, list):
return str_to_check in acceptable_string_or_strings
else:
return str_to_check == acceptable_string_or_strings

return str_to_check == acceptable_string_or_strings

class TestPresetMessageResponse:

@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES, rerun_filter=delay_rerun)
@pytest.mark.usefixtures('bot', 'bot_chat_id', 'generate_new_update')
@pytest.mark.parametrize('payload, preset_response', PAYLOADS_PRESET_RESPONSES_MATRIX)
def test_with_matrix(self, bot, bot_chat_id, generate_new_update, payload, preset_response):
@pytest.mark.parametrize('payload', RESPONSE_PAYLOADS)
@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES, rerun_filter=delay_rerun)
@pytest.mark.usefixtures('bot', 'bot_chat_id', 'generate_new_update')
class TestPresetResponsesWithParameterMatrices:

SINGLE_PRESET_TEXT = 'one'
MULTIPLE_PRESET_TEXTS = ['two', 'three']
RESPONSE_PRESET_TEXTS = [SINGLE_PRESET_TEXT, MULTIPLE_PRESET_TEXTS]
PAYLOADS_PRESET_RESPONSES_MATRIX = itertools.product(RESPONSE_PAYLOADS, RESPONSE_PRESET_TEXTS)
DOCUMENT_URL = 'https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif'

@pytest.mark.parametrize('preset_response', RESPONSE_PRESET_TEXTS)
def test_preset_message_response(self, bot, bot_chat_id, generate_new_update, payload, preset_response):
response = _PresetMessageResponse(preset_response)
update = generate_new_update(chat_id=bot_chat_id)

message = response.respond(bot, update, payload)
assert message
assert is_acceptable(message.text, preset_response)



class TestPresetReplyResponse:

@pytest.mark.parametrize('payload, preset_response', PAYLOADS_PRESET_RESPONSES_MATRIX)
@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES, rerun_filter=delay_rerun)
@pytest.mark.usefixtures('bot', 'bot_chat_id', 'generate_new_update')
def test_with_matrix(self, bot, bot_chat_id, generate_new_update, payload, preset_response):
@pytest.mark.parametrize('preset_response', RESPONSE_PRESET_TEXTS)
def test_preset_reply_response(self, bot, bot_chat_id, generate_new_update, payload, preset_response):
response = _PresetReplyResponse(preset_response)
message_to_reply_to = bot.send_message(chat_id=bot_chat_id, text='Message to reply to')
update = generate_new_update(chat_id=bot_chat_id, message_id=message_to_reply_to.message_id)
Expand All @@ -58,15 +55,7 @@ def test_with_matrix(self, bot, bot_chat_id, generate_new_update, payload, prese
assert is_acceptable(message.text, preset_response)
assert message.reply_to_message.message_id == message_to_reply_to.message_id


class TestPresetDocumentResponse:

DOCUMENT_URL = 'https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif'

@pytest.mark.parametrize('payload', RESPONSE_PAYLOADS)
@flaky(max_runs=FLAKY_MAX_RUNS, min_passes=FLAKY_MIN_PASSES, rerun_filter=delay_rerun)
@pytest.mark.usefixtures('bot', 'bot_chat_id', 'generate_new_update')
def test_with_matrix(self, bot, bot_chat_id, generate_new_update, payload):
def test_preset_document_response(self, bot, bot_chat_id, generate_new_update, payload):
response = _PresetDocumentResponse(self.DOCUMENT_URL)
update = generate_new_update(chat_id=bot_chat_id)

Expand All @@ -85,13 +74,11 @@ class TestPresetTextResponse:
def test_get_response_text_from_single_preset_text(self):
# _PresetTextResponse is abstract, so we're using _PresetMessageResponse to test it.
response = _PresetMessageResponse(self.SINGLE_PRESET_TEXT)
response_text = response.get_response_text()
assert response_text == self.SINGLE_PRESET_TEXT
assert response.get_response_text() == self.SINGLE_PRESET_TEXT

@pytest.mark.parametrize('preset_index', range(len(MULTIPLE_PRESET_TEXTS)))
@flaky(max_runs=FLAKY_MAX_RUNS_FOR_RANDOMNESS, min_passes=FLAKY_MIN_PASSES_FOR_RANDOMNESS)
def test_get_response_text_from_multiple_preset_texts(self, preset_index):
# _PresetTextResponse is abstract, so we're using _PresetMessageResponse to test it.
response = _PresetMessageResponse(self.MULTIPLE_PRESET_TEXTS)
response_text = response.get_response_text()
assert response_text == self.MULTIPLE_PRESET_TEXTS[preset_index]
assert response.get_response_text() == self.MULTIPLE_PRESET_TEXTS[preset_index]
2 changes: 1 addition & 1 deletion tests/tests.pylintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[basic]
disable=missing-docstring, unsubscriptable-object, no-self-use, unsupported-membership-test
disable=missing-docstring, unsubscriptable-object, no-self-use, unsupported-membership-test, too-many-arguments

[design]
min-public-methods=1
Expand Down

0 comments on commit 9fa5079

Please sign in to comment.