Esempio n. 1
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']})
Esempio n. 2
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']
Esempio n. 3
0
def test_belongs():
    msg = "rinse me a robot"
    v = V.belongs('pinko widget frog lump'.split(), msg=msg)
    assert v.__name__ == "belongs"
    assert v('pinko') == 'pinko'
    assert_invalid(
        lambda: v('snot'),
        {None: msg})
Esempio n. 4
0
def test_excursion():
    x='*****@*****.**'

    v=V.excursion(lambda x: x.split('@')[0],
                  V.belongs(['gadzooks', 'willy'],
                            msg='pancreatic'))
    assert x==v(x)
    assert_invalid(lambda: v('hieratic impulses'), 'pancreatic')
Esempio n. 5
0
def test_excursion():
    x = '*****@*****.**'

    v = V.excursion(lambda x, context: x.split('@')[0],
                    V.belongs(['gadzooks', 'willy'], msg='pancreatic'))
    assert v.__name__ == "excursion"
    assert x == v(x)
    assert_invalid(lambda: v('hieratic impulses'), {None: 'pancreatic'})

    v = V.excursion(lambda x, context: x.add('foo'))
    data = set(['bar'])
    result = v(data)
    assert result == set(['bar'])
    assert data == set(['bar', 'foo'])
Esempio n. 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']})
Esempio n. 7
0
def test_excursion():
    x = '*****@*****.**'

    v = V.excursion(
        lambda x, context: x.split('@')[0],
        V.belongs(['gadzooks', 'willy'], msg='pancreatic'))
    assert v.__name__ == "excursion"
    assert x == v(x)
    assert_invalid(
        lambda: v('hieratic impulses'),
        {None: 'pancreatic'})

    v = V.excursion(lambda x, context: x.add('foo'))
    data = set(['bar'])
    result = v(data)
    assert result == set(['bar'])
    assert data == set(['bar', 'foo'])
Esempio n. 8
0
def test_credit_card_3():
    cc='4000000000998'
    invalid_cc=str(int(cc)-1)
    validators=dict(cc_card=(V.strip, V.not_empty("Please enter a credit card number")),
                    cc_type=V.belongs(('Visa', 'Discover'), msg="belongs"))
    v=V.credit_card(require_type=True,
                    cc_field='cc_card',
                    cc_type_field='cc_type')
    validators[('cc_card', 'cc_type')]=v
    s=V.Schema(validators)
    data=dict(cc_card=invalid_cc,
              cc_type='')
    try:
        s(data)
    except V.Invalid, e:
        errors=e.unpack_errors()
        assert set(errors)==set(('cc_card', 'cc_type', None))
Esempio n. 9
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'])
Esempio n. 10
0
def test_credit_card_3():
    cc = '4000000000998'
    invalid_cc = str(int(cc) - 1)
    validators = dict(
        cc_card=(V.strip, V.not_empty("Please enter a credit card number")),
        cc_type=V.belongs(('Visa', 'Discover'), msg="belongs"))
    v = V.credit_card(require_type=True,
                      cc_field='cc_card',
                      cc_type_field='cc_type')
    validators[('cc_card', 'cc_type')] = v
    s = V.Schema(validators)
    data = dict(cc_card=invalid_cc, cc_type='')
    try:
        s(data)
    except V.Invalid, e:
        errors = e.unpack_errors()
        assert set(errors) == set(('cc_card', 'cc_type', None))
Esempio n. 11
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']
Esempio n. 12
0
def test_belongs():
    msg = "rinse me a robot"
    v = V.belongs('pinko widget frog lump'.split(), msg=msg)
    assert v.__name__ == "belongs"
    assert v('pinko') == 'pinko'
    assert_invalid(lambda: v('snot'), {None: msg})