From 9bd3aa8dcb9e1a5fd65f63330ad3502437fe9a11 Mon Sep 17 00:00:00 2001 From: Kelly McDonald Date: Wed, 24 Feb 2021 12:55:23 -0500 Subject: [PATCH] service and scripts with working test - still needs a few more tests to try out, but should work to get Mike going --- crc/scripts/script.py | 2 +- crc/services/study_service.py | 16 +++-- migrations/versions/cb892916166a_.py | 38 +++++++++++ .../study_sponsors_associate.bpmn | 65 +++++++++++++++++++ tests/study/test_study_associate_script.py | 52 +++++++++++++++ 5 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 migrations/versions/cb892916166a_.py create mode 100644 tests/data/study_sponsors_associate/study_sponsors_associate.bpmn create mode 100644 tests/study/test_study_associate_script.py diff --git a/crc/scripts/script.py b/crc/scripts/script.py index 8d1aef9d..edb3cbf5 100644 --- a/crc/scripts/script.py +++ b/crc/scripts/script.py @@ -68,7 +68,7 @@ class Script(object): def make_closure_validate(subclass,task,study_id,workflow_id): instance = subclass() - return lambda *a : subclass.do_task_validate_only(instance,task,study_id,workflow_id,*a) + return lambda *a, **b: subclass.do_task_validate_only(instance,task,study_id,workflow_id,*a,**b) execlist = {} subclasses = Script.get_all_subclasses() for x in range(len(subclasses)): diff --git a/crc/services/study_service.py b/crc/services/study_service.py index 1660c402..55c19441 100644 --- a/crc/services/study_service.py +++ b/crc/services/study_service.py @@ -107,7 +107,7 @@ class StudyService(object): person = db.session.query(StudyAssociated).filter((StudyAssociated.study_id == study_id)&( StudyAssociated.uid == uid)) if person: - newAssociate = {'uid',person.uid} + newAssociate = {'uid':person.uid} newAssociate['role'] = person.role newAssociate['send_email'] = person.send_email newAssociate['access'] = person.access @@ -129,7 +129,7 @@ class StudyService(object): people_list = [{'uid':ownerid,'role':'owner','send_email':True,'access':True}] people = db.session.query(StudyAssociated).filter(StudyAssociated.study_id == study_id) for person in people: - newAssociate = {'uid',person.uid} + newAssociate = {'uid':person.uid} newAssociate['role'] = person.role newAssociate['send_email'] = person.send_email newAssociate['access'] = person.access @@ -165,12 +165,13 @@ class StudyService(object): db.session.query(StudyAssociated).filter(StudyAssociated.study_id == study_id).delete() for person in associates: newAssociate = StudyAssociated() + newAssociate.study_id = study_id newAssociate.uid = person['uid'] newAssociate.role = person.get('role', None) newAssociate.send_email = person.get('send_email', False) newAssociate.access = person.get('access',False) - db.session.add(newAssociate) - db.commit() + session.add(newAssociate) + session.commit() @staticmethod def update_study_associate(study_id=None,uid=None,role="",send_email=False,access=False): @@ -191,13 +192,14 @@ class StudyService(object): uid) ).delete() newAssociate = StudyAssociated() + newAssociate.study_id = study_id newAssociate.uid = uid newAssociate.role = role newAssociate.send_email = send_email newAssociate.access = access - db.session.add(newAssociate) - db.commit() - return true + session.add(newAssociate) + session.commit() + return True @staticmethod diff --git a/migrations/versions/cb892916166a_.py b/migrations/versions/cb892916166a_.py new file mode 100644 index 00000000..1e981d2c --- /dev/null +++ b/migrations/versions/cb892916166a_.py @@ -0,0 +1,38 @@ +"""empty message + +Revision ID: cb892916166a +Revises: ff29528a9909 +Create Date: 2021-02-24 12:16:45.779821 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'cb892916166a' +down_revision = 'ff29528a9909' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('study_associated_user', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('study_id', sa.Integer(), nullable=False), + sa.Column('uid', sa.String(), nullable=False), + sa.Column('role', sa.String(), nullable=True), + sa.Column('send_email', sa.Boolean(), nullable=True), + sa.Column('access', sa.Boolean(), nullable=True), + sa.ForeignKeyConstraint(['study_id'], ['study.id'], ), + sa.ForeignKeyConstraint(['uid'], ['ldap_model.uid'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('study_associated_user') + # ### end Alembic commands ### diff --git a/tests/data/study_sponsors_associate/study_sponsors_associate.bpmn b/tests/data/study_sponsors_associate/study_sponsors_associate.bpmn new file mode 100644 index 00000000..7397e856 --- /dev/null +++ b/tests/data/study_sponsors_associate/study_sponsors_associate.bpmn @@ -0,0 +1,65 @@ + + + + + SequenceFlow_1nfe5m9 + + + + SequenceFlow_1nfe5m9 + SequenceFlow_1bqiin0 + sponsors = study_info('sponsors') + + + + + SequenceFlow_1bqiin0 + Flow_09cika8 + update_study_associate(uid='lb3dp',role='SuperDude',send_email=False,access=True) + + + Flow_09cika8 + Flow_0cttkwp + out = get_study_associates() + + + Flow_0cttkwp + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/study/test_study_associate_script.py b/tests/study/test_study_associate_script.py new file mode 100644 index 00000000..1e994fdf --- /dev/null +++ b/tests/study/test_study_associate_script.py @@ -0,0 +1,52 @@ +from unittest.mock import patch +import flask + + +from tests.base_test import BaseTest + +from crc import session, app +from crc.models.study import StudyModel +from crc.models.user import UserModel +from crc.services.study_service import StudyService +from crc.services.workflow_processor import WorkflowProcessor +from crc.services.workflow_service import WorkflowService + +class TestSudySponsorsScript(BaseTest): + test_uid = "dhf8r" + test_study_id = 1 + + + def test_study_sponsors_script_validation(self): + flask.g.user = UserModel(uid='dhf8r') + self.load_example_data() # study_info script complains if irb_documents.xls is not loaded + # during the validate phase I'm going to assume that we will never + # have a case where irb_documents.xls is not loaded ?? + + self.load_test_spec("study_sponsors_associate") + WorkflowService.test_spec("study_sponsors_associate") # This would raise errors if it didn't validate + + + @patch('crc.services.protocol_builder.requests.get') + def test_study_sponsors_script(self, mock_get): + mock_get.return_value.ok = True + mock_get.return_value.text = self.protocol_builder_response('sponsors.json') + flask.g.user = UserModel(uid='dhf8r') + app.config['PB_ENABLED'] = True + + self.load_example_data() + self.create_reference_document() + study = session.query(StudyModel).first() + workflow_spec_model = self.load_test_spec("study_sponsors_associate") + workflow_model = StudyService._create_workflow_model(study, workflow_spec_model) + WorkflowService.test_spec("study_sponsors_associate") + processor = WorkflowProcessor(workflow_model) + processor.do_engine_steps() + self.assertTrue(processor.bpmn_workflow.is_completed()) + data = processor.next_task().data + self.assertIn('sponsors', data) + self.assertIn('out', data) + print(data['out']) + self.assertEquals([{'uid': 'dhf8r', 'role': 'owner', 'send_email': True, 'access': True}, + {'uid': 'lb3dp', 'role': 'SuperDude', 'send_email': False, 'access': True}] + , data['out']) + self.assertEquals(3, len(data['sponsors']))