import unittest import unittest.mock from slz_config.const import OPTIONAL_CONFIG from slz_config.entities import DSPConfig, DSPSettings from slz_downloader.dsp.entities import S3Path from slz_downloader.sqs_service import SQSService from slz_notification.entities import Message, Metadata from slz_meta_retrigger import handler class HandlerTestCase(unittest.TestCase): @unittest.mock.patch('slz_downloader.dsp.amazon.AmazonPrimeTheorchardClient.configure') @unittest.mock.patch( 'slz_downloader.dsp.amazon.AmazonPrimeTheorchardClient.get_s3_paths', side_effect=[ ( S3Path(bucket='archive', path='path', name='name.gz'), S3Path(bucket='decompressed', path='path', name='name'), None, ) ] ) def test_retrigger_meta_flow(self, configure_mock, get_s3_paths_mock): logger = unittest.mock.Mock() cs_mock = unittest.mock.Mock() cs_mock.unit_of_work.readable = unit_of_work_code = \ 'amazonprime-20190109-theorchard-activity-v1' cs_mock.unit_of_work.unit_of_work_id = unit_of_work_id = 123 cs_mock.content_name = content_name = 'A_ORCA_AT_2019-00-09_activity.txt.zip' cs_mock.context = context = 'ORCA_AT' cs_service = unittest.mock.Mock() config = { 'env': 'dev', 'config_bucket': 'test-config-bucket', 'dsp_settings_path': 'test-dsp-settings-path', 'bucket_decompressed': 'test-bucket-decompressed', 'bucket_archive': 'test-bucket-archive', 'aws_region': 'us-east-1', 'sqs_arns': 'queue', OPTIONAL_CONFIG: {}, } sqs_notification_service = unittest.mock.Mock() sqs_notification_service.push.return_value = lambda x: True, None sqs_service = SQSService( logger, sqs_notification_service, config['sqs_arns'], config[OPTIONAL_CONFIG] ) payload = {'bucket_source': 0} dsp_config_item = DSPConfig.parse( { "unit_of_work": "amazonprime-{yyyymmdd}-theorchard-activity-v1", "uow_active_hours_threshold": 2, "schedule": "*/10 * * * *", "args": { "dsp": "amazonprime", "extension": "txt", "type": "activity", "version": "v1", "licensor": "theorchard", "valid_from": "2018-06-06" } } ) dsp_config = { dsp_config_item.unit_of_work: dsp_config_item, } dsp_spec_settings_item = DSPSettings.parse( { "uow_active_days_limit": 180, "implementations": [ { "rule": "theorchard/*", "label": "amazonprime-theorchard" }, { "rule": "*/*", "label": "amazonprime" } ], "days_offset": 1, "validation_thresholds": { "low": 10, "high": 20 }, "default_priority_hrs": 24 } ) dsp_spec_settings = { "amazonprime": dsp_spec_settings_item, } result = handler.retrigger_meta_flow( logger, dsp_spec_settings, dsp_config, cs_mock, sqs_service, cs_service, config, payload, ) expected = Metadata( version='2.0.0', queues=['queue'], message=Message( uow_id=unit_of_work_code, unit_of_work_id=unit_of_work_id, compressed_path='s3://archive/path/name.gz', decompressed_paths='s3://decompressed/path/name', content_name=content_name, context=context, optional_config={}, ) ) sqs_notification_service.push.assert_called_with(expected) self.assertTrue(result)