From 2d0471bb506da3e1c02b25d89d0b8d65baf13424 Mon Sep 17 00:00:00 2001 From: Y_SAKAI Date: Wed, 17 Aug 2022 18:34:01 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=89=8D=E5=9B=9E=E5=8F=96=E5=BE=97?= =?UTF-8?q?=E6=97=A5=E6=99=82=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=87=A6=E7=90=86=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...test_upload_last_fetch_datetime_process.py | 218 ++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 ecs/crm-datafetch/tests/test_upload_last_fetch_datetime_process.py diff --git a/ecs/crm-datafetch/tests/test_upload_last_fetch_datetime_process.py b/ecs/crm-datafetch/tests/test_upload_last_fetch_datetime_process.py new file mode 100644 index 00000000..e04c7943 --- /dev/null +++ b/ecs/crm-datafetch/tests/test_upload_last_fetch_datetime_process.py @@ -0,0 +1,218 @@ +from unittest.mock import MagicMock, patch + +import pytest +from src.config.objects import LastFetchDatetime, TargetObject +from src.error.exceptions import FileUploadException +from src.system_var.constants import UPD_JP_NAME +from src.upload_last_fetch_datetime_process import \ + upload_last_fetch_datetime_process +from src.util.execute_datetime import ExecuteDateTime + +from .test_utils.log_message import generate_log_message_tuple + + +class TestUploadLastFetchDatetimeProcess: + + @pytest.fixture + def bucket_name(self): + return 'test-config-bucket' + + @pytest.fixture + def prepare_bucket(self, s3_client, bucket_name): + s3_client.create_bucket(Bucket=bucket_name) + yield + + def test_run_process_success(self, s3_client, prepare_bucket, bucket_name, monkeypatch, caplog): + """ + Cases: + 前回取得日時ファイル更新処理が正常終了し、期待通りの結果が返ること + Arranges: + - prepare_bucketフィクスチャで、前回取得日時ファイルを置くためのモックバケットを作る + - モックバケットを指すように環境変数を設定する + - オブジェクト情報を準備する + - 最終更新日時インスタンスを準備する + Expects: + - PUTした前回取得日時ファイルが存在し内容が想定と一致する + - 前回取得日時ファイル更新処理の仕様に沿った正常系ログが出力されること(デバッグログは除く) + """ + + # Arranges + + # 環境変数を編集 + monkeypatch.setattr('src.aws.s3.CRM_CONFIG_BUCKET', bucket_name) + monkeypatch.setattr('src.aws.s3.LAST_FETCH_DATE_FOLDER', 'crm/last_fetch_datetime') + + target_objects_dict = { + 'object_name': 'Account', + 'columns': [ + 'Id', + 'Name' + ], + 'is_update_last_fetch_datetime': True + } + + last_fetch_datetime_dict = { + "last_fetch_datetime_from": "1999-01-01T00:00:00.000Z", + "last_fetch_datetime_to": "2100-12-31T23:59:59.000Z", + } + + execute_datetime = ExecuteDateTime() + + target_object = TargetObject(target_objects_dict, execute_datetime) + last_fetch_datetime = LastFetchDatetime(last_fetch_datetime_dict, execute_datetime) + + # Act + upload_last_fetch_datetime_process(target_object, last_fetch_datetime) + + # Assert + # ファイル確認 + actual = s3_client.get_object(Bucket=bucket_name, Key=f'crm/last_fetch_datetime/Account.json') + assert actual['Body'].read().decode( + 'utf-8') == '{"last_fetch_datetime_from": "2100-12-31T23:59:59.000Z", "last_fetch_datetime_to": ""}' + + # ログの確認 + assert generate_log_message_tuple(log_message='I-UPD-01 [Account] の前回取得日時ファイルの更新処理を開始します') in caplog.record_tuples + assert generate_log_message_tuple(log_message='I-UPD-02 [Account] の前回取得日時ファイルの更新処理をスキップします') not in caplog.record_tuples + assert generate_log_message_tuple(log_message=f'I-UPD-04 [Account] の前回取得日時ファイルの更新処理を終了します') in caplog.record_tuples + + def test_run_process_success_skip(self, s3_client, prepare_bucket, bucket_name, monkeypatch, caplog): + """ + Cases: + 前回取得日時ファイル更新処理が正常終了し、期待通りの結果が返ること + Arranges: + - prepare_bucketフィクスチャで、前回取得日時ファイルを置くためのモックバケットを作る + - モックバケットを指すように環境変数を設定する + - ファイルPUT処理のモックを準備する + - オブジェクト情報を準備する + - 最終更新日時インスタンスを準備する + Expects: + - ファイルPUT処理が実行されないこと + - 前回取得日時ファイル更新処理の仕様に沿った正常系ログが出力されること(デバッグログは除く) + """ + + # Arranges + + # 環境変数を編集 + monkeypatch.setattr('src.aws.s3.CRM_CONFIG_BUCKET', bucket_name) + monkeypatch.setattr('src.aws.s3.LAST_FETCH_DATE_FOLDER', 'crm/last_fetch_datetime') + + mock_config_bucket = MagicMock(return_value=None) + + target_objects_dict = { + 'object_name': 'Account', + 'columns': [ + 'Id', + 'Name' + ], + 'is_update_last_fetch_datetime': False + } + + last_fetch_datetime_dict = { + "last_fetch_datetime_from": "1999-01-01T00:00:00.000Z", + "last_fetch_datetime_to": "2100-12-31T23:59:59.000Z", + } + + execute_datetime = ExecuteDateTime() + + target_object = TargetObject(target_objects_dict, execute_datetime) + last_fetch_datetime = LastFetchDatetime(last_fetch_datetime_dict, execute_datetime) + + # Act + with patch('src.aws.s3.ConfigBucket.put_last_fetch_datetime_file', mock_config_bucket): + upload_last_fetch_datetime_process(target_object, last_fetch_datetime) + + # Assert + # 処理実行確認 + assert mock_config_bucket.called is False + + # ログの確認 + assert generate_log_message_tuple(log_message='I-UPD-01 [Account] の前回取得日時ファイルの更新処理を開始します') in caplog.record_tuples + assert generate_log_message_tuple(log_message='I-UPD-02 [Account] の前回取得日時ファイルの更新処理をスキップします') in caplog.record_tuples + assert generate_log_message_tuple(log_message=f'I-UPD-04 [Account] の前回取得日時ファイルの更新処理を終了します') not in caplog.record_tuples + + def test_call_depended_modules(self): + """ + Cases: + 前回取得日時ファイル更新処理内で依存しているモジュールが正しく呼ばれていること + Arranges: + - 前回取得日時ファイル更新処理の依存モジュールをモック化する + - オブジェクト情報を準備する + - 最終更新日時インスタンスを準備する + Expects: + - 依存しているモジュールが正しく呼ばれている + """ + + # Arrange + mock_config_bucket = MagicMock(return_value=None) + + target_objects_dict = { + 'object_name': 'Account', + 'columns': [ + 'Id', + 'Name' + ], + 'is_update_last_fetch_datetime': True + } + + last_fetch_datetime_dict = { + "last_fetch_datetime_from": "1999-01-01T00:00:00.000Z", + "last_fetch_datetime_to": "2100-12-31T23:59:59.000Z", + } + + execute_datetime = ExecuteDateTime() + + target_object = TargetObject(target_objects_dict, execute_datetime) + last_fetch_datetime = LastFetchDatetime(last_fetch_datetime_dict, execute_datetime) + + # Act + with patch('src.aws.s3.ConfigBucket.put_last_fetch_datetime_file', mock_config_bucket): + upload_last_fetch_datetime_process(target_object, last_fetch_datetime) + + # Assert + assert mock_config_bucket.called is True + + def test_raise_put_last_fetch_datetime_file(self, monkeypatch): + """ + Cases: + 前回取得日時ファイルをアップロードできない場合、エラーが発生すること + Arranges: + - 前回取得日時ファイル更新処理で例外を発生させるモックを生成する + - 取得処理実施結果を準備する + - 実行日時取得インスタンスを生成する + Expects: + - 例外が発生する + - ファイルがアップロードできないエラーが返却される + """ + + # Arrange + mock_config_bucket = MagicMock(side_effect=Exception('ファイルアップロードエラー')) + + target_objects_dict = { + 'object_name': 'Account', + 'columns': [ + 'Id', + 'Name' + ], + 'is_update_last_fetch_datetime': True + } + + last_fetch_datetime_dict = { + "last_fetch_datetime_from": "1999-01-01T00:00:00.000Z", + "last_fetch_datetime_to": "2100-12-31T23:59:59.000Z", + } + + execute_datetime = ExecuteDateTime() + + target_object = TargetObject(target_objects_dict, execute_datetime) + last_fetch_datetime = LastFetchDatetime(last_fetch_datetime_dict, execute_datetime) + + # Act + with patch('src.aws.s3.ConfigBucket.put_last_fetch_datetime_file', mock_config_bucket): + with pytest.raises(FileUploadException) as e: + upload_last_fetch_datetime_process(target_object, last_fetch_datetime) + + # Assert + assert mock_config_bucket.called is True + assert e.value.error_id == 'E-UPD-01' + assert e.value.func_name == UPD_JP_NAME + assert e.value.args[0] == f'[Account] 前回処理日時ファイルのアップロードに失敗しました ファイル名:[Account.json] エラー内容:[ファイルアップロードエラー]'