"""Test Processor.""" from unittest.mock import MagicMock, patch from adjustments_apply.constants.error import BALANCES_ARE_CLOSED from adjustments_apply.ledger_adjustment_applied_item import LedgerAdjustmentAppliedItem from adjustments_apply.processor import AdjustmentsApplyProcessor from config import BATCH_SIZE_GET @patch('adjustments_apply.processor.app_logger') def test__can_apply_adjustments(mock_logger, mock_event, mock_pending_adjustments): """Test _can_apply_adjustments returns True if adjustments can be applied.""" payment_entity_close_balance_statuses = {1: 'init', 2: 'init'} processor = AdjustmentsApplyProcessor(mock_event) res = processor._can_apply_adjustments( payment_entity_close_balance_statuses, mock_pending_adjustments ) assert res is True mock_logger.error.assert_not_called() @patch('adjustments_apply.processor.app_logger') def test__can_apply_adjustments_closed_balances( mock_logger, mock_event, mock_pending_adjustments ): """Test _can_apply_adjustments is False if there are closed balances.""" payment_entity_close_balance_statuses = {1: 'complete', 2: 'init'} processor = AdjustmentsApplyProcessor(mock_event) res = processor._can_apply_adjustments( payment_entity_close_balance_statuses, mock_pending_adjustments ) assert res is False mock_logger.error.assert_called_once_with(BALANCES_ARE_CLOSED) @patch('adjustments_apply.processor.app_logger') def test__can_apply_adjustments_all_null_payment_entities( mock_logger, mock_event, mock_pending_adjustments ): """Test _can_apply_adjustments returns True if all accounts payment entities are null.""" # noqa : E501 payment_entity_close_balance_statuses = {1: 'complete', 2: 'init'} pending_adjustments = mock_pending_adjustments for pending_adjustment in pending_adjustments: pending_adjustment['account_payment_entity_id'] = None processor = AdjustmentsApplyProcessor(mock_event) res = processor._can_apply_adjustments( payment_entity_close_balance_statuses, pending_adjustments ) assert res is True mock_logger.error.assert_not_called() @patch('adjustments_apply.processor.app_logger') def test__can_apply_adjustments_null_payment_entity( mock_logger, mock_event, mock_pending_adjustments ): """Test _can_apply_adjustments method. returns True if the payment entity of an account is null and all other accounts are associated with payment entity id 1, which has "close_balance" status "init". """ payment_entity_close_balance_statuses = {1: 'init', 2: 'complete'} pending_adjustments = mock_pending_adjustments pending_adjustments[1]['account_payment_entity_id'] = None processor = AdjustmentsApplyProcessor(mock_event) res = processor._can_apply_adjustments( payment_entity_close_balance_statuses, pending_adjustments ) assert res is True mock_logger.error.assert_not_called() @patch('adjustments_apply.processor.app_logger') def test__can_apply_adjustments_any_null_payment_entity( mock_logger, mock_event, mock_pending_adjustments ): """Test _can_apply_adjustments method. returns False if the payment entity of an account is null and if any accounts are associated with payment entity id 2, which has "close_balance" status "closed". """ payment_entity_close_balance_statuses = {1: 'init', 2: 'complete'} pending_adjustments = mock_pending_adjustments pending_adjustments[0]['account_payment_entity_id'] = None processor = AdjustmentsApplyProcessor(mock_event) res = processor._can_apply_adjustments( payment_entity_close_balance_statuses, pending_adjustments ) assert res is False mock_logger.error.assert_called_once_with(BALANCES_ARE_CLOSED) @patch('adjustments_apply.processor.get_statement_period_payment_entity_statuses') def test__get_close_balance_statuses( mock_get_statement_period_payment_entity_statuses, mock_payment_entity_close_balance_statuses, mock_event, ): """Test getting statement_period_payment_entity 'close_balance' abacus_states.""" statement_period_id = 1 mock_get_statement_period_payment_entity_statuses.return_value = ( mock_payment_entity_close_balance_statuses ) processor = AdjustmentsApplyProcessor(mock_event) res = processor._get_close_balance_statuses(statement_period_id) assert res == {1: 'complete', 2: 'init'} mock_get_statement_period_payment_entity_statuses.assert_called_once_with( statement_period_id ) @patch('adjustments_apply.processor.get_adjustments_by_adjustment_file_id') def test_get_pending_adjustments( mock_get_pending_adjustments, mock_event, mock_pending_adjustments ): """Test get pending adjustments for statement_period_adjustment_file.""" mock_get_pending_adjustments.return_value = { 'items': mock_pending_adjustments, 'total_count': 1000, } processor = AdjustmentsApplyProcessor(mock_event) res = processor._get_pending_adjustments() assert len(res) == len(mock_pending_adjustments) mock_get_pending_adjustments.assert_called_once_with( mock_event.get('target_id'), BATCH_SIZE_GET, 0 ) @patch('adjustments_apply.processor.bulk_insert_ledger_adjustment_applied') def test_insert_ledger_adjustment_applied_entries( mock_bulk_insert_leger_adjustment_applied, mock_event, mock_pending_adjustments, mock_exchange_rates, ): """Test bulk insert for ledger_adjustment_applied records. apply_to_flowthrough_payment is always included in posted entries. """ statement_period_id = 1 adjustment = mock_pending_adjustments[0] processor = AdjustmentsApplyProcessor(mock_event) processor._insert_ledger_adjustment_applied_entries( [ LedgerAdjustmentAppliedItem( adjustment['account_id'], adjustment['contract_id'], adjustment['abacus_event_id'], adjustment['worksheet_adjustment_id'], adjustment['adjustment_currency_code'], adjustment['adjustment_amount'], adjustment['account_currency_code'], mock_exchange_rates[0]['rate'], adjustment['apply_to_flowthrough_payment'], adjustment['reference_adjustment_type_id'], adjustment['details'], ) ], statement_period_id, ) mock_bulk_insert_leger_adjustment_applied.assert_called_once() posted_entries = mock_bulk_insert_leger_adjustment_applied.call_args[0][1] assert 'apply_to_flowthrough_payment' in posted_entries[0] assert ( posted_entries[0]['apply_to_flowthrough_payment'] == adjustment['apply_to_flowthrough_payment'] ) @patch('adjustments_apply.processor.bulk_insert_ledger_entries') def test_insert_ledger_entries( mock_bulk_insert_ledger_entries, mock_event, mock_ledger_accounts, mock_ledger_deposits, ): """Test bulk insert for ledger entries.""" processor = AdjustmentsApplyProcessor(mock_event) processor._insert_ledger_entries(mock_ledger_accounts, mock_ledger_deposits) mock_bulk_insert_ledger_entries.assert_called_once() @patch('adjustments_apply.processor.bulk_insert_ledger_contract_flowthrough_entries') def test__insert_ledger_contract_flowthrough_entries( mock_bulk_insert_ledger_flowthrough_entries, mock_event, mock_ledger_accounts, mock_ledger_deposits, ): """Test bulk insert for ledger_contract_flowthrough entries.""" processor = AdjustmentsApplyProcessor(mock_event) processor._insert_ledger_contract_flowthrough_entries( mock_ledger_accounts, mock_ledger_deposits ) mock_bulk_insert_ledger_flowthrough_entries.assert_called_once() @patch('adjustments_apply.processor.get_current_statement_period') @patch('adjustments_apply.processor.get_exchange_rates_by_statement_period_id') @patch('adjustments_apply.processor.LedgerAdjustmentAppliedProcessor') def test_process_success( mock_ledger_adjustment_applied_processor, mock_get_exchange_rates, mock_get_current_statement_period, mock_current_statement_period, mock_exchange_rates, mock_pending_adjustments, mock_ledger_adjustment_applied, mock_ledger_accounts, mock_ledger_deposits, mock_event, ): """Test AdjustmentsApplyProcessor's main process method.""" statement_period_id = mock_current_statement_period.get('statement_period_id') payment_entity_close_balance_statuses = {1: 'init', 2: 'init'} mock_get_current_statement_period.return_value = mock_current_statement_period mock_get_exchange_rates.return_value = mock_exchange_rates mock_applied_processor = MagicMock( ledger_adjustment_applied_items=mock_ledger_adjustment_applied, ledger_account_entries=mock_ledger_accounts, ledger_deposit_entries=mock_ledger_deposits, ledger_contract_flowthrough_entries=mock_ledger_accounts, ledger_flowthrough_deposit_entries=mock_ledger_deposits, ) AdjustmentsApplyProcessor._get_pending_adjustments = MagicMock( return_value=mock_pending_adjustments ) AdjustmentsApplyProcessor._get_close_balance_statuses = MagicMock( return_value=payment_entity_close_balance_statuses ) AdjustmentsApplyProcessor._can_apply_adjustments = MagicMock(return_value=True) AdjustmentsApplyProcessor._insert_ledger_adjustment_applied_entries = MagicMock() AdjustmentsApplyProcessor._insert_ledger_entries = MagicMock() AdjustmentsApplyProcessor._insert_ledger_contract_flowthrough_entries = MagicMock() mock_ledger_adjustment_applied_processor.return_value = mock_applied_processor processor = AdjustmentsApplyProcessor(mock_event) processor.process() mock_get_current_statement_period.assert_called_once() mock_get_exchange_rates.assert_called_once_with(statement_period_id) AdjustmentsApplyProcessor._get_pending_adjustments.assert_called_once() AdjustmentsApplyProcessor._get_close_balance_statuses.assert_called_once_with( statement_period_id ) AdjustmentsApplyProcessor._can_apply_adjustments.assert_called_once_with( payment_entity_close_balance_statuses, mock_pending_adjustments ) mock_ledger_adjustment_applied_processor.assert_called_once_with( mock_event.get('abacus_event_id'), mock_event.get('target_id'), statement_period_id, mock_exchange_rates, mock_pending_adjustments, ) mock_ledger_adjustment_applied_processor.return_value.process.assert_called_once() AdjustmentsApplyProcessor._insert_ledger_adjustment_applied_entries.assert_called_once_with( mock_ledger_adjustment_applied, statement_period_id ) AdjustmentsApplyProcessor._insert_ledger_entries.assert_called_once_with( mock_ledger_accounts, mock_ledger_deposits ) AdjustmentsApplyProcessor._insert_ledger_contract_flowthrough_entries.assert_called_once_with( mock_ledger_accounts, mock_ledger_deposits ) @patch('adjustments_apply.processor.get_current_statement_period') @patch('adjustments_apply.processor.get_exchange_rates_by_statement_period_id') @patch('adjustments_apply.processor.LedgerAdjustmentAppliedProcessor') def test_process_cannot_apply_adjustments( mock_ledger_adjustment_applied_processor, mock_get_exchange_rates, mock_get_current_statement_period, mock_current_statement_period, mock_exchange_rates, mock_pending_adjustments, mock_event, ): """Test main process method when adjustments cannot be applied.""" statement_period_id = mock_current_statement_period.get('statement_period_id') payment_entity_close_balance_statuses = {1: 'init', 2: 'init'} mock_get_current_statement_period.return_value = mock_current_statement_period mock_get_exchange_rates.return_value = mock_exchange_rates AdjustmentsApplyProcessor._get_pending_adjustments = MagicMock( return_value=mock_pending_adjustments ) AdjustmentsApplyProcessor._get_close_balance_statuses = MagicMock( return_value=payment_entity_close_balance_statuses ) AdjustmentsApplyProcessor._can_apply_adjustments = MagicMock(return_value=False) AdjustmentsApplyProcessor._insert_ledger_adjustment_applied_entries = MagicMock() AdjustmentsApplyProcessor._insert_ledger_entries = MagicMock() mock_ledger_adjustment_applied_processor.return_value = MagicMock() processor = AdjustmentsApplyProcessor(mock_event) processor.process() mock_get_current_statement_period.assert_called_once() mock_get_exchange_rates.assert_called_once_with(statement_period_id) AdjustmentsApplyProcessor._get_pending_adjustments.assert_called_once() AdjustmentsApplyProcessor._get_close_balance_statuses.assert_called_once_with( statement_period_id ) AdjustmentsApplyProcessor._can_apply_adjustments.assert_called_once_with( payment_entity_close_balance_statuses, mock_pending_adjustments ) mock_ledger_adjustment_applied_processor.assert_not_called() mock_ledger_adjustment_applied_processor.return_value.process.assert_not_called() AdjustmentsApplyProcessor._insert_ledger_adjustment_applied_entries.assert_not_called() AdjustmentsApplyProcessor._insert_ledger_entries.assert_not_called()