187 lines
7.4 KiB
Python
187 lines
7.4 KiB
Python
import json
|
|
from collections import OrderedDict
|
|
from unittest.mock import 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 FileUploadException
|
|
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),
|
|
('Name', 'テスト取引先1')
|
|
]),
|
|
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),
|
|
('Name', 'テスト取引先2')
|
|
]),
|
|
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),
|
|
('Name', 'テスト取引先3')
|
|
]),
|
|
]
|
|
|
|
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, ensure_ascii=False)
|
|
|
|
# ログの確認
|
|
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
|
|
|
|
def test_call_depended_modules(self):
|
|
"""
|
|
Cases:
|
|
CRM電文データバックアップ処理内で依存しているモジュールが正しく呼ばれていること
|
|
Arranges:
|
|
- CRM電文データバックアップ処理の依存モジュールをモック化する
|
|
Expects:
|
|
- 依存しているモジュールが正しく呼ばれている
|
|
"""
|
|
|
|
# Arrange
|
|
target_object_dict = {
|
|
'object_name': 'Account',
|
|
'columns': [
|
|
'Id',
|
|
'AccountNumber',
|
|
'LastModifiedDate',
|
|
'LastModifiedById',
|
|
'SystemModstamp',
|
|
'IsDeleted'
|
|
]
|
|
}
|
|
|
|
execute_datetime = ExecuteDateTime()
|
|
target_object = TargetObject(target_object_dict, execute_datetime)
|
|
|
|
with patch('src.backup_crm_data_process.BackupBucket') as mock_backup_bucket:
|
|
mock_backup_bucket_inst = mock_backup_bucket.return_value
|
|
mock_backup_bucket_inst.put_response_json.return_value = ''
|
|
# Act
|
|
backup_crm_data_process(target_object, {}, execute_datetime)
|
|
|
|
# Assert
|
|
|
|
assert mock_backup_bucket_inst.put_response_json.called is True
|
|
|
|
def test_raise_put_response_json(self, bucket_name, monkeypatch, caplog):
|
|
"""
|
|
Cases:
|
|
CRM電文データをS3に配置できない場合、エラーが発生すること
|
|
Arranges:
|
|
- オブジェクト情報ファイル取得処理で例外が発生するようにする
|
|
Expects:
|
|
- 例外が発生する
|
|
- ファイル登録できないエラーが返却される
|
|
"""
|
|
|
|
# Arrange
|
|
target_object_dict = {
|
|
'object_name': 'Account',
|
|
'columns': [
|
|
'Id',
|
|
'AccountNumber',
|
|
'LastModifiedDate',
|
|
'LastModifiedById',
|
|
'SystemModstamp',
|
|
'IsDeleted'
|
|
]
|
|
}
|
|
|
|
execute_datetime = ExecuteDateTime()
|
|
target_object = TargetObject(target_object_dict, execute_datetime)
|
|
|
|
with patch('src.backup_crm_data_process.BackupBucket') as mock_backup_bucket:
|
|
mock_backup_bucket_inst = mock_backup_bucket.return_value
|
|
mock_backup_bucket_inst.put_response_json.side_effect = Exception('登録エラー')
|
|
# Act
|
|
with pytest.raises(FileUploadException) as e:
|
|
backup_crm_data_process(target_object, {}, execute_datetime)
|
|
|
|
# Assert
|
|
|
|
assert mock_backup_bucket_inst.put_response_json.called is True
|
|
assert e.value.error_id == 'E-RESBK-01'
|
|
assert e.value.func_name == RESBK_JP_NAME
|
|
assert e.value.args[0] == \
|
|
f'[Account] 電文データのバックアップに失敗しました ファイル名:[CRM_Account_{execute_datetime.format_date()}.json] エラー内容:[登録エラー]'
|