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 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_copy_schema_basic(db_session): """ It should let the user copy schemata """ from copy import deepcopy from occams_datastore import models schema = models.Schema(name='Foo', title=u'Foo', attributes={ 'section1': models.Attribute( name=u'section1', title=u'Section 1', type='section', order=0, attributes={ 'foo': models.Attribute( name='foo', title=u'Enter Foo', type='choice', order=1, choices={ '001': models.Choice(name='001', title=u'Foo', order=0), '002': models.Choice(name='002', title=u'Bar', order=1), '003': models.Choice(name='003', title=u'Baz', order=2) }, ) }) }) db_session.add(schema) db_session.flush() schema_copy = deepcopy(schema) db_session.add(schema_copy) db_session.flush() # The ones that matter for checksums assert schema.name == schema_copy.name attribute = schema.attributes['foo'] for prop in ('name', 'title', 'description', 'type', 'is_collection', 'is_required'): attribute_copy = schema_copy.attributes['foo'] assert getattr(attribute, prop) == getattr(attribute_copy, prop) for choice in schema.attributes['foo'].choices.values(): choice_copy = schema_copy.attributes['foo'].choices[choice.name] for prop in ('name', 'title', 'order'): assert getattr(choice, prop) == getattr(choice_copy, prop)
def test_entity_choices(db_session): """ It should properly handle choices """ from datetime import date 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) db_session.add(entity) db_session.flush() # Do simple values simpleName = 'choicesimple' schema.attributes[simpleName] = models.Attribute( schema=schema, parent_attribute=s1, name=simpleName, title=u'', type='choice', is_required=False, order=1, choices={ '001': models.Choice(name=u'001', title=u'Foo', order=1), '002': models.Choice(name=u'002', title=u'Bar', order=2), '003': models.Choice(name=u'003', title=u'Baz', order=3), '004': models.Choice(name=u'004', title=u'Caz', order=4), '005': models.Choice(name=u'005', title=u'Jaz', order=5), }) entity[simpleName] = None db_session.flush() assert entity[simpleName] is None entity[simpleName] = u'002' db_session.flush() assert u'002' == entity[simpleName] # Now try collections collectionName = 'choicecollection' schema.attributes[collectionName] = models.Attribute( schema=schema, parent_attribute=s1, name=collectionName, title=u'', type='choice', is_collection=True, order=2, choices={ '001': models.Choice(name=u'001', title=u'Foo', order=1), '002': models.Choice(name=u'002', title=u'Bar', order=2), '003': models.Choice(name=u'003', title=u'Baz', order=3), '004': models.Choice(name=u'004', title=u'Caz', order=4), '005': models.Choice(name=u'005', title=u'Jaz', order=5)}) entity[collectionName] = [u'001', u'002', u'005'] db_session.flush() assert sorted([u'001', u'002', u'005']) == \ sorted(entity['choicecollection'])
def get_choices(raw_choices): """ Sample input = [[u'0', label], [u'1': label2]] Sample output = { u'0': models.Choice( name=u'1', title=u'label', order=0 ) u'1': models.Choice( name=u'1', title=u'label2', order=1 ) } :param raw_choices: a dict of choices...key is option, value is label :return: a dictionary of choice datastore objects """ choices = {} if raw_choices: for i, item in enumerate(raw_choices): code = item[0] label = item[1] choices[code] = datastore.Choice(name=code.strip(), title=label.strip(), order=i) return choices
def test_get_attributes_choice_to_value(self): """Should return the title of the source choice.""" 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='uUCSD', 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={ 'race_1': datastore.Attribute(name=u'race_1', title=u'race_1', type=u'choice', order=0, choices={ u'0': datastore.Choice(name=u'0', title=u'elf', order=0), u'1': datastore.Choice(name=u'1', title=u'halfling', order=1) }) }) record = models.SiteData(schema=schema, study=study, data=SITEDATA) source_variable = u'race_1' choices_mapping = [] target_value = self._call_fut(choices_mapping, record, source_variable, schema) assert target_value == u'elf'
def test_choice_constraint(db_session): """ It should validate against choice 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) models.Attribute( schema=schema, parent_attribute=s1, name=u'test', title=u'', type=u'choice', is_required=False, order=0, choices={ '001': models.Choice(name=u'001', title=u'Foo', order=0), '002': models.Choice(name=u'002', title=u'Bar', order=1), '003': models.Choice(name=u'003', title=u'Baz', order=2)}) db_session.add(schema) db_session.flush() entity = models.Entity(schema=schema) db_session.add(entity) entity['test'] = None entity['test'] = u'002' db_session.flush() entry = ( db_session.query(models.ValueChoice) .filter(models.ValueChoice.value.has(name=u'002')) .one()) assert entry.value.name == '002' # Should not be able to set it to something outside of the specified # choice constraints with pytest.raises(ConstraintError): entity['test'] = u'999'
def test_schema_attribute(db_session): """ It should implement full schema/attribute/subattribute hierarchies """ from datetime import date from occams_datastore import models schema = models.Schema(name=u'aform', title=u'A Form', publish_date=date.today(), attributes={ 'section1': models.Attribute(name=u'section1', title=u'Section 1', type='section', order=0, attributes={ 'foo': models.Attribute( name=u'foo', title=u'Foo', type=u'choice', order=0, choices={ '001': models.Choice( name=u'001', title=u'Green', order=0) }) }) }) db_session.add(schema) db_session.flush() assert 'section1' in schema.attributes # Works both ways assert 'foo' in schema.attributes assert 'foo' in schema.attributes['section1'].attributes