Example #1
0
def test_clamp_length():
    msg='You are a pear'
    v=V.clamp_length(min=3, msg=msg)
    assert v('500')=='500'
    assert_invalid(lambda: v('eh'), msg)
    v=V.clamp_length(max=10, msg=dict(minlen='haha', maxlen='kong'))
    assert v('40')=='40'
    assert_invalid(lambda: v('I told you that Ronald would eat it when you were in the bathroom'), 'kong')
Example #2
0
def test_clamp_length():
    msg = 'You are a pear'
    v = V.clamp_length(min=3, msg=msg)
    assert v.__name__ == "clamp_length"
    assert v('500') == '500'
    assert_invalid(lambda: v('eh'), {None: msg})
    v = V.clamp_length(max=10, msg=dict(minlen='haha', maxlen='kong'))
    assert v('40') == '40'
    assert_invalid(
        lambda:
        v('I told you that Ronald would eat it when you were in the bathroom'),
        {None: 'kong'})
Example #3
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 #4
0
def test_clamp_length():
    msg = 'You are a pear'
    v = V.clamp_length(min=3, msg=msg)
    assert v.__name__ == "clamp_length"
    assert v('500') == '500'
    assert_invalid(
        lambda: v('eh'),
        {None: msg})
    v = V.clamp_length(max=10, msg=dict(minlen='haha', maxlen='kong'))
    assert v('40') == '40'
    assert_invalid(
        lambda: v('I told you that Ronald would eat it when you were in the bathroom'),
        {None: 'kong'})

    msg = "Enter less than %(max)s.  You entered %(length)s."
    v = V.clamp_length(max=30, msg=msg)
    with py.test.raises(V.Invalid) as e:
        v("*"*50)
    errors = e.value.unpack_errors()
    expected = {None: "Enter less than 30.  You entered 50."}
    assert errors == expected
Example #5
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 #6
0
def test_map():
    data=['pig', 'frog', 'lump']
    v=lambda value: map(V.clamp_length(max=4), value)
    assert v(data)==data