"""Abacus State model tests.""" from abacus_state.constants.constants import ( ACCOUNTING_PERIOD_ACTION_NAMES, ACTION_STATUSES, ) from abacus_state.models.abacus_state import AbacusState from tests.utils.factories import AbacusStateFactory def test_get_action_status_list(): """Test to get action statuses list.""" parent_table_name = 'accounting_period' period_ids = [1, 2] for period_id in period_ids: AbacusStateFactory.create( parent_table_name=parent_table_name, parent_table_id=period_id ) assert AbacusState.count() == 2 statuses_list = AbacusState.get_action_status_list( parent_table_name='accounting_period', parent_table_id=1 ) assert len(statuses_list) == 1 assert all(item.parent_table_id == 1 for item in statuses_list) def test_get_formatted_action_statuses(): """Test to get formatted action statuses.""" action_name = ACCOUNTING_PERIOD_ACTION_NAMES.DELIVER_SALES_FILES parent_table_name = 'accounting_period' period_id = 1 AbacusStateFactory.create( action_name=action_name, action_status=ACTION_STATUSES.RUNNING, parent_table_name=parent_table_name, parent_table_id=period_id, ) statuses_list = AbacusState.get_formatted_action_statuses( parent_table_name=parent_table_name, parent_table_id=period_id ) assert statuses_list == {action_name: ACTION_STATUSES.RUNNING} def test_get_filtered_query(test_app): """Test get_filtered_query method.""" test_state1 = AbacusStateFactory.create(action_name='send_payments') AbacusStateFactory.create(action_name='payments_generate') assert AbacusState.get_filtered_query( state_ids=[test_state1.abacus_state_id] ).all() == [test_state1] def test_reset_abacus_state_actions(test_app): """Test reset_abacus_state_actions method.""" parent_table_name = 'statement_period_adjustment_file' parent_table_id = 9999 AbacusStateFactory.create( parent_table_name=parent_table_name, parent_table_id=parent_table_id, action_name='upload_file', action_status=ACTION_STATUSES.COMPLETE, ) AbacusStateFactory.create( parent_table_name=parent_table_name, parent_table_id=parent_table_id, action_name='validate_file', action_status=ACTION_STATUSES.COMPLETE, ) AbacusStateFactory.create( parent_table_name=parent_table_name, parent_table_id=parent_table_id, action_name='import_file', action_status=ACTION_STATUSES.ERROR, ) result = AbacusState.reset_abacus_state_actions(parent_table_name, parent_table_id) assert ( all([state.action_status == ACTION_STATUSES.INIT for state in result]) is True )