def check_regex(pattern, match, result): field = RegexField(pattern=pattern) if result: equal(match, field.validate(match)) else: with value_error_ctx: field.validate(match)
def test_totime_notz(): dt = datetime(2013, 5, 9, 0, 55, 24) with assert_raises(ValueError) as exc: jsonformat.totime(dt) exc = exc.exception expect = ('Value must contain timezone information: 2013-05-09T00:55:24') equal(exc.message, expect)
def test_words(): f0 = WordField() s = '' equal(s, f0.validate(s)) s = 'goodstr' equal(s, f0.validate(s)) s = 'goodstr_with_underscore' equal(s, f0.validate(s)) with value_error_ctx: f0.validate('should not contain space') with value_error_ctx: f0.validate('miscsymbols*(*^&') f1 = WordField(length=(4, 8)) s = 'four' equal(s, f1.validate(s)) s = 'fourfour' equal(s, f1.validate(s)) with value_error_ctx: f1.validate('s') with value_error_ctx: f1.validate('longggggg') f2 = WordField(null=False) with value_error_ctx: f2.validate('')
def check_uuid(v, iseq): f = UUIDStringField() if iseq: equal(v, f.validate(v)) else: with value_error_ctx: f.validate(v)
def test_drop_negative(): value = '-1' with assert_raises(ValueError) as exc: jsonformat.drop(value) exc = exc.exception expect = 'Value must be a positive integer: -1' equal(exc.message, expect)
def check_int(kwargs, v, iseq, convert=False): f = IntegerField(**kwargs) if iseq: equal(int(v), f.validate(v, convert=convert)) else: print(v, f.min, f.max) assert_raises(ValueError, f.validate, v, convert=convert)
def test_utc_from_iso_local(fake_local): fake_local.return_value = tz.tzoffset(None, -10800) dt = jsonformat.utc_from_iso( '2011-11-16T18:36:06.795119', assume_local=True, ) expect = datetime(2011, 11, 16, 21, 36, 06, 795119, tz.tzutc()) equal(dt, expect)
def test_totime_earlier(): dt = datetime(1999, 5, 9, 0, 55, 24, tzinfo=tz.tzutc()) with assert_raises(ValueError) as exc: jsonformat.totime(dt) exc = exc.exception expect = ('Value cannot be earlier than 2000-01-01T00:00:00+00:00: ' '1999-05-09T00:55:24+00:00') equal(exc.message, expect)
def test_utc_from_notz(): with assert_raises(ValueError) as exc: jsonformat.utc_from_iso('2013-05-09T00:55:24') exc = exc.exception expect = ( 'Value must contain timezone information: 2013-05-09T00:55:24' ) equal(exc.message, expect)
def test_totime_notz(): dt = datetime(2013, 5, 9, 0, 55, 24) with assert_raises(ValueError) as exc: jsonformat.totime(dt) exc = exc.exception expect = ( 'Value must contain timezone information: 2013-05-09T00:55:24' ) equal(exc.message, expect)
def test_drop_sys_min(): min_value = -sys.maxint - 2 value = str(min_value) with assert_raises(ValueError) as exc: jsonformat.drop(value) exc = exc.exception expect = 'Value must be a positive integer: {min_value}'.format( min_value=min_value, ) equal(exc.message, expect)
def test_check_options_empty(): @jsonrpc.check_options() def _test(*args, **kwargs): raise AssertionError('Function should not have been called') with assert_raises(ValueError) as exc: _test() exc = exc.exception expect = 'At least one option set is needed: ledger' equal(str(exc), expect)
def test_check_options_invalid(): @jsonrpc.check_options('foo', 'ledger') def _test(*args, **kwargs): raise AssertionError('Function should not have been called') with assert_raises(ValueError) as exc: _test() exc = exc.exception expect = 'Invalid option set: foo' equal(str(exc), expect)
def test_totime_earlier(): dt = datetime(1999, 5, 9, 0, 55, 24, tzinfo=tz.tzutc()) with assert_raises(ValueError) as exc: jsonformat.totime(dt) exc = exc.exception expect = ( 'Value cannot be earlier than 2000-01-01T00:00:00+00:00: ' '1999-05-09T00:55:24+00:00' ) equal(exc.message, expect)
def test_check_options_missing(): @jsonrpc.check_options('ledger') def _test(*args, **kwargs): raise AssertionError('Function should not have been called') with assert_raises(ValueError) as exc: _test('foo', 'bar', baz='qux') exc = exc.exception expect = ( 'At least one option is needed: ledger_hash, ledger_index' ) equal(str(exc), expect)
def test_type_list_convert(): list_field = ListField(item_field=IntegerField(min=1, max=9), choices=[1, 2, 3]) equal([1, 2, 3], list_field.validate(['1', '2', '3'], convert=True)) with value_error_ctx: list_field.validate(['0', '1', '2'], convert=True) with value_error_ctx: list_field.validate(['1', '2', '3', '4'], convert=True) with value_error_ctx: list_field.validate(['a', '2', '3'], convert=True)
def test_simple_list(): list_field = ListField(choices=['a', 'b', 'c']) l = ['a'] equal(l, list_field.validate(l)) l = ['a', 'b', 'c'] equal(l, list_field.validate(l)) with value_error_ctx: list_field.validate(['b', 'c', 'd']) with value_error_ctx: list_field.validate(['z', 'a', 'b'])
def test_check_result_no_result(): response = dict([ ('status', 'success'), ('foo', 'bar'), ]) @jsonrpc.check_result def _test(res): raise AssertionError('Function should not have been called') with assert_raises(KeyError) as exc: _test(response) exc = exc.exception expect = 'The response did not return a "result" field' equal(exc.message, expect)
def test_check_options_many(): @jsonrpc.check_options('ledger') def _test(*args, **kwargs): raise AssertionError('Function should not have been called') with assert_raises(ValueError) as exc: _test( 'foo', 'bar', baz='qux', ledger_hash='foo', ledger_index='bar', ) exc = exc.exception expect = ( 'Only one option can be specified: ledger_hash, ledger_index' ) equal(str(exc), expect)
def test_check_options_multiple(): jsonrpc.unique_options = dict([ ('foo', ['foo_hash', 'foo_index']), ('bar', ['bar_hash', 'bar_index']), ]) @jsonrpc.check_options('foo', 'bar') def _test(*args, **kwargs): raise AssertionError('Function should not have been called') with assert_raises(ValueError) as exc: _test( foo_hash='foo hash', bar_hash='bar hash', bar_index='bar index', ) exc = exc.exception expect = 'Only one option can be specified: bar_hash, bar_index' equal(str(exc), expect)
def test_check_result_error(): response = dict([ ('status', 'error'), ('error', 'foo_name'), ('error_code', 'foo_code'), ('error_message', 'foo_message'), ]) response = dict([ ('result', response), ]) @jsonrpc.check_result def _test(res): raise AssertionError('Function should not have been called') with assert_raises(jsonrpc.RippleRPCError) as exc: _test(response) exc = exc.exception expect = 'foo_name, foo_code: foo_message' equal(str(exc), expect)
def test_boollean(): with assert_raises(TypeError): f = BooleanField(default='a') f = BooleanField() v = f.validate(True) assert v is True v = f.validate(False) assert v is False # string to bool for i in ['True', 'true', '1']: v = f.validate(i, convert=True) equal(v, True) for i in ['False', 'false', '0']: v = f.validate(i, convert=True) equal(v, False) with assert_raises(ValueError): v = f.validate('wtf', convert=True)
def test_utc_from_empty(): with assert_raises(ValueError) as exc: jsonformat.utc_from_iso('') exc = exc.exception expect = 'Value cannot be empty string' equal(exc.message, expect)
def test_client(): c = Client() resp = c.get('/') equal(str_(resp.content), 'GET')
def test_utc_from_iso_utc(): dt = jsonformat.utc_from_iso('2011-10-12T19:55:58.345128+0000') expect = datetime(2011, 10, 12, 19, 55, 58, 345128, tz.tzutc()) equal(dt, expect)
def test_fromtime_simple(): res = jsonformat.fromtime(421376124) expect = datetime(2013, 5, 9, 0, 55, 24, tzinfo=tz.tzutc()) equal(res, expect)
def test_drop_sys_max(): max_value = sys.maxint + 1 value = str(max_value) res = jsonformat.drop(value) equal(res, max_value)
def test_totime_simple(): dt = datetime(2013, 5, 9, 0, 55, 24, tzinfo=tz.tzutc()) res = jsonformat.totime(dt) expect = 421376124 equal(res, expect)
def test_drop_max(): value = '100000000000' res = jsonformat.drop(value) equal(res, 100000000000L)
def _test(res): expect = dict([ ('foo', 'bar') ]) equal(res, expect)
def test_expand_function_name(): test_fnc_path = parallelize.expand_function_name(mapper_dummy) true_fnc_path = 'tests.test_parallelize.mapper_dummy' print 'test', test_fnc_path print 'true', true_fnc_path equal(test_fnc_path, true_fnc_path)
def test_utc_from_iso_simple(): dt = jsonformat.utc_from_iso('2011-11-16T18:36:06.795119-08:00') expect = datetime(2011, 11, 17, 2, 36, 06, 795119, tz.tzutc()) equal(dt, expect)
def test_utc_from_notz(): with assert_raises(ValueError) as exc: jsonformat.utc_from_iso('2013-05-09T00:55:24') exc = exc.exception expect = ('Value must contain timezone information: 2013-05-09T00:55:24') equal(exc.message, expect)