Add some tests
This commit is contained in:
parent
70425eee0d
commit
e9979efb0d
|
@ -10,12 +10,12 @@ class DataStoreModel(db.Model):
|
|||
__tablename__ = 'data_store'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
last_updated = db.Column(db.DateTime(timezone=True), default=func.now())
|
||||
key = db.Column(db.String,nullable=False)
|
||||
key = db.Column(db.String, nullable=False)
|
||||
workflow_id = db.Column(db.Integer)
|
||||
study_id = db.Column(db.Integer)
|
||||
study_id = db.Column(db.Integer, nullable=True)
|
||||
task_id = db.Column(db.String)
|
||||
spec_id = db.Column(db.String)
|
||||
user_id = db.Column(db.String)
|
||||
user_id = db.Column(db.String, nullable=True)
|
||||
value = db.Column(db.String)
|
||||
|
||||
|
||||
|
@ -24,8 +24,8 @@ class DataStoreSchema(ma.Schema):
|
|||
key = fields.String(required=True)
|
||||
last_updated = fields.DateTime(server_default=func.now(), onupdate=func.now())
|
||||
workflow_id = fields.Integer()
|
||||
study_id = fields.Integer()
|
||||
study_id = fields.Integer(allow_none=True)
|
||||
task_id = fields.String()
|
||||
spec_id = fields.String()
|
||||
user_id = fields.String()
|
||||
user_id = fields.String(allow_none=True)
|
||||
value = fields.String()
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
import json
|
||||
from profile import Profile
|
||||
|
||||
from tests.base_test import BaseTest
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import patch
|
||||
from crc.models.data_store import DataStoreModel, DataStoreSchema
|
||||
from crc import session, app
|
||||
|
||||
|
||||
|
||||
|
||||
class DataStoreTest(BaseTest):
|
||||
TEST_STUDY_ITEM = {
|
||||
"key": "MyKey",
|
||||
"workflow_id": 12,
|
||||
"study_id": 42,
|
||||
"task_id": "MyTask",
|
||||
"spec_id": "My Spec Name",
|
||||
"value": "Some Value"
|
||||
|
||||
}
|
||||
def add_test_study(self):
|
||||
study_data = DataStoreSchema().dump(self.TEST_STUDY_ITEM)
|
||||
rv = self.app.post('/v1.0/datastore',
|
||||
content_type="application/json",
|
||||
headers=self.logged_in_headers(),
|
||||
data=json.dumps(study_data))
|
||||
self.assert_success(rv)
|
||||
return json.loads(rv.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_get_study_data(self):
|
||||
"""Generic test, but pretty detailed, in that the study should return a categorized list of workflows
|
||||
This starts with out loading the example data, to show that all the bases are covered from ground 0."""
|
||||
|
||||
"""NOTE: The protocol builder is not enabled or mocked out. As the master workflow (which is empty),
|
||||
and the test workflow do not need it, and it is disabled in the configuration."""
|
||||
self.load_example_data()
|
||||
new_study = self.add_test_study()
|
||||
new_study = session.query(DataStoreModel).filter_by(id=new_study["id"]).first()
|
||||
|
||||
api_response = self.app.get('/v1.0/datastore/%i' % new_study.id,
|
||||
headers=self.logged_in_headers(), content_type="application/json")
|
||||
self.assert_success(api_response)
|
||||
d = api_response.get_data(as_text=True)
|
||||
study_data = DataStoreSchema().loads(d)
|
||||
|
||||
self.assertEqual(study_data['key'], self.TEST_STUDY_ITEM['key'])
|
||||
self.assertEqual(study_data['value'], self.TEST_STUDY_ITEM['value'])
|
||||
self.assertEqual(study_data['user_id'], None)
|
||||
|
||||
|
||||
|
||||
def test_update_study(self):
|
||||
self.load_example_data()
|
||||
new_study = self.add_test_study()
|
||||
new_study = session.query(DataStoreModel).filter_by(id=new_study["id"]).first()
|
||||
new_study.value = 'MyNewValue'
|
||||
api_response = self.app.put('/v1.0/datastore/%i' % new_study.id,
|
||||
data=DataStoreSchema().dump(new_study),
|
||||
headers=self.logged_in_headers(), content_type="application/json")
|
||||
|
||||
|
||||
api_response = self.app.get('/v1.0/datastore/%i' % new_study.id,
|
||||
headers=self.logged_in_headers(), content_type="application/json")
|
||||
self.assert_success(api_response)
|
||||
study_data = DataStoreSchema().loads(api_response.get_data(as_text=True))
|
||||
|
||||
self.assertEqual(study_data['key'], self.TEST_STUDY_ITEM['key'])
|
||||
self.assertEqual(study_data['value'], 'MyNewValue')
|
||||
self.assertEqual(study_data['user_id'], None)
|
||||
|
||||
|
||||
|
||||
def test_delete_study(self):
|
||||
self.load_example_data()
|
||||
new_study = self.add_test_study()
|
||||
oldid = new_study['id']
|
||||
new_study = session.query(DataStoreModel).filter_by(id=new_study["id"]).first()
|
||||
rv = self.app.delete('/v1.0/datastore/%i' % new_study.id, headers=self.logged_in_headers())
|
||||
self.assert_success(rv)
|
||||
studyreponse = session.query(DataStoreModel).filter_by(id=oldid).first()
|
||||
self.assertEqual(studyreponse,None)
|
||||
|
Loading…
Reference in New Issue