Exemple #1
0
def test_choice_defaults(dbsession):
    """
    It should set choice defaults
    """

    from occams 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)

    dbsession.add_all([schema, attribute, choice1, choice2, choice3])
    dbsession.flush()
    count = dbsession.query(models.Choice).count()
    assert count, 3 == 'Did not find any choices'
Exemple #2
0
def test_datadict_multiple_choice(dbsession):
    """
    It should retain answer choices in the columns dictionary
    """

    from copy import deepcopy
    from datetime import date, timedelta
    from six import iterkeys
    from occams import models, reporting

    today = date.today()

    schema1 = models.Schema(name=u'A',
                            title=u'A',
                            publish_date=today,
                            attributes={
                                's1':
                                models.Attribute(
                                    name=u's1',
                                    title=u'S1',
                                    type='section',
                                    order=0,
                                    attributes={
                                        'a':
                                        models.Attribute(
                                            name=u'a',
                                            title=u'',
                                            type='string',
                                            is_collection=True,
                                            order=1,
                                            choices={
                                                '001':
                                                models.Choice(name=u'001',
                                                              title=u'Foo',
                                                              order=0),
                                                '002':
                                                models.Choice(name=u'002',
                                                              title=u'Bar',
                                                              order=1)
                                            })
                                    })
                            })

    dbsession.add(schema1)
    dbsession.flush()

    columns = reporting.build_columns(dbsession, u'A')
    assert 'a' in columns
    assert sorted(['001', '002']) == sorted(iterkeys(columns['a'].choices))

    schema2 = deepcopy(schema1)
    schema2.publish_date = today + timedelta(1)
    schema2.attributes['s1'].attributes['a'].choices['003'] = \
        models.Choice(name=u'003', title=u'Baz', order=3)
    dbsession.add(schema2)
    dbsession.flush()
    columns = reporting.build_columns(dbsession, u'A')
    assert sorted(['001', '002', '003']) == \
        sorted(iterkeys(columns['a'].choices))
Exemple #3
0
def test_copy_schema_basic(dbsession):
    """
    It should let the user copy schemata
    """
    from copy import deepcopy
    from occams 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)
                                           },
                                       )
                                   })
                           })
    dbsession.add(schema)
    dbsession.flush()

    schema_copy = deepcopy(schema)
    dbsession.add(schema_copy)
    dbsession.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)
Exemple #4
0
def test_datadict_duplicate_vocabulary_term(dbsession):
    """
    It should use the most recent version of a choice label
    """

    from copy import deepcopy
    from datetime import date, timedelta
    from six import itervalues
    from occams import models, reporting

    today = date.today()

    schema1 = models.Schema(name=u'A',
                            title=u'A',
                            publish_date=today,
                            attributes={
                                's1':
                                models.Attribute(
                                    name=u's1',
                                    title=u'S1',
                                    type='section',
                                    order=0,
                                    attributes={
                                        'a':
                                        models.Attribute(
                                            name=u'a',
                                            title=u'',
                                            type='string',
                                            is_collection=True,
                                            order=1,
                                            choices={
                                                '001':
                                                models.Choice(name=u'001',
                                                              title=u'Foo',
                                                              order=0),
                                                '002':
                                                models.Choice(name=u'002',
                                                              title=u'Bar',
                                                              order=1)
                                            })
                                    })
                            })

    schema2 = deepcopy(schema1)
    schema2.state = u'published'
    schema2.publish_date = today + timedelta(1)
    for choice in itervalues(schema2.attributes['s1'].attributes['a'].choices):
        choice.title = 'New ' + choice.title

    dbsession.add_all([schema1, schema2])
    dbsession.flush()

    columns = reporting.build_columns(dbsession, u'A')
    assert '001' in columns['a'].choices
    assert '002' in columns['a'].choices
    assert 'New Foo' == columns['a'].choices['001']
    assert 'New Bar' == columns['a'].choices['002']
Exemple #5
0
def test_choice_constraint(dbsession):
    """
    It should validate against choice constraints
    """
    from datetime import date
    from occams import models
    from occams.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)
                     })
    dbsession.add(schema)
    dbsession.flush()

    entity = models.Entity(schema=schema)
    dbsession.add(entity)

    entity['test'] = None
    entity['test'] = u'002'
    dbsession.flush()

    entry = (dbsession.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'
Exemple #6
0
def test_schema_attribute(dbsession):
    """
    It should implement full schema/attribute/subattribute hierarchies
    """
    from datetime import date
    from occams 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)
                                                        })
                                                })
                           })

    dbsession.add(schema)
    dbsession.flush()
    assert 'section1' in schema.attributes
    # Works both ways
    assert 'foo' in schema.attributes
    assert 'foo' in schema.attributes['section1'].attributes
