From 53a88117695204fcecadc3d89204d7004bea8601 Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Wed, 18 Dec 2019 15:22:46 -0500 Subject: [PATCH] Add workflow to study --- crc/api.py | 7 ++++++- crc/models.py | 1 - crc/workflow_processor.py | 12 ++++++------ tests/test_api.py | 14 +++++++++++--- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/crc/api.py b/crc/api.py index 0f1089a8..c954166b 100644 --- a/crc/api.py +++ b/crc/api.py @@ -52,8 +52,13 @@ def add_workflow_to_study(study_id, body): if workflow_spec_model is None: error = ApiError('unknown_spec', 'The specification "' + body['id'] + '" is not recognized.') return ApiErrorSchema.dump(error), 404 - processor = WorkflowProcessor.create(workflow_spec_model.id) + processor = WorkflowProcessor.create(workflow_spec_model.id) + workflow = WorkflowModel(bpmn_workflow_json=processor.serialize(), + status=processor.get_status(), + study_id=study_id, + workflow_spec_id=workflow_spec_model.id) + db.session.add(workflow) def get_workflow(workflow_id): return db.session.query(WorkflowModel).filter_by(id=workflow_id).first() diff --git a/crc/models.py b/crc/models.py index a56c17bc..8626eca1 100644 --- a/crc/models.py +++ b/crc/models.py @@ -55,7 +55,6 @@ class WorkflowModel(db.Model): status = db.Column(db.Enum(WorkflowStatus)) study_id = db.Column(db.Integer, db.ForeignKey('study.id')) workflow_spec_id = db.Column(db.Integer, db.ForeignKey('workflow_spec.id')) - messages: db.Column class WorkflowSchema(ModelSchema): class Meta: diff --git a/crc/workflow_processor.py b/crc/workflow_processor.py index e224f062..99a7ab6e 100644 --- a/crc/workflow_processor.py +++ b/crc/workflow_processor.py @@ -33,12 +33,12 @@ class CustomBpmnScriptEngine(BpmnScriptEngine): class WorkflowProcessor: - script_engine = CustomBpmnScriptEngine() - serializer = CompactWorkflowSerializer() + _script_engine = CustomBpmnScriptEngine() + _serializer = CompactWorkflowSerializer() def __init__(self, workflow_spec_id, bpmn_json): - self.bpmn_workflow = self.serializer.deserialize_workflow(bpmn_json, self.get_spec(workflow_spec_id)) - self.bpmn_workflow.script_engine = self.script_engine + self.bpmn_workflow = self._serializer.deserialize_workflow(bpmn_json, self.get_spec(workflow_spec_id)) + self.bpmn_workflow.script_engine = self._script_engine @staticmethod def get_spec(workflow_spec_id): @@ -48,9 +48,9 @@ class WorkflowProcessor: @classmethod def create(cls, workflow_spec_id): spec = cls.get_spec(workflow_spec_id) - bpmn_workflow = BpmnWorkflow(spec, script_engine=cls.script_engine) + bpmn_workflow = BpmnWorkflow(spec, script_engine=cls._script_engine) bpmn_workflow.do_engine_steps() - json = cls.serializer.serialize_workflow(bpmn_workflow) + json = cls._serializer.serialize_workflow(bpmn_workflow) processor = cls(workflow_spec_id, json) return processor diff --git a/tests/test_api.py b/tests/test_api.py index af9b6e1d..ed0e913c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,7 +1,10 @@ import json import unittest + +from sqlalchemy import func + from crc import db -from crc.models import StudyModel, StudySchema, WorkflowSpecModel, WorkflowSpecSchema +from crc.models import StudyModel, StudySchema, WorkflowSpecModel, WorkflowSpecSchema, WorkflowModel, WorkflowStatus from tests.base_test import BaseTest @@ -47,9 +50,14 @@ class TestStudy(BaseTest, unittest.TestCase): def test_add_workflow_to_study(self): self.load_example_data() study = db.session.query(StudyModel).first() + self.assertEqual(0, db.session.query(WorkflowModel).count()) spec = db.session.query(WorkflowSpecModel).first() rv = self.app.post('/v1.0/study/%i/workflows' % study.id,content_type="application/json", data=json.dumps(WorkflowSpecSchema().dump(spec))) self.assert_success(rv) - - + self.assertEqual(1, db.session.query(WorkflowModel).count()) + workflow = db.session.query(WorkflowModel).first() + self.assertEqual(study.id, workflow.study_id) + self.assertEqual(WorkflowStatus.user_input_required, workflow.status) + self.assertIsNotNone(workflow.bpmn_workflow_json) + self.assertEqual(spec.id, workflow.workflow_spec_id) \ No newline at end of file