add user_data_get/set and alter test so that it ensures that we don't grab study_data stuff from the user_data calls

change the test to mock up the user.
This commit is contained in:
Kelly McDonald 2020-11-10 10:32:37 -05:00
parent 92595e3c42
commit 3ba11f5e6b
4 changed files with 101 additions and 5 deletions

View File

@ -0,0 +1,28 @@
import requests
from crc.scripts.script import Script, DataStoreBase
from crc import session
from crc.models.data_store import DataStoreModel
from flask import g
class UserDataGet(Script,DataStoreBase):
def get_description(self):
return """Gets user data from the data store."""
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
self.do_task(task, study_id, workflow_id, *args, **kwargs)
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
self.check_args(args,2)
study = session.query(DataStoreModel).filter_by(study_id=None,user_id=g.user.uid,key=args[0]).first()
if study:
return study.value
else:
return args[1]
def check_args(self, args, maxlen=1):
if len(args) < 1 or len(args) > maxlen :
raise ApiError(code="missing_argument",
message="The study_data_get script takes either one or two arguments, starting with the key and an " + \
"optional default")

View File

@ -0,0 +1,50 @@
import requests
from crc.scripts.script import Script, DataStoreBase
from crc import session
from crc.models.workflow import WorkflowModel
from crc.models.data_store import DataStoreModel
from flask import g
class UserDataSet(Script,DataStoreBase):
def get_description(self):
return """Sets user data to the data store."""
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
self.check_args(args)
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
self.get_prev_value(study_id,args[0])
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
self.check_args(args)
study = self.get_prev_value(study_id,args[0])
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
if study is not None:
prev_value = study.key
else:
prev_value = None
study = DataStoreModel(key=args[0],value=args[1],
study_id=None,
task_id=task.id,
user_id=g.user.uid, # Make this available to any study, but only one user
workflow_id= workflow_id,
spec_id=workflow.workflow_spec_id)
overwritten = self.overwritten(study.value,prev_value)
session.add(study)
return (study.value, prev_value, overwritten)
def get_prev_value(self,study_id,key):
study = session.query(DataStoreModel).filter_by(study_id=None,user_id=g.user.uid,key=key).first()
return study
def check_args(self, args):
if len(args) != 2:
raise ApiError(code="missing_argument",
message="The study_data_set script takes two arguments, starting with the key and a " +\
"value for the key")

View File

@ -12,7 +12,7 @@
</bpmn:scriptTask>
<bpmn:sequenceFlow id="SequenceFlow_1bqiin0" sourceRef="Task_Script_Load_Study_Sponsors" targetRef="Activity_0cm6tn2" />
<bpmn:endEvent id="EndEvent_171dj09">
<bpmn:incoming>Flow_0g9waf3</bpmn:incoming>
<bpmn:incoming>Flow_05136ua</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_09cika8" sourceRef="Activity_0cm6tn2" targetRef="Activity_0d8iftx" />
<bpmn:scriptTask id="Activity_0cm6tn2" name="setval">
@ -26,12 +26,18 @@
<bpmn:outgoing>Flow_1oeqjuy</bpmn:outgoing>
<bpmn:script>out = study_data_get('testme','bogus')</bpmn:script>
</bpmn:scriptTask>
<bpmn:sequenceFlow id="Flow_0g9waf3" sourceRef="Activity_1yup9u7" targetRef="EndEvent_171dj09" />
<bpmn:sequenceFlow id="Flow_0g9waf3" sourceRef="Activity_1yup9u7" targetRef="Activity_0xw717o" />
<bpmn:scriptTask id="Activity_1yup9u7" name="reset value">
<bpmn:incoming>Flow_1oeqjuy</bpmn:incoming>
<bpmn:outgoing>Flow_0g9waf3</bpmn:outgoing>
<bpmn:script>study_data_set('testme','badval')</bpmn:script>
</bpmn:scriptTask>
<bpmn:scriptTask id="Activity_0xw717o" name="Make sure user_data_get doesn&#39;t get the study_data_set variable">
<bpmn:incoming>Flow_0g9waf3</bpmn:incoming>
<bpmn:outgoing>Flow_05136ua</bpmn:outgoing>
<bpmn:script>empty = user_data_get('testme','empty')</bpmn:script>
</bpmn:scriptTask>
<bpmn:sequenceFlow id="Flow_05136ua" sourceRef="Activity_0xw717o" targetRef="EndEvent_171dj09" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_0exnnpv">
@ -50,7 +56,7 @@
<dc:Bounds x="270" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="EndEvent_171dj09_di" bpmnElement="EndEvent_171dj09">
<dc:Bounds x="932" y="99" width="36" height="36" />
<dc:Bounds x="792" y="672" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_09cika8_di" bpmnElement="Flow_09cika8">
<di:waypoint x="540" y="117" />
@ -67,12 +73,19 @@
<dc:Bounds x="600" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_0g9waf3_di" bpmnElement="Flow_0g9waf3">
<di:waypoint x="860" y="117" />
<di:waypoint x="932" y="117" />
<di:waypoint x="810" y="157" />
<di:waypoint x="810" y="250" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Activity_0cj83fx_di" bpmnElement="Activity_1yup9u7">
<dc:Bounds x="760" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0pqth07_di" bpmnElement="Activity_0xw717o">
<dc:Bounds x="760" y="250" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_05136ua_di" bpmnElement="Flow_05136ua">
<di:waypoint x="810" y="330" />
<di:waypoint x="810" y="672" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

View File

@ -1,9 +1,12 @@
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
@ -27,6 +30,7 @@ class TestSudySponsorsScript(BaseTest):
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()
@ -41,5 +45,6 @@ class TestSudySponsorsScript(BaseTest):
data = processor.next_task().data
self.assertIn('sponsors', data)
self.assertIn('out', data)
self.assertEquals('empty', data['empty'])
self.assertEquals('newval', data['out'])
self.assertEquals(3, len(data['sponsors']))