コード例 #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
コード例 #2
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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
コード例 #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'
コード例 #4
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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'
コード例 #5
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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'
コード例 #6
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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')
コード例 #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'
コード例 #8
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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'
コード例 #9
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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'
コード例 #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'
コード例 #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'
コード例 #12
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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'
コード例 #13
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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'
コード例 #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
コード例 #15
0
def test_dict_valid_1():
    """Should be ok"""

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

    assert True
コード例 #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')
コード例 #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 == []
コード例 #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'
コード例 #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'
コード例 #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'
コード例 #21
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
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')
コード例 #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"
コード例 #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')
コード例 #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'
コード例 #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'
コード例 #26
0
class Animal(Model):
    name = String(validators=[
        min_length(5, context='colloquial'),
        min_length(10, context='academic')
    ])
コード例 #27
0
 class Foo(Model):
     bar = String()
コード例 #28
0
class Author(Model):
    name = String()
コード例 #29
0
class Revision(Model):
    id = Uuid(default_value=uuid4)
    changes = String(default_value='Fake changes LOL')
コード例 #30
0
 class HilariousBlogPost(BlogPost):
     lol = String()
コード例 #31
0
def test_string_valid_1():
    """'foo' is a valid string"""
    test_string = String()
    test_string.validate('foo')

    assert True
コード例 #32
0
ファイル: test_basic.py プロジェクト: youhaveajack/kelly
def test_string_valid_1():
    """'foo' is a valid string"""
    test_string = String()
    test_string.validate('foo')

    assert True
コード例 #33
0
def test_string_default():
    """Test property default values"""

    test_string = String(default_value="bar")

    assert test_string.default == "bar"