def test_list_single(self): l = loc(query="foo=bar") @ultramini.annotate(foo=[str]) def somefunc(foo): assert foo == ["bar"] ultramini.applyWithTypeConversion(somefunc, l)
def test_float_converter(self): l = loc(query="pi=3.14") @ultramini.annotate(pi=float) def somefunc(pi): assert pi == 3.14 ultramini.applyWithTypeConversion(somefunc, l)
def test_quoted_query_converter(self): l = loc(query="channel=462280&ver=3&p=run") @ultramini.annotate(channel=int) def some_function(channel): assert channel == 462280 ultramini.applyWithTypeConversion(some_function, l)
def test_int_converter(self): l = loc(query="channel=462280") @ultramini.annotate(channel=int) def some_function(channel): assert channel == 462280 ultramini.applyWithTypeConversion(some_function, l)
def test_unicode_converter(self): l = loc(query=u'foo=\u00bfHabla%20espa\u00f1ol?'.encode('utf8')) @ultramini.annotate(foo=unicode) def somefunc(foo): assert foo == u'\u00bfHabla espa\u00f1ol?' ultramini.applyWithTypeConversion(somefunc, l)
def test_list_single(self): l = loc(query="foo=bar") @ultramini.annotate(foo=[str]) def somefunc(foo): assert foo == ['bar'] ultramini.applyWithTypeConversion(somefunc, l)
def test_str_converter(self): l = loc(query='foo=foo') @ultramini.annotate(foo=str) def somefunc(foo): assert foo == 'foo' ultramini.applyWithTypeConversion(somefunc, l)
def test_unicode_converter(self): l = loc(query=u"foo=\u00bfHabla%20espa\u00f1ol?".encode("utf8")) @ultramini.annotate(foo=unicode) def somefunc(foo): assert foo == u"\u00bfHabla espa\u00f1ol?" ultramini.applyWithTypeConversion(somefunc, l)
def test_str_converter(self): l = loc(query="foo=foo") @ultramini.annotate(foo=str) def somefunc(foo): assert foo == "foo" ultramini.applyWithTypeConversion(somefunc, l)
def test_dict_converter(self): l = loc(query="foo.bar=1&foo.baz=asdf") @ultramini.annotate(foo=dict(bar=int, baz=str)) def somefunc(foo): assert isinstance(foo, dict) assert foo['bar'] == 1 assert foo['baz'] == 'asdf' ultramini.applyWithTypeConversion(somefunc, l)
def test_dict_converter(self): l = loc(query="foo.bar=1&foo.baz=asdf") @ultramini.annotate(foo=dict(bar=int, baz=str)) def somefunc(foo): assert isinstance(foo, dict) assert foo["bar"] == 1 assert foo["baz"] == "asdf" ultramini.applyWithTypeConversion(somefunc, l)
def test_function_converter(self): l = loc(query="foo=something") def converter_function(request, value): return "else" @ultramini.annotate(foo=converter_function) def somefunc(foo): assert foo == "else" ultramini.applyWithTypeConversion(somefunc, l)
def test_tuple_converter(self): l = loc(query="foo=1&foo=hello&foo=3.1415") @ultramini.annotate(foo=(int, str, float)) def somefunc(foo): (fi, fs, ff) = foo assert fi == 1 assert fs == "hello" assert ff == 3.1415 ultramini.applyWithTypeConversion(somefunc, l)
def test_function_converter(self): l = loc(query='foo=something') def converter_function(request, value): return "else" @ultramini.annotate(foo=converter_function) def somefunc(foo): assert foo == 'else' ultramini.applyWithTypeConversion(somefunc, l)
def test_tuple_converter(self): l = loc(query="foo=1&foo=hello&foo=3.1415") @ultramini.annotate(foo=(int, str, float)) def somefunc(foo): (fi, fs, ff) = foo assert fi == 1 assert fs == 'hello' assert ff == 3.1415 ultramini.applyWithTypeConversion(somefunc, l)
def test_annotate_return_converter(self): def converter_function(something): assert something == 'wrong' return "right" @ultramini.annotate(converter_function) def somefunc(): return 'wrong' success, result = ultramini.applyWithTypeConversion(somefunc, loc()) # applyWithTypeConversion should return (hasConverted, result) # so we want (True, 'right') assert success, ('Failed to properly convert') assert result == 'right', (result, 'Incorrect return')
def test_annotate_return_converter(self): def converter_function(something): assert something == "wrong" return "right" @ultramini.annotate(converter_function) def somefunc(): return "wrong" success, result = ultramini.applyWithTypeConversion(somefunc, loc()) # applyWithTypeConversion should return (hasConverted, result) # so we want (True, 'right') assert success, "Failed to properly convert" assert result == "right", (result, "Incorrect return")
def test_custom_converter(self): old = ultramini.converters[str] try: ultramini.registerConverter(str, lambda r, s: ultramini.xml(s)) @ultramini.annotate(str) def handler(): return '<strong/>' l = loc() success, result = ultramini.applyWithTypeConversion(handler, l) result = ultramini.convert(l, result) assert success assert result == '<strong/>', (success, result) finally: ultramini.registerConverter(str, old)
def test_custom_converter(self): old = ultramini.converters[str] try: ultramini.registerConverter(str, lambda r, s: ultramini.xml(s)) @ultramini.annotate(str) def handler(): return "<strong/>" l = loc() success, result = ultramini.applyWithTypeConversion(handler, l) result = ultramini.convert(l, result) assert success assert result == "<strong/>", (success, result) finally: ultramini.registerConverter(str, old)
def test_chained_input_converters(self): def intify(resource, request, params): for k in params.iterkeys(): params[k] = int(params[k]) return params def subtract_five(resource, request, params): for k in params.iterkeys(): params[k] = params[k] - 5 return params @ultramini.annotate(unicode, (intify, subtract_five), foo=int) def somefunc(foo): return foo l = loc(query='foo=10') success, result = ultramini.applyWithTypeConversion(somefunc, l) assert success assert result == u'5', (success, result)
def test_input_and_output_converter(self): def caps_in(resource, request, parameters): for k, v in parameters.iteritems(): parameters[k] = unicode(v).upper() return parameters def lower_out(result): return unicode(result).lower() @ultramini.annotate(lower_out, caps_in, foo=str) def somefunc(foo): if not foo == 'WORLD': return 'World was supposed to be caps :(' return 'HeLLo %s' % foo l = loc(query='foo=world') success, result = ultramini.applyWithTypeConversion(somefunc, l) assert success assert result == 'hello world', (success, result)
def test_input_and_output_converter(self): def caps_in(resource, request, parameters): for k, v in parameters.iteritems(): parameters[k] = unicode(v).upper() return parameters def lower_out(result): return unicode(result).lower() @ultramini.annotate(lower_out, caps_in, foo=str) def somefunc(foo): if not foo == "WORLD": return "World was supposed to be caps :(" return "HeLLo %s" % foo l = loc(query="foo=world") success, result = ultramini.applyWithTypeConversion(somefunc, l) assert success assert result == "hello world", (success, result)
def test_chained_input_converters(self): def intify(resource, request, params): for k in params.iterkeys(): params[k] = int(params[k]) return params def subtract_five(resource, request, params): for k in params.iterkeys(): params[k] = params[k] - 5 return params @ultramini.annotate(unicode, (intify, subtract_five), foo=int) def somefunc(foo): return foo l = loc(query="foo=10") success, result = ultramini.applyWithTypeConversion(somefunc, l) assert success assert result == u"5", (success, result)