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))
def test_unique(self): field = fields.array(fields.string(), unique=True) assert 'uniqueItems' in field.__schema__ assert field.__schema__['uniqueItems'] is True
def test_min_items(self): field = fields.array(fields.string(), min_items=5) assert 'minItems' in field.__schema__ assert field.__schema__['minItems'] == 5
def test_max_items(self): field = fields.array(fields.string(), max_items=42) assert 'maxItems' in field.__schema__ assert field.__schema__['maxItems'] == 42
def field_class(self, **kwargs): return fields.array(fields.string(), **kwargs)
def test_defaults(self): field = fields.array(fields.string()) assert not field.required assert field.__schema__ == {'type': 'array', 'items': {'type': 'string'}}
def test_with_empty_callable_enum(self): enum = lambda: [] # noqa field = fields.string(enum=enum) assert not field.required assert field.__schema__ == {'type': 'string'}
def test_with_default(self): field = fields.string(default='aaa') assert field.__schema__ == {'type': 'string', 'default': 'aaa'}
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'}
def test_with_empty_enum(self): field = fields.string(enum=[]) assert not field.required assert field.__schema__ == {'type': 'string'}
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]}
def test_defaults(self): field = fields.string() assert not field.required assert field.__schema__ == {'type': 'string'}
def field_class(self, **kwargs): return fields.string(**kwargs)