2019-12-18 19:02:17 +00:00
|
|
|
import json
|
|
|
|
import unittest
|
2020-01-14 16:45:12 +00:00
|
|
|
from datetime import datetime
|
2019-12-18 20:22:46 +00:00
|
|
|
|
2020-01-14 16:45:12 +00:00
|
|
|
from crc import session
|
2020-01-03 16:44:24 +00:00
|
|
|
from crc.models.study import StudyModel, StudyModelSchema, ProtocolBuilderStatus
|
|
|
|
from crc.models.workflow import WorkflowSpecModel, WorkflowSpecModelSchema, WorkflowModel, WorkflowStatus, \
|
|
|
|
WorkflowModelSchema, TaskSchema
|
2019-12-18 19:02:17 +00:00
|
|
|
from tests.base_test import BaseTest
|
|
|
|
|
|
|
|
|
|
|
|
class TestStudy(BaseTest, unittest.TestCase):
|
|
|
|
|
|
|
|
def test_study_basics(self):
|
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).first()
|
2019-12-18 19:02:17 +00:00
|
|
|
self.assertIsNotNone(study)
|
|
|
|
|
2020-01-03 16:44:24 +00:00
|
|
|
def test_add_study(self):
|
2020-01-14 16:46:59 +00:00
|
|
|
self.load_example_data()
|
2020-01-03 16:44:24 +00:00
|
|
|
study = {
|
|
|
|
"id": 12345,
|
|
|
|
"title": "Phase III Trial of Genuine People Personalities (GPP) Autonomous Intelligent Emotional Agents for Interstellar Spacecraft",
|
|
|
|
"last_updated": datetime.now(),
|
|
|
|
"protocol_builder_status": ProtocolBuilderStatus.in_process,
|
|
|
|
"primary_investigator_id": "tricia.marie.mcmillan@heartofgold.edu",
|
|
|
|
"sponsor": "Sirius Cybernetics Corporation",
|
|
|
|
"ind_number": "567890",
|
|
|
|
}
|
|
|
|
rv = self.app.post('/v1.0/study',
|
|
|
|
content_type="application/json",
|
|
|
|
data=json.dumps(StudyModelSchema().dump(study)))
|
|
|
|
self.assert_success(rv)
|
2020-01-14 16:46:59 +00:00
|
|
|
db_study = session.query(StudyModel).filter_by(id=12345).first()
|
2020-01-03 16:44:24 +00:00
|
|
|
self.assertIsNotNone(db_study)
|
|
|
|
self.assertEqual(study["title"], db_study.title)
|
|
|
|
self.assertEqual(study["last_updated"], db_study.last_updated)
|
|
|
|
self.assertEqual(study["protocol_builder_status"], db_study.protocol_builder_status)
|
|
|
|
self.assertEqual(study["primary_investigator_id"], db_study.primary_investigator_id)
|
|
|
|
self.assertEqual(study["sponsor"], db_study.sponsor)
|
|
|
|
self.assertEqual(study["ind_number"], db_study.ind_number)
|
|
|
|
|
|
|
|
def test_update_study(self):
|
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
study: StudyModel = session.query(StudyModel).first()
|
2020-01-03 16:44:24 +00:00
|
|
|
study.title = "Pilot Study of Fjord Placement for Single Fraction Outcomes to Cortisol Susceptibility"
|
|
|
|
study.protocol_builder_status = ProtocolBuilderStatus.complete
|
|
|
|
|
|
|
|
rv = self.app.post('/v1.0/study',
|
|
|
|
content_type="application/json",
|
|
|
|
data=json.dumps(StudyModelSchema().dump(study)))
|
|
|
|
self.assert_success(rv)
|
2020-01-14 16:45:12 +00:00
|
|
|
db_study = session.query(StudyModel).first()
|
2020-01-03 16:44:24 +00:00
|
|
|
self.assertIsNotNone(db_study)
|
|
|
|
self.assertEqual(study.title, db_study.title)
|
|
|
|
self.assertEqual(study.protocol_builder_status, db_study.protocol_builder_status)
|
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
def test_study_api_get_single_study(self):
|
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).first()
|
2019-12-18 19:02:17 +00:00
|
|
|
rv = self.app.get('/v1.0/study/%i' % study.id,
|
|
|
|
follow_redirects=True,
|
|
|
|
content_type="application/json")
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
2020-01-14 16:45:12 +00:00
|
|
|
study2 = StudyModelSchema().load(json_data, session=session)
|
2019-12-18 19:02:17 +00:00
|
|
|
self.assertEqual(study, study2)
|
|
|
|
self.assertEqual(study.id, study2.id)
|
|
|
|
self.assertEqual(study.title, study2.title)
|
|
|
|
self.assertEqual(study.last_updated, study2.last_updated)
|
|
|
|
self.assertEqual(study.protocol_builder_status, study2.protocol_builder_status)
|
|
|
|
self.assertEqual(study.primary_investigator_id, study2.primary_investigator_id)
|
|
|
|
self.assertEqual(study.sponsor, study2.sponsor)
|
|
|
|
self.assertEqual(study.ind_number, study2.ind_number)
|
|
|
|
|
|
|
|
def test_list_workflow_specifications(self):
|
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
spec = session.query(WorkflowSpecModel).first()
|
2019-12-18 19:02:17 +00:00
|
|
|
rv = self.app.get('/v1.0/workflow-specification',
|
|
|
|
follow_redirects=True,
|
|
|
|
content_type="application/json")
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
2020-01-14 16:45:12 +00:00
|
|
|
specs = WorkflowSpecModelSchema(many=True).load(json_data, session=session)
|
2019-12-18 19:02:17 +00:00
|
|
|
spec2 = specs[0]
|
|
|
|
self.assertEqual(spec.id, spec2.id)
|
|
|
|
self.assertEqual(spec.display_name, spec2.display_name)
|
|
|
|
self.assertEqual(spec.description, spec2.description)
|
|
|
|
|
2020-01-14 02:43:09 +00:00
|
|
|
def test_add_new_workflow_specification(self):
|
|
|
|
self.load_example_data();
|
2020-01-14 16:45:12 +00:00
|
|
|
num_before = session.query(WorkflowSpecModel).count()
|
|
|
|
spec = WorkflowSpecModel(id='make_cookies', display_name='Cooooookies',
|
|
|
|
description='Om nom nom delicious cookies')
|
2020-01-14 02:43:09 +00:00
|
|
|
rv = self.app.post('/v1.0/workflow-specification', content_type="application/json",
|
|
|
|
data=json.dumps(WorkflowSpecModelSchema().dump(spec)))
|
|
|
|
self.assert_success(rv)
|
2020-01-14 16:45:12 +00:00
|
|
|
db_spec = session.query(WorkflowSpecModel).filter_by(id='make_cookies').first()
|
2020-01-14 02:43:09 +00:00
|
|
|
self.assertEqual(spec.display_name, db_spec.display_name)
|
2020-01-14 16:45:12 +00:00
|
|
|
num_after = session.query(WorkflowSpecModel).count()
|
2020-01-14 02:43:09 +00:00
|
|
|
self.assertEqual(num_after, num_before + 1)
|
|
|
|
|
2020-01-14 19:10:53 +00:00
|
|
|
def test_modify_workflow_specification(self):
|
|
|
|
self.load_example_data()
|
|
|
|
old_id = 'random_fact'
|
2020-01-21 20:24:17 +00:00
|
|
|
spec: WorkflowSpecModel = session.query(WorkflowSpecModel).filter_by(id=old_id).first()
|
2020-01-14 19:10:53 +00:00
|
|
|
""":type: WorkflowSpecModel"""
|
|
|
|
|
|
|
|
spec.id = 'odd_datum'
|
|
|
|
num_before = session.query(WorkflowSpecModel).count()
|
|
|
|
|
|
|
|
rv = self.app.post('/v1.0/workflow-specification?spec_id=%s' % old_id, content_type="application/json",
|
|
|
|
data=json.dumps(WorkflowSpecModelSchema().dump(spec)))
|
|
|
|
self.assert_success(rv)
|
|
|
|
db_spec = session.query(WorkflowSpecModel).filter_by(id=spec.id).first()
|
|
|
|
self.assertEqual(spec.display_name, db_spec.display_name)
|
2020-01-21 20:24:17 +00:00
|
|
|
|
|
|
|
num_old_after = session.query(WorkflowSpecModel).filter_by(id=old_id).count()
|
|
|
|
self.assertEqual(num_old_after, 0)
|
|
|
|
|
2020-01-14 19:10:53 +00:00
|
|
|
num_after = session.query(WorkflowSpecModel).count()
|
|
|
|
self.assertEqual(num_after, num_before)
|
|
|
|
|
2020-01-23 21:14:05 +00:00
|
|
|
def test_delete_workflow_specification(self):
|
|
|
|
self.load_example_data()
|
|
|
|
spec_id = 'random_fact'
|
|
|
|
rv = self.app.delete('/v1.0/workflow-specification/' + spec_id)
|
|
|
|
self.assert_success(rv)
|
|
|
|
db_spec = session.query(WorkflowSpecModel).filter_by(id=spec_id).first()
|
|
|
|
self.assertIsNone(db_spec)
|
|
|
|
|
|
|
|
# TODO: Make sure that all items in the database with the workflow spec ID are deleted as well?
|
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
def test_add_workflow_to_study(self):
|
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).first()
|
|
|
|
self.assertEqual(0, session.query(WorkflowModel).count())
|
|
|
|
spec = session.query(WorkflowSpecModel).first()
|
2020-01-03 16:44:24 +00:00
|
|
|
rv = self.app.post('/v1.0/study/%i/workflows' % study.id, content_type="application/json",
|
|
|
|
data=json.dumps(WorkflowSpecModelSchema().dump(spec)))
|
2019-12-18 19:02:17 +00:00
|
|
|
self.assert_success(rv)
|
2020-01-14 16:45:12 +00:00
|
|
|
self.assertEqual(1, session.query(WorkflowModel).count())
|
|
|
|
workflow = session.query(WorkflowModel).first()
|
2019-12-18 20:22:46 +00:00
|
|
|
self.assertEqual(study.id, workflow.study_id)
|
|
|
|
self.assertEqual(WorkflowStatus.user_input_required, workflow.status)
|
|
|
|
self.assertIsNotNone(workflow.bpmn_workflow_json)
|
2019-12-18 20:32:06 +00:00
|
|
|
self.assertEqual(spec.id, workflow.workflow_spec_id)
|
|
|
|
|
2019-12-19 16:58:51 +00:00
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
2020-01-14 16:45:12 +00:00
|
|
|
workflow2 = WorkflowModelSchema().load(json_data, session=session)
|
2020-01-06 14:18:13 +00:00
|
|
|
self.assertEqual(workflow.id, workflow2.id)
|
2019-12-19 16:58:51 +00:00
|
|
|
|
2019-12-30 18:15:39 +00:00
|
|
|
def test_delete_workflow(self):
|
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).first()
|
|
|
|
spec = session.query(WorkflowSpecModel).first()
|
2019-12-30 21:00:33 +00:00
|
|
|
rv = self.app.post('/v1.0/study/%i/workflows' % study.id, content_type="application/json",
|
2020-01-03 16:44:24 +00:00
|
|
|
data=json.dumps(WorkflowSpecModelSchema().dump(spec)))
|
2020-01-14 16:45:12 +00:00
|
|
|
self.assertEqual(1, session.query(WorkflowModel).count())
|
2019-12-30 18:15:39 +00:00
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
2020-01-14 16:45:12 +00:00
|
|
|
workflow = WorkflowModelSchema().load(json_data, session=session)
|
2019-12-30 18:15:39 +00:00
|
|
|
rv = self.app.delete('/v1.0/workflow/%i' % workflow.id)
|
|
|
|
self.assert_success(rv)
|
2020-01-14 16:45:12 +00:00
|
|
|
self.assertEqual(0, session.query(WorkflowModel).count())
|
2019-12-30 18:15:39 +00:00
|
|
|
|
2019-12-19 16:58:51 +00:00
|
|
|
def test_get_current_user_tasks(self):
|
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).first()
|
|
|
|
spec = session.query(WorkflowSpecModel).filter_by(id='random_fact').first()
|
2019-12-19 16:58:51 +00:00
|
|
|
self.app.post('/v1.0/study/%i/workflows' % study.id, content_type="application/json",
|
2020-01-03 16:44:24 +00:00
|
|
|
data=json.dumps(WorkflowSpecModelSchema().dump(spec)))
|
2019-12-19 16:58:51 +00:00
|
|
|
rv = self.app.get('/v1.0/workflow/%i/tasks' % study.id, content_type="application/json")
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
|
|
|
tasks = TaskSchema(many=True).load(json_data)
|
|
|
|
self.assertEqual("Task_User_Select_Type", tasks[0].name)
|
2019-12-31 21:32:47 +00:00
|
|
|
self.assertEqual(3, len(tasks[0].form["fields"][0]["options"]))
|
|
|
|
|
|
|
|
def test_two_forms_task(self):
|
2020-01-14 16:02:44 +00:00
|
|
|
# Set up a new workflow
|
2019-12-31 21:32:47 +00:00
|
|
|
self.load_example_data()
|
2020-01-14 16:45:12 +00:00
|
|
|
study = session.query(StudyModel).first()
|
|
|
|
spec = session.query(WorkflowSpecModel).filter_by(id='two_forms').first()
|
2019-12-31 21:32:47 +00:00
|
|
|
rv = self.app.post('/v1.0/study/%i/workflows' % study.id, content_type="application/json",
|
2020-01-03 16:44:24 +00:00
|
|
|
data=json.dumps(WorkflowSpecModelSchema().dump(spec)))
|
2019-12-31 21:32:47 +00:00
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
2020-01-14 16:45:12 +00:00
|
|
|
workflow = WorkflowModelSchema().load(json_data, session=session)
|
2019-12-31 21:32:47 +00:00
|
|
|
|
2020-01-14 16:46:59 +00:00
|
|
|
# get the first form in the two form workflow.
|
2019-12-31 21:32:47 +00:00
|
|
|
rv = self.app.get('/v1.0/workflow/%i/tasks' % workflow.id, content_type="application/json")
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
|
|
|
tasks = TaskSchema(many=True).load(json_data)
|
|
|
|
self.assertEqual(1, len(tasks))
|
|
|
|
self.assertIsNotNone(tasks[0].form)
|
2020-01-07 18:23:46 +00:00
|
|
|
self.assertEqual("StepOne", tasks[0].name)
|
2019-12-31 21:32:47 +00:00
|
|
|
self.assertEqual(1, len(tasks[0].form['fields']))
|
2020-01-07 18:23:46 +00:00
|
|
|
|
|
|
|
# Complete the form for Step one and post it.
|
2020-01-14 16:02:44 +00:00
|
|
|
tasks[0].form['fields'][0]['value'] = "Blue"
|
2020-01-14 16:45:12 +00:00
|
|
|
rv = self.app.put('/v1.0/workflow/%i/task/%s/data' % (workflow.id, tasks[0].id),
|
|
|
|
content_type="application/json",
|
|
|
|
data=json.dumps({"color": "blue"}))
|
2019-12-31 21:32:47 +00:00
|
|
|
self.assert_success(rv)
|
2020-01-07 18:23:46 +00:00
|
|
|
|
|
|
|
# Get the next Task
|
|
|
|
rv = self.app.get('/v1.0/workflow/%i/tasks' % study.id, content_type="application/json")
|
|
|
|
self.assert_success(rv)
|
|
|
|
json_data = json.loads(rv.get_data(as_text=True))
|
|
|
|
tasks = TaskSchema(many=True).load(json_data)
|
|
|
|
self.assertEqual("StepTwo", tasks[0].name)
|
2020-01-23 20:32:53 +00:00
|
|
|
|