import json from unittest.mock import MagicMock, patch import pytest from src.config.objects import LastFetchDatetime, TargetObject from src.error.exceptions import FileNotFoundException, InvalidConfigException from src.set_datetime_period_process import set_datetime_period_process from src.util.execute_datetime import ExecuteDateTime from .test_utils.log_message import generate_log_message_tuple class TestSetDatetimePeriodProcess: @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: - チェック対象のdictオブジェクトを宣言する Expects: - チェック後のオブジェクト情報コレクションクラスのインスタンスが返却される - データ取得期間設定処理の仕様に沿った正常系ログが出力されること(デバッグログは除く) """ # Arrange target_objects_dict = { 'object_name': 'Account', 'columns': [ 'Id', 'Name' ] } 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) s3_client.put_object(Bucket=bucket_name, Key='crm/last_fetch_datetime/Account.json', Body=json.dumps(last_fetch_datetime_dict)) # 環境変数を編集 monkeypatch.setattr('src.aws.s3.CRM_CONFIG_BUCKET', bucket_name) monkeypatch.setattr('src.set_datetime_period_process.CRM_CONFIG_BUCKET', bucket_name) monkeypatch.setattr('src.aws.s3.LAST_FETCH_DATE_FOLDER', 'crm/last_fetch_datetime') # Act actual_last_fetch_datetime = set_datetime_period_process(target_object, execute_datetime) # Assert # 返り値の期待値チェック assert isinstance(actual_last_fetch_datetime, LastFetchDatetime), '最終取得日時オブジェクトクラスのインスタンスが返却される' # ログの確認 assert generate_log_message_tuple(log_message='I-DATE-01 [Account] のデータ取得期間設定処理を開始します') in caplog.record_tuples assert generate_log_message_tuple( log_message='I-DATE-02 前回取得日時ファイルの取得開始します ファイルパス:[s3://test-config-bucket/crm/last_fetch_datetime/Account.json]') in caplog.record_tuples assert generate_log_message_tuple(log_message='I-DATE-03 前回取得日時ファイルの取得成功しました') in caplog.record_tuples assert generate_log_message_tuple( log_message='I-DATE-06 取得範囲 From: [1999-01-01T00:00:00.000Z] To: [2100-12-31T23:59:59.000Z]') in caplog.record_tuples assert generate_log_message_tuple(log_message='I-DATE-07 [Account] のデータ取得期間設定処理を終了します') in caplog.record_tuples def test_call_depended_modules(self, s3_client, prepare_bucket, bucket_name, monkeypatch, caplog): """ Cases: データ取得期間設定処理内で依存しているモジュールが正しく呼ばれていること Arranges: - データ取得期間設定処理内の依存モジュールをモック化する Expects: - 依存しているモジュールが正しく呼ばれている """ # Arrange target_objects_dict = { 'object_name': 'Account', 'columns': [ 'Id', 'Name' ] } 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) mock_config_bucket = MagicMock(return_value=last_fetch_datetime_dict) mock_json_parser = MagicMock(return_value=last_fetch_datetime_dict) # Act with patch('src.aws.s3.ConfigBucket.get_last_fetch_datetime_file', mock_config_bucket), \ patch('src.parser.json_parser.JsonParser.parse', mock_json_parser): set_datetime_period_process(target_object, execute_datetime) # Assert assert mock_config_bucket.called is True assert mock_json_parser.called is True def test_raise_get_last_fetch_datetime_file(self): """ Cases: 最終取得日時ファイル取得処理でエラーが発生すること Arranges: - オブジェクト形式チェック処理で例外が発生するようにする Expects: - 例外が発生する - 形式チェックが失敗したエラーが返却される """ # Arrange target_objects_dict = { 'object_name': 'Account', 'columns': [ 'Id', 'Name' ] } 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) mock_config_bucket = MagicMock(side_effect=Exception('取得エラー')) mock_json_parser = MagicMock(return_value=last_fetch_datetime_dict) mock_last_fetch_datetime = MagicMock(return_value=None) # Act with patch('src.aws.s3.ConfigBucket.get_last_fetch_datetime_file', mock_config_bucket), \ patch('src.parser.json_parser.JsonParser.parse', mock_json_parser), \ patch('src.config.objects.LastFetchDatetime.__init__', mock_last_fetch_datetime): with pytest.raises(FileNotFoundException) as e: set_datetime_period_process(target_object, execute_datetime) # Assert assert mock_config_bucket.called is True assert mock_json_parser.called is False assert e.value.args[0] == f'前回取得日時ファイルが存在しません ファイル名:[Account.json] エラー内容:[取得エラー]' def test_raise_json_parse(self): """ Cases: Jsonパース処理でエラーが発生すること Arranges: - Jsonパース処理で例外が発生するようにする Expects: - 例外が発生する - 形式チェックが失敗したエラーが返却される """ # Arrange target_objects_dict = { 'object_name': 'Account', 'columns': [ 'Id', 'Name' ] } execute_datetime = ExecuteDateTime() target_object = TargetObject(target_objects_dict, execute_datetime) mock_config_bucket = MagicMock(return_value='') mock_json_parser = MagicMock(side_effect=Exception('変換エラー')) mock_last_fetch_datetime = MagicMock(return_value=None) # Act with patch('src.aws.s3.ConfigBucket.get_last_fetch_datetime_file', mock_config_bucket), \ patch('src.parser.json_parser.JsonParser.parse', mock_json_parser), \ patch('src.config.objects.LastFetchDatetime.__init__', mock_last_fetch_datetime): with pytest.raises(InvalidConfigException) as e: set_datetime_period_process(target_object, execute_datetime) # Assert assert mock_config_bucket.called is True assert mock_json_parser.called is True assert e.value.args[0] == f'前回取得日時ファイルの形式チェック処理が失敗しました エラー内容:[変換エラー]' def test_raise_check_last_fetch_datetime(self): """ Cases: Jsonパース処理でエラーが発生すること Arranges: - Jsonパース処理で例外が発生するようにする Expects: - 例外が発生する - 形式チェックが失敗したエラーが返却される """ # Arrange target_objects_dict = { 'object_name': 'Account', 'columns': [ 'Id', 'Name' ] } 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) mock_config_bucket = MagicMock(return_value='') mock_json_parser = MagicMock(return_value=last_fetch_datetime_dict) mock_last_fetch_datetime = MagicMock(side_effect=Exception('形式エラー')) # Act with patch('src.aws.s3.ConfigBucket.get_last_fetch_datetime_file', mock_config_bucket), \ patch('src.parser.json_parser.JsonParser.parse', mock_json_parser), \ patch('src.config.objects.LastFetchDatetime.__init__', mock_last_fetch_datetime): with pytest.raises(InvalidConfigException) as e: set_datetime_period_process(target_object, execute_datetime) # Assert assert mock_config_bucket.called is True assert mock_json_parser.called is True assert mock_last_fetch_datetime.called is True assert e.value.args[0] == f'前回取得日時ファイルの形式チェック処理が失敗しました エラー内容:[形式エラー]'