We now test whether we have a valid StudyModel when getting a study by study model.

We raise an ApiError if the model is None or empty.
This commit is contained in:
mike cullerton 2021-02-09 17:37:55 -05:00
parent 329146237e
commit 777cfbdecd
2 changed files with 27 additions and 6 deletions

View File

@ -8,7 +8,7 @@ from marshmallow_enum import EnumField
from sqlalchemy import func
from crc import db, ma
from crc.api.common import ApiErrorSchema
from crc.api.common import ApiErrorSchema, ApiError
from crc.models.file import FileModel, SimpleFileSchema, FileSchema
from crc.models.protocol_builder import ProtocolBuilderStatus, ProtocolBuilderStudy
from crc.models.workflow import WorkflowSpecCategoryModel, WorkflowState, WorkflowStatus, WorkflowSpecModel, \
@ -165,11 +165,14 @@ class Study(object):
@classmethod
def from_model(cls, study_model: StudyModel):
id = study_model.id # Just read some value, in case the dict expired, otherwise dict may be empty.
args = dict((k, v) for k, v in study_model.__dict__.items() if not k.startswith('_'))
args['events_history'] = study_model.events_history # For some reason this attribute is not picked up
instance = cls(**args)
return instance
if study_model is not None and len(study_model.__dict__.items()) > 0:
args = dict((k, v) for k, v in study_model.__dict__.items() if not k.startswith('_'))
args['events_history'] = study_model.events_history # For some reason this attribute is not picked up
instance = cls(**args)
return instance
else:
raise ApiError(code='empty_study_model',
message='There was a problem retrieving your study. StudyModel is empty.')
def model_args(self):
"""Arguments that can be passed into the Study Model to update it."""

View File

@ -0,0 +1,18 @@
from tests.base_test import BaseTest
from crc import session
from crc.models.study import StudyModel
import json
class TestGetStudyFromModel(BaseTest):
def test_get_study_from_model(self):
self.load_example_data()
study = session.query(StudyModel).order_by(StudyModel.id.desc()).first()
id = study.id + 1
result = self.app.get('/v1.0/study/%i' % id,
headers=self.logged_in_headers())
json_data = json.loads(result.get_data(as_text=True))
self.assertIn('code', json_data)
self.assertEqual('empty_study_model', json_data['code'])