"""Theatrical cuts ETL tasks tests.""" from datetime import date from unittest import mock from unittest.mock import Mock from unittest.mock import patch import pytest from flows.theatrical_cuts import queries from flows.theatrical_cuts import status from flows.theatrical_cuts import tasks @pytest.fixture def correlation_id(): """Set correlation_id fixture.""" return '13536586-b8b7-11e6-8c7b-acbc32ca6ded' @patch('flows.theatrical_cuts.tasks.log') @patch('flows.theatrical_cuts.tasks.util') def test_bootstrap(util_mock, log_mock, correlation_id): """Test bootstrap task.""" # prepare test data workflow_run_id = 'run_id' s3_bucket = 'test_bucket' s3_key = 'test/key.csv' upc = '889845077732' drop_url = 's3://{bucket}/{key}'.format(bucket=s3_bucket, key=s3_key) expected_response = {'upc': upc, 'drop_url': drop_url} # mock required functions util_mock.get_cuts_upc.return_value = (upc) # call test function response = tasks.bootstrap( Mock(), correlation_id, workflow_run_id, s3_bucket, s3_key) # check function calls util_mock.get_cuts_upc.assert_called_once_with(drop_url) log_mock.create.assert_called_once_with( correlation_id, workflow_run_id, upc) assert response == expected_response @patch('flows.theatrical_cuts.tasks.util') @patch('flows.theatrical_cuts.tasks.log') def test_determine_anchor_date(log, util, correlation_id): """Test determine_anchor_date task.""" upc = '54321' util.get_upc_theatrical_cuts_anchor_date.return_value = date( 2012, 12, 21) results = tasks.determine_anchor_date(None, correlation_id, upc) assert results['anchor_date'] == '2012-12-21' assert 'stop' not in results util.get_upc_theatrical_cuts_anchor_date.assert_called_once_with(upc) log.update_status.assert_called_once_with( correlation_id, status.ANCHOR_DATE_DETERMINED) @patch('flows.theatrical_cuts.tasks.util') @patch('flows.theatrical_cuts.tasks.log') def test_determine_anchor_date_none(log, util, correlation_id): """Test determine_anchor_date task when there is no anchor date.""" upc = '54321' util.get_upc_theatrical_cuts_anchor_date.return_value = None results = tasks.determine_anchor_date(None, correlation_id, upc) assert results['anchor_date'] is None assert results['stop'] is True util.get_upc_theatrical_cuts_anchor_date.assert_called_once_with(upc) log.update_status.assert_called_once_with( correlation_id, status.ANCHOR_DATE_UNDETERMINED) @patch('flows.theatrical_cuts.tasks.datastore') @patch('flows.theatrical_cuts.tasks.log') def test_update_db_task(log_mock, datastore_mock, correlation_id): """Test of update_db_task.""" upc = '123456789012' params = {'activity': Mock(), 'correlation_id': correlation_id, 'upc': upc} tasks.update_db(**params) datastore_mock.execute.assert_called_once_with( queries.UPDATE_REVENUE, {'upc': upc}) log_mock.update_status.assert_called_once_with( correlation_id, status.REVENUE_TABLE_UPDATED) @patch('flows.theatrical_cuts.tasks.s3') @patch('flows.theatrical_cuts.tasks.util') @patch('flows.theatrical_cuts.tasks.datastore') @patch('flows.theatrical_cuts.tasks.log') def test_download_cuts_to_db( log_mock, datastore_mock, utils_mock, s3_mock, database_context, correlation_id): """Test of update_db_task.""" drop_url = 's3://bucket/key' upc = '123456789012' dummy_query_parameters = {'dummy': 'dummy'} datastore_mock.context = database_context s3_mock.download_csv = Mock(return_value=['dummy']) utils_mock.transpose_percentage_data = Mock( return_value=[dummy_query_parameters]) tasks.download_cuts_to_db( Mock(), Mock(), correlation_id, drop_url, upc) database_context._cursor.execute.assert_has_calls( [mock.call('START TRANSACTION;'), mock.call(queries.DELETE_EXIST_UPC_CUTS, {'upc': upc})] ) database_context._cursor.executemany.assert_called_once_with( queries.INSERT_UPC_CUTS, [dummy_query_parameters]) log_mock.update_status.assert_called_once_with( correlation_id, status.THEATRICAL_CUTS_RAW_TABLE_UPDATED) @patch('flows.theatrical_cuts.tasks.log') @patch('flows.theatrical_cuts.tasks.s3') def test_archive_dropped_csv(s3, log, correlation_id): """Test archive_dropped_csv task.""" bucket = 'test-bucket' archive_key = 'some/location/archives/test-file' archive_url = 's3://test-bucket/' + archive_key archive_path = Mock() archive_path.format.return_value = archive_key archive_obj = Mock() drop_key = 'some/location/drop/test-file' drop_url = 's3://test-bucket/' + drop_key drop_obj = Mock() drop_obj.bucket_name = bucket drop_obj.key = drop_key s3_objects = {archive_url: archive_obj, drop_url: drop_obj} s3.get_object = lambda x: s3_objects[x] results = tasks.archive_dropped_csv( None, archive_path, bucket, correlation_id, drop_url) assert results['archive_url'] == archive_url assert drop_obj.delete.called archive_obj.copy.assert_called_once_with({ 'Bucket': bucket, 'Key': drop_key}) log.update_status.assert_called_once_with( correlation_id, status.DROP_FILE_ARCHIVED) @patch('flows.theatrical_cuts.tasks.log') def test_set_final_status(log_mock): """Test to set_final_status task.""" tasks.set_final_status(Mock(), correlation_id) log_mock.update_status.assert_called_once_with( correlation_id, status.INGESTED)