From cbffd5cd2c3dc3152ca97e39ae828c5158030f68 Mon Sep 17 00:00:00 2001 From: "shimoda.m@nds-tyo.co.jp" Date: Wed, 17 Aug 2022 13:28:59 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E3=83=91=E3=83=A9=E3=83=A1?= =?UTF-8?q?=E3=82=BF=E5=8C=96=E3=83=86=E3=82=B9=E3=83=88=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/test_fetch_crm_data_process.py | 148 ++++-------------- 1 file changed, 30 insertions(+), 118 deletions(-) diff --git a/ecs/crm-datafetch/tests/test_fetch_crm_data_process.py b/ecs/crm-datafetch/tests/test_fetch_crm_data_process.py index 3ce7b43f..3145ba32 100644 --- a/ecs/crm-datafetch/tests/test_fetch_crm_data_process.py +++ b/ecs/crm-datafetch/tests/test_fetch_crm_data_process.py @@ -205,12 +205,20 @@ class TestFetchCrmDataProcess: assert e.value.func_name == FETCH_JP_NAME assert e.value.args[0] == f'[Account] のレコード取得に失敗しました エラー内容:[生成エラー]' - def test_raise_fetch_sf_count_connection_timeout_with_retry_success(self, monkeypatch, caplog): + @pytest.mark.parametrize('timeout_env_name, exception, expect_message', [ + ('CRM_AUTH_TIMEOUT', ConnectTimeout('接続タイムアウト'), 'W-FETCH-01 CRMの接続処理がタイムアウトしため、リトライします:[1] エラー内容:[接続タイムアウト]'), + ('CRM_GET_RECORD_COUNT_TIMEOUT', ReadTimeout('読み取りタイムアウト'), 'W-FETCH-02 [Account] の件数取得処理がタイムアウトしたため、リトライします:[1] エラー内容:[読み取りタイムアウト]'), + ('CRM_AUTH_TIMEOUT', Exception('予期せぬ例外'), 'W-FETCH-03 [Account] の件数取得に失敗したため、リトライします エラー内容:[予期せぬ例外]'), + ], ids=['connection_timeout', 'read_timeout', 'unexpected_exception']) + def test_raise_fetch_sf_count_with_retry_success(self, monkeypatch, caplog, timeout_env_name, exception, expect_message): """ Cases: - データ件数取得処理で接続タイムアウト例外が発生した場合、リトライした結果復旧し、正常終了すること + 1. データ件数取得処理で接続タイムアウト例外が発生した場合、リトライした結果復旧し、正常終了すること + 2. データ件数取得処理で読み取りタイムアウト例外が発生した場合、リトライした結果復旧し、正常終了すること + 3. データ件数取得処理で予期せぬ例外が発生した場合、リトライした結果復旧し、正常終了すること Arranges: - データ件数取得処理の最大リトライ試行回数を3に設定する + - timeout_env_nameに指定されたリトライタイムアウト時間の秒数を1に設定する - データ件数取得処理の初回に接続タイムアウト例外が発生するようにする Expects: - 正常終了する @@ -221,7 +229,7 @@ class TestFetchCrmDataProcess: monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MAX_INTERVAL', 1) monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MIN_INTERVAL', 1) monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_AUTH_TIMEOUT', 1) + monkeypatch.setattr(f'src.fetch_crm_data_process.{timeout_env_name}', 1) # Arrange with patch('src.fetch_crm_data_process.CounterObject', ) as mock_counter, \ patch('src.fetch_crm_data_process.SOQLBuilder') as mock_soql_builder, \ @@ -234,7 +242,7 @@ class TestFetchCrmDataProcess: mock_builder_inst.create_count_soql.return_value = '' mock_builder_inst.create_fetch_soql.return_value = '' mock_client_inst = mock_api_client.return_value - mock_client_inst.fetch_sf_count.side_effect = [ConnectTimeout('接続タイムアウト'), len(common_expect)] + mock_client_inst.fetch_sf_count.side_effect = [exception, len(common_expect)] mock_client_inst.fetch_sf_data.return_value = common_expect # Act fetch_crm_data_process(common_target_object, common_last_fetch_datetime) @@ -244,31 +252,38 @@ class TestFetchCrmDataProcess: assert mock_counter_inst.describe.call_count == 1 assert mock_counter_inst.increment.call_count == 1 - retry_log_message = 'W-FETCH-01 CRMの接続処理がタイムアウトしため、リトライします:[1] エラー内容:[接続タイムアウト]' assert generate_log_message_tuple( log_level=logging.WARNING, - log_message=retry_log_message) in caplog.record_tuples - called_log_counts = len([log for log in caplog.messages if log == retry_log_message]) + log_message=expect_message) in caplog.record_tuples + called_log_counts = len([log for log in caplog.messages if log == expect_message]) assert called_log_counts == 1 assert generate_log_message_tuple(log_message='I-FETCH-06 [Account] のCRMからのデータ取得処理を終了します') in caplog.record_tuples - def test_raise_fetch_sf_count_connection_timeout_with_retry_fail(self, monkeypatch, caplog): + @pytest.mark.parametrize('timeout_env_name, exception, expect_message', [ + ('CRM_AUTH_TIMEOUT', ConnectTimeout('接続タイムアウト'), 'W-FETCH-01 CRMの接続処理がタイムアウトしため、リトライします:[1] エラー内容:[接続タイムアウト]'), + ('CRM_GET_RECORD_COUNT_TIMEOUT', ReadTimeout('読み取りタイムアウト'), 'W-FETCH-02 [Account] の件数取得処理がタイムアウトしたため、リトライします:[1] エラー内容:[読み取りタイムアウト]'), + ('CRM_AUTH_TIMEOUT', Exception('予期せぬ例外'), 'W-FETCH-03 [Account] の件数取得に失敗したため、リトライします エラー内容:[予期せぬ例外]'), + ], ids=['connection_timeout', 'read_timeout', 'unexpected_exception']) + def test_raise_fetch_sf_count_with_retry_fail(self, monkeypatch, caplog, timeout_env_name, exception, expect_message): """ Cases: - データ件数取得処理で接続タイムアウト例外が発生した場合、リトライした結果復旧せず、異常終了すること + 1. データ件数取得処理で接続タイムアウト例外が発生した場合、リトライした結果復旧せず、異常終了すること + 2. データ件数取得処理で読み取りタイムアウト例外が発生した場合、リトライした結果復旧せず、異常終了すること + 3. データ件数取得処理で予期せぬ例外が発生した場合、リトライした結果復旧せず、異常終了すること Arranges: - データ件数取得処理の最大リトライ試行回数を3に設定する + - timeout_env_nameに指定されたリトライタイムアウト時間の秒数を1に設定する - データ件数取得処理の1回目、2回目、3回目で接続タイムアウト例外が発生するようにする Expects: - - 正常終了する - - データ件数取得に失敗した胸のエラーが出力されない + - 異常終了する + - データ件数取得に失敗した旨のエラーが出力される """ monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_MAX_RETRY_ATTEMPT', 3) monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MAX_INTERVAL', 1) monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MIN_INTERVAL', 1) monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_AUTH_TIMEOUT', 1) + monkeypatch.setattr(f'src.fetch_crm_data_process.{timeout_env_name}', 1) # Arrange with patch('src.fetch_crm_data_process.CounterObject', ) as mock_counter, \ patch('src.fetch_crm_data_process.SOQLBuilder') as mock_soql_builder, \ @@ -281,7 +296,7 @@ class TestFetchCrmDataProcess: mock_builder_inst.create_count_soql.return_value = '' mock_builder_inst.create_fetch_soql.return_value = '' mock_client_inst = mock_api_client.return_value - mock_client_inst.fetch_sf_count.side_effect = [ConnectTimeout('接続タイムアウト'), ConnectTimeout('接続タイムアウト'), ConnectTimeout('接続タイムアウト')] + mock_client_inst.fetch_sf_count.side_effect = [exception, exception, exception] mock_client_inst.fetch_sf_data.return_value = common_expect # Act with pytest.raises(SalesforceAPIException) as e: @@ -294,113 +309,10 @@ class TestFetchCrmDataProcess: # 足し込みは2回のみ assert mock_counter_inst.increment.call_count == 2 - retry_log_message = 'W-FETCH-01 CRMの接続処理がタイムアウトしため、リトライします:[1] エラー内容:[接続タイムアウト]' assert generate_log_message_tuple( log_level=logging.WARNING, - log_message=retry_log_message) in caplog.record_tuples - called_log_counts = len([log for log in caplog.messages if log == retry_log_message]) - assert called_log_counts == 2 - assert generate_log_message_tuple(log_message='I-FETCH-06 [Account] のCRMからのデータ取得処理を終了します') not in caplog.record_tuples - - assert e.value.error_id == 'E-FETCH-01' - assert e.value.func_name == FETCH_JP_NAME - # リトライ例外のオブジェクトIDが違うため、in句で比較 - assert f'[Account] の件数取得に失敗しました エラー内容:[RetryError' in e.value.args[0] - - def test_raise_fetch_sf_count_read_timeout_with_retry_success(self, monkeypatch, caplog): - """ - Cases: - データ件数取得処理で読み取り例外が発生した場合、リトライした結果復旧し、正常終了すること - Arranges: - - データ件数取得処理の最大リトライ試行回数を3に設定する - - データ件数取得処理の初回に読み取り例外が発生するようにする - Expects: - - 正常終了する - - データ件数取得に失敗した胸のエラーが出力されない - """ - - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_MAX_RETRY_ATTEMPT', 3) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MAX_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MIN_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_TIMEOUT', 1) - # Arrange - with patch('src.fetch_crm_data_process.CounterObject', ) as mock_counter, \ - patch('src.fetch_crm_data_process.SOQLBuilder') as mock_soql_builder, \ - patch('src.fetch_crm_data_process.SalesforceApiClient') as mock_api_client: - # モック化 - mock_counter_inst = mock_counter.return_value - mock_counter_inst.describe.side_effect = [1, 2, 3] - mock_counter_inst.increment.side_effect = [2, 3, 4] - mock_builder_inst = mock_soql_builder.return_value - mock_builder_inst.create_count_soql.return_value = '' - mock_builder_inst.create_fetch_soql.return_value = '' - mock_client_inst = mock_api_client.return_value - mock_client_inst.fetch_sf_count.side_effect = [ReadTimeout('読み取りタイムアウト'), len(common_expect)] - mock_client_inst.fetch_sf_data.return_value = common_expect - # Act - fetch_crm_data_process(common_target_object, common_last_fetch_datetime) - - # Assert - - assert mock_counter_inst.describe.call_count == 1 - assert mock_counter_inst.increment.call_count == 1 - - retry_log_message = 'W-FETCH-02 [Account] の件数取得処理がタイムアウトしたため、リトライします:[1] エラー内容:[読み取りタイムアウト]' - assert generate_log_message_tuple( - log_level=logging.WARNING, - log_message=retry_log_message) in caplog.record_tuples - called_log_counts = len([log for log in caplog.messages if log == retry_log_message]) - assert called_log_counts == 1 - assert generate_log_message_tuple(log_message='I-FETCH-06 [Account] のCRMからのデータ取得処理を終了します') in caplog.record_tuples - - def test_raise_fetch_sf_count_read_timeout_with_retry_fail(self, monkeypatch, caplog): - """ - Cases: - データ件数取得処理で読み取り例外が発生した場合、リトライした結果復旧せず、異常終了すること - Arranges: - - データ件数取得処理の最大リトライ試行回数を3に設定する - - データ件数取得処理の1回目、2回目、3回目で読み取り例外が発生するようにする - Expects: - - 正常終了する - - データ件数取得に失敗した胸のエラーが出力されない - """ - - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_MAX_RETRY_ATTEMPT', 3) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MAX_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_MIN_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_RETRY_INTERVAL', 1) - monkeypatch.setattr('src.fetch_crm_data_process.CRM_GET_RECORD_COUNT_TIMEOUT', 1) - # Arrange - with patch('src.fetch_crm_data_process.CounterObject', ) as mock_counter, \ - patch('src.fetch_crm_data_process.SOQLBuilder') as mock_soql_builder, \ - patch('src.fetch_crm_data_process.SalesforceApiClient') as mock_api_client: - # モック化 - mock_counter_inst = mock_counter.return_value - mock_counter_inst.describe.side_effect = [1, 2, 3] - mock_counter_inst.increment.side_effect = [2, 3, 4] - mock_builder_inst = mock_soql_builder.return_value - mock_builder_inst.create_count_soql.return_value = '' - mock_builder_inst.create_fetch_soql.return_value = '' - mock_client_inst = mock_api_client.return_value - mock_client_inst.fetch_sf_count.side_effect = [ReadTimeout('読み取りタイムアウト'), ReadTimeout('読み取りタイムアウト'), ReadTimeout('読み取りタイムアウト')] - mock_client_inst.fetch_sf_data.return_value = common_expect - # Act - with pytest.raises(SalesforceAPIException) as e: - fetch_crm_data_process(common_target_object, common_last_fetch_datetime) - - # Assert - - # 取得は3回行われる - assert mock_counter_inst.describe.call_count == 3 - # 足し込みは2回のみ - assert mock_counter_inst.increment.call_count == 2 - - retry_log_message = 'W-FETCH-02 [Account] の件数取得処理がタイムアウトしたため、リトライします:[1] エラー内容:[読み取りタイムアウト]' - assert generate_log_message_tuple( - log_level=logging.WARNING, - log_message=retry_log_message) in caplog.record_tuples - called_log_counts = len([log for log in caplog.messages if log == retry_log_message]) + log_message=expect_message) in caplog.record_tuples + called_log_counts = len([log for log in caplog.messages if log == expect_message]) assert called_log_counts == 2 assert generate_log_message_tuple(log_message='I-FETCH-06 [Account] のCRMからのデータ取得処理を終了します') not in caplog.record_tuples