refactor: パラメタ化テストにした

This commit is contained in:
shimoda.m@nds-tyo.co.jp 2022-08-17 13:28:59 +09:00
parent 9504b8ce50
commit cbffd5cd2c

View File

@ -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