Example #1
0
def test_all_of():
    v = V.all_of(V.to_string('foo'), V.not_empty('bar'))
    assert v.__name__ == "all_of"
    assert v('bob') == 'bob'
    with py.test.raises(V.Invalid) as e:
        assert v('')
    assert e.value.unpack_errors() == {None: "bar"}

    v = V.all_of(is_in_context(), V.not_empty('bar'))
    assert v('bob', context=dict(bob=1)) == 'bob'
Example #2
0
def test_all_of():
    v = V.all_of(V.to_string('foo'), V.not_empty('bar'))
    assert v.__name__ == "all_of"
    assert v('bob') == 'bob'
    with py.test.raises(V.Invalid) as e:
        assert v('')
    assert e.value.unpack_errors() == {None: "bar"}

    v = V.all_of(
        is_in_context(),
        V.not_empty('bar'))
    assert v('bob', context=dict(bob=1)) == 'bob'
Example #3
0
def test_schema_2():
    s = V.Schema(
        dict(x=(V.is_integer('intx'), V.clamp(min=5, max=100, msg='clampx')),
             y=(V.is_integer('inty'), V.clamp(min=5, max=100, msg='clampy')),
             text=V.strip),
        "schema"
        )
    def check_keys(data, context=None):
        allkeys = set(('x', 'y', 'text'))
        found = set(data.keys())
        if allkeys.difference(found):
            raise V.Invalid("incomplete data")
        if found.difference(allkeys):
            raise V.Invalid("extra data")
    v = V.all_of(V.check(check_keys), s)
    d1 = dict(x=40, y=20, text='hi there')
    assert v(d1) == d1
    d2 = dict(x=1, y=20, text='hi there')
    assert_invalid(
        lambda: v(d2),
        {None: 'schema', 'x': 'clampx'})
    d3 = dict(x=10, y=10)
    assert_invalid(
        lambda: v(d3),
        {None: 'incomplete data'})
    d4 = dict(x=10, y=10, text='ho', pingpong='lather')
    assert_invalid(
        lambda: v(d4),
        {None: 'extra data'})
Example #4
0
def test_schema_1():
    s = V.Schema(
        dict(username=(V.strip,
                       V.regex('[a-z][a-z0-9]+',
                               'invalid username'),
                       V.clamp_length(max=16,
                                      msg='username is too long'),
                       ),
             user_id=V.either(V.empty(),
                              V.all_of(V.to_integer('not an integer'),
                                        V.clamp(min=1, max=9999, msg='out of range')
                                        )
                              ),
             department=(V.strip,
                         V.belongs(['interactive', 'programming'],
                                   'department not recognized')
                         ),
             ),
        "there were errors with your submission"
        )
    data = dict(username='******',
                user_id='1',
                department='interactive')
    newdata = s(data)
    assert data['username'] == newdata['username']
    assert int(data['user_id']) == newdata['user_id']
    assert data['department'] == newdata['department']
Example #5
0
def test_all_of_2():
    messages = dict(to_integer='not an integer',
                  belongs='invalid choice',
                  min='too small',
                  max='too big')
    v = V.all_of(V.default(40),
                V.strip,
                V.to_integer(msg=messages),
                V.belongs(range(4, 100, 4), messages),
                V.clamp(min=20, max=50, msg=messages))
    assert v(None) == 40
    assert v('40') == 40
    assert v('44  ') == 44
    assert_invalid(
        lambda: v(' prick '),
        {None: messages['to_integer']})
    assert_invalid(
        lambda: v(' 41  '),
        {None: messages['belongs']})
    assert_invalid(
        lambda: v('96'),
        {None: messages['max']})
    assert_invalid(
        lambda: v('8'),
        {None: messages['min']})
Example #6
0
def test_nested_missing():
    data = dict(
        flim="Flim")
    validator = V.nested(
        flim=(
            V.to_unicode(),
            V.not_empty()),
        flam=V.to_unicode())
    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors == dict(flam="key 'flam' is missing")

    with py.test.raises(V.Invalid) as e:
        validator(None)
    errors = e.value.unpack_errors()
    assert errors == dict(
        flam="key 'flam' is missing",
        flim="key 'flim' is missing")

    validator = V.nested(
        flim=V.to_unicode(),
        flam=V.all_of(
            V.to_unicode(),
            is_in_context()))

    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors == dict(flam="key 'flam' is missing")