Exemple #7
0
def test_entity_choices(dbsession):
    """
    It should properly handle choices
    """
    from datetime import date
    from occams 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)
    dbsession.add(entity)
    dbsession.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
    dbsession.flush()
    assert entity[simpleName] is None

    entity[simpleName] = u'002'
    dbsession.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']
    dbsession.flush()
    assert sorted([u'001', u'002', u'005']) == \
        sorted(entity['choicecollection'])
Exemple #8
0
def test_build_report_expand_none_selected(dbsession):
    """
    It should leave all choices blank (not zero) on if no option was selected
    """
    from datetime import date
    from occams import models, reporting

    today = date.today()

    schema1 = models.Schema(name=u'A',
                            title=u'A',
                            publish_date=today,
                            attributes={
                                's1':
                                models.Attribute(
                                    name=u's1',
                                    title=u'S1',
                                    type='section',
                                    order=0,
                                    attributes={
                                        'a':
                                        models.Attribute(
                                            name=u'a',
                                            title=u'',
                                            type='choice',
                                            is_collection=True,
                                            order=1,
                                            choices={
                                                '001':
                                                models.Choice(name=u'001',
                                                              title=u'Green',
                                                              order=0),
                                                '002':
                                                models.Choice(name=u'002',
                                                              title=u'Red',
                                                              order=1),
                                                '003':
                                                models.Choice(name=u'003',
                                                              title=u'Blue',
                                                              order=2)
                                            })
                                    })
                            })
    dbsession.add(schema1)
    dbsession.flush()

    entity1 = models.Entity(schema=schema1)
    dbsession.add(entity1)
    dbsession.flush()

    # delimited multiple-choice, labels off
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=False,
                                    use_choice_labels=False)
    result = dbsession.query(report).one()
    assert result.a is None

    # delimited multiple-choice, labels on
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=False,
                                    use_choice_labels=True)
    result = dbsession.query(report).one()
    assert result.a is None

    # expanded multiple-choice, labels off
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=True,
                                    use_choice_labels=False)
    result = dbsession.query(report).one()
    assert result.a_001 is None
    assert result.a_002 is None
    assert result.a_003 is None

    # expanded multiple-choice, labels on
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=True,
                                    use_choice_labels=True)
    result = dbsession.query(report).one()
    assert result.a_001 is None
    assert result.a_002 is None
    assert result.a_003 is None
Exemple #9
0
def test_build_report_choice_types(dbsession):
    """
    It should be able to use choice labels instead of codes.
    (for human readibily)
    """

    from datetime import date
    from occams import models, reporting

    today = date.today()

    schema1 = models.Schema(name=u'A',
                            title=u'A',
                            publish_date=today,
                            attributes={
                                's1':
                                models.Attribute(
                                    name=u's1',
                                    title=u'S1',
                                    type='section',
                                    order=0,
                                    attributes={
                                        'a':
                                        models.Attribute(
                                            name=u'a',
                                            title=u'',
                                            type='choice',
                                            is_collection=False,
                                            order=0,
                                            choices={
                                                '001':
                                                models.Choice(name=u'001',
                                                              title=u'Green',
                                                              order=0),
                                                '002':
                                                models.Choice(name=u'002',
                                                              title=u'Red',
                                                              order=1),
                                                '003':
                                                models.Choice(name=u'003',
                                                              title=u'Blue',
                                                              order=2)
                                            })
                                    })
                            })
    dbsession.add(schema1)
    dbsession.flush()

    entity1 = models.Entity(schema=schema1)
    entity1['a'] = u'002'
    dbsession.add(entity1)
    dbsession.flush()

    # labels off
    report = reporting.build_report(dbsession, u'A', use_choice_labels=False)
    result = dbsession.query(report).one()
    assert result.a == '002'

    # labels on
    report = reporting.build_report(dbsession, u'A', use_choice_labels=True)
    result = dbsession.query(report).one()
    assert result.a == 'Red'

    # switch to multiple-choice
    schema1.attributes['a'].is_collection = True
    entity1['a'] = ['002', '003']
    dbsession.flush()

    # delimited multiple-choice, labels off
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=False,
                                    use_choice_labels=False)
    result = dbsession.query(report).one()
    assert sorted(result.a.split(';')) == sorted(['002', '003'])

    # delimited multiple-choice, labels on
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=False,
                                    use_choice_labels=True)
    result = dbsession.query(report).one()
    assert sorted(result.a.split(';')) == sorted(['Red', 'Blue'])

    # expanded multiple-choice, labels off
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=True,
                                    use_choice_labels=False)
    result = dbsession.query(report).one()
    assert result.a_001 == 0
    assert result.a_002 == 1
    assert result.a_003 == 1

    # expanded multiple-choice, labels on
    report = reporting.build_report(dbsession,
                                    u'A',
                                    expand_collections=True,
                                    use_choice_labels=True)
    result = dbsession.query(report).one()
    assert result.a_001 is None
    assert result.a_002 == 'Red'
    assert result.a_003 == 'Blue'