From d60d1a65364903d8dd53078f4a9a3fd76d2baf61 Mon Sep 17 00:00:00 2001 From: "shimoda.m@nds-tyo.co.jp" Date: Wed, 17 Aug 2022 20:32:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CRM=E9=9B=BB=E6=96=87=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=83=90=E3=83=83=E3=82=AF=E3=82=A2=E3=83=83=E3=83=97?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=80=82=E3=83=90=E3=83=83=E3=82=AF=E3=82=A2?= =?UTF-8?q?=E3=83=83=E3=83=97=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E5=90=8D?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/backup_crm_data_process.py | 10 +- ecs/crm-datafetch/src/controller.py | 2 +- .../tests/test_backup_crm_data_process.py | 99 +++++++++++++++++++ 3 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 ecs/crm-datafetch/tests/test_backup_crm_data_process.py diff --git a/ecs/crm-datafetch/src/backup_crm_data_process.py b/ecs/crm-datafetch/src/backup_crm_data_process.py index 0375d927..a5739e07 100644 --- a/ecs/crm-datafetch/src/backup_crm_data_process.py +++ b/ecs/crm-datafetch/src/backup_crm_data_process.py @@ -1,15 +1,16 @@ from src.aws.s3 import BackupBucket +from src.config.objects import TargetObject from src.error.exceptions import FileUploadException from src.system_var.constants import RESBK_JP_NAME from src.util.execute_datetime import ExecuteDateTime from src.util.logger import logger_instance as logger -def backup_crm_data_process(object_name: str, sf_object_dict: dict, execute_datetime: ExecuteDateTime): +def backup_crm_data_process(target_object: TargetObject, sf_object_dict: dict, execute_datetime: ExecuteDateTime): """CRM電文データバックアップ処理 Args: - object_name (str): 取得対象オブジェクト名 + target_object (TargetObject): 取得対象オブジェクト名 sf_object_dict (dict): Salesforceオブジェクトデータ execute_datetime (ExecuteDateTime): 実行日次取得インスタンス @@ -17,12 +18,13 @@ def backup_crm_data_process(object_name: str, sf_object_dict: dict, execute_date FileUploadException: S3のファイルアップロード失敗 """ + object_name = target_object.object_name # ① CRM電文データバックアップ処理の開始ログを出力する logger.info(f'I-RESBK-01 [{object_name}] のCRM電文データバックアップ処理を開始します') try: # ② CRMバックアップ保管用バケットに、CRMから取得したJSONの電文データのバックアップを保管する - file_name = f'{execute_datetime.to_path()}/{object_name}.json' + file_name = f'{execute_datetime.to_path()}/{target_object.upload_file_name}.json' backup_bucket = BackupBucket() backup_bucket.put_response_json(file_name, sf_object_dict) @@ -31,7 +33,7 @@ def backup_crm_data_process(object_name: str, sf_object_dict: dict, execute_date except Exception as e: raise FileUploadException( - 'E-RESBK-01', RESBK_JP_NAME, f'[{object_name}] 電文データのバックアップに失敗しました ファイル名:[{object_name}.json] エラー内容:[{e}]') + 'E-RESBK-01', RESBK_JP_NAME, f'[{object_name}] 電文データのバックアップに失敗しました ファイル名:[{target_object.upload_file_name}.json] エラー内容:[{e}]') # ③ CRM電文データバックアップ処理の終了ログを出力する logger.info(f'I-RESBK-03 [{object_name}] のCRM電文データバックアップ処理を終了します') diff --git a/ecs/crm-datafetch/src/controller.py b/ecs/crm-datafetch/src/controller.py index f9c87960..97c1f10e 100644 --- a/ecs/crm-datafetch/src/controller.py +++ b/ecs/crm-datafetch/src/controller.py @@ -152,7 +152,7 @@ def _fetch_crm_data_per_object(object_info: dict, execute_datetime: ExecuteDateT # 8. CRM電文データバックアップ処理を呼び出す logger.info( f'I-CTRL-11 [{target_object_name}] CRM電文データバックアップ処理呼び出し') - backup_crm_data_process(target_object_name, crm_data_response, execute_datetime) + backup_crm_data_process(target_object, crm_data_response, execute_datetime) # 9. CSV変換処理を呼び出す logger.info( diff --git a/ecs/crm-datafetch/tests/test_backup_crm_data_process.py b/ecs/crm-datafetch/tests/test_backup_crm_data_process.py new file mode 100644 index 00000000..e476dce3 --- /dev/null +++ b/ecs/crm-datafetch/tests/test_backup_crm_data_process.py @@ -0,0 +1,99 @@ +import json +from collections import OrderedDict +from unittest.mock import MagicMock, patch + +import pytest +from src.backup_crm_data_process import backup_crm_data_process +from src.config.objects import TargetObject +from src.error.exceptions import FileNotFoundException, InvalidConfigException +from src.system_var.constants import RESBK_JP_NAME +from src.util.execute_datetime import ExecuteDateTime + +from .test_utils.log_message import generate_log_message_tuple + + +class TestBackupCrmDataProcess: + + @pytest.fixture + def bucket_name(self): + return 'test-backup-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: + CRM電文データバックアップ処理が正常終了し、期待通りの結果となること + Arranges: + - prepare_bucketフィクスチャで、CRM電文データをバックアップするためのモックバケットを作る + - 作成したモックバケットを指すように環境変数を設定する + Expects: + - CRM電文データバックアップファイルがバケットに配置される + - CRM電文データバックアップ処理の仕様に沿った正常系ログが出力されること(デバッグログは除く) + """ + # Arrange + response_json = [ + OrderedDict([ + ('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v1.0/sobjects/Account/TEST001')])), + ('Id', 'TEST001'), + ('AccountNumber', 'test001'), + ('LastModifiedDate', '2022-06-01T00:00:00.000+0000'), + ('LastModifiedById', 1.234567E+6), + ('SystemModstamp', '2022-06-01T00:00:00.000+0000'), + ('IsDeleted', False) + ]), + OrderedDict([ + ('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v1.0/sobjects/Account/TEST002')])), + ('Id', 'TEST002'), + ('AccountNumber', 'test002'), + ('LastModifiedDate', '2022-06-01T00:00:00.000+0000'), + ('LastModifiedById', 1.234567E+6), + ('SystemModstamp', '2022-06-01T00:00:00.000+0000'), + ('IsDeleted', False) + ]), + OrderedDict([ + ('attributes', OrderedDict([('type', 'Account'), ('url', '/services/data/v1.0/sobjects/Account/TEST002')])), + ('Id', 'TEST002'), + ('AccountNumber', 'test002'), + ('LastModifiedDate', '2022-06-01T00:00:00.000+0000'), + ('LastModifiedById', 1.234567E+6), + ('SystemModstamp', '2022-06-01T00:00:00.000+0000'), + ('IsDeleted', False) + ]), + ] + + target_object_dict = { + 'object_name': 'Account', + 'columns': [ + 'Id', + 'AccountNumber', + 'LastModifiedDate', + 'LastModifiedById', + 'SystemModstamp', + 'IsDeleted' + ] + } + + execute_datetime = ExecuteDateTime() + target_object = TargetObject(target_object_dict, execute_datetime) + + # 環境変数を編集 + monkeypatch.setattr('src.aws.s3.CRM_BACKUP_BUCKET', bucket_name) + monkeypatch.setattr('src.aws.s3.RESPONSE_JSON_BACKUP_FOLDER', 'response_json') + + # Act + backup_crm_data_process(target_object, response_json, execute_datetime) + + # Assert + + # ファイル確認 + actual = s3_client.get_object( + Bucket=bucket_name, Key=f'response_json/{execute_datetime.to_path()}/CRM_Account_{execute_datetime.format_date()}.json') + assert actual['Body'].read().decode('utf-8') == json.dumps(response_json) + + # ログの確認 + assert generate_log_message_tuple(log_message='I-RESBK-01 [Account] のCRM電文データバックアップ処理を開始します') in caplog.record_tuples + assert generate_log_message_tuple(log_message='I-RESBK-03 [Account] のCRM電文データバックアップ処理を終了します') in caplog.record_tuples