Exemplo n.º 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'})
Exemplo n.º 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'})
Exemplo n.º 3
0
def test_schema_4():
    s = V.Schema(
        {
            'foo': V.is_integer(),
            'bar': V.is_integer(),
            ('foo', 'bar'): V.fields_equal(msg='flam', field=None)
        },
        msg="flibble")
    d = dict(foo=1, bar=2)
    with py.test.raises(V.Invalid) as e:
        s(d)
    errors = e.value.unpack_errors()
    assert errors == {None: 'flam'}
Exemplo n.º 4
0
def test_schema_4():
    s = V.Schema(
        {
            'foo': V.is_integer(),
            'bar': V.is_integer(),
            ('foo', 'bar'): V.fields_equal(msg='flam', field=None)
        },
        msg="flibble")
    d = dict(foo=1, bar=2)
    with py.test.raises(V.Invalid) as e:
        s(d)
    errors = e.value.unpack_errors()
    assert errors == {None: 'flam'}
Exemplo n.º 5
0
def test_nested_with_bad_data():
    validator = V.nested(flam=V.to_unicode(), 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_unicode(), 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"
Exemplo n.º 6
0
def test_is_integer():
    msg = "please enter an integer"
    v = V.is_integer(msg=msg)
    assert v.__name__ == "is_integer"
    assert v(40) == 40
    assert_invalid(
        lambda: v('whack him until he screams'),
        {None: msg})
Exemplo n.º 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'})
Exemplo n.º 8
0
def test_Schema_errors():
    schema = V.Schema(dict(foo=V.is_integer(msg="number, not word, idiot!")))
    data = dict(foo="one")
    expected = {
        'foo': "number, not word, idiot!",
        None: "Problems were found in the submitted data."
    }
    with py.test.raises(V.Invalid) as e:
        schema(data)
    result = e.value.errors
    assert expected == result
Exemplo n.º 9
0
def test_nested_many_fail_nested_errors():
    schema = V.Schema(dict(foo=V.nested_many(V.is_integer())))
    data = dict(foo=dict(a=1, b="two", c=3))
    with py.test.raises(V.Invalid) as e:
        result = schema(data)
    errors = e.value.unpack_errors()
    expected = {
        'foo': {
            'b': "not an integer"
        },
        None: "Problems were found in the submitted data."
    }
    assert expected == errors
Exemplo n.º 10
0
def test_nested_many_fail():
    validator = V.nested_many(V.is_integer())
    data = dict(a=1, b="two", c=3)

    with py.test.raises(V.Invalid) as e:
        result = validator(data)
    errors = e.value.unpack_errors()
    assert errors['b'] == "not an integer"

    with py.test.raises(V.Invalid) as e:
        result = validator(None)
    errors = e.value.unpack_errors()
    assert errors == {None: "No data found"}
Exemplo n.º 11
0
def test_Schema_errors():
    schema = V.Schema(
        dict(
            foo=V.is_integer(msg="number, not word, idiot!")))
    data = dict(
        foo="one")
    expected = {
        'foo': "number, not word, idiot!",
        None: "Problems were found in the submitted data."}
    with py.test.raises(V.Invalid) as e:
        schema(data)
    result = e.value.errors
    assert expected == result
Exemplo n.º 12
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'})
Exemplo n.º 13
0
def test_nested_with_bad_data():
    validator = V.nested(
        flam=V.to_unicode(),
        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_unicode(),
            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"
Exemplo n.º 14
0
def test_nested_many_fail_nested_errors():
    schema = V.Schema(
        dict(
            foo=V.nested_many(
                V.is_integer())))
    data = dict(
        foo=dict(
            a=1,
            b="two",
            c=3))
    with py.test.raises(V.Invalid) as e:
        result = schema(data)
    errors = e.value.unpack_errors()
    expected = {
        'foo': {'b': "not an integer"},
        None: "Problems were found in the submitted data."}
    assert expected == errors
Exemplo n.º 15
0
def test_nested_many_fail():
    validator = V.nested_many(
        V.is_integer())
    data = dict(
        a=1,
        b="two",
        c=3)

    with py.test.raises(V.Invalid) as e:
        result = validator(data)
    errors = e.value.unpack_errors()
    assert errors['b'] == "not an integer"

    with py.test.raises(V.Invalid) as e:
        result = validator(None)
    errors = e.value.unpack_errors()
    assert errors == {None: "No data found"}
Exemplo n.º 16
0
def test_errors():
    schema = V.Schema(dict(foo=(V.to_unicode(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
Exemplo n.º 17
0
def test_errors():
    schema = V.Schema(
        dict(
            foo=(
                V.to_unicode(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
Exemplo n.º 18
0
def test_is_integer():
    msg = "please enter an integer"
    v = V.is_integer(msg=msg)
    assert v.__name__ == "is_integer"
    assert v(40) == 40
    assert_invalid(lambda: v('whack him until he screams'), {None: msg})