"""Unit tests for snowflake_match_contract_terms task.""" from unittest.mock import patch from lib import config from tasks.accounting_run_calculate_nr.snowflake_match_contract_terms \ import match_contract_terms_to_sales_task import_path = 'tasks.accounting_run_calculate_nr.snowflake_match_contract_terms' @patch(f'{import_path}.RoyaltySnowflakeHook') @patch(f'{import_path}.shared_helpers') @patch(f'{import_path}.helpers') @patch(f'{import_path}.insert_contract_transaction_staging_nr_template') def test_match_contract_terms_to_sales_task( mock_template, mock_helpers, mock_shared_helpers, mock_hook, mock_accounting_run_calculate_nr_dag_run ): """Test task renders snowflake template to match transactions to contracts.""" accounting_run_id = 123 accounting_period = { 'accounting_period_id': 1, 'accounting_period_name': 'Test Period' } mock_helpers.get_event_from_params.return_value.target_id = accounting_run_id mock_shared_helpers.get_event_records.return_value = (accounting_period, {}) mock_shared_helpers.get_sales_file_ids.return_value = [3, 2, 1] mock_template.return_value.render.return_value = \ 'MATCH CONTRACT TERMS TO TXNS AND INSERT INTO CONTRACT_TRANSACTION_NR_STAGING' mock_hook.return_value.run.return_value = True match_contract_terms_to_sales_task(mock_accounting_run_calculate_nr_dag_run) mock_helpers.get_event_from_params.assert_called_once_with( mock_accounting_run_calculate_nr_dag_run ) mock_shared_helpers.get_event_records.assert_called_once_with(accounting_run_id) mock_shared_helpers.get_sales_file_ids.assert_called_once_with( accounting_period.get('accounting_period_id') ) 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, sales_file_ids=[3, 2, 1], schema=config.OWS_ENV ) mock_hook.return_value.run.assert_called_once() @patch(f'{import_path}.RoyaltySnowflakeHook') @patch(f'{import_path}.shared_helpers') @patch(f'{import_path}.helpers') @patch( f'{import_path}.insert_contributor_only_contract_transaction_staging_nr_template' ) def test_match_contributor_only_contract_terms_to_sales_task( mock_template, mock_helpers, mock_shared_helpers, mock_hook, mock_accounting_run_calculate_nr_dag_run ): """Test task renders snowflake template to match contrib-only sales to contracts.""" accounting_run_id = 123 accounting_period = { 'accounting_period_id': 1, 'accounting_period_name': 'Test Period' } mock_helpers.get_event_from_params.return_value.target_id = accounting_run_id mock_shared_helpers.get_event_records.return_value = (accounting_period, {}) mock_shared_helpers.get_sales_file_ids.return_value = [3, 2, 1] mock_template.return_value.render.return_value = \ 'MATCH CONTRIB-ONLY TERMS/TXNS AND INSERT INTO CONTRACT_TRANSACTION_NR_STAGING' mock_hook.return_value.run.return_value = True match_contract_terms_to_sales_task( mock_accounting_run_calculate_nr_dag_run, is_contributor_only=True ) mock_helpers.get_event_from_params.assert_called_once_with( mock_accounting_run_calculate_nr_dag_run ) mock_shared_helpers.get_event_records.assert_called_once_with(accounting_run_id) mock_shared_helpers.get_sales_file_ids.assert_called_once_with( accounting_period.get('accounting_period_id') ) 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, sales_file_ids=[3, 2, 1], schema=config.OWS_ENV ) mock_hook.return_value.run.assert_called_once()