Example #7
0
def test_nested():
    data = dict(flim="Flim", flam="Flam", bubble="Bubble")
    expected = dict(flim="Flim", flam="Flam")
    validator = V.nested(flim=V.to_unicode(), flam=V.to_unicode())
    assert validator(data) == expected
    assert validator.__name__ == "nested"

    validator = V.nested(flim=V.to_unicode(),
                         flam=V.all_of(V.to_unicode(), is_in_context()))
    assert validator(data, context=dict(Flam=1)) == expected
Example #8
0
def test_all_of_2():
    messages = dict(to_integer='not an integer',
                    belongs='invalid choice',
                    min='too small',
                    max='too big')
    v = V.all_of(V.default(40), V.strip, V.to_integer(msg=messages),
                 V.belongs(range(4, 100, 4), messages),
                 V.clamp(min=20, max=50, msg=messages))
    assert v(None) == 40
    assert v('40') == 40
    assert v('44  ') == 44
    assert_invalid(lambda: v(' prick '), {None: messages['to_integer']})
    assert_invalid(lambda: v(' 41  '), {None: messages['belongs']})
    assert_invalid(lambda: v('96'), {None: messages['max']})
    assert_invalid(lambda: v('8'), {None: messages['min']})
Example #9
0
def test_nested():
    data = dict(
        flim="Flim",
        flam="Flam",
        bubble="Bubble")
    expected = dict(
        flim="Flim",
        flam="Flam")
    validator = V.nested(
        flim=V.to_unicode(),
        flam=V.to_unicode())
    assert validator(data) == expected
    assert validator.__name__ == "nested"

    validator = V.nested(
        flim=V.to_unicode(),
        flam=V.all_of(
            V.to_unicode(),
            is_in_context()))
    assert validator(data, context=dict(Flam=1)) == expected
Example #10
0
def test_schema_1():
    s = V.Schema(
        dict(
            username=(
                V.strip,
                V.regex('[a-z][a-z0-9]+', 'invalid username'),
                V.clamp_length(max=16, msg='username is too long'),
            ),
            user_id=V.either(
                V.empty(),
                V.all_of(V.to_integer('not an integer'),
                         V.clamp(min=1, max=9999, msg='out of range'))),
            department=(V.strip,
                        V.belongs(['interactive', 'programming'],
                                  'department not recognized')),
        ), "there were errors with your submission")
    data = dict(username='******', user_id='1', department='interactive')
    newdata = s(data)
    assert data['username'] == newdata['username']
    assert int(data['user_id']) == newdata['user_id']
    assert data['department'] == newdata['department']
Example #11
0
def test_nested_missing():
    data = dict(flim="Flim")
    validator = V.nested(flim=(V.to_unicode(), V.not_empty()),
                         flam=V.to_unicode())
    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors == dict(flam="key 'flam' is missing")

    with py.test.raises(V.Invalid) as e:
        validator(None)
    errors = e.value.unpack_errors()
    assert errors == dict(flam="key 'flam' is missing",
                          flim="key 'flim' is missing")

    validator = V.nested(flim=V.to_unicode(),
                         flam=V.all_of(V.to_unicode(), is_in_context()))

    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors == dict(flam="key 'flam' is missing")
Example #12
0
def test_schema_2():
    s = V.Schema(
        dict(x=(V.is_integer('intx'), V.clamp(min=5, max=100, msg='clampx')),
             y=(V.is_integer('inty'), V.clamp(min=5, max=100, msg='clampy')),
             text=V.strip), "schema")

    def check_keys(data, context=None):
        allkeys = set(('x', 'y', 'text'))
        found = set(data.keys())
        if allkeys.difference(found):
            raise V.Invalid("incomplete data")
        if found.difference(allkeys):
            raise V.Invalid("extra data")

    v = V.all_of(V.check(check_keys), s)
    d1 = dict(x=40, y=20, text='hi there')
    assert v(d1) == d1
    d2 = dict(x=1, y=20, text='hi there')
    assert_invalid(lambda: v(d2), {None: 'schema', 'x': 'clampx'})
    d3 = dict(x=10, y=10)
    assert_invalid(lambda: v(d3), {None: 'incomplete data'})
    d4 = dict(x=10, y=10, text='ho', pingpong='lather')
    assert_invalid(lambda: v(d4), {None: 'extra data'})