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}
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
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
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 } }
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}
def test_empty(): assert marshmallow_load(TaxonomySchemaV1(), {}) == {}
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
def test_metadata(): marshmallow_load(InvenioRecordMetadataSchemaV1Mixin(), {'$schema': 'http://blah'})
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 }
def test_load_no_id_in_context(): marshmallow_load(InvenioRecordMetadataSchemaV1Mixin(), {'InvenioID': '1'}) == {}