Exemplo n.º 1
0
def test_of_fields():
    field_1_mock = fields.StringField()
    field_2_mock = fields.BooleanField()
    field_3_mock = fields.ArrayField(fields.IntField())
    of_fields = [field_1_mock, field_2_mock, field_3_mock]

    f = fields.OneOfField(of_fields)
    _, schema = f.get_definitions_and_schema()
    assert normalize(schema) == {'oneOf': [f.get_schema() for f in of_fields]}

    f = fields.AnyOfField(of_fields)
    _, schema = f.get_definitions_and_schema()
    assert normalize(schema) == {'anyOf': [f.get_schema() for f in of_fields]}

    f = fields.AllOfField(of_fields)
    _, schema = f.get_definitions_and_schema()
    assert normalize(schema) == {'allOf': [f.get_schema() for f in of_fields]}
Exemplo n.º 2
0
 class Tree(Document):
     node = fields.OneOfField([
         fields.ArrayField(fields.DocumentField('self')),
         fields.StringField(),
     ])
Exemplo n.º 3
0
def test_dict_field_to_schema():
    f = fields.DictField(title='Hey!', enum=[{'x': 1}, {'y': 2}])
    assert f.get_definitions_and_schema() == ({}, {
        'type': 'object',
        'enum': [
            {
                'x': 1
            },
            {
                'y': 2
            },
        ],
        'title': 'Hey!',
    })
    check_field_schema(f)

    a_field_mock = fields.StringField()
    b_field_mock = fields.BooleanField()
    c_field_mock = fields.EmailField()
    f = fields.DictField(properties={
        'a': a_field_mock,
        'b': b_field_mock,
    },
                         pattern_properties={
                             'c*': c_field_mock,
                         },
                         min_properties=5,
                         max_properties=10)
    assert f.get_definitions_and_schema() == ({}, {
        'type': 'object',
        'properties': {
            'a': a_field_mock.get_schema(),
            'b': b_field_mock.get_schema(),
        },
        'patternProperties': {
            'c*': c_field_mock.get_schema(),
        },
        'minProperties': 5,
        'maxProperties': 10,
    })

    additional_prop_field_mock = fields.OneOfField(
        (fields.StringField(), fields.NumberField()))
    f = fields.DictField(additional_properties=additional_prop_field_mock)
    assert f.get_definitions_and_schema() == ({}, {
        'type':
        'object',
        'additionalProperties':
        additional_prop_field_mock.get_schema(),
    })
    f = fields.DictField(additional_properties=False)
    assert f.get_schema()['additionalProperties'] is False

    f = fields.DictField(pattern_properties={'(': fields.StringField()})
    with pytest.raises(ValueError) as e:
        f.get_definitions_and_schema()
    assert str(e.value) == 'Invalid regular expression: unbalanced parenthesis'

    # test nested required fields
    f = fields.DictField(properties={
        'a': fields.StringField(required=True),
    },
                         pattern_properties={
                             'c*': fields.StringField(required=True),
                         })
    assert f.get_definitions_and_schema() == ({}, {
        'type': 'object',
        'properties': {
            'a': {
                'type': 'string'
            },
        },
        'patternProperties': {
            'c*': {
                'type': 'string'
            },
        },
        'required': ['a'],
    })
    check_field_schema(f)
Exemplo n.º 4
0
def test_dict_field_to_schema():
    f = fields.DictField(title='Hey!', enum=[{'x': 1}, {'y': 2}])
    definitions, schema = f.get_definitions_and_schema()
    assert s(schema) == {
        'type': 'object',
        'enum': [
            {'x': 1},
            {'y': 2},
        ],
        'title': 'Hey!',
    }

    a_field_mock = fields.StringField()
    b_field_mock = fields.BooleanField()
    c_field_mock = fields.EmailField()
    f = fields.DictField(properties={
        'a': a_field_mock,
        'b': b_field_mock,
    }, pattern_properties={
        'c*': c_field_mock,
    }, min_properties=5, max_properties=10)
    definitions, schema = f.get_definitions_and_schema()
    assert s(schema) == {
        'type': 'object',
        'properties': {
            'a': a_field_mock.get_schema(),
            'b': b_field_mock.get_schema(),
        },
        'patternProperties': {
            'c*': c_field_mock.get_schema(),
        },
        'minProperties': 5,
        'maxProperties': 10,
    }

    additional_prop_field_mock = fields.OneOfField(
        (fields.StringField(), fields.NumberField()))
    f = fields.DictField(additional_properties=additional_prop_field_mock)
    definitions, schema = f.get_definitions_and_schema()
    assert s(schema) == s({
        'type': 'object',
        'additionalProperties': additional_prop_field_mock.get_schema(),
    })
    f = fields.DictField(additional_properties=False)
    assert f.get_schema()['additionalProperties'] is False

    # test nested required fields and make sure that field names
    # do not override property names
    f = fields.DictField(properties={
        'a': fields.StringField(name='A', required=True),
    }, pattern_properties={
        'c*': fields.StringField(name='C', required=True),
    })
    definitions, schema = f.get_definitions_and_schema()
    assert s(schema) == s({
        'type': 'object',
        'properties': {
            'a': {'type': 'string'},
        },
        'patternProperties': {
            'c*': {'type': 'string'},
        },
        'required': ['a'],
    })