예제 #1
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']
예제 #2
0
def test_empty():
    v = V.empty(msg="scorch me")
    assert v.__name__ == "empty"
    assert v('') == ''
    assert v(None) == None
    assert_invalid(
        lambda: v("bob"),
        {None: 'scorch me'})
예제 #3
0
def test_either():
    msg = "please enter an integer"
    v = V.either(V.empty(), V.to_integer(msg=msg))
    assert v.__name__ == "either"
    assert v('') == ''
    assert v('40') == 40
    assert_invalid(lambda: v('bonk'), {None: msg})

    v = V.either(is_in_context(), V.to_integer(msg=msg))
    assert v('40') == 40
    assert v('foo', context=['foo']) == 'foo'
예제 #4
0
def test_either():
    msg = "please enter an integer"
    v = V.either(V.empty(), V.to_integer(msg=msg))
    assert v.__name__ == "either"
    assert v('') == ''
    assert v('40') == 40
    assert_invalid(
        lambda: v('bonk'),
        {None: msg})

    v = V.either(
        is_in_context(),
        V.to_integer(msg=msg))
    assert v('40') == 40
    assert v('foo', context=['foo']) == 'foo'
예제 #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']
예제 #6
0
def test_empty():
    v = V.empty(msg="scorch me")
    assert v.__name__ == "empty"
    assert v('') == ''
    assert v(None) == None
    assert_invalid(lambda: v("bob"), {None: 'scorch me'})
예제 #7
0
def test_empty():
    v=V.empty(msg="scorch me")
    assert v('')==''
    assert v(None)==None
    assert_invalid(lambda: v("bob"), 'scorch me')
예제 #8
0
def test_either():
    msg="please enter an integer"
    v=V.either(V.empty(), V.integer(msg=msg))
    assert v('')==''
    assert v('40')==40
    assert_invalid(lambda: v('bonk'), msg)