Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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'
Exemplo n.º 4
0
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'
Exemplo n.º 5
0
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'
Exemplo n.º 6
0
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')
Exemplo n.º 7
0
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'
Exemplo n.º 8
0
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'
Exemplo n.º 9
0
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'
Exemplo n.º 10
0
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'
Exemplo n.º 11
0
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'
Exemplo n.º 12
0
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'
Exemplo n.º 13
0
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'
Exemplo n.º 14
0
def test_list_valid_2():
    """Should be ok"""

    test_list = List(property=String(validators=[min_length(3)]))
    test_list.validate(['abcdef'])

    assert True
Exemplo n.º 15
0
def test_dict_valid_1():
    """Should be ok"""

    test_dict = Dict(mapping={'foo': String()})
    test_dict.validate({'foo': 'bar'})

    assert True
Exemplo n.º 16
0
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')
Exemplo n.º 17
0
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 == []
Exemplo n.º 18
0
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'
Exemplo n.º 19
0
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'
Exemplo n.º 20
0
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'
Exemplo n.º 21
0
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')
Exemplo n.º 22
0
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"
Exemplo n.º 23
0
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')
Exemplo n.º 24
0
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'
Exemplo n.º 25
0
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'
Exemplo n.º 26
0
class Animal(Model):
    name = String(validators=[
        min_length(5, context='colloquial'),
        min_length(10, context='academic')
    ])
Exemplo n.º 27
0
 class Foo(Model):
     bar = String()
Exemplo n.º 28
0
class Author(Model):
    name = String()
Exemplo n.º 29
0
class Revision(Model):
    id = Uuid(default_value=uuid4)
    changes = String(default_value='Fake changes LOL')
Exemplo n.º 30
0
 class HilariousBlogPost(BlogPost):
     lol = String()
Exemplo n.º 31
0
def test_string_valid_1():
    """'foo' is a valid string"""
    test_string = String()
    test_string.validate('foo')

    assert True
Exemplo n.º 32
0
def test_string_valid_1():
    """'foo' is a valid string"""
    test_string = String()
    test_string.validate('foo')

    assert True
Exemplo n.º 33
0
def test_string_default():
    """Test property default values"""

    test_string = String(default_value="bar")

    assert test_string.default == "bar"