コード例 #1
0
def test_load():
    class TestSchema(DraftEnabledSchema):
        fld = Integer(required=True)

    schema = TestSchema()
    assert marshmallow_load(schema, {'fld': 1}) == {'fld': 1}

    with pytest.raises(ValidationError) as exc:
        marshmallow_load(schema, {'fld': None})
    assert exc.value.args[0] == {'fld': ['Field may not be null.']}

    schema = DraftSchemaWrapper(TestSchema)()
    assert marshmallow_load(schema, {'fld': None}) == {'fld': None}
コード例 #2
0
def test_simple():
    value = dict(id=1,
                 slug='/test',
                 path='/abc/def',
                 title=[dict(lang='cs', value='titul')],
                 tooltip='blah',
                 level=1,
                 ancestors=[dict(slug='/parent', level=0)])
    assert marshmallow_load(TaxonomySchemaV1(), value) == value
コード例 #3
0
def test_files_mixin():
    schema = type(
        'SchemaWithFiles',
        (InvenioRecordMetadataSchemaV1Mixin, InvenioRecordMetadataFilesMixin),
        {})
    data = {
        '$schema': 'http://blah',
        '_bucket': '123',
        '_files': [{
            'key': 'test.json'
        }]
    }
    loaded = marshmallow_load(schema(), data)
    assert loaded == data
コード例 #4
0
def test_load_nested():
    class TestNestedSchema(DraftEnabledSchema):
        fld = Integer(required=True)

    class TestSchema(DraftEnabledSchema):
        nest = Nested(TestNestedSchema)

    class TestSchema2(DraftEnabledSchema):
        nest = Nested(TestNestedSchema())

    schema = TestSchema()
    schema2 = TestSchema2()
    assert marshmallow_load(schema, {'nest': {
        'fld': 1
    }}) == {
        'nest': {
            'fld': 1
        }
    }
    assert marshmallow_load(schema2, {'nest': {
        'fld': 1
    }}) == {
        'nest': {
            'fld': 1
        }
    }

    err = {'nest': {'fld': ['Field may not be null.']}}

    with pytest.raises(ValidationError) as exc:
        assert marshmallow_load(schema, {'nest': {'fld': None}}) == err
    assert exc.value.args[0] == err
    with pytest.raises(ValidationError) as exc:
        assert marshmallow_load(schema2, {'nest': {'fld': None}}) == err
    assert exc.value.args[0] == err

    schema = DraftSchemaWrapper(TestSchema)()
    assert marshmallow_load(schema, {'nest': {
        'fld': None
    }}) == {
        'nest': {
            'fld': None
        }
    }

    schema2 = DraftSchemaWrapper(TestSchema)()
    assert marshmallow_load(schema2, {'nest': {
        'fld': None
    }}) == {
        'nest': {
            'fld': None
        }
    }
コード例 #5
0
def test_validators_allowed():
    class TestSchema(DraftEnabledSchema):
        fld = Integer(required=True, validate=[draft_allowed(lambda x: x > 1)])

    schema = TestSchema()
    assert marshmallow_load(schema, {'fld': 10}) == {'fld': 10}

    with pytest.raises(ValidationError) as exc:
        marshmallow_load(schema, {'fld': 1})
    assert exc.value.args[0] == {'fld': ['Invalid value.']}

    with pytest.raises(ValidationError) as exc:
        marshmallow_load(schema, {'fld': None})
    assert exc.value.args[0] == {'fld': ['Field may not be null.']}

    schema = DraftSchemaWrapper(TestSchema)()
    assert marshmallow_load(schema, {'fld': 10}) == {'fld': 10}

    with pytest.raises(ValidationError) as exc:
        marshmallow_load(schema, {'fld': 1})
    assert exc.value.args[0] == {'fld': ['Invalid value.']}

    assert marshmallow_load(schema, {'fld': None}) == {'fld': None}
コード例 #6
0
def test_empty():
    assert marshmallow_load(TaxonomySchemaV1(), {}) == {}
コード例 #7
0
def test_ref(app, db, root_taxonomy):
    t1 = root_taxonomy.create_term(slug="leaf1")
    value = {'$ref': "http://localhost/taxonomies/root/leaf1"}
    assert marshmallow_load(TaxonomySchemaV1(), value) == value
コード例 #8
0
def test_metadata():
    marshmallow_load(InvenioRecordMetadataSchemaV1Mixin(),
                     {'$schema': 'http://blah'})
コード例 #9
0
def test_load_id_in_context():
    pid = PersistentIdentifier(object_uuid=uuid1(), pid_value='1')
    assert marshmallow_load(
        InvenioRecordMetadataSchemaV1Mixin(context={'pid': pid}), {}) == {
            'InvenioID': pid.pid_value
        }
コード例 #10
0
def test_load_no_id_in_context():
    marshmallow_load(InvenioRecordMetadataSchemaV1Mixin(),
                     {'InvenioID': '1'}) == {}