"""Update Statement Period Payment Entity State logic tests.""" from unittest.mock import patch from abacus_state.constants.constants import ( ABACUS_OUTBOX_EVENT_TYPES, ABACUS_OUTBOX_STATUSES, STATEMENT_PERIOD_PAYMENT_ENTITY_ACTION_NAMES, ) from abacus_state.logic import statement_period_payment_entity as logic from tests.utils.factories import AbacusStateFactory @patch('abacus_state.logic.statement_period_payment_entity.AbacusOutbox') @patch('abacus_state.logic.statement_period_payment_entity.AbacusState') def test_update_state_running_does_not_create_outbox( mock_model, mock_outbox, request_engine ): """Test that non-complete status updates do not create an outbox entry.""" abacus_state = AbacusStateFactory.create( action_name=STATEMENT_PERIOD_PAYMENT_ENTITY_ACTION_NAMES.CLOSE_BALANCE, action_status='init', parent_table_name='statement_period_payment_entity', parent_table_id=1, ) mock_put_request = {'action_status': 'running', 'message': 'testing'} res = logic.update_statement_period_payment_entity_state( abacus_state, **mock_put_request ) assert res.status == 200 assert res.message['action_status'] == 'running' assert res.message['message'] == 'testing' mock_model.commit_changes.assert_called_once() mock_outbox.build.assert_not_called() @patch('abacus_state.logic.statement_period_payment_entity.AbacusOutbox') @patch('abacus_state.logic.statement_period_payment_entity.AbacusState') def test_update_state_complete_creates_outbox(mock_model, mock_outbox, request_engine): """Test that completing the state creates an outbox entry.""" abacus_state = AbacusStateFactory.create( action_name=STATEMENT_PERIOD_PAYMENT_ENTITY_ACTION_NAMES.CLOSE_BALANCE, action_status='running', parent_table_name='statement_period_payment_entity', parent_table_id=42, ) mock_put_request = {'action_status': 'complete'} res = logic.update_statement_period_payment_entity_state( abacus_state, **mock_put_request ) assert res.status == 200 assert res.message['action_status'] == 'complete' mock_model.commit_changes.assert_called_once() mock_outbox.build.assert_called_once_with( target_type='statement_period_payment_entity', target_id=42, event_type=ABACUS_OUTBOX_EVENT_TYPES.CLOSE_BALANCE_COMPLETED, status=ABACUS_OUTBOX_STATUSES.PENDING, )