from unittest import mock import pytest from db_schema.schemas.slz import ContentStatus, UnitOfWork from slz_appreciationengine_scrapper.config import App from slz_appreciationengine_scrapper.dsp.entities import ( DecompressedFile, MetaDataKey, ReportMeta, S3Path, ) from slz_appreciationengine_scrapper.entities import Job from slz_appreciationengine_scrapper.worker.appreciationengine import BaseWorker @pytest.fixture def job_mock(): return Job( uow_id='apple-v1', unit_of_work_id=0, dsp='apple', report_type='users', subtype='', version='v1', report_date='2020-12-02', licensor='smejp', extension='tsv', config_bucket='sme/bucket', context='de', context_params={}, job_id='', ) @pytest.fixture def meta_mock(): path_quarantine = S3Path( bucket='quarantine', path='/path', name='report.tsv', ) path = S3Path( bucket='archive', path='/path', name='report.tsv.gz', ) return ReportMeta( actual_size=123, expected_size=123, destination_path=S3Path( bucket='archive', path='/path', name='report.tsv', ), destination_path_quarantine=path_quarantine, destination_corrupted_path=S3Path( bucket='corrupted', path='/path', name='report.tsv', ), destination_decompressed_path_quarantine=None, files=[ DecompressedFile(name='report.tsv', path=path, path_quarantine=path_quarantine), ], ) @pytest.fixture def s3_service_mock(): return mock.Mock() @pytest.fixture def validation_service_mock(): return mock.Mock() @pytest.fixture def content_status_service_mock(): return mock.Mock() @pytest.fixture def dsp_client_mock(): return mock.Mock() @pytest.fixture def sqs_service_mock(): return mock.Mock() @pytest.fixture def params_mock(): return App.empty() @pytest.fixture def worker_mock( s3_service_mock, validation_service_mock, content_status_service_mock, dsp_client_mock, sqs_service_mock, params_mock ): dsp_settings = mock.Mock() dsp_criterias = mock.Mock() return BaseWorker( mock.Mock(), params=params_mock, s3_service=s3_service_mock, validation_service=validation_service_mock, content_status_service=content_status_service_mock, dsp_client=dsp_client_mock, dsp_settings=dsp_settings, dsp_criterias=dsp_criterias, sqs_service=sqs_service_mock, ) def test_prevent_data_reloading( dsp_client_mock, content_status_service_mock, worker_mock, job_mock ): uow_1 = UnitOfWork(timeslot=None) cs = ContentStatus( unit_of_work=uow_1, meta_data={MetaDataKey.PARTIAL_DOWNLOAD_COMPLETED_AT.value: None} ) dsp_client_mock.get_content_name.return_value = 'content_name' dsp_client_mock.configure.side_effect = None # exception not raised dsp_client_mock.download.return_value = meta_mock content_status_service_mock.start_processing.return_value = True content_status_service_mock.save_active.return_value = True content_status_service_mock.restore_to_complete_status.return_value = True content_status_service_mock.get_content_status_record.return_value = cs worker_mock._get_meta_data = lambda v: {MetaDataKey.PARTIAL_DOWNLOAD_COMPLETED_AT.value: None} result = worker_mock.process(job_mock) assert result is False