Merge pull request #237 from sartography/get-study-from-model-error-191

We now test whether we have a valid StudyModel when getting a study b…
This commit is contained in:
Dan Funk 2021-02-11 11:23:59 -05:00 committed by GitHub
commit df0cfc755b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 sqlalchemy import func
from crc import db, ma 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.file import FileModel, SimpleFileSchema, FileSchema
from crc.models.protocol_builder import ProtocolBuilderStatus, ProtocolBuilderStudy from crc.models.protocol_builder import ProtocolBuilderStatus, ProtocolBuilderStudy
from crc.models.workflow import WorkflowSpecCategoryModel, WorkflowState, WorkflowStatus, WorkflowSpecModel, \ from crc.models.workflow import WorkflowSpecCategoryModel, WorkflowState, WorkflowStatus, WorkflowSpecModel, \
@ -165,11 +165,14 @@ class Study(object):
@classmethod @classmethod
def from_model(cls, study_model: StudyModel): 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. 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 = 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 args['events_history'] = study_model.events_history # For some reason this attribute is not picked up
instance = cls(**args) instance = cls(**args)
return instance return instance
else:
raise ApiError(code='empty_study_model',
message='There was a problem retrieving your study. StudyModel is empty.')
def model_args(self): def model_args(self):
"""Arguments that can be passed into the Study Model to update it.""" """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'])