def test_nested_missing(): data = dict( flim="Flim") validator = V.nested( flim=( V.to_unicode(), V.not_empty()), flam=V.to_unicode()) 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_unicode(), flam=V.all_of( V.to_unicode(), 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")
def test_nested(): data = dict(flim="Flim", flam="Flam", bubble="Bubble") expected = dict(flim="Flim", flam="Flam") validator = V.nested(flim=V.to_unicode(), flam=V.to_unicode()) assert validator(data) == expected assert validator.__name__ == "nested" validator = V.nested(flim=V.to_unicode(), flam=V.all_of(V.to_unicode(), is_in_context())) assert validator(data, context=dict(Flam=1)) == expected
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"
def test_nested_schema(): nested_validators = dict(foo=V.to_unicode(), bar=V.nested(flim=V.to_unicode(), flam=V.to_unicode())) schema = V.Schema(nested_validators) data = dict(foo="Foo", bar=dict(flim="Flim", flam="Flam")) assert schema(data) == data
def test_nested(): data = dict( flim="Flim", flam="Flam", bubble="Bubble") expected = dict( flim="Flim", flam="Flam") validator = V.nested( flim=V.to_unicode(), flam=V.to_unicode()) assert validator(data) == expected assert validator.__name__ == "nested" validator = V.nested( flim=V.to_unicode(), flam=V.all_of( V.to_unicode(), is_in_context())) assert validator(data, context=dict(Flam=1)) == expected
def test_nested_missing(): data = dict(flim="Flim") validator = V.nested(flim=(V.to_unicode(), V.not_empty()), flam=V.to_unicode()) 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_unicode(), flam=V.all_of(V.to_unicode(), 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")
def test_nested_schema(): nested_validators = dict( foo=V.to_unicode(), bar=V.nested( flim=V.to_unicode(), flam=V.to_unicode())) schema = V.Schema(nested_validators) data = dict( foo="Foo", bar=dict( flim="Flim", flam="Flam")) assert schema(data) == data
def test_ContextValidator(): def _is_in_database(msg="it's not in the database"): "Checks if something is in the database" def f(value, context): if not value in context['database']: raise V.Invalid(msg) return True return f is_in_database = _is_in_database assert is_in_database.__name__ == _is_in_database.__name__ assert is_in_database.__doc__ == _is_in_database.__doc__ v = is_in_database() context = dict(database=['foo', 'bar', 'bum']) assert is_in_database()('foo', context) with py.test.raises(V.Invalid) as e: assert is_in_database()('flooble', context) errors = e.value.unpack_errors() assert errors == {None: "it's not in the database"} with py.test.raises(V.Invalid) as e: assert is_in_database("ting nah in db")('flooble', context) errors = e.value.unpack_errors() assert errors == {None: "ting nah in db"} foo_schema = V.Schema(dict(flibble=is_in_database())) data = dict(flibble='foo') context = dict(database=['foo', 'bar', 'bum']) result = foo_schema(data, context) assert result == dict(flibble=True) with py.test.raises(V.Invalid) as e: data = dict(flibble='flansit') context = dict(database=['foo', 'bar', 'bum']) result = foo_schema(data, context) errors = e.value.unpack_errors() assert errors == { None: "Problems were found in the submitted data.", 'flibble': "it's not in the database" } validator = V.nested(flim=is_in_database('doodah')) data = dict(flim='foo') context = dict(database=['foo', 'bar', 'bum']) result = validator(data, context) assert result == dict(flim=True)
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"
def test_ContextValidator(): def _is_in_database(msg="it's not in the database"): "Checks if something is in the database" def f(value, context): if not value in context['database']: raise V.Invalid(msg) return True return f is_in_database = _is_in_database assert is_in_database.__name__ == _is_in_database.__name__ assert is_in_database.__doc__ == _is_in_database.__doc__ v = is_in_database() context = dict(database=['foo', 'bar', 'bum']) assert is_in_database()('foo', context) with py.test.raises(V.Invalid) as e: assert is_in_database()('flooble', context) errors = e.value.unpack_errors() assert errors == {None: "it's not in the database"} with py.test.raises(V.Invalid) as e: assert is_in_database("ting nah in db")('flooble', context) errors = e.value.unpack_errors() assert errors == {None: "ting nah in db"} foo_schema = V.Schema(dict(flibble=is_in_database())) data = dict(flibble='foo') context = dict(database=['foo', 'bar', 'bum']) result = foo_schema(data, context) assert result == dict(flibble=True) with py.test.raises(V.Invalid) as e: data = dict(flibble='flansit') context = dict(database=['foo', 'bar', 'bum']) result = foo_schema(data, context) errors = e.value.unpack_errors() assert errors == { None: "Problems were found in the submitted data.", 'flibble': "it's not in the database"} validator = V.nested(flim=is_in_database('doodah')) data = dict(flim='foo') context = dict(database=['foo', 'bar', 'bum']) result = validator(data, context) assert result == dict(flim=True)