def test_parse_time(): fmt='%m %d %Y' msg="potted shrimp" v=V.parse_time(fmt, msg) ts=v('10 03 2007')[:3] assert ts==(2007, 10, 3) assert_invalid(lambda: v('tough nuggie'), msg)
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_ip(): v = V.ip('donkey') i = '192.168.1.243' assert v(i) == i assert_invalid( lambda: v("this is not an ip"), {None: 'donkey'})
def test_translate(): v = V.translate(dict(y=True, f=False), 'dong') assert v.__name__ == "translate" assert v('y') == True assert_invalid( lambda: v('pod'), {None: 'dong'})
def test_confirm_type(): v = V.confirm_type((int, float), 'not a number') assert v.__name__ == "confirm_type" assert v(45) == 45 assert_invalid( lambda: v('45'), {None: 'not a number'})
def test_equal(): v = V.equal('egg', msg="not equal") assert v.__name__ == "equal" assert v('egg') == 'egg' assert_invalid( lambda: v('bob'), {None: 'not equal'})
def test_not_equal(): v = V.not_equal('egg', msg='equal') assert v.__name__ == "not_equal" assert v('plop') == 'plop' assert_invalid( lambda: v('egg'), {None: 'equal'})
def test_fields_equal(): values = ("pong", "pong") v = V.fields_equal('hog') assert v.__name__ == "fields_equal" assert values == v(values) values = ('tim', 'worthy') assert_invalid(lambda: v(values), {None: 'hog'}) s = V.Schema({ 'foo': V.to_integer(), ('foo', 'bar'): V.fields_equal(u"foo and bar don't match") }) d = dict(foo='1', bar=1) expected = dict(foo=1, bar=1) assert s(d) == expected # Check field=None s = V.Schema({ 'foo': V.to_integer(), ('foo', 'bar'): V.fields_equal(u"foo and bar don't match", field=None) }) d = dict(foo='1', bar=2) with py.test.raises(V.Invalid) as e: s(d) errors = e.value.unpack_errors() assert errors == {None: u"foo and bar don't match"}
def test_regex(): v = V.regex('shrubbery\d{3}$', 'regex') assert v.__name__ == "regex" assert v('shrubbery222') == 'shrubbery222' assert_invalid( lambda: v('buy a shrubbery333, ok?'), {None: 'regex'})
def test_empty(): v = V.empty(msg="scorch me") assert v.__name__ == "empty" assert v('') == '' assert v(None) == None assert_invalid( lambda: v("bob"), {None: 'scorch me'})
def test_msg(): messages=dict( integer="hey, I said use a number") with msg(messages): assert messages == getMessages() assert_invalid(lambda: V.integer()('lump'), messages['integer']) assert getMessages() != messages assert_invalid(lambda: V.integer()('lump'), "not an integer")
def test_is_list(): msg = "list" v = V.is_list(msg=msg) assert v.__name__ == "is_list" assert v([40]) == [40] assert_invalid( lambda: v(40), {None: msg})
def test_is_scalar(): msg = 'sc' v = V.is_scalar(msg=msg) assert v.__name__ == "is_scalar" assert v(40) == 40 assert_invalid( lambda: v([40]), {None: msg})
def test_email(): v=V.email() e="*****@*****.**" assert v(e)==e e='*****@*****.**' v=V.email(True, 'snog') assert v(e)==e assert_invalid(lambda: v('*****@*****.**'), 'snog')
def test_credit_card_1(): cc='4000000000998' v=V.credit_card(msg="aha") assert v(cc)==cc assert_invalid(lambda: v('pain chocolat'), dict(cc_number=['aha'])) assert_invalid(lambda: v(str(int(cc)-1)), dict(cc_number=['aha'])) v=V.credit_card(require_type=True, msg='aha') assert v((cc, 'Visa'))==(cc, 'Visa')
def test_clamp_length(): msg='You are a pear' v=V.clamp_length(min=3, msg=msg) assert v('500')=='500' assert_invalid(lambda: v('eh'), msg) v=V.clamp_length(max=10, msg=dict(minlen='haha', maxlen='kong')) assert v('40')=='40' assert_invalid(lambda: v('I told you that Ronald would eat it when you were in the bathroom'), 'kong')
def test_excursion(): x='*****@*****.**' v=V.excursion(lambda x: x.split('@')[0], V.belongs(['gadzooks', 'willy'], msg='pancreatic')) assert x==v(x) assert_invalid(lambda: v('hieratic impulses'), 'pancreatic')
def test_fields_match(): d=dict(foo=3, goo=3, poo=56) v=V.fields_match('foo', 'goo') assert d==v(d) v=V.fields_match('foo', 'poo', 'oink') assert_invalid(lambda: v(d), 'oink')
def test_credit_card_1(): cc = '4000000000998' v = V.credit_card(msg="aha") assert v(cc) == cc assert_invalid(lambda: v('pain chocolat'), dict(cc_number=['aha'])) assert_invalid(lambda: v(str(int(cc) - 1)), dict(cc_number=['aha'])) v = V.credit_card(require_type=True, msg='aha') assert v((cc, 'Visa')) == (cc, 'Visa')
def test_not_belongs(): msg = "belittle my humbug" v = V.not_belongs(range(5), msg=msg) assert v.__name__ == "not_belongs" assert v('pinko') == 'pinko' assert_invalid( lambda: v(4), {None: msg})
def test_parse_time(): fmt = '%m %d %Y' msg = "potted shrimp" v = V.parse_time(fmt, msg) assert v.__name__ == "parse_time" ts = v('10 03 2007')[:3] assert ts == (2007, 10, 3) assert_invalid(lambda: v('tough nuggie'), {None: msg})
def test_belongs(): msg = "rinse me a robot" v = V.belongs('pinko widget frog lump'.split(), msg=msg) assert v.__name__ == "belongs" assert v('pinko') == 'pinko' assert_invalid( lambda: v('snot'), {None: msg})
def test_to_integer(): msg = "please enter an integer" v = V.to_integer(msg=msg) assert v.__name__ == "to_integer" assert v('40') == 40 assert_invalid( lambda: v('whack him until he screams'), {None: msg})
def test_email(): py.test.importorskip("pydns") v = V.email() e = "*****@*****.**" assert v(e) == e e = '*****@*****.**' v = V.email(True, 'snog') assert v(e) == e assert_invalid(lambda: v('*****@*****.**'), 'snog')
def test_clamp(): msg='You are a pear' v=V.clamp(min=30, msg=msg) assert v(50)==50 assert_invalid(lambda: v(20), msg) v=V.clamp(max=100, msg=dict(min='haha', max='kong')) assert v(40)==40 assert_invalid(lambda: v(120), 'kong')
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_either(): msg = "please enter an integer" v = V.either(V.empty(), V.to_integer(msg=msg)) assert v.__name__ == "either" assert v('') == '' assert v('40') == 40 assert_invalid(lambda: v('bonk'), {None: msg}) v = V.either(is_in_context(), V.to_integer(msg=msg)) assert v('40') == 40 assert v('foo', context=['foo']) == 'foo'
def test_fields_match(): d = dict(foo=3, goo=3, poo=56) v = V.fields_match('foo', 'goo') assert v.__name__ == "fields_match" assert d == v(d) v = V.fields_match('foo', 'poo', 'oink') assert_invalid(lambda: v(d), {None: 'oink'}) # Check field=None v = V.fields_match('foo', 'bar', msg='flibble', field=None) with py.test.raises(V.Invalid) as e: v(dict(foo=1, bar=2)) assert e.value.unpack_errors() == {None: 'flibble'}
def test_clamp_length(): msg = 'You are a pear' v = V.clamp_length(min=3, msg=msg) assert v.__name__ == "clamp_length" assert v('500') == '500' assert_invalid(lambda: v('eh'), {None: msg}) v = V.clamp_length(max=10, msg=dict(minlen='haha', maxlen='kong')) assert v('40') == '40' assert_invalid( lambda: v('I told you that Ronald would eat it when you were in the bathroom'), {None: 'kong'})
def test_all_of_2(): messages = dict(to_integer='not an integer', belongs='invalid choice', min='too small', max='too big') v = V.all_of(V.default(40), V.strip, V.to_integer(msg=messages), V.belongs(range(4, 100, 4), messages), V.clamp(min=20, max=50, msg=messages)) assert v(None) == 40 assert v('40') == 40 assert v('44 ') == 44 assert_invalid( lambda: v(' prick '), {None: messages['to_integer']}) assert_invalid( lambda: v(' 41 '), {None: messages['belongs']}) assert_invalid( lambda: v('96'), {None: messages['max']}) assert_invalid( lambda: v('8'), {None: messages['min']})
def test_excursion(): x = '*****@*****.**' v = V.excursion(lambda x, context: x.split('@')[0], V.belongs(['gadzooks', 'willy'], msg='pancreatic')) assert v.__name__ == "excursion" assert x == v(x) assert_invalid(lambda: v('hieratic impulses'), {None: 'pancreatic'}) v = V.excursion(lambda x, context: x.add('foo')) data = set(['bar']) result = v(data) assert result == set(['bar']) assert data == set(['bar', 'foo'])
def test_either(): msg = "please enter an integer" v = V.either(V.empty(), V.to_integer(msg=msg)) assert v.__name__ == "either" assert v('') == '' assert v('40') == 40 assert_invalid( lambda: v('bonk'), {None: msg}) v = V.either( is_in_context(), V.to_integer(msg=msg)) assert v('40') == 40 assert v('foo', context=['foo']) == 'foo'
def test_fields_match(): d = dict(foo=3, goo=3, poo=56) v = V.fields_match('foo', 'goo') assert v.__name__ == "fields_match" assert d == v(d) v = V.fields_match('foo', 'poo', 'oink') assert_invalid( lambda: v(d), {None: 'oink'}) # Check field=None v = V.fields_match('foo', 'bar', msg='flibble', field=None) with py.test.raises(V.Invalid) as e: v(dict(foo=1, bar=2)) assert e.value.unpack_errors() == {None: 'flibble'}
def test_excursion(): x = '*****@*****.**' v = V.excursion( lambda x, context: x.split('@')[0], V.belongs(['gadzooks', 'willy'], msg='pancreatic')) assert v.__name__ == "excursion" assert x == v(x) assert_invalid( lambda: v('hieratic impulses'), {None: 'pancreatic'}) v = V.excursion(lambda x, context: x.add('foo')) data = set(['bar']) result = v(data) assert result == set(['bar']) assert data == set(['bar', 'foo'])
def test_clamp_length(): msg = 'You are a pear' v = V.clamp_length(min=3, msg=msg) assert v.__name__ == "clamp_length" assert v('500') == '500' assert_invalid( lambda: v('eh'), {None: msg}) v = V.clamp_length(max=10, msg=dict(minlen='haha', maxlen='kong')) assert v('40') == '40' assert_invalid( lambda: v('I told you that Ronald would eat it when you were in the bathroom'), {None: 'kong'}) msg = "Enter less than %(max)s. You entered %(length)s." v = V.clamp_length(max=30, msg=msg) with py.test.raises(V.Invalid) as e: v("*"*50) errors = e.value.unpack_errors() expected = {None: "Enter less than 30. You entered 50."} assert errors == expected
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'})
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'})
def test_all_of_2(): messages = dict(to_integer='not an integer', belongs='invalid choice', min='too small', max='too big') v = V.all_of(V.default(40), V.strip, V.to_integer(msg=messages), V.belongs(range(4, 100, 4), messages), V.clamp(min=20, max=50, msg=messages)) assert v(None) == 40 assert v('40') == 40 assert v('44 ') == 44 assert_invalid(lambda: v(' prick '), {None: messages['to_integer']}) assert_invalid(lambda: v(' 41 '), {None: messages['belongs']}) assert_invalid(lambda: v('96'), {None: messages['max']}) assert_invalid(lambda: v('8'), {None: messages['min']})
def test_compose(): messages=dict(integer='please enter an integer', belongs='invalid choice', min='too small', max='too big') v=V.compose(V.default(40), V.strip, V.integer(msg=messages), V.belongs(range(4, 100, 4), messages), V.clamp(min=20, max=50, msg=messages)) assert v(None)==40 assert v('40')==40 assert v('44 ')==44 assert_invalid(lambda: v(' prick '), messages['integer']) assert_invalid(lambda: v(' 41 '), messages['belongs']) assert_invalid(lambda: v('96'), messages['max']) assert_invalid(lambda: v('8'), messages['min'])
def test_fields_equal(): values = ("pong", "pong") v = V.fields_equal('hog') assert v.__name__ == "fields_equal" assert values == v(values) values = ('tim', 'worthy') assert_invalid( lambda: v(values), {None: 'hog'}) s = V.Schema({ 'foo': V.to_integer(), ('foo', 'bar'): V.fields_equal(u"foo and bar don't match")}) d = dict(foo='1', bar=1) expected = dict(foo=1, bar=1) assert s(d) == expected # Check field=None s = V.Schema({ 'foo': V.to_integer(), ('foo', 'bar'): V.fields_equal(u"foo and bar don't match", field=None)}) d = dict(foo='1', bar=2) with py.test.raises(V.Invalid) as e: s(d) errors = e.value.unpack_errors() assert errors == {None: u"foo and bar don't match"}
def test_clamp(): msg = 'You are a pear' v = V.clamp(min=30, msg=msg) assert v.__name__ == "clamp" assert v(50) == 50 assert_invalid(lambda: v(20), {None: msg}) v = V.clamp(max=100, msg=dict(min='haha', max='kong')) assert v(40) == 40 assert_invalid(lambda: v(120), {None: 'kong'}) v = V.clamp(max=100, msg=dict(min='haha')) assert_invalid(lambda: v(120), {None: 'value above maximum'})
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'})
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'})
def test_to_integer(): msg = "please enter an integer" v = V.to_integer(msg=msg) assert v.__name__ == "to_integer" assert v('40') == 40 assert_invalid(lambda: v('whack him until he screams'), {None: msg})
def test_regex(): v = V.regex('shrubbery\d{3}$', 'regex') assert v.__name__ == "regex" assert v('shrubbery222') == 'shrubbery222' assert_invalid(lambda: v('buy a shrubbery333, ok?'), {None: 'regex'})
def test_not_belongs(): msg = "belittle my humbug" v = V.not_belongs(range(5), msg=msg) assert v.__name__ == "not_belongs" assert v('pinko') == 'pinko' assert_invalid(lambda: v(4), {None: msg})
def test_belongs(): msg = "rinse me a robot" v = V.belongs('pinko widget frog lump'.split(), msg=msg) assert v.__name__ == "belongs" assert v('pinko') == 'pinko' assert_invalid(lambda: v('snot'), {None: msg})
def test_is_list(): msg = "list" v = V.is_list(msg=msg) assert v.__name__ == "is_list" assert v([40]) == [40] assert_invalid(lambda: v(40), {None: msg})
def test_not_equal(): v = V.not_equal('egg', msg='equal') assert v.__name__ == "not_equal" assert v('plop') == 'plop' assert_invalid(lambda: v('egg'), {None: 'equal'})
def test_equal(): v = V.equal('egg', msg="not equal") assert v.__name__ == "equal" assert v('egg') == 'egg' assert_invalid(lambda: v('bob'), {None: 'not equal'})
def test_empty(): v = V.empty(msg="scorch me") assert v.__name__ == "empty" assert v('') == '' assert v(None) == None assert_invalid(lambda: v("bob"), {None: 'scorch me'})
def test_confirm_type(): v = V.confirm_type((int, float), 'not a number') assert v.__name__ == "confirm_type" assert v(45) == 45 assert_invalid(lambda: v('45'), {None: 'not a number'})
def test_is_scalar(): msg = 'sc' v = V.is_scalar(msg=msg) assert v.__name__ == "is_scalar" assert v(40) == 40 assert_invalid(lambda: v([40]), {None: msg})
def test_translate(): v = V.translate(dict(y=True, f=False), 'dong') assert v.__name__ == "translate" assert v('y') == True assert_invalid(lambda: v('pod'), {None: 'dong'})
def test_ip(): v = V.ip('donkey') i = '192.168.1.243' assert v(i) == i assert_invalid(lambda: v("this is not an ip"), 'donkey')