"""Tests for updating statement period state.""" from unittest.mock import patch from abacus_state.constants.constants import ( STATEMENT_PERIOD_ACTION_NAMES, STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES, ) from abacus_state.constants.error import ( ERROR_ADJUSTMENTS_NOT_APPLIED, ERROR_PREVIOUS_ACTIONS_STATUS, ) from abacus_state.logic import statement_period_state from tests.utils.factories import AbacusStateFactory @patch('abacus_state.logic.statement_period_state._validate_statement_period_actions') @patch('abacus_state.logic.statement_period_state.AbacusState') def test_update_statement_period_state(mock_model, mock_validation): """Test update_statement_period_state method.""" statement_period_id = 123 abacus_state = AbacusStateFactory.create( action_name=STATEMENT_PERIOD_ACTION_NAMES.UPLOAD_EXCHANGE_RATES, action_status='init', parent_table_name='statement_period', parent_table_id=statement_period_id, ) mock_put_request = {'action_status': 'complete', 'message': 'testing'} mock_validation.return_value = True res = statement_period_state.update_statement_period_state( abacus_state, **mock_put_request ) assert res.status == 200 mock_model.commit_changes.assert_called_once() @patch('abacus_state.logic.statement_period_state._validate_statement_period_actions') @patch('abacus_state.logic.statement_period_state.AbacusState') def test_update_statement_period_state_validation_error(mock_model, mock_validation): """Test update_statement_period_state method.""" statement_period_id = 123 abacus_state = AbacusStateFactory.create( action_name=STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE, action_status='init', parent_table_name='statement_period', parent_table_id=statement_period_id, ) mock_put_request = {'action_status': 'complete', 'message': 'testing'} mock_validation.return_value = False res = statement_period_state.update_statement_period_state( abacus_state, **mock_put_request ) assert res.status == 400 res.errors['message'] == ERROR_PREVIOUS_ACTIONS_STATUS.format( STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE ) + ' OR ' + ERROR_ADJUSTMENTS_NOT_APPLIED def test_validate_statement_period_actions(): """Test _validate_statement_period_actions -- a previous action is not completed.""" parent_table_id = 1 actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'init'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.RELEASE_RESERVES res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert not res def test_validate_statement_period_actions_success(): """Test _validate_statement_period_actions -- previous actions are complete.""" parent_table_id = 1 actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'complete'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE actions.update({action_name: 'init'}) res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert res @patch('abacus_state.logic.statement_period_state.AbacusState') @patch('abacus_state.logic.statement_period_state.StatementPeriodAdjustmentFile') def test_validate_statement_period_actions_file_not_uploaded( mock_adjustment_file_model, mock_abacus_state_model ): """Test _validate_statement_period_actions -- adjustment file is not uploaded.""" statement_period_adjustment_file_id = 1 adjustment_file_action_name = ( STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.UPLOAD_FILE ) mock_adjustment_file_model.get_adjustment_files_by_statement_period_id.return_value = [ {'statement_period_adjustment_file_id': statement_period_adjustment_file_id} ] abacus_state = AbacusStateFactory.create( action_name=adjustment_file_action_name, action_status='init', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) mock_abacus_state_model.get_formatted_action_statuses.return_value = { adjustment_file_action_name: abacus_state.action_status } parent_table_id = statement_period_adjustment_file_id actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'complete'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert res is True @patch('abacus_state.logic.statement_period_state.AbacusState') @patch('abacus_state.logic.statement_period_state.StatementPeriodAdjustmentFile') def test_validate_statement_period_actions_file_not_imported( mock_adjustment_file_model, mock_abacus_state_model ): """Test _validate_statement_period_actions. Where adjustment file is uploaded but not imported. """ statement_period_adjustment_file_id = 1 upload_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.UPLOAD_FILE import_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.IMPORT_FILE mock_adjustment_file_model.get_adjustment_files_by_statement_period_id.return_value = [ {'statement_period_adjustment_file_id': statement_period_adjustment_file_id} ] upload_file_state = AbacusStateFactory.create( action_name=upload_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) import_file_state = AbacusStateFactory.create( action_name=import_file_action, action_status='init', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) mock_abacus_state_model.get_formatted_action_statuses.return_value = { upload_file_action: upload_file_state.action_status, import_file_action: import_file_state.action_status, } parent_table_id = statement_period_adjustment_file_id actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'complete'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert res is True @patch('abacus_state.logic.statement_period_state.AbacusState') @patch('abacus_state.logic.statement_period_state.StatementPeriodAdjustmentFile') def test_validate_statement_period_actions_file_not_approved( mock_adjustment_file_model, mock_abacus_state_model ): """Test _validate_statement_period_actions. Where adjustment file is uploaded but not approved. """ statement_period_adjustment_file_id = 1 upload_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.UPLOAD_FILE import_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.IMPORT_FILE approve_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.APPROVE_FILE mock_adjustment_file_model.get_adjustment_files_by_statement_period_id.return_value = [ {'statement_period_adjustment_file_id': statement_period_adjustment_file_id} ] upload_file_state = AbacusStateFactory.create( action_name=upload_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) import_file_state = AbacusStateFactory.create( action_name=import_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) approve_file_state = AbacusStateFactory.create( action_name=approve_file_action, action_status='init', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) mock_abacus_state_model.get_formatted_action_statuses.return_value = { upload_file_action: upload_file_state.action_status, import_file_action: import_file_state.action_status, approve_file_action: approve_file_state.action_status, } parent_table_id = statement_period_adjustment_file_id actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'complete'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert res is False @patch('abacus_state.logic.statement_period_state.AbacusState') @patch('abacus_state.logic.statement_period_state.StatementPeriodAdjustmentFile') def test_validate_statement_period_actions_file_not_applied( mock_adjustment_file_model, mock_abacus_state_model ): """Test _validate_statement_period_actions. Where adjustment file is uploaded and approved, but not applied. """ statement_period_adjustment_file_id = 1 upload_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.UPLOAD_FILE import_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.IMPORT_FILE approve_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.APPROVE_FILE apply_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.APPLY_FILE mock_adjustment_file_model.get_adjustment_files_by_statement_period_id.return_value = [ {'statement_period_adjustment_file_id': statement_period_adjustment_file_id} ] upload_file_state = AbacusStateFactory.create( action_name=upload_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) import_file_state = AbacusStateFactory.create( action_name=import_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) approve_file_state = AbacusStateFactory.create( action_name=approve_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) apply_file_state = AbacusStateFactory.create( action_name=apply_file_action, action_status='init', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) mock_abacus_state_model.get_formatted_action_statuses.return_value = { upload_file_action: upload_file_state.action_status, import_file_action: import_file_state.action_status, approve_file_action: approve_file_state.action_status, apply_file_action: apply_file_state.action_status, } parent_table_id = statement_period_adjustment_file_id actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'complete'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert res is False @patch('abacus_state.logic.statement_period_state.AbacusState') @patch('abacus_state.logic.statement_period_state.StatementPeriodAdjustmentFile') def test_validate_statement_period_actions_file_applied( mock_adjustment_file_model, mock_abacus_state_model ): """Test _validate_statement_period_actions. Where adjustment file is uploaded, approved, and applied. """ statement_period_adjustment_file_id = 1 upload_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.UPLOAD_FILE import_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.IMPORT_FILE approve_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.APPROVE_FILE apply_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.APPLY_FILE mock_adjustment_file_model.get_adjustment_files_by_statement_period_id.return_value = [ {'statement_period_adjustment_file_id': statement_period_adjustment_file_id} ] upload_file_state = AbacusStateFactory.create( action_name=upload_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) import_file_state = AbacusStateFactory.create( action_name=import_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) approve_file_state = AbacusStateFactory.create( action_name=approve_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) apply_file_state = AbacusStateFactory.create( action_name=apply_file_action, action_status='complete', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) mock_abacus_state_model.get_formatted_action_statuses.return_value = { upload_file_action: upload_file_state.action_status, import_file_action: import_file_state.action_status, approve_file_action: approve_file_state.action_status, apply_file_action: apply_file_state.action_status, } parent_table_id = statement_period_adjustment_file_id actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'complete'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert res is True @patch('abacus_state.logic.statement_period_state.AbacusState') @patch('abacus_state.logic.statement_period_state.StatementPeriodAdjustmentFile') def test_validate_statement_period_actions_file_upload_error( mock_adjustment_file_model, mock_abacus_state_model ): """Test _validate_statement_period_actions. Where adjustment file is uploaded but there is an error. """ statement_period_adjustment_file_id = 1 upload_file_action = STATEMENT_PERIOD_ADJUSTMENT_FILE_ACTION_NAMES.UPLOAD_FILE mock_adjustment_file_model.get_adjustment_files_by_statement_period_id.return_value = [ {'statement_period_adjustment_file_id': statement_period_adjustment_file_id} ] upload_file_state = AbacusStateFactory.create( action_name=upload_file_action, action_status='error', parent_table_name='statement_period_adjustment_file', parent_table_id=statement_period_adjustment_file_id, ) mock_abacus_state_model.get_formatted_action_statuses.return_value = { upload_file_action: upload_file_state.action_status } parent_table_id = statement_period_adjustment_file_id actions = dict() for action in STATEMENT_PERIOD_ACTION_NAMES: actions.update({action: 'complete'}) action_name = STATEMENT_PERIOD_ACTION_NAMES.STATEMENT_PERIOD_CLOSE res = statement_period_state._validate_statement_period_actions( actions, action_name, parent_table_id ) assert res is True