Пример #1
0
def test_fields_equal():
    values = ("pong", "pong")
    v = V.fields_equal('hog')
    assert v.__name__ == "fields_equal"
    assert values == v(values)
    values = ('tim', 'worthy')
    assert_invalid(lambda: v(values), {None: 'hog'})
    s = V.Schema({
        'foo': V.to_integer(),
        ('foo', 'bar'): V.fields_equal(u"foo and bar don't match")
    })
    d = dict(foo='1', bar=1)
    expected = dict(foo=1, bar=1)
    assert s(d) == expected
    # Check field=None
    s = V.Schema({
        'foo':
        V.to_integer(),
        ('foo', 'bar'):
        V.fields_equal(u"foo and bar don't match", field=None)
    })
    d = dict(foo='1', bar=2)
    with py.test.raises(V.Invalid) as e:
        s(d)
    errors = e.value.unpack_errors()
    assert errors == {None: u"foo and bar don't match"}
Пример #2
0
def test_filter_missing():
    s = V.Schema(
        dict(
            x=V.to_integer(),
            y=V.to_integer()),
        filter_extra=False)

    d1 = dict(x="1", y="2", foo="bar")
    expected = dict(x=1, y=2, foo="bar")
    assert s(d1) == expected
Пример #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_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_nested_many():
    validator = V.nested_many(
        V.to_integer())
    data = dict(
        a="1",
        b="2",
        c=3.0)
    expected = dict(
        a=1,
        b=2,
        c=3)
    result = validator(data)
    assert result == expected
    assert validator.__name__ == "nested_many"

    validator = V.nested_many(
        is_in_context())
    data = dict(
        a="flibble",
        b="flamble",
        c="flooble")
    expected = dict(
        a="flibble",
        b="flamble",
        c="flooble")
    result = validator(data, context=['flibble', 'flamble', 'flooble'])
    assert result == expected
Пример #8
0
def test_to_integer():
    msg = "please enter an integer"
    v = V.to_integer(msg=msg)
    assert v.__name__ == "to_integer"
    assert v('40') == 40
    assert_invalid(
        lambda: v('whack him until he screams'),
        {None: msg})
Пример #9
0
def test_nested_many():
    validator = V.nested_many(V.to_integer())
    data = dict(a="1", b="2", c=3.0)
    expected = dict(a=1, b=2, c=3)
    result = validator(data)
    assert result == expected
    assert validator.__name__ == "nested_many"

    validator = V.nested_many(is_in_context())
    data = dict(a="flibble", b="flamble", c="flooble")
    expected = dict(a="flibble", b="flamble", c="flooble")
    result = validator(data, context=['flibble', 'flamble', 'flooble'])
    assert result == expected
Пример #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_only_one_of():
    v = V.only_one_of(msg="Please only choose one value")
    assert v.__name__ == "only_one_of"
    assert v((0, 1)) == (0, 1)
    assert v((1, False, None, [])) == (1, False, None, [])
    with py.test.raises(V.Invalid) as e:
        v((1, False, None, True))
    assert e.value.unpack_errors() == {None: "Please only choose one value"}
    schema = V.Schema({
        'field1': (V.to_integer()),
        'field2': (V.to_integer()),
        ('field1', 'field2'):
        V.only_one_of(msg="Please only choose one value", field='field1')
    })
    assert schema(dict(field1="0", field2="1")) == dict(field1=0, field2=1)
    with py.test.raises(V.Invalid) as e:
        schema(dict(field1=True, field2=1))

    errors = e.value.unpack_errors()
    expected = {
        None: "Problems were found in the submitted data.",
        "field1": "Please only choose one value"
    }
    assert expected == errors
Пример #12
0
def test_fields_equal():
    values = ("pong", "pong")
    v = V.fields_equal('hog')
    assert v.__name__ == "fields_equal"
    assert values == v(values)
    values = ('tim', 'worthy')
    assert_invalid(
        lambda: v(values),
        {None: 'hog'})
    s = V.Schema({
        'foo': V.to_integer(),
        ('foo', 'bar'): V.fields_equal(u"foo and bar don't match")})
    d = dict(foo='1', bar=1)
    expected = dict(foo=1, bar=1)
    assert s(d) == expected
    # Check field=None
    s = V.Schema({
        'foo': V.to_integer(),
        ('foo', 'bar'): V.fields_equal(u"foo and bar don't match", field=None)})
    d = dict(foo='1', bar=2)
    with py.test.raises(V.Invalid) as e:
        s(d)
    errors = e.value.unpack_errors()
    assert errors == {None: u"foo and bar don't match"}
Пример #13
0
def test_only_one_of():
    v = V.only_one_of(msg="Please only choose one value")
    assert v.__name__ == "only_one_of"
    assert v((0, 1)) == (0, 1)
    assert v((1, False, None, [])) == (1, False, None, [])
    with py.test.raises(V.Invalid) as e:
        v((1, False, None, True))
    assert e.value.unpack_errors() == {None: "Please only choose one value"}
    schema = V.Schema({
        'field1': (V.to_integer()),
        'field2': (V.to_integer()),
        ('field1', 'field2'):
            V.only_one_of(
                msg="Please only choose one value",
                field='field1')})
    assert schema(dict(field1="0", field2="1")) == dict(field1=0, field2=1)
    with py.test.raises(V.Invalid) as e:
        schema(dict(field1=True, field2=1))

    errors = e.value.unpack_errors()
    expected = {
        None: "Problems were found in the submitted data.",
        "field1": "Please only choose one value"}
    assert expected == errors
Пример #14
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']
Пример #15
0
def test_filter_missing():
    s = V.Schema(dict(x=V.to_integer(), y=V.to_integer()), filter_extra=False)

    d1 = dict(x="1", y="2", foo="bar")
    expected = dict(x=1, y=2, foo="bar")
    assert s(d1) == expected
Пример #16
0
def test_to_integer():
    msg = "please enter an integer"
    v = V.to_integer(msg=msg)
    assert v.__name__ == "to_integer"
    assert v('40') == 40
    assert_invalid(lambda: v('whack him until he screams'), {None: msg})