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'
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'
def test_not_empty(): msg = "hammer my xylophone" v = V.not_empty(msg=msg) assert v.__name__ == "not_empty" assert v("frog") == 'frog' assert_invalid(lambda: v(''), {None: msg}) assert_invalid(lambda: v(None), {None: msg})
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_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
def test_not_empty(): msg = "hammer my xylophone" v = V.not_empty(msg=msg) assert v.__name__ == "not_empty" assert v("frog") == 'frog' assert_invalid( lambda: v(''), {None: msg}) assert_invalid( lambda: v(None), {None: msg})
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
def test_credit_card_3(): cc='4000000000998' invalid_cc=str(int(cc)-1) validators=dict(cc_card=(V.strip, V.not_empty("Please enter a credit card number")), cc_type=V.belongs(('Visa', 'Discover'), msg="belongs")) v=V.credit_card(require_type=True, cc_field='cc_card', cc_type_field='cc_type') validators[('cc_card', 'cc_type')]=v s=V.Schema(validators) data=dict(cc_card=invalid_cc, cc_type='') try: s(data) except V.Invalid, e: errors=e.unpack_errors() assert set(errors)==set(('cc_card', 'cc_type', None))
def test_credit_card_3(): cc = '4000000000998' invalid_cc = str(int(cc) - 1) validators = dict( cc_card=(V.strip, V.not_empty("Please enter a credit card number")), cc_type=V.belongs(('Visa', 'Discover'), msg="belongs")) v = V.credit_card(require_type=True, cc_field='cc_card', cc_type_field='cc_type') validators[('cc_card', 'cc_type')] = v s = V.Schema(validators) data = dict(cc_card=invalid_cc, cc_type='') try: s(data) except V.Invalid, e: errors = e.unpack_errors() assert set(errors) == set(('cc_card', 'cc_type', None))
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_not_empty(): msg="hammer my xylophone" v=V.not_empty(msg=msg) assert v("frog")=='frog' assert_invalid(lambda: v(''), msg) assert_invalid(lambda: v(None), msg)