def test_string_not_required(): """If the property is not required, None is a valid value""" test_string = String(required=False) test_string.validate(None) assert True
def test_string_required(): """By default, a value is required""" test_string = String() with assert_raises(InvalidPropertyError) as cm: test_string.validate(None) assert cm.exception.error == 'required'
def test_string_invalid_3(): """'foobar" is too long""" test_string = String(validators=[max_length(5)]) with assert_raises(InvalidPropertyError) as cm: test_string.validate('foobar') assert cm.exception.error == 'invalid'
def test_property_required_context(): """Test context-aware property validators""" test_property = String(required=lambda c: c == 'foo') test_property.validate(None) with assert_raises(InvalidPropertyError): test_property.validate(None, context='foo')
def test_string_invalid_2(): """'fo' is too short""" test_string = String(validators=[min_length(3)]) with assert_raises(InvalidPropertyError) as cm: test_string.validate('fo') assert cm.exception.error == 'invalid'
def test_string_invalid_1(): """Integer is not a valid value""" test_string = String() with assert_raises(InvalidPropertyError) as cm: test_string.validate(5) assert cm.exception.error == 'invalid'
def test_string_regex(): """By default, a value is required""" test_string = String(validators=[regex(r'^([a-z]*)$')]) test_string.validate('abc') with assert_raises(InvalidPropertyError) as cm: test_string.validate('1236580002EEEEE') assert cm.exception.error == 'invalid'
def test_string_choices(): """Value can only be 'published' or 'draft'""" test_string = String(validators=[choices(['published', 'draft'])]) test_string.validate('published') with assert_raises(InvalidPropertyError) as cm: test_string.validate('not_a_status') assert cm.exception.error == 'invalid'
def test_list_valid_2(): """Should be ok""" test_list = List(property=String(validators=[min_length(3)])) test_list.validate(['abcdef']) assert True
def test_dict_valid_1(): """Should be ok""" test_dict = Dict(mapping={'foo': String()}) test_dict.validate({'foo': 'bar'}) assert True
def test_property_context(): """Test context-aware property validators""" test_property = String(validators=[min_length(2, context='foo')]) test_property.validate('a') test_property.validate('ab', context='foo') with assert_raises(InvalidPropertyError): test_property.validate('a', context='foo')
def test_list_default_mutable(): """Mutating the property default value after providing it should not result in an altered default""" default_list = [] test_list = List(property=String(), default_value=default_list) default_list.append('foo') assert test_list.default == []
def test_dict_invalid_3(): """Not in mapping""" test_dict = Dict(mapping={'foo': String()}) with assert_raises(InvalidPropertyError) as cm: test_dict.validate({'bar': 'baz'}) assert cm.exception.error == 'invalid'
def test_dict_invalid_2(): """Wrong type for inner value""" test_dict = Dict(mapping={'foo': String()}) with assert_raises(InvalidPropertyError) as cm: test_dict.validate({'foo': 3}) assert cm.exception.error == 'invalid'
def test_list_invalid_2(): """List contains invalid values""" test_list = List(property=String()) with assert_raises(InvalidPropertyError) as cm: test_list.validate([3, 5]) assert cm.exception.error == 'invalid'
class BlogPost(Model): id = Uuid(default_value=uuid4) title = String(validators=[ min_length(3), max_length(100), regex(r'^([A-Za-z0-9- !.]*)$') ]) body = String(default_value=u'Lorem ipsum', error_key='text') meta_data = Dict(mapping={'corrector': String(), 'reviewer': String()}) published = Boolean() likes = Integer(required=False) category = String(required=False) tags = List(property=String(validators=[min_length(3)]), error_key='category') author = Object(model_class=Author) created_on = DateTime(default_value=datetime.now) updated_on = DateTime(default_value=datetime.now) revisions = List(required=False, property=Object(model_class=Revision)) @model_validator(error_key='category') def category_or_tags(self): assert self.tags is not None or self.category is not None, ERROR_REQUIRED def __init__(self, **kwargs): super(BlogPost, self).__init__(**kwargs) self.foo = "bar"
class Animal(Model): name = String(validators=[ min_length(5, context='colloquial'), min_length(10, context='academic') ])
class Foo(Model): bar = String()
class Author(Model): name = String()
class Revision(Model): id = Uuid(default_value=uuid4) changes = String(default_value='Fake changes LOL')
class HilariousBlogPost(BlogPost): lol = String()
def test_string_valid_1(): """'foo' is a valid string""" test_string = String() test_string.validate('foo') assert True
def test_string_default(): """Test property default values""" test_string = String(default_value="bar") assert test_string.default == "bar"