"""Test snowflake_export_mechanicals task.""" from unittest.mock import MagicMock from unittest.mock import patch from lib import config from tasks.accounting_run_commit.snowflake_export_mechanicals \ import export_mechanicals_task import_path = 'tasks.accounting_run_commit.snowflake_export_mechanicals' @patch(f'{import_path}.RoyaltySnowflakeHook') @patch(f'{import_path}.ows') @patch(f'{import_path}.build_mechanical_export_location') @patch(f'{import_path}.get_event_from_params') @patch(f'{import_path}.export_mechanical_deductions_to_s3') @patch(f'{import_path}.config') def test_export_mechanicals_task( mock_config, mock_template, mock_get_event_from_params, mock_build_export_location, mock_ows, mock_hook, mock_commit_dag_run ): """Test exporting mechanical deduction results to S3 as TSV file.""" 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_template.return_value.render.return_value = 'COPY MECH RESULTS TO S3 AS TSV' mock_hook.return_value.run.return_value = True export_mechanicals_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_hook.assert_called_once_with(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) mock_template.return_value.render.assert_called_once_with( accounting_run_id=accounting_run_id, s3_path=s3_key, schema=mock_config.OWS_ENV, stage=config.ABACUS_TSV_STAGE ) mock_hook.return_value.run.assert_called_once()