"""Test snowflake_get_eligible_sales task.""" from unittest.mock import MagicMock, patch from lib import config from tasks.sales_get_eligible.snowflake_get_eligible_sales import get_eligible_sales import_path = 'tasks.sales_get_eligible.snowflake_get_eligible_sales' @patch(f'{import_path}.ows.get_accounting_period_details') @patch(f'{import_path}.ows.get_sales_file_details') @patch(f'{import_path}.RoyaltySnowflakeHook') @patch(f'{import_path}.copy_distribution_sales_from_staging_to_temp') @patch(f'{import_path}.get_event_from_params') def test_get_eligible_distribution_sales( mock_get_event: MagicMock, mock_distribution_template: MagicMock, mock_hook: MagicMock, mock_ows_get_sales_file_details: MagicMock, mock_ows_get_accounting_period_details: MagicMock, mock_sales_get_eligible_event, mock_sales_get_eligible_dag_run, ): """Test using snowflake hook to copy distribution sales from staging to temp.""" mock_sales_file_response = { 'sales_file_id': 123, 'accounting_period_id': 1 } mock_accounting_period_response = { 'accounting_period_id': 1, 'accounting_period_status': 'open', 'contract_type': 'distribution' } sales_file_id = mock_sales_get_eligible_event.get('target_id') mock_ows_get_sales_file_details.return_value = mock_sales_file_response mock_ows_get_accounting_period_details.return_value =\ mock_accounting_period_response mock_get_event.return_value.target_id = sales_file_id mock_distribution_template.return_value.render.return_value = 'INSERT STATEMENT' mock_hook.return_value.run.return_value = True get_eligible_sales(mock_sales_get_eligible_dag_run) mock_get_event.assert_called_once_with(mock_sales_get_eligible_dag_run) mock_hook.assert_called_once_with(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) mock_distribution_template.assert_called_once() mock_distribution_template.return_value.render.assert_called_once_with( env=config.OWS_ENV, sales_file_id=sales_file_id ) mock_hook.return_value.run.assert_called_once() mock_ows_get_sales_file_details.assert_called_once_with(sales_file_id) mock_ows_get_accounting_period_details.assert_called_once_with( mock_sales_file_response['accounting_period_id']) @patch(f'{import_path}.ows.get_accounting_period_details') @patch(f'{import_path}.ows.get_sales_file_details') @patch(f'{import_path}.RoyaltySnowflakeHook') @patch(f'{import_path}.copy_nr_sales_from_staging_to_temp') @patch(f'{import_path}.get_event_from_params') def test_get_eligible_nr_sales( mock_get_event: MagicMock, mock_nr_template: MagicMock, mock_hook: MagicMock, mock_ows_get_sales_file_details: MagicMock, mock_ows_get_accounting_period_details: MagicMock, mock_sales_get_eligible_event, mock_sales_get_eligible_dag_run, ): """Test using snowflake hook to copy NR sales from staging to temp.""" mock_sales_file_response = { 'sales_file_id': 123, 'accounting_period_id': 2 } mock_accounting_period_response = { 'accounting_period_id': 2, 'accounting_period_status': 'open', 'contract_type': 'neighbouring_rights' } sales_file_id = mock_sales_get_eligible_event.get('target_id') mock_ows_get_sales_file_details.return_value = mock_sales_file_response mock_ows_get_accounting_period_details.return_value =\ mock_accounting_period_response mock_get_event.return_value.target_id = sales_file_id mock_nr_template.return_value.render.return_value = 'INSERT STATEMENT' mock_hook.return_value.run.return_value = True get_eligible_sales(mock_sales_get_eligible_dag_run) mock_get_event.assert_called_once_with(mock_sales_get_eligible_dag_run) mock_hook.assert_called_once_with(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) mock_nr_template.assert_called_once() mock_nr_template.return_value.render.assert_called_once_with( env=config.OWS_ENV, sales_file_id=sales_file_id ) mock_hook.return_value.run.assert_called_once() mock_ows_get_sales_file_details.assert_called_once_with(sales_file_id) mock_ows_get_accounting_period_details.assert_called_once_with( mock_sales_file_response['accounting_period_id'])