"""Test load_mech_results_into_publishing_escrow_task task.""" from unittest.mock import MagicMock from unittest.mock import patch from lib import config from tasks.accounting_run_commit.load_mech_results_into_publishing_escrow \ import load_mech_results_into_publishing_escrow_task import_path = 'tasks.accounting_run_commit.load_mech_results_into_publishing_escrow' @patch(f'{import_path}.ArtRelationsMySqlHook') @patch(f'{import_path}.load_mechanical_deduction_results_into_publishing_escrow') @patch(f'{import_path}.file_exists') @patch(f'{import_path}.build_mechanical_export_location') @patch(f'{import_path}.ows') @patch(f'{import_path}.get_event_from_params') @patch('dags.lib.config') def test_export_mechanicals_task( mock_config, mock_get_event_from_params, mock_ows, mock_build_export_location, mock_file_exists, mock_template, mock_hook, mock_commit_dag_run ): """Test task reads S3 file and loads data into art_relations.publishing_escrow.""" accounting_run_id = 123 accounting_period_id = 456 accounting_run = { 'accounting_run_id': accounting_run_id, 'accounting_period_id': accounting_period_id, 'run_controller_name': 'run controller name' } accounting_period = { 'accounting_period_id': accounting_period_id, 'accounting_period_name': 'accounting period name' } s3_key = '456-accounting-period-name/123-run-controller-name/mechanical-export.tsv' s3_url = f's3://qa-royalties-sales-files/{s3_key}' mock_s3_location = MagicMock(key=s3_key, url=s3_url) mock_config.OWS_ENV = 'test' mock_config.SNOWFLAKE_CONN_NAME = config.SNOWFLAKE_CONN_NAME mock_config.ABACUS_TSV_STAGE = config.ABACUS_TSV_STAGE mock_get_event_from_params.return_value.target_id = accounting_run_id mock_ows.get_accounting_run_details.return_value = accounting_run mock_ows.get_accounting_period_details.return_value = accounting_period mock_build_export_location.return_value = mock_s3_location mock_file_exists.return_value = True mock_template.return_value.render.return_value = \ 'COPY MECH RESULTS FROM S3 INTO PUBLISHING_ESCROW' mock_hook.return_value.run.return_value = True load_mech_results_into_publishing_escrow_task(mock_commit_dag_run) mock_get_event_from_params.assert_called_once_with(mock_commit_dag_run) mock_ows.get_accounting_run_details.assert_called_once_with(accounting_run_id) mock_ows.get_accounting_period_details.assert_called_once_with(accounting_period_id) mock_build_export_location.assert_called_once_with( accounting_period, accounting_run ) mock_file_exists.assert_called_once_with(s3_url) mock_template.return_value.render.assert_called_once_with(s3_url=s3_url) mock_hook.assert_called_once() mock_hook.return_value.run.assert_called_once() @patch(f'{import_path}.ArtRelationsMySqlHook') @patch(f'{import_path}.load_mechanical_deduction_results_into_publishing_escrow') @patch(f'{import_path}.file_exists') @patch(f'{import_path}.build_mechanical_export_location') @patch(f'{import_path}.ows') @patch(f'{import_path}.get_event_from_params') @patch('dags.lib.config') def test_export_mechanicals_task_no_file( mock_config, mock_get_event_from_params, mock_ows, mock_build_export_location, mock_file_exists, mock_template, mock_hook, mock_commit_dag_run ): """Test if there is no s3 file, data is not loaded into art_relations.""" accounting_run_id = 123 accounting_period_id = 456 accounting_run = { 'accounting_run_id': accounting_run_id, 'accounting_period_id': accounting_period_id, 'run_controller_name': 'run controller name' } accounting_period = { 'accounting_period_id': accounting_period_id, 'accounting_period_name': 'accounting period name' } s3_key = '456-accounting-period-name/123-run-controller-name/mechanical-export.tsv' s3_url = f's3://qa-royalties-sales-files/{s3_key}' mock_s3_location = MagicMock(key=s3_key, url=s3_url) mock_config.OWS_ENV = 'test' mock_config.SNOWFLAKE_CONN_NAME = config.SNOWFLAKE_CONN_NAME mock_config.ABACUS_TSV_STAGE = config.ABACUS_TSV_STAGE mock_get_event_from_params.return_value.target_id = accounting_run_id mock_ows.get_accounting_run_details.return_value = accounting_run mock_ows.get_accounting_period_details.return_value = accounting_period mock_build_export_location.return_value = mock_s3_location mock_file_exists.return_value = False mock_template.return_value.render.return_value = \ 'THIS WILL NOT BE CALLED' mock_hook.return_value.run.return_value = True load_mech_results_into_publishing_escrow_task(mock_commit_dag_run) mock_get_event_from_params.assert_called_once_with(mock_commit_dag_run) mock_ows.get_accounting_run_details.assert_called_once_with(accounting_run_id) mock_ows.get_accounting_period_details.assert_called_once_with(accounting_period_id) mock_build_export_location.assert_called_once_with( accounting_period, accounting_run ) mock_file_exists.assert_called_once_with(s3_url) mock_template.assert_not_called() mock_hook.assert_not_called() mock_hook.return_value.run.assert_not_called()