100 lines
4.2 KiB
Python
100 lines
4.2 KiB
Python
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
|