Beispiel #1
0
def get_model_attribute_type(attr_type):
    if attr_type == str:
        return fields.string()
    if attr_type == bool:
        return fields.boolean()
    if attr_type == dict:
        return fields.raw()
    if attr_type == int:
        return fields.integer()
    if attr_type == datetime.date:
        return fields.date()
    if attr_type == datetime.datetime:
        return fields.date_time()

    type_str = str(type(attr_type))
    is_list = 'Generic' in type_str

    if is_list:
        child_type = attr_type.__args__[0]
        return fields.array(get_model_attribute_type(child_type))

    return fields.nested(build_model(attr_type))
Beispiel #2
0
 def test_unique(self):
     field = fields.array(fields.string(), unique=True)
     assert 'uniqueItems' in field.__schema__
     assert field.__schema__['uniqueItems'] is True
Beispiel #3
0
 def test_min_items(self):
     field = fields.array(fields.string(), min_items=5)
     assert 'minItems' in field.__schema__
     assert field.__schema__['minItems'] == 5
Beispiel #4
0
 def test_max_items(self):
     field = fields.array(fields.string(), max_items=42)
     assert 'maxItems' in field.__schema__
     assert field.__schema__['maxItems'] == 42
Beispiel #5
0
 def field_class(self, **kwargs):
     return fields.array(fields.string(), **kwargs)
Beispiel #6
0
 def test_defaults(self):
     field = fields.array(fields.string())
     assert not field.required
     assert field.__schema__ == {'type': 'array', 'items': {'type': 'string'}}
Beispiel #7
0
 def test_with_empty_callable_enum(self):
     enum = lambda: []  # noqa
     field = fields.string(enum=enum)
     assert not field.required
     assert field.__schema__ == {'type': 'string'}
Beispiel #8
0
 def test_with_default(self):
     field = fields.string(default='aaa')
     assert field.__schema__ == {'type': 'string', 'default': 'aaa'}
Beispiel #9
0
 def test_with_callable_enum(self):
     enum = lambda: ['A', 'B', 'C']  # noqa
     field = fields.string(enum=enum)
     assert not field.required
     assert field.__schema__ == {'type': 'string', 'enum': ['A', 'B', 'C'], 'example': 'A'}
Beispiel #10
0
 def test_with_empty_enum(self):
     field = fields.string(enum=[])
     assert not field.required
     assert field.__schema__ == {'type': 'string'}
Beispiel #11
0
 def test_with_enum(self):
     enum = ['A', 'B', 'C']
     field = fields.string(enum=enum)
     assert not field.required
     assert field.__schema__ == {'type': 'string', 'enum': enum, 'example': enum[0]}
Beispiel #12
0
 def test_defaults(self):
     field = fields.string()
     assert not field.required
     assert field.__schema__ == {'type': 'string'}
Beispiel #13
0
 def field_class(self, **kwargs):
     return fields.string(**kwargs)