def test_exclude_schema_used_versions(self, req, db_session, check_csrf_token): """ It should exclude general versions already used by the form (editing) """ from datetime import date, timedelta from webob.multidict import MultiDict from occams_datastore import models as datastore from occams_studies import models today = date.today() tomorrow = today + timedelta(days=1) y0 = datastore.Schema(name='y', title=u'Y', publish_date=today) y1 = datastore.Schema(name='y', title=u'Y', publish_date=tomorrow) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today(), schemata=set([y0])) db_session.add_all([y0, y1, study]) db_session.flush() req.GET = MultiDict() res = self._call_fut(study, req) assert 1 == len(res['schemata']) assert str(tomorrow) == res['schemata'][0]['publish_date']
def populate(self, db_session): from datetime import date from occams_datastore import models as datastore from occams_studies import models as studies drsc = studies.Study(name=u'drsc', title=u'DRSC', short_title=u'dr', code=u'drs', consent_date=date.today(), is_randomized=False) schema1 = datastore.Schema( name=u'demographics', title=u'demographics', publish_date=u'2015-01-01', attributes={ 'question': datastore.Attribute(name=u'question', title=u'question', type=u'choice', order=0, choices={ u'0': datastore.Choice(name=u'0', title=u'always', order=0), u'1': datastore.Choice(name=u'1', title=u'never', order=1) }) }) schema2 = datastore.Schema( name=u'ucsd_demographics', title=u'ucsd_demographics', publish_date=u'2015-01-01', attributes={ 'ucsd_question': datastore.Attribute(name=u'ucsd_question', title=u'ucsd_question', type=u'choice', order=0, choices={ u'0': datastore.Choice(name=u'0', title=u'always', order=0), u'1': datastore.Choice(name=u'1', title=u'never', order=1) }) }) drsc.schemata.add(schema1) drsc.schemata.add(schema2) db_session.add(drsc) db_session.flush()
def test_schema_publish_date_unique(db_session): """ It should enforce unique publish dates """ from datetime import date import sqlalchemy.exc from occams_datastore import models # First version db_session.add( models.Schema(name='Foo', title=u'Foo', publish_date=date(2014, 3, 31))) db_session.flush() # Draft version db_session.add(models.Schema(name='Foo', title=u'Foo', publish_date=None)) db_session.flush() # Add another published schema (not on the same date) # Publish, not on the same date db_session.add( models.Schema(name='Foo', title=u'Foo', publish_date=date(2014, 4, 1))) db_session.flush() # New version, same date (wrong) db_session.add( models.Schema(name='Foo', title=u'Foo', publish_date=date(2014, 4, 1))) with pytest.raises(sqlalchemy.exc.IntegrityError): db_session.flush()
def test_exclude_schema(self, req, db_session, check_csrf_token): """ It should exlude general forms used by the study (editing) """ from datetime import date from webob.multidict import MultiDict from occams_datastore import models as datastore from occams_studies import models x = datastore.Schema(name='x', title=u'x', publish_date=date.today()) y = datastore.Schema(name='y', title=u'Y', publish_date=date.today()) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today(), schemata=set([x])) db_session.add_all([x, y, study]) db_session.flush() req.GET = MultiDict() res = self._call_fut(study, req) assert 1 == len(res['schemata']) assert 'y' == res['schemata'][0]['name']
def test_update_cycles(self, req, db_session, check_csrf_token): """ It should also update cycle versions """ from datetime import date, timedelta from occams_datastore import models as datastore from occams_studies import models today = date.today() tomorrow = today + timedelta(days=1) v1 = datastore.Schema(name=u'test', title=u'', publish_date=today) v2 = datastore.Schema(name=u'test', title=u'', publish_date=tomorrow) cycle = models.Cycle(name=u'wk-001', title=u'WK-001', schemata=set([v1])) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', cycles=[cycle], schemata=set([v1]), consent_date=date.today()) db_session.add_all([study, v1, v2]) db_session.flush() req.json_body = {'schema': v1.name, 'versions': [v2.id]} self._call_fut(study, req) assert v2 in study.schemata # v2 should have been passed on to the cycle using it as well assert v2 in cycle.schemata
def test_enable(self, req, db_session, check_csrf_token): """ It should successfully add a schema to a cycle """ from datetime import date from occams_datastore import models as datastore from occams_studies import models schema = datastore.Schema(name='test', title=u'Test', publish_date=date.today()) cycle = models.Cycle(name='week-1', title=u'Week 1', week=1) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today(), cycles=[cycle], schemata=set([schema])) db_session.add_all([study, schema]) db_session.flush() req.json_body = { 'schema': schema.name, 'cycle': cycle.id, 'enabled': True } self._call_fut(study, req) assert schema in cycle.schemata
def test_get_attributes_value_value(self): """Should return the value to be matched.""" from datetime import date from occams_datastore import models as datastore from occams_studies import models as studies from occams_imports import models study = studies.Study(name=u'UCSD', title=u'UCSD', short_title=u'UCSD', code=u'001', consent_date=date(2015, 01, 01)) schema = datastore.Schema(name=u'PatientRegistrationAndDemographics', title=u'PatientRegistrationAndDemographics', publish_date=date(2013, 02, 25), attributes={ 'birthyear': datastore.Attribute(name=u'birthyear', title=u'birthyear', type=u'number', order=0) }) record = models.SiteData(schema=schema, study=study, data=SITEDATA) source_variable = u'birthyear' choices_mapping = [] target_value = self._call_fut(choices_mapping, record, source_variable, schema) assert target_value == '35'
def test_get_errors_empty(self, db_session): """Should return no errors.""" from datetime import date from occams_datastore import models as datastore target_schema = datastore.Schema(name=u'Demographics', title=u'Demographics', publish_date=date(2013, 02, 25), attributes={ 'yob': datastore.Attribute( name=u'yob', title=u'yob', type=u'number', order=0) }) db_session.add(target_schema) db_session.flush() target_variable = u'yob' target_value = u'25' errors = self._call_fut(db_session, target_schema, target_variable, target_value) assert errors == []
def test_not_empty(self, req, db_session): """ It should return a listing of schemata with links to each version """ from datetime import date from occams_datastore import models as datastore from occams_forms import models db_session.add( datastore.Schema(name=u'sample', title=u'Sample', publish_date=date(2014, 6, 1))) db_session.flush() res = self._call_fut(models.FormFactory(req), req) assert 1 == len(res['forms']) record = res['forms'][0] assert 'sample' == record['name'] assert False == record['has_private'] assert 'Sample' == record['title'] versions = record['versions'] assert '2014-06-01' == versions[0]['publish_date']
def test_add_to_patient(self, req, db_session): from datetime import date from occams_datastore import models as datastore from occams_studies import models schema = datastore.Schema(name=u'schema', title=u'Schema', publish_date=date.today()) study = models.Study(name='some-study', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today(), schemata=set([schema])) site = models.Site(name=u'somewhere', title=u'Somewhere') patient = models.Patient(pid=u'12345', site=site) db_session.add_all([study, patient]) db_session.flush() req.method = 'POST' req.matchdict = {'patient': patient} req.json_body = { 'schema': schema.id, 'collect_date': str(date.today()), } factory = models.FormFactory(req, patient) self._call_fut(factory, req) contexts = db_session.query(datastore.Context).all() assert len(contexts) == 1 assert contexts[0].entity.schema == schema
def check_value_max_constraint(db_session, type_, limit, below, equal, over): """ It should validate against maximum constraints """ from datetime import date from occams_datastore import models from occams_datastore.exc import ConstraintError schema = models.Schema(name=u'Foo', title=u'', publish_date=date(2000, 1, 1)) s1 = models.Attribute( schema=schema, name='s1', title=u'Section 1', type='section', order=0) entity = models.Entity(schema=schema) db_session.add(entity) db_session.flush() models.Attribute( schema=schema, parent_attribute=s1, name=type_, title=u'', type=type_, is_required=False, value_max=limit, order=0) entity[type_] = None entity[type_] = below entity[type_] = equal with pytest.raises(ConstraintError): entity[type_] = over models.Attribute( schema=schema, parent_attribute=s1, name=u'boolean', title=u'', type=u'boolean', value_max=10, order=1) with pytest.raises(NotImplementedError): entity['boolean'] = True
def test_process_import(self, db_session): from datetime import date from occams_datastore import models as datastore from occams_studies import models as studies from occams_imports.views.codebook import process_import attr_dict = {} schema = datastore.Schema(name=u'test_name', title=u'test_title', publish_date=date.today()) study = studies.Study(name=u'test_site', title=u'test_title', short_title=u'tt', code=u'tt1', consent_date=date.today(), is_randomized=False) process_import(schema, attr_dict, study, db_session) imported_study = (db_session.query( studies.Study).filter(studies.Study.title == study.title)).one() assert imported_study.title == study.title
def test_choice_defaults(db_session): """ It should set choice defaults """ from occams_datastore import models schema = models.Schema(name=u'Foo', title=u'Foo') attribute = models.Attribute(schema=schema, name=u'foo', title=u'Enter Foo', type=u'choice', order=0) choice1 = models.Choice(attribute=attribute, name='001', title=u'Foo', order=0) choice2 = models.Choice(attribute=attribute, name='002', title=u'Bar', order=1) choice3 = models.Choice(attribute=attribute, name='003', title=u'Baz', order=2) db_session.add_all([schema, attribute, choice1, choice2, choice3]) db_session.flush() count = db_session.query(models.Choice).count() assert count, 3 == 'Did not find any choices'
def test_schema_has_private(db_session): """ It should be able to determine if a schema has private attributes """ from datetime import date from occams_datastore import models schema = models.Schema(name='Foo', title=u'Foo', publish_date=date(2014, 3, 31), attributes={ 'not_private': models.Attribute(name='not_private', title=u'', type='string', is_private=False, order=0) }) db_session.add(schema) db_session.flush() assert not schema.has_private schema.attributes['is_private'] = models.Attribute(name='is_private', title=u'', type='string', is_private=True, order=1) assert schema.has_private
def test_success(self, req, db_session, check_csrf_token): """ It should remove the schema from the study and cascade to its cycles """ from datetime import date from occams_datastore import models as datastore from occams_studies import models schema = datastore.Schema(name='test', title=u'', publish_date=date.today()) cycle = models.Cycle(name='week-1', title=u'Week 1', week=1, schemata=set([schema])) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today(), cycles=[cycle], schemata=set([schema])) db_session.add_all([study, schema]) db_session.flush() req.matchdict = {'schema': schema.name} self._call_fut(study, req) assert schema not in study.schemata assert schema not in cycle.schemata
def test_fail_if_termination_schema(self, req, db_session, check_csrf_token): """ It should not allow termination schemata to be used as study schemata """ from datetime import date from pyramid.httpexceptions import HTTPBadRequest from occams_datastore import models as datastore from occams_studies import models schema = datastore.Schema(name='test', title=u'', publish_date=date.today()) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today(), termination_schema=schema) db_session.add_all([study, schema]) db_session.flush() req.json_body = {'schema': schema.name, 'versions': [schema.id]} with pytest.raises(HTTPBadRequest) as excinfo: self._call_fut(study, req) assert 'already a termination form' in \ excinfo.value.json['errors']['schema'].lower()
def test_fail_if_not_same_schema(self, req, db_session, check_csrf_token): """ It should fail if the schema and versions do not match """ from datetime import date from pyramid.httpexceptions import HTTPBadRequest from occams_datastore import models as datastore from occams_studies import models schema = datastore.Schema(name='test', title=u'', publish_date=date.today()) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today()) db_session.add_all([study, schema]) db_session.flush() req.json_body = {'schema': u'otherform', 'versions': [schema.id]} with pytest.raises(HTTPBadRequest) as excinfo: self._call_fut(study, req) assert 'Incorrect versions' in \ excinfo.value.json['errors']['versions']
def test_fail_if_not_published(self, req, db_session, check_csrf_token): """ It should fail if the schema is not published """ from datetime import date from pyramid.httpexceptions import HTTPBadRequest from occams_datastore import models as datastore from occams_studies import models schema = datastore.Schema(name='test', title=u'') study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today()) db_session.add_all([study, schema]) db_session.flush() db_session.execute(models.patient_schema_table.insert().values( {'schema_id': schema.id})) req.json_body = {'schema': schema.name, 'versions': [schema.id]} with pytest.raises(HTTPBadRequest) as excinfo: self._call_fut(study, req) assert 'not published' in \ excinfo.value.json['errors']['versions-0']
def test_basic(self, req, db_session, check_csrf_token): """ It should allow adding a schema to a study """ from datetime import date from occams_datastore import models as datastore from occams_studies import models schema = datastore.Schema(name='test', title=u'', publish_date=date.today()) study = models.Study(name=u'somestudy', title=u'Some Study', short_title=u'sstudy', code=u'000', consent_date=date.today()) db_session.add_all([study, schema]) db_session.flush() req.json_body = {'schema': schema.name, 'versions': [schema.id]} self._call_fut(study, req) assert schema in study.schemata
def test_validator_max_constraint(db_session): """ It should validate string/number value min/max """ from datetime import date from occams_datastore import models from occams_datastore.exc import ConstraintError schema = models.Schema(name=u'Foo', title=u'', publish_date=date(2000, 1, 1)) s1 = models.Attribute( schema=schema, name='s1', title=u'Section 1', type='section', order=0) models.Attribute( schema=schema, parent_attribute=s1, name=u'test', title=u'', type=u'string', is_required=False, value_max=3, order=0) db_session.add(schema) db_session.flush() entity = models.Entity(schema=schema) db_session.add(entity) entity['test'] = None with pytest.raises(ConstraintError): entity['test'] = u'foobar' entity['test'] = u'foo' db_session.flush() assert 'foo' == entity['test']
def test_attribute_unique_case_insensitive(db_session): """ It should enforce case-insensitive attributes """ from datetime import date import sqlalchemy.exc from occams_datastore import models schema = models.Schema(name='Foo', title=u'Foo', publish_date=date(2014, 3, 31)) schema.attributes['MyAttr'] = models.Attribute(name=u'MyAttr', title=u'My Attribute', type=u'string', order=0) db_session.add(schema) db_session.flush() schema.attributes['myattr'] = models.Attribute(name=u'myattr', title=u'My Attribute 2', type=u'string', order=1) with pytest.raises(sqlalchemy.exc.IntegrityError): db_session.flush()
def test_add_section_into_section(self, req, db_session, check_csrf_token): """ It should not allow adding a new section into a another section """ from pyramid.httpexceptions import HTTPBadRequest from occams_datastore import models as datastore schema = datastore.Schema(name='testform', title=u'Test Form', attributes={ 'section1': datastore.Attribute(name='section1', title=u'Section 1', type='section', order=0) }) db_session.add(schema) db_session.flush() req.json_body = { 'target': 'section1', 'name': 'section2', 'title': u'Section 2', 'type': 'section' } with pytest.raises(HTTPBadRequest) as excinfo: self._call_fut(schema['fields'], req) assert 'nested sections are not supported' in \ excinfo.value.json['errors']['target'].lower()
def test_file_is_deleted(self, db_session): """ Test file is deleted on the system after a non-FieldStorage object is passed to apply_data """ import os from datetime import date from occams_datastore import models as datastore from mock import Mock schema = datastore.Schema( name=u'test', title=u'', publish_date=date.today(), attributes={ 'q1': datastore.Attribute( name=u'q1', title=u'', type='blob', order=0 ) }) entity = datastore.Entity(schema=schema) formdata = {'q1': u''} with open(os.path.join(self.tmpdir, 'test.txt'), 'w'): fullpath = os.path.join(self.tmpdir, 'test.txt') entity['q1'] = Mock(path=fullpath) self._call_fut(db_session, entity, formdata, self.tmpdir) assert not os.path.exists(fullpath)
def test_add_duplicate_variable_name(self, req, db_session, check_csrf_token): """ It should make sure the variable name is not repeated """ from pyramid.httpexceptions import HTTPBadRequest from occams_datastore import models as datastore schema = datastore.Schema(name='testform', title=u'Test Form', attributes={ 'myvar': datastore.Attribute(name='myvar', title=u'My Var', type='string', order=0) }) db_session.add(schema) db_session.flush() req.json_body = {'name': 'myvar'} with pytest.raises(HTTPBadRequest) as excinfo: self._call_fut(schema['fields'], req) assert check_csrf_token.called assert 'name already exists' in \ excinfo.value.json['errors']['name'].lower()
def test_is_duplicate_schema(self, db_session): import transaction from occams_datastore import models as datastore from occams_imports.views.codebook import is_duplicate_schema forms = {} errors = [] errors = is_duplicate_schema(forms, errors, db_session) assert errors == [] forms = [{ 'name': u'test_schema_name', 'title': u'test_schema_title', 'publish_date': u'2015-01-01', 'extra_key': u'test_title' }] schema = datastore.Schema(name=u'test_schema_name', title=u'test_schema_title', publish_date=u'2015-01-01') with transaction.manager: db_session.add(schema) db_session.flush() errors = is_duplicate_schema(forms, errors, db_session) expected = u'Duplicate schema - already exists in the db' exists = errors[0]['errors'] == expected assert exists is True
def test_entity_force_date(db_session): """ It should maintain a date object for date types. (Sometimes applications will blindly assign datetimes...) """ from datetime import date, datetime from occams_datastore import models schema = models.Schema(name=u'Foo', title=u'', publish_date=date(2000, 1, 1)) s1 = models.Attribute( schema=schema, name='s1', title=u'Section 1', type='section', order=0) entity = models.Entity(schema=schema) # Do simple values simpleName = 'choicesimple' schema.attributes[simpleName] = models.Attribute( schema=schema, parent_attribute=s1, title=u'', type='date', is_required=False, order=1) now = datetime.now() today = now.date() entity[simpleName] = now db_session.flush() assert isinstance(entity[simpleName], date) assert today == entity[simpleName]
def test_from_section_to_schema(self, req, db_session, check_csrf_token): """ It should be able to move a field from a section to the root """ from occams_datastore import models as datastore schema = datastore.Schema( name='testform', title=u'Test Form', attributes={ 'section1': datastore.Attribute(name='section1', title=u'Section 1', type='section', attributes={ 'myvar': datastore.Attribute(name='myvar', title=u'My Var', type='string', order=1) }, order=0) }) db_session.add(schema) db_session.flush() req.json_body = {'target': None, 'index': 1} self._call_fut(schema.attributes['myvar'], req) assert sorted([(None, 'section1', 0), (None, 'myvar', 1)]) == \ sorted(self._comparable(schema))
def populate(self, db_session): from datetime import date from occams_datastore import models as datastore from occams_studies import models as studies drsc = studies.Study(name=u'drsc', title=u'DRSC', short_title=u'dr', code=u'drs', consent_date=date.today(), is_randomized=False) schema1 = datastore.Schema(name=u'demographics', title=u'demographics', publish_date=u'2015-01-01', attributes={ 'myfield': datastore.Attribute(name=u'myfield', title=u'My Field', type=u'string', order=0), 'question': datastore.Attribute(name=u'question', title=u'question', type=u'string', order=0) }) drsc.schemata.add(schema1) db_session.add(drsc) db_session.flush()
def test_list_not_include_private(self, db_session): """ It should not include private data if specified. Note this is not the same as de-identification) """ from datetime import date from occams_datastore import models as datastore from occams_studies.exports.schema import SchemaPlan schema = datastore.Schema(name=u'contact', title=u'Contact Details', publish_date=date.today(), attributes={ 'foo': datastore.Attribute(name='foo', title=u'', type='string', order=0, is_private=True) }) db_session.add_all([schema]) db_session.flush() plans = SchemaPlan.list_all(db_session, include_private=True) assert len(plans) == 1 plans = SchemaPlan.list_all(db_session, include_private=False) assert len(plans) == 0
def test_file(self, req, db_session): """ It should return the json rows for the codebook fragment """ from datetime import date from webob.multidict import MultiDict from occams_datastore import models as datastore from occams_studies import models from occams_studies.exports.schema import SchemaPlan db_session.add( datastore.Schema(name=u'aform', title=u'', publish_date=date.today(), attributes={ u'myfield': datastore.Attribute(name=u'myfield', title=u'', type=u'string', order=0) })) db_session.flush() req.GET = MultiDict([('file', 'aform')]) req.registry.settings['studies.export.plans'] = [SchemaPlan.list_all] res = self._call_fut(models.ExportFactory(req), req) assert res is not None