Esempio 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'})
Esempio n. 2
0
def test_check():
    d=dict(x=5, y=100)
    def add_z(val):
        val['z']=300
    def len_d(v2, size):
        if len(v2)!=size:
            raise V.Invalid("wrong size")
    d2=V.check(add_z, partial(len_d, size=3))(d)
    assert d2 is d
    assert d['z']==300
Esempio n. 3
0
def test_check():
    d = dict(x=5, y=100)

    def add_z(val, context=None):
        val['z'] = 300

    def len_d(v2, size, context=None):
        if len(v2) != size:
            raise V.Invalid("wrong size")

    d2 = V.check(add_z, partial(len_d, size=3))(d)
    assert d2 is d
    assert d['z'] == 300

    d = dict(x=5, y=100)
    validator = V.check(is_in_context(), add_z, partial(len_d, size=3))
    assert validator.__name__ == "check"
    result = validator(d, context=[dict(x=5, y=100)])
    assert result is d
    assert d['z'] == 300
Esempio n. 4
0
def test_check():
    d = dict(x=5, y=100)
    def add_z(val, context=None):
        val['z'] = 300
    def len_d(v2, size, context=None):
        if len(v2) != size:
            raise V.Invalid("wrong size")
    d2 = V.check(add_z, partial(len_d, size=3))(d)
    assert d2 is d
    assert d['z'] == 300

    d = dict(x=5, y=100)
    validator = V.check(
        is_in_context(),
        add_z,
        partial(len_d, size=3))
    assert validator.__name__ == "check"
    result = validator(d, context=[dict(x=5, y=100)])
    assert result is d
    assert d['z'] == 300
Esempio n. 5
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'})