From b62d9f88677f6d40fb55e8dc688ac6511e1be416 Mon Sep 17 00:00:00 2001 From: jasquat Date: Wed, 28 Dec 2022 14:02:39 -0500 Subject: [PATCH] delete human tasks when process instance is deleted w/ burnettk --- spiffworkflow-backend/migrations/env.py | 2 + .../{b7face8078df_.py => 907bcf0c3d75_.py} | 6 +-- .../models/process_instance.py | 7 ++- .../scripts/test_get_localtime.py | 4 +- .../unit/test_authorization_service.py | 4 +- .../unit/test_process_instance_processor.py | 48 +++++++++++-------- 6 files changed, 43 insertions(+), 28 deletions(-) rename spiffworkflow-backend/migrations/versions/{b7face8078df_.py => 907bcf0c3d75_.py} (99%) diff --git a/spiffworkflow-backend/migrations/env.py b/spiffworkflow-backend/migrations/env.py index 630e381ad..68feded2a 100644 --- a/spiffworkflow-backend/migrations/env.py +++ b/spiffworkflow-backend/migrations/env.py @@ -1,3 +1,5 @@ +from __future__ import with_statement + import logging from logging.config import fileConfig diff --git a/spiffworkflow-backend/migrations/versions/b7face8078df_.py b/spiffworkflow-backend/migrations/versions/907bcf0c3d75_.py similarity index 99% rename from spiffworkflow-backend/migrations/versions/b7face8078df_.py rename to spiffworkflow-backend/migrations/versions/907bcf0c3d75_.py index 86ff3bcb6..552afe485 100644 --- a/spiffworkflow-backend/migrations/versions/b7face8078df_.py +++ b/spiffworkflow-backend/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/spiffworkflow-backend/src/spiffworkflow_backend/models/process_instance.py b/spiffworkflow-backend/src/spiffworkflow_backend/models/process_instance.py index f41897fd7..d1d63510f 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/models/process_instance.py +++ b/spiffworkflow-backend/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/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_localtime.py b/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_localtime.py index 8f3864fe8..90e4158da 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/scripts/test_get_localtime.py +++ b/spiffworkflow-backend/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/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_authorization_service.py b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_authorization_service.py index 51ccaf294..83ed7fd8e 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_authorization_service.py +++ b/spiffworkflow-backend/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/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_process_instance_processor.py b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_process_instance_processor.py index 1a96ca882..e727dba2f 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_process_instance_processor.py +++ b/spiffworkflow-backend/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