"""Test snowflake_truncate_temp_sales task.""" from unittest.mock import MagicMock, patch from lib import config from tasks.accounting_period_sales_approve.snowflake_truncate_temp_sales \ import truncate_temp_sales_task import_path = 'tasks.accounting_period_sales_approve.snowflake_truncate_temp_sales' @patch(f'{import_path}.ows.get_accounting_period_details') @patch(f'{import_path}.RoyaltySnowflakeHook') @patch(f'{import_path}.truncate_temp_sales') @patch(f'{import_path}.get_event_from_params') def test_truncate_distribution_temp_sales_task( mock_get_event: MagicMock, mock_template: MagicMock, mock_hook: MagicMock, mock_ows_get_accounting_period_details: MagicMock, mock_accounting_period_sales_approve_event, mock_accounting_period_sales_approve_dag_run, ) -> None: """Test using snowflake hook to truncate distribution sales from temp table.""" accounting_period_id = mock_accounting_period_sales_approve_event.get('target_id') mock_accounting_period_response = { 'accounting_period_id': accounting_period_id, 'accounting_period_status': 'closed', 'contract_type': 'distribution' } mock_ows_get_accounting_period_details.return_value =\ mock_accounting_period_response mock_template.return_value.render.return_value = 'TRUNCATE TABLE' mock_hook.return_value.run_return_value = True mock_get_event.return_value.target_id = accounting_period_id truncate_temp_sales_task(mock_accounting_period_sales_approve_dag_run) mock_hook.assert_called_once_with(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) mock_template.assert_called_once() mock_template.return_value.render.assert_called_once_with( env=config.OWS_ENV, temp_table='STMT_DB_SALES_DISTRO_TEMP' ) mock_hook.return_value.run.assert_called_once() mock_ows_get_accounting_period_details.assert_called_once_with(accounting_period_id) @patch(f'{import_path}.ows.get_accounting_period_details') @patch(f'{import_path}.RoyaltySnowflakeHook') @patch(f'{import_path}.truncate_temp_sales') @patch(f'{import_path}.get_event_from_params') def test_truncate_nr_temp_sales_task( mock_get_event: MagicMock, mock_template: MagicMock, mock_hook: MagicMock, mock_ows_get_accounting_period_details: MagicMock, mock_accounting_period_sales_approve_event, mock_accounting_period_sales_approve_dag_run, ): """Test using snowflake hook to truncate NR sales from temp table.""" accounting_period_id = mock_accounting_period_sales_approve_event.get('target_id') mock_accounting_period_response = { 'accounting_period_id': accounting_period_id, 'accounting_period_status': 'closed', 'contract_type': 'neighbouring_rights' } mock_ows_get_accounting_period_details.return_value =\ mock_accounting_period_response mock_template.return_value.render.return_value = 'TRUNCATE TABLE' mock_hook.return_value.run_return_value = True mock_get_event.return_value.target_id = accounting_period_id truncate_temp_sales_task(mock_accounting_period_sales_approve_dag_run) mock_hook.assert_called_once_with(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) mock_template.assert_called_once() mock_template.return_value.render.assert_called_once_with( env=config.OWS_ENV, temp_table='STMT_DB_SALES_NR_TEMP' ) mock_hook.return_value.run.assert_called_once() mock_ows_get_accounting_period_details.assert_called_once_with(accounting_period_id)