Пример #1
0
def test_array_field():
    f = ArrayField(items=())
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema()
    assert list(e.value.steps) == [FieldStep(f), AttributeStep('items')]

    f = ArrayField(items=(
        Var({'role_x': StringField()}),
        Var({'role_x': IntField()}),
    ))
    role = 'role_y'
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema(role='role_y')
    assert list(e.value.steps) == [
        FieldStep(f, role=role),
        AttributeStep('items', role=role)
    ]

    f = ArrayField(items=(None, None))
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema()
    assert list(
        e.value.steps) == [FieldStep(f),
                           AttributeStep('items'),
                           ItemStep(0)]

    f = ArrayField(items=object())
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema()
    assert list(e.value.steps) == [FieldStep(f), AttributeStep('items')]

    f = ArrayField(additional_items=object())
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema()
    assert list(
        e.value.steps) == [FieldStep(f),
                           AttributeStep('additional_items')]

    f = ArrayField(items=FieldStub())
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema()
    e = e.value
    assert e.message == FieldStub.ERROR_MESSAGE
    assert list(e.steps) == [FieldStep(f), AttributeStep('items')]

    f = ArrayField(items=(FieldStub(), ))
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema()
    e = e.value
    assert e.message == FieldStub.ERROR_MESSAGE
    assert list(e.steps) == [FieldStep(f), AttributeStep('items'), ItemStep(0)]

    f = ArrayField(additional_items=FieldStub())
    with pytest.raises(SchemaGenerationException) as e:
        f.get_schema()
    e = e.value
    assert e.message == FieldStub.ERROR_MESSAGE
    assert list(e.steps) == [FieldStep(f), AttributeStep('additional_items')]
Пример #2
0
 class Resource(Document):
     task_id = IntField(required=True)
     user = DocumentField(User, required=True)
Пример #3
0
    class User(Document):
        class Options(object):
            additional_properties = True
            title = 'User'

        id = IntField(required=True)