示例#1
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'})
示例#2
0
def test_schema_3():
    v = 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.error' : 'schema',
         'schema.extra' : 'extra',
         'schema.missing' : 'missing'},
        False,
        False
        )

    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: 'missing'})
    d4 = dict(x=10, y=10, text='ho', pingpong='lather')
    assert_invalid(
        lambda: v(d4),
        {None: 'extra'})
示例#3
0
def test_clamp():
    msg='You are a pear'
    v=V.clamp(min=30, msg=msg)
    assert v(50)==50
    assert_invalid(lambda: v(20), msg)

    v=V.clamp(max=100, msg=dict(min='haha', max='kong'))
    assert v(40)==40
    assert_invalid(lambda: v(120), 'kong')
示例#4
0
def test_clamp():
    msg = 'You are a pear'
    v = V.clamp(min=30, msg=msg)
    assert v.__name__ == "clamp"
    assert v(50) == 50
    assert_invalid(lambda: v(20), {None: msg})

    v = V.clamp(max=100, msg=dict(min='haha', max='kong'))
    assert v(40) == 40
    assert_invalid(lambda: v(120), {None: 'kong'})

    v = V.clamp(max=100, msg=dict(min='haha'))
    assert_invalid(lambda: v(120), {None: 'value above maximum'})
示例#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_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']})
示例#7
0
def test_schema_3():
    v = 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.error': 'schema',
                 'schema.extra': 'extra',
                 'schema.missing': 'missing'
             }, False, False)

    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: 'missing'})
    d4 = dict(x=10, y=10, text='ho', pingpong='lather')
    assert_invalid(lambda: v(d4), {None: 'extra'})
示例#8
0
def test_clamp():
    msg = 'You are a pear'
    v = V.clamp(min=30, msg=msg)
    assert v.__name__ == "clamp"
    assert v(50) == 50
    assert_invalid(
        lambda: v(20),
        {None: msg})

    v = V.clamp(max=100, msg=dict(min='haha', max='kong'))
    assert v(40) == 40
    assert_invalid(
        lambda: v(120),
        {None: 'kong'})

    v = V.clamp(max=100, msg=dict(min='haha'))
    assert_invalid(
        lambda: v(120),
        {None: 'value above maximum'})
示例#9
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'})
示例#10
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']})
示例#11
0
def test_compose():
    messages=dict(integer='please enter an integer',
                  belongs='invalid choice',
                  min='too small',
                  max='too big')
    v=V.compose(V.default(40),
                V.strip,
                V.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 '), messages['integer'])
    assert_invalid(lambda: v(' 41  '), messages['belongs'])
    assert_invalid(lambda: v('96'), messages['max'])
    assert_invalid(lambda: v('8'), messages['min'])
示例#12
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']