Exemplo n.º 1
0
 def test_metaschema_uniqueness_is_enforced_in_the_database(self):
     MetaSchema(name='foo', schema={'foo': 42}, schema_version=1).save()
     assert_raises(
         ValidationError,
         MetaSchema(name='foo', schema={
             'bar': 24
         }, schema_version=1).save)
Exemplo n.º 2
0
def generate_schema_from_data(data):
    def from_property(id, prop):
        if isinstance(prop.get('value'), dict):
            return {
                'id': id,
                'type': 'object',
                'properties': [
                    from_property(pid, sp)
                    for pid, sp in prop['value'].items()
                ]
            }
        else:
            return {
                'id': id,
                'type': 'osf-upload' if prop.get('extra') else 'string'
            }
    def from_question(qid, question):
        if q.get('extra'):
            return {
                'qid': qid,
                'type': 'osf-upload'
            }
        elif isinstance(q.get('value'), dict):
            return {
                'qid': qid,
                'type': 'object',
                'properties': [
                    from_property(id, value)
                    for id, value in question.get('value').items()
                ]
            }
        else:
            return {
                'qid': qid,
                'type': 'string'
            }
    _schema = {
        'name': 'Test',
        'version': 2,
        'config': {
            'hasFiles': True
        },
        'pages': [{
            'id': 'page1',
            'questions': [
                from_question(qid, q)
                for qid, q in data.items()
            ]
        }]
    }
    schema = MetaSchema(
        name=_schema['name'],
        schema_version=_schema['version'],
        schema=_schema
    )
    try:
        schema.save()
    except KeyExistsException:

        # Unfortunately, we don't have db isolation between test cases for some
        # reason. Update the doc currently in the db rather than saving a new
        # one.

        schema = MetaSchema.find_one(
            Q('name', 'eq', _schema['name']) &
            Q('schema_version', 'eq', _schema['version'])
        )
        schema.schema = _schema
        schema.save()

    return schema
Exemplo n.º 3
0
 def test_metaschema_is_fine_with_same_name_but_different_version(self):
     MetaSchema(name='foo', schema_version=1).save()
     MetaSchema(name='foo', schema_version=2).save()
     assert_equal(MetaSchema.find(name='foo').count(), 2)
Exemplo n.º 4
0
 def test_metaschema_uniqueness_is_enforced_in_the_database(self):
     # Using MongoDB's uniqueness instead of modular-odm's allows us to
     # kludge a race-less upsert in ensure_schema.
     MetaSchema(name='foo', schema_version=1).save()
     assert_raises(KeyExistsException, MetaSchema(name='foo', schema_version=1).save)