Beispiel #1
0
def test_nested_missing():
    data = dict(
        flim="Flim")
    validator = V.nested(
        flim=(
            V.to_string(),
            V.not_empty()),
        flam=V.to_string())
    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors == dict(flam="key 'flam' is missing")

    with py.test.raises(V.Invalid) as e:
        validator(None)
    errors = e.value.unpack_errors()
    assert errors == dict(
        flam="key 'flam' is missing",
        flim="key 'flim' is missing")

    validator = V.nested(
        flim=V.to_string(),
        flam=V.all_of(
            V.to_string(),
            is_in_context()))

    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors == dict(flam="key 'flam' is missing")
Beispiel #2
0
def test_nested_schema():
    nested_validators = dict(
        foo=V.to_string(),
        bar=V.nested(
            flim=V.to_string(),
            flam=V.to_string()))
    schema = V.Schema(nested_validators)
    data = dict(
        foo="Foo",
        bar=dict(
            flim="Flim",
            flam="Flam"))
    assert schema(data) == data
Beispiel #3
0
def test_to_string():
    v = V.to_string(msg="cats")
    assert v.__name__ == "to_string"
    assert v('parrots') == 'parrots'
    for t in [
        u'parrots', 'parrots', 1, object(), None,
        ]:
        assert isinstance(v(t), str)
    u = u"\N{GREEK CAPITAL LETTER OMEGA} my gawd"
    s = u.encode('utf-8')
    assert v(u) == s
    with py.test.raises(V.Invalid) as e:
        v = V.to_string(encoding='ascii', msg='cats')
        v(u)
    assert e.value.unpack_errors() == {None: "cats"}
Beispiel #4
0
def test_to_string():
    v = V.to_string(msg='cats')
    assert v.__name__ == "to_string"
    assert v("brisbane") == "brisbane"
    assert v(1) == "1"
    for t in [
        'parrots', 'parrots', 1, object(), None,
        ]:
        assert isinstance(v(t), str)
    s = "\N{GREEK CAPITAL LETTER OMEGA} my gawd"
    b = s.encode('utf-8')
    assert v(b) == s
    with py.test.raises(V.Invalid) as e:
        v = V.to_string(encoding='ascii', msg='cats')
        v(b)
    assert e.value.unpack_errors() == {None: "cats"}
Beispiel #5
0
def test_to_string():
    v = V.to_string(msg="cats")
    assert v.__name__ == "to_string"
    assert v('parrots') == 'parrots'
    for t in [
            u'parrots',
            'parrots',
            1,
            object(),
            None,
    ]:
        assert isinstance(v(t), str)
    u = u"\N{GREEK CAPITAL LETTER OMEGA} my gawd"
    s = u.encode('utf-8')
    assert v(u) == s
    with py.test.raises(V.Invalid) as e:
        v = V.to_string(encoding='ascii', msg='cats')
        v(u)
    assert e.value.unpack_errors() == {None: "cats"}
Beispiel #6
0
def test_all_of():
    v = V.all_of(V.to_string('foo'), V.not_empty('bar'))
    assert v.__name__ == "all_of"
    assert v('bob') == 'bob'
    with py.test.raises(V.Invalid) as e:
        assert v('')
    assert e.value.unpack_errors() == {None: "bar"}

    v = V.all_of(is_in_context(), V.not_empty('bar'))
    assert v('bob', context=dict(bob=1)) == 'bob'
Beispiel #7
0
def test_nested():
    data = dict(
        flim="Flim",
        flam="Flam",
        bubble="Bubble")
    expected = dict(
        flim="Flim",
        flam="Flam")
    validator = V.nested(
        flim=V.to_string(),
        flam=V.to_string())
    assert validator(data) == expected
    assert validator.__name__ == "nested"

    validator = V.nested(
        flim=V.to_string(),
        flam=V.all_of(
            V.to_string(),
            is_in_context()))
    assert validator(data, context=dict(Flam=1)) == expected
Beispiel #8
0
def test_all_of():
    v = V.all_of(V.to_string('foo'), V.not_empty('bar'))
    assert v.__name__ == "all_of"
    assert v('bob') == 'bob'
    with py.test.raises(V.Invalid) as e:
        assert v('')
    assert e.value.unpack_errors() == {None: "bar"}

    v = V.all_of(
        is_in_context(),
        V.not_empty('bar'))
    assert v('bob', context=dict(bob=1)) == 'bob'
Beispiel #9
0
def test_nested_with_bad_data():
    validator = V.nested(
        flam=V.to_string(),
        flim=V.is_integer())
    data = dict(
        flim="Flim",
        flam="Flam")
    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors['flim'] == "not an integer"

    validator = V.nested(
        foo=V.nested(
            flam=V.to_string(),
            flim=V.is_integer()))
    data = dict(
            foo=dict(
                flim="Flim",
                flam="Flam"))
    with py.test.raises(V.Invalid) as e:
        validator(data)
    errors = e.value.unpack_errors()
    assert errors['foo']['flim'] == "not an integer"
Beispiel #10
0
def test_errors():
    schema = V.Schema(
        dict(
            foo=(
                V.to_string(msg="foo can't be converted"),
                V.not_empty(msg="foo is empty")),
            bar=(
                V.is_integer(msg="bar isn't an integer"),
                V.not_empty(msg="bar is empty"))),
        msg="Check the errors and try again.  Moron.")

    with py.test.raises(V.Invalid) as e:
        data = schema(dict(foo=None, bar=None))

    expected = {
        None: 'Check the errors and try again.  Moron.',
        'bar': "bar isn't an integer",
        'foo': 'foo is empty'}

    result = e.value.unpack_errors()
    assert result == expected