diff --git a/migrations/env.py b/migrations/env.py index 630e381a..68feded2 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -1,3 +1,5 @@ +from __future__ import with_statement + import logging from logging.config import fileConfig diff --git a/migrations/versions/b7face8078df_.py b/migrations/versions/907bcf0c3d75_.py similarity index 99% rename from migrations/versions/b7face8078df_.py rename to migrations/versions/907bcf0c3d75_.py index 86ff3bcb..552afe48 100644 --- a/migrations/versions/b7face8078df_.py +++ b/migrations/versions/907bcf0c3d75_.py @@ -1,8 +1,8 @@ """empty message -Revision ID: b7face8078df +Revision ID: 907bcf0c3d75 Revises: -Create Date: 2022-12-22 21:12:03.739025 +Create Date: 2022-12-28 13:52:13.030028 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'b7face8078df' +revision = '907bcf0c3d75' down_revision = None branch_labels = None depends_on = None diff --git a/src/spiffworkflow_backend/models/process_instance.py b/src/spiffworkflow_backend/models/process_instance.py index f41897fd..d1d63510 100644 --- a/src/spiffworkflow_backend/models/process_instance.py +++ b/src/spiffworkflow_backend/models/process_instance.py @@ -60,10 +60,15 @@ class ProcessInstanceModel(SpiffworkflowBaseDBModel): process_initiator_id: int = db.Column(ForeignKey(UserModel.id), nullable=False) process_initiator = relationship("UserModel") + active_human_tasks = relationship( + "HumanTaskModel", + primaryjoin="and_(HumanTaskModel.process_instance_id==ProcessInstanceModel.id, HumanTaskModel.completed == False)", + ) # type: ignore + human_tasks = relationship( "HumanTaskModel", cascade="delete", - primaryjoin="and_(HumanTaskModel.process_instance_id==ProcessInstanceModel.id, HumanTaskModel.completed == False)", + overlaps="active_human_tasks", ) # type: ignore message_instances = relationship("MessageInstanceModel", cascade="delete") # type: ignore message_correlations = relationship("MessageCorrelationModel", cascade="delete") # type: ignore diff --git a/tests/spiffworkflow_backend/scripts/test_get_localtime.py b/tests/spiffworkflow_backend/scripts/test_get_localtime.py index 8f3864fe..90e4158d 100644 --- a/tests/spiffworkflow_backend/scripts/test_get_localtime.py +++ b/tests/spiffworkflow_backend/scripts/test_get_localtime.py @@ -68,7 +68,7 @@ class TestGetLocaltime(BaseTest): processor = ProcessInstanceProcessor(process_instance) processor.do_engine_steps(save=True) - human_task = process_instance.human_tasks[0] + human_task = process_instance.active_human_tasks[0] spiff_task = processor.__class__.get_task_by_bpmn_identifier( human_task.task_name, processor.bpmn_process_instance ) @@ -81,7 +81,7 @@ class TestGetLocaltime(BaseTest): human_task, ) - human_task = process_instance.human_tasks[0] + human_task = process_instance.active_human_tasks[0] spiff_task = processor.__class__.get_task_by_bpmn_identifier( human_task.task_name, processor.bpmn_process_instance ) diff --git a/tests/spiffworkflow_backend/unit/test_authorization_service.py b/tests/spiffworkflow_backend/unit/test_authorization_service.py index 51ccaf29..83ed7fd8 100644 --- a/tests/spiffworkflow_backend/unit/test_authorization_service.py +++ b/tests/spiffworkflow_backend/unit/test_authorization_service.py @@ -125,7 +125,7 @@ class TestAuthorizationService(BaseTest): ) processor = ProcessInstanceProcessor(process_instance) processor.do_engine_steps(save=True) - human_task = process_instance.human_tasks[0] + human_task = process_instance.active_human_tasks[0] spiff_task = processor.__class__.get_task_by_bpmn_identifier( human_task.task_name, processor.bpmn_process_instance ) @@ -133,7 +133,7 @@ class TestAuthorizationService(BaseTest): processor, spiff_task, {}, initiator_user, human_task ) - human_task = process_instance.human_tasks[0] + human_task = process_instance.active_human_tasks[0] spiff_task = processor.__class__.get_task_by_bpmn_identifier( human_task.task_name, processor.bpmn_process_instance ) diff --git a/tests/spiffworkflow_backend/unit/test_process_instance_processor.py b/tests/spiffworkflow_backend/unit/test_process_instance_processor.py index 1a96ca88..e727dba2 100644 --- a/tests/spiffworkflow_backend/unit/test_process_instance_processor.py +++ b/tests/spiffworkflow_backend/unit/test_process_instance_processor.py @@ -31,10 +31,14 @@ class TestProcessInstanceProcessor(BaseTest): with_db_and_bpmn_file_cleanup: None, ) -> None: """Test_script_engine_takes_data_and_returns_expected_results.""" + app.config["THREAD_LOCAL_DATA"].process_model_identifier = 'hey' + app.config["THREAD_LOCAL_DATA"].process_instance_id = 0 script_engine = ProcessInstanceProcessor._script_engine result = script_engine._evaluate("a", {"a": 1}) assert result == 1 + app.config["THREAD_LOCAL_DATA"].process_model_identifier = None + app.config["THREAD_LOCAL_DATA"].process_instance_id = None def test_script_engine_can_use_custom_scripts( self, @@ -42,12 +46,16 @@ class TestProcessInstanceProcessor(BaseTest): with_db_and_bpmn_file_cleanup: None, ) -> None: """Test_script_engine_takes_data_and_returns_expected_results.""" + app.config["THREAD_LOCAL_DATA"].process_model_identifier = 'hey' + app.config["THREAD_LOCAL_DATA"].process_instance_id = 0 script_engine = ProcessInstanceProcessor._script_engine result = script_engine._evaluate("fact_service(type='norris')", {}) assert ( result == "Chuck Norris doesn’t read books. He stares them down until he gets the information he wants." ) + app.config["THREAD_LOCAL_DATA"].process_model_identifier = None + app.config["THREAD_LOCAL_DATA"].process_instance_id = None def test_sets_permission_correctly_on_human_task( self, @@ -80,8 +88,8 @@ class TestProcessInstanceProcessor(BaseTest): processor = ProcessInstanceProcessor(process_instance) processor.do_engine_steps(save=True) - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] assert human_task.lane_assignment_id is None assert len(human_task.potential_owners) == 1 assert human_task.potential_owners[0] == initiator_user @@ -97,8 +105,8 @@ class TestProcessInstanceProcessor(BaseTest): processor, spiff_task, {}, initiator_user, human_task ) - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] assert human_task.lane_assignment_id == finance_group.id assert len(human_task.potential_owners) == 1 assert human_task.potential_owners[0] == finance_user @@ -114,8 +122,8 @@ class TestProcessInstanceProcessor(BaseTest): ProcessInstanceService.complete_form_task( processor, spiff_task, {}, finance_user, human_task ) - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] assert human_task.lane_assignment_id is None assert len(human_task.potential_owners) == 1 assert human_task.potential_owners[0] == initiator_user @@ -163,8 +171,8 @@ class TestProcessInstanceProcessor(BaseTest): processor.do_engine_steps(save=True) processor.save() - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] assert human_task.lane_assignment_id is None assert len(human_task.potential_owners) == 1 assert human_task.potential_owners[0] == initiator_user @@ -181,8 +189,8 @@ class TestProcessInstanceProcessor(BaseTest): ) assert human_task.completed_by_user_id == initiator_user.id - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] assert human_task.lane_assignment_id is None assert len(human_task.potential_owners) == 2 assert human_task.potential_owners == [finance_user_three, finance_user_four] @@ -200,8 +208,8 @@ class TestProcessInstanceProcessor(BaseTest): processor, spiff_task, {}, finance_user_three, human_task ) assert human_task.completed_by_user_id == finance_user_three.id - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] assert human_task.lane_assignment_id is None assert len(human_task.potential_owners) == 1 assert human_task.potential_owners[0] == finance_user_four @@ -218,8 +226,8 @@ class TestProcessInstanceProcessor(BaseTest): processor, spiff_task, {}, finance_user_four, human_task ) assert human_task.completed_by_user_id == finance_user_four.id - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] assert human_task.lane_assignment_id is None assert len(human_task.potential_owners) == 1 assert human_task.potential_owners[0] == initiator_user @@ -231,8 +239,8 @@ class TestProcessInstanceProcessor(BaseTest): processor, spiff_task, {}, initiator_user, human_task ) - assert len(process_instance.human_tasks) == 1 - human_task = process_instance.human_tasks[0] + assert len(process_instance.active_human_tasks) == 1 + human_task = process_instance.active_human_tasks[0] spiff_task = processor.__class__.get_task_by_bpmn_identifier( human_task.task_name, processor.bpmn_process_instance ) @@ -276,11 +284,11 @@ class TestProcessInstanceProcessor(BaseTest): ) processor = ProcessInstanceProcessor(process_instance) processor.do_engine_steps(save=True) - assert len(process_instance.human_tasks) == 1 - initial_human_task_id = process_instance.human_tasks[0].id + assert len(process_instance.active_human_tasks) == 1 + initial_human_task_id = process_instance.active_human_tasks[0].id # save again to ensure we go attempt to process the human tasks again processor.save() - assert len(process_instance.human_tasks) == 1 - assert initial_human_task_id == process_instance.human_tasks[0].id + assert len(process_instance.active_human_tasks) == 1 + assert initial_human_task_id == process_instance.active_human_tasks[0].id