Add mostly-working elements of the data-store, compare with dev to see why db is not working after running tests

This commit is contained in:
Kelly McDonald 2020-11-10 08:21:46 -05:00
parent 5c4aae6802
commit 245124779e
5 changed files with 146 additions and 0 deletions

15
crc/models/data_store.py Normal file
View File

@ -0,0 +1,15 @@
from flask_marshmallow.sqla import SQLAlchemyAutoSchema
from marshmallow import EXCLUDE
from sqlalchemy import func
from crc import db
class DataStoreModel(db.Model):
__tablename__ = 'data_store'
key = db.Column(db.String, primary_key=True)
workflow_id = db.Column(db.Integer)
study_id = db.Column(db.Integer)
task_id = db.Column(db.String)
spec_id = db.Column(db.String)
user_id = db.Column(db.String)
value = db.Column(db.String)

View File

@ -109,3 +109,14 @@ class ScriptValidationError:
@classmethod
def from_api_error(cls, api_error: ApiError):
return cls(api_error.code, api_error.message)
class DataStoreBase():
def overwritten(self,value,prev_value):
if prev_value == None:
overwritten = False
else:
if prev_value == value:
overwritten = False
else:
overwritten = True
return overwritten

View File

@ -0,0 +1,27 @@
import requests
from crc.scripts.script import Script, DataStoreBase
from crc import session
from crc.models.data_store import DataStoreModel
class StudyDataGet(Script,DataStoreBase):
def get_description(self):
return """Gets study 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=study_id,user_id=None,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,48 @@
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
class StudyDataSet(Script,DataStoreBase):
def get_description(self):
return """Sets study data from 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=study_id,
task_id=task.id,
user_id=None, # Make this available to any 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=study_id,user_id=None,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

@ -0,0 +1,45 @@
from unittest.mock import patch
from tests.base_test import BaseTest
from crc import session, app
from crc.models.study import StudyModel
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):
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_data_store")
WorkflowService.test_spec("study_sponsors_data_store") # 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')
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_data_store")
workflow_model = StudyService._create_workflow_model(study, workflow_spec_model)
WorkflowService.test_spec("study_sponsors_data_store")
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)
self.assertEquals('newval', data['out'])
self.assertEquals(3, len(data['sponsors']))