Пример #1
0
def test_array_field_to_schema():
    items_mock = fields.StringField()
    additional_items_mock = fields.DictField()

    f = fields.ArrayField(items_mock)
    assert f.get_definitions_and_schema() == ({}, {
        'type': 'array',
        'items': items_mock.get_schema(),
    })

    expected_items = [
        ('type', 'array'),
        ('id', 'test'),
        ('title', 'Array'),
        ('items', items_mock.get_schema()),
        ('additionalItems', additional_items_mock.get_schema()),
        ('minItems', 0),
        ('maxItems', 10),
        ('uniqueItems', True),
    ]
    f = fields.ArrayField(items_mock,
                          id='test',
                          title='Array',
                          min_items=0,
                          max_items=10,
                          unique_items=True,
                          additional_items=additional_items_mock)
    assert f.get_definitions_and_schema() == ({}, dict(expected_items))
    definitions, ordered_schema = f.get_definitions_and_schema(ordered=True)
    assert isinstance(ordered_schema, OrderedDict)
    assert ordered_schema == OrderedDict(expected_items)

    f = fields.ArrayField(items_mock, additional_items=True)
    assert f.get_definitions_and_schema() == ({}, {
        'type': 'array',
        'items': items_mock.get_schema(),
        'additionalItems': True,
    })

    f = fields.ArrayField(items_mock, additional_items=additional_items_mock)
    assert f.get_definitions_and_schema() == ({}, {
        'type':
        'array',
        'items':
        items_mock.get_schema(),
        'additionalItems':
        additional_items_mock.get_schema(),
    })

    item_1_mock = fields.NumberField()
    item_2_mock = fields.ArrayField(fields.StringField())
    f = fields.ArrayField([item_1_mock, item_2_mock])
    assert f.get_definitions_and_schema() == ({}, {
        'type':
        'array',
        'items': [item_1_mock.get_schema(),
                  item_2_mock.get_schema()],
    })
Пример #2
0
def test_array_field_to_schema():
    s_f = fields.StringField()
    d_f = fields.DictField()

    f = fields.ArrayField(s_f)
    definitions, schema = f.get_definitions_and_schema()
    assert normalize(schema) == {
        'type': 'array',
        'items': s_f.get_schema(),
    }

    expected_items = [
        ('type', 'array'),
        ('id', 'test'),
        ('title', 'Array'),
        ('items', s_f.get_schema()),
        ('additionalItems', d_f.get_schema()),
        ('minItems', 0),
        ('maxItems', 10),
        ('uniqueItems', True),
    ]
    f = fields.ArrayField(s_f,
                          id='test',
                          title='Array',
                          min_items=0,
                          max_items=10,
                          unique_items=True,
                          additional_items=d_f)
    definitions, schema = f.get_definitions_and_schema()
    assert normalize(schema) == dict(expected_items)
    definitions, ordered_schema = f.get_definitions_and_schema(ordered=True)
    assert isinstance(ordered_schema, OrderedDict)
    assert normalize(ordered_schema) == OrderedDict(expected_items)

    f = fields.ArrayField(s_f, additional_items=True)
    definitions, schema = f.get_definitions_and_schema()
    assert normalize(schema) == {
        'type': 'array',
        'items': s_f.get_schema(),
        'additionalItems': True,
    }

    f = fields.ArrayField(s_f, additional_items=d_f)
    definitions, schema = f.get_definitions_and_schema()
    assert normalize(schema) == {
        'type': 'array',
        'items': s_f.get_schema(),
        'additionalItems': d_f.get_schema(),
    }

    n_f = fields.NumberField()
    a_f = fields.ArrayField(fields.StringField())
    f = fields.ArrayField([n_f, a_f])
    definitions, schema = f.get_definitions_and_schema()
    assert normalize(schema) == {
        'type': 'array',
        'items': [n_f.get_schema(), a_f.get_schema()],
    }
Пример #3
0
def test_dict_field_walk():
    aa = fields.StringField()
    a = fields.DictField(properties={'aa': aa})
    bb = fields.StringField()
    b = fields.DictField(properties={'bb': bb})
    cc = fields.StringField()
    c = fields.DictField(properties={'cc': cc})
    dd = fields.StringField()
    d = fields.DictField(properties={'dd': dd})
    dict_field = fields.DictField(properties={
        'a': a,
        'b': b,
    },
                                  pattern_properties={
                                      'c': c,
                                  },
                                  additional_properties=d)
    path = list(dict_field.walk())
    expected_path_1 = [dict_field, a, aa, b, bb, c, cc, d, dd]
    expected_path_2 = [dict_field, b, bb, a, aa, c, cc, d, dd]
    assert path == expected_path_1 or path == expected_path_2
Пример #4
0
def test_array_field_walk():
    aa = fields.StringField()
    a = fields.DictField(properties={'aa': aa})
    b = fields.StringField()
    c = fields.StringField()

    array_field = fields.ArrayField((a, b), additional_items=c)
    path = list(array_field.walk())
    expected_path = [array_field, a, aa, b, c]
    assert path == expected_path

    array_field = fields.ArrayField(a, additional_items=False)
    path = list(array_field.walk())
    expected_path = [array_field, a, aa]
    assert path == expected_path
Пример #5
0
        class ParentTwo(Document):
            b = fields.DictField()

            class Options:
                b = 2
                d = 2
Пример #6
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)
Пример #7
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'],
    })