"""Test for debiting reserves for accounting run task.""" from unittest.mock import MagicMock from unittest.mock import patch from lib.constants import CONTRACT_TYPES from tasks.accounting_run_commit.debit_reserves import \ debit_reserves_for_accounting_run_task @patch('tasks.accounting_run_commit.debit_reserves.ows') @patch('tasks.accounting_run_commit.debit_reserves.helpers') def test_debit_reserves_task_distribution_period( mock_helpers, mock_ows, accounting_run_commit_dag_config ): """Test task debits reserves for a run in a distribution accounting_period.""" accounting_run_id = 10 accounting_period = { 'accounting_period_id': 1, 'accounting_period_name': 'A Distribution Period', 'contract_type': CONTRACT_TYPES.DISTRIBUTION } accounting_run = { 'accounting_run_id': accounting_run_id, 'accounting_period_id': 1 } mock_task_instance = MagicMock() mock_task_instance.xcom_pull.return_value = None mock_helpers.get_event_from_params.return_value.target_id = accounting_run_id mock_helpers.get_event_records.return_value = (accounting_period, accounting_run) mock_ows.debit_reserves_from_ledger_account_by_run.return_value = { 'message': '20 ledger entries created.' } debit_reserves_for_accounting_run_task( accounting_run_commit_dag_config, task_instance=mock_task_instance ) mock_helpers.get_event_from_params.assert_called_once_with( accounting_run_commit_dag_config, task_instance=mock_task_instance ) mock_task_instance.xcom_pull.assert_called_once_with( task_ids='contract_type_branch', key='contract_type' ) mock_helpers.get_event_records.assert_called_once_with(accounting_run_id) mock_ows.debit_reserves_from_ledger_account_by_run.assert_called_once_with( accounting_run_id) @patch('tasks.accounting_run_commit.debit_reserves.ows') @patch('tasks.accounting_run_commit.debit_reserves.helpers') def test_debit_reserves_task_nr_period( mock_helpers, mock_ows, accounting_run_commit_dag_config ): """Test task does not debits reserves for a run in an NR accounting_period.""" accounting_run_id = 10 accounting_period = { 'accounting_period_id': 1, 'accounting_period_name': 'A Neighbouring Rights Period', 'contract_type': CONTRACT_TYPES.NEIGHBOURING_RIGHTS } accounting_run = { 'accounting_run_id': accounting_run_id, 'accounting_period_id': 1 } mock_task_instance = MagicMock() mock_task_instance.xcom_pull.return_value = None mock_helpers.get_event_from_params.return_value.target_id = accounting_run_id mock_helpers.get_event_records.return_value = (accounting_period, accounting_run) debit_reserves_for_accounting_run_task( accounting_run_commit_dag_config, task_instance=mock_task_instance ) mock_helpers.get_event_from_params.assert_called_once_with( accounting_run_commit_dag_config, task_instance=mock_task_instance ) mock_task_instance.xcom_pull.assert_called_once_with( task_ids='contract_type_branch', key='contract_type' ) mock_helpers.get_event_records.assert_called_once_with(accounting_run_id) mock_ows.debit_reserves_from_ledger_account_by_run.assert_not_called() @patch('tasks.accounting_run_commit.debit_reserves.ows') @patch('tasks.accounting_run_commit.debit_reserves.helpers') def test_debit_reserves_task_get_contract_type( mock_helpers, mock_ows, accounting_run_commit_dag_config ): """Test when task gets contract_type from xcom it does not use helper method.""" accounting_run_id = 10 accounting_period = { 'accounting_period_id': 1, 'accounting_period_name': 'A Neighbouring Rights Period', 'contract_type': CONTRACT_TYPES.NEIGHBOURING_RIGHTS } accounting_run = { 'accounting_run_id': accounting_run_id, 'accounting_period_id': 1 } mock_task_instance = MagicMock() mock_task_instance.xcom_pull.return_value = CONTRACT_TYPES.NEIGHBOURING_RIGHTS mock_helpers.get_event_from_params.return_value.target_id = accounting_run_id mock_helpers.get_event_records.return_value = (accounting_period, accounting_run) debit_reserves_for_accounting_run_task( accounting_run_commit_dag_config, task_instance=mock_task_instance ) mock_helpers.get_event_from_params.assert_called_once_with( accounting_run_commit_dag_config, task_instance=mock_task_instance ) mock_task_instance.xcom_pull.assert_called_once_with( task_ids='contract_type_branch', key='contract_type' ) mock_helpers.get_event_records.assert_not_called() mock_ows.debit_reserves_from_ledger_account_by_run.assert_not_called()