From 06574b85f453a1c3ca6d5a2f09213a791296e0ca Mon Sep 17 00:00:00 2001 From: Or Bin Date: Sat, 13 Apr 2019 18:28:57 +0300 Subject: [PATCH] #10 Parametrized tests --- tests/responses/test_preset_responses.py | 125 ++++++----------------- 1 file changed, 33 insertions(+), 92 deletions(-) diff --git a/tests/responses/test_preset_responses.py b/tests/responses/test_preset_responses.py index 4de1c7e..5f618ca 100644 --- a/tests/responses/test_preset_responses.py +++ b/tests/responses/test_preset_responses.py @@ -1,4 +1,6 @@ +import itertools import time +from typing import Union, List import pytest from flaky import flaky from ...gramhopper.responses.preset_responses import _PresetDocumentResponse, \ @@ -7,6 +9,11 @@ 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 @@ -14,101 +21,52 @@ def delay_rerun(*args): # pylint: disable=unused-argument return True -@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 TestPresetMessageResponse: - - SINGLE_PRESET_TEXT = 'one' - MULTIPLE_PRESET_TEXTS = ['two', 'three'] +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 - def _test_single_preset_message(self, bot, bot_chat_id, generate_new_update, payload): - response = _PresetMessageResponse(self.SINGLE_PRESET_TEXT) - update = generate_new_update(chat_id=bot_chat_id) - message = response.respond(bot, update, payload) - assert message - assert message.text == self.SINGLE_PRESET_TEXT +class TestPresetMessageResponse: - def _test_multiple_preset_messages(self, bot, bot_chat_id, generate_new_update, payload): - response = _PresetMessageResponse(self.MULTIPLE_PRESET_TEXTS) + @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): + response = _PresetMessageResponse(preset_response) update = generate_new_update(chat_id=bot_chat_id) message = response.respond(bot, update, payload) assert message - assert message.text in self.MULTIPLE_PRESET_TEXTS - - def test_single_preset_message_with_none_payload(self, bot, bot_chat_id, generate_new_update): - self._test_single_preset_message(bot, bot_chat_id, generate_new_update, None) + assert is_acceptable(message.text, preset_response) - def test_single_preset_message_with_empty_payload(self, bot, bot_chat_id, generate_new_update): - self._test_single_preset_message(bot, bot_chat_id, generate_new_update, {}) - def test_single_preset_message_with_some_payload(self, bot, bot_chat_id, generate_new_update): - self._test_single_preset_message(bot, bot_chat_id, generate_new_update, {'key': 'value'}) - def test_multiple_preset_messages_with_none_payload(self, bot, bot_chat_id, generate_new_update): - self._test_multiple_preset_messages(bot, bot_chat_id, generate_new_update, None) - - def test_multiple_preset_messages_with_empty_payload(self, bot, bot_chat_id, generate_new_update): - self._test_multiple_preset_messages(bot, bot_chat_id, generate_new_update, {}) - - def test_multiple_preset_messages_with_some_payload(self, bot, bot_chat_id, generate_new_update): - self._test_multiple_preset_messages(bot, bot_chat_id, generate_new_update, {'key': 'value'}) - - -@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 TestPresetReplyResponse: - SINGLE_PRESET_TEXT = 'one' - MULTIPLE_PRESET_TEXTS = ['two', 'three'] - - def _test_single_preset_reply(self, bot, bot_chat_id, generate_new_update, payload): - response = _PresetReplyResponse(self.SINGLE_PRESET_TEXT) + @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): + 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) message = response.respond(bot, update, payload) assert message - assert message.text == self.SINGLE_PRESET_TEXT + assert is_acceptable(message.text, preset_response) assert message.reply_to_message.message_id == message_to_reply_to.message_id - def _test_multiple_preset_replies(self, bot, bot_chat_id, generate_new_update, payload): - response = _PresetReplyResponse(self.MULTIPLE_PRESET_TEXTS) - 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) - 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 - - def test_single_preset_reply_with_none_payload(self, bot, bot_chat_id, generate_new_update): - self._test_single_preset_reply(bot, bot_chat_id, generate_new_update, None) - - def test_single_preset_reply_with_empty_payload(self, bot, bot_chat_id, generate_new_update): - self._test_single_preset_reply(bot, bot_chat_id, generate_new_update, {}) - - def test_single_preset_reply_with_some_payload(self, bot, bot_chat_id, generate_new_update): - self._test_single_preset_reply(bot, bot_chat_id, generate_new_update, {'key': 'value'}) - - def test_multiple_preset_replies_with_none_payload(self, bot, bot_chat_id, generate_new_update): - self._test_multiple_preset_replies(bot, bot_chat_id, generate_new_update, None) - - def test_multiple_preset_replies_with_empty_payload(self, bot, bot_chat_id, generate_new_update): - self._test_multiple_preset_replies(bot, bot_chat_id, generate_new_update, {}) - - def test_multiple_preset_replies_with_some_payload(self, bot, bot_chat_id, generate_new_update): - self._test_multiple_preset_replies(bot, bot_chat_id, generate_new_update, {'key': 'value'}) - - -@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 TestPresetDocumentResponse: DOCUMENT_URL = 'https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif' - def _test_preset_document_url(self, bot, bot_chat_id, generate_new_update, payload): + @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): response = _PresetDocumentResponse(self.DOCUMENT_URL) update = generate_new_update(chat_id=bot_chat_id) @@ -116,15 +74,6 @@ def _test_preset_document_url(self, bot, bot_chat_id, generate_new_update, paylo assert message assert message.document - def test_single_preset_reply_with_none_payload(self, bot, bot_chat_id, generate_new_update): - self._test_preset_document_url(bot, bot_chat_id, generate_new_update, None) - - def test_single_preset_reply_with_empty_payload(self, bot, bot_chat_id, generate_new_update): - self._test_preset_document_url(bot, bot_chat_id, generate_new_update, {}) - - def test_single_preset_reply_with_some_payload(self, bot, bot_chat_id, generate_new_update): - self._test_preset_document_url(bot, bot_chat_id, generate_new_update, {'key': 'value'}) - class TestPresetTextResponse: @@ -139,18 +88,10 @@ def test_get_response_text_from_single_preset_text(self): response_text = response.get_response_text() assert response_text == self.SINGLE_PRESET_TEXT - def _test_from_multiple_texts(self, response, preset_index): - response_text = response.get_response_text() - assert response_text == self.MULTIPLE_PRESET_TEXTS[preset_index] - - @flaky(max_runs=FLAKY_MAX_RUNS_FOR_RANDOMNESS, min_passes=FLAKY_MIN_PASSES_FOR_RANDOMNESS) - def test_get_response_text_from_multiple_preset_texts_first(self): - # _PresetTextResponse is abstract, so we're using _PresetMessageResponse to test it. - response = _PresetMessageResponse(self.MULTIPLE_PRESET_TEXTS) - self._test_from_multiple_texts(response, 0) - + @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_second(self): + 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) - self._test_from_multiple_texts(response, 1) + response_text = response.get_response_text() + assert response_text == self.MULTIPLE_PRESET_TEXTS[preset_index]