def test_get_args(self): @functions.signature(str, str, int, atypes.ArrayType(int), types.listtype, body=NestedObj) def myfunc(self, first, second, third, foo, nested): pass funcdef = functions.FunctionDefinition.get(myfunc) params = multidict.MultiDict(foo='0,1, 2, three', second='2') mimetype = 'application/json' body = b'{"o": {"id": 1234, "name": "an object"}}' fromargs = ['one'] fromkwargs = {'third': '1'} newargs, newkwargs = args.get_args(funcdef, fromargs, fromkwargs, params, body, mimetype) self.assertEqual([], newargs) n = newkwargs.pop('nested') self.assertEqual( { 'first': 'one', 'foo': ['0', '1', '2', 'three'], 'second': 2, 'third': [1] }, newkwargs) self.assertEqual(1234, n.o.id) self.assertEqual('an object', n.o.name) # check_arguments missing mandatory argument 'second' params = multidict.MultiDict(foo='0,1, 2, three', ) self.assertRaises(exception.MissingArgument, args.get_args, funcdef, fromargs, fromkwargs, params, body, mimetype)
def test_fromjson_array(self): atype = atypes.ArrayType(int) self.assertEqual([0, 1, 1234, None], args.fromjson_array(atype, [0, '1', '1_234', None])) self.assertRaises(ValueError, args.fromjson_array, atype, ['one', 'two', 'three']) self.assertRaises(ValueError, args.fromjson_array, atype, 'one')
def test_from_param(self): # datetime param expected = datetime.datetime(2015, 8, 13, 11, 38, 9, 496475) self.assertEqual( expected, args.from_param(datetime.datetime, '2015-08-13T11:38:09.496475')) self.assertIsNone(args.from_param(datetime.datetime, None)) # usertype param self.assertEqual(['0', '1', '2', 'three'], args.from_param(types.listtype, '0,1, 2, three')) # array param atype = atypes.ArrayType(int) self.assertEqual([0, 1, 1234, None], args.from_param(atype, [0, '1', '1_234', None])) self.assertIsNone(args.from_param(atype, None)) # string param self.assertEqual('foo', args.from_param(str, 'foo')) self.assertIsNone(args.from_param(str, None)) # string param with from_params hit_paths = set() params = multidict.MultiDict(foo='bar', ) self.assertEqual('bar', args.from_params(str, params, 'foo', hit_paths)) self.assertEqual({'foo'}, hit_paths)
def test_render_array(self): self.assertEqual( json.dumps(['one', 'two', 'three']), self.renderer.render('/', { 'result': ['one', 'two', 'three'], 'datatype': atypes.ArrayType(str) }) )
def test_array_from_params(self): hit_paths = set() datatype = atypes.ArrayType(str) params = multidict.MultiDict(foo='bar', one='two') self.assertEqual(['bar'], args.from_params(datatype, params, 'foo', hit_paths)) self.assertEqual({'foo'}, hit_paths) self.assertEqual(['two'], args.array_from_params(datatype, params, 'one', hit_paths)) self.assertEqual({'foo', 'one'}, hit_paths)
def test_args_from_args(self): fromargs = ['one', 2, [0, '1', '2_34']] fromkwargs = {'foo': '1, 2, 3'} @functions.signature(str, str, int, atypes.ArrayType(int), types.listtype) def myfunc(self, first, second, third, foo): pass funcdef = functions.FunctionDefinition.get(myfunc) newargs, newkwargs = args.args_from_args(funcdef, fromargs, fromkwargs) self.assertEqual(['one', 2, [0, 1, 234]], newargs) self.assertEqual({'foo': ['1', '2', '3']}, newkwargs)
def test_args_from_params(self): @functions.signature(str, str, int, atypes.ArrayType(int), types.listtype) def myfunc(self, first, second, third, foo): pass funcdef = functions.FunctionDefinition.get(myfunc) params = multidict.MultiDict(foo='0,1, 2, three', third='1', second='2') self.assertEqual(([], { 'foo': ['0', '1', '2', 'three'], 'second': 2, 'third': [1] }), args.args_from_params(funcdef, params)) # unexpected param params = multidict.MultiDict(bar='baz') self.assertRaises(exception.UnknownArgument, args.args_from_params, funcdef, params) # no params plus a body params = multidict.MultiDict(__body__='') self.assertEqual(([], {}), args.args_from_params(funcdef, params))
def test_array_sample(self): s = types.ArrayType(str).sample() assert isinstance(s, list) assert s assert s[0] == ''
def test_array_eq(self): ell = [types.ArrayType(str)] assert types.ArrayType(str) in ell
def test_validate_list_none(self): v = types.ArrayType(int) assert v.validate(None) is None
def test_fromjson(self): # parse None self.assertIsNone(args.fromjson(None, None)) # parse array atype = atypes.ArrayType(int) self.assertEqual([0, 1, 1234, None], args.fromjson(atype, [0, '1', '1_234', None])) # parse dict dtype = atypes.DictType(str, int) self.assertEqual({ 'zero': 0, 'one': 1, 'etc': 1234, 'none': None }, args.fromjson(dtype, { 'zero': 0, 'one': '1', 'etc': '1_234', 'none': None })) # parse bytes self.assertEqual(b'asdf', args.fromjson(bytes, b'asdf')) self.assertEqual(b'asdf', args.fromjson(bytes, 'asdf')) self.assertEqual(b'33', args.fromjson(bytes, 33)) self.assertEqual(b'3.14', args.fromjson(bytes, 3.14)) # parse str self.assertEqual('asdf', args.fromjson(str, b'asdf')) self.assertEqual('asdf', args.fromjson(str, 'asdf')) # parse int/float self.assertEqual(3, args.fromjson(int, '3')) self.assertEqual(3, args.fromjson(int, 3)) self.assertEqual(3.14, args.fromjson(float, 3.14)) # parse bool self.assertFalse(args.fromjson(bool, 'no')) self.assertTrue(args.fromjson(bool, 'yes')) # parse decimal self.assertEqual(decimal.Decimal(3.14), args.fromjson(decimal.Decimal, 3.14)) # parse datetime expected = datetime.datetime(2015, 8, 13, 11, 38, 9, 496475) self.assertEqual( expected, args.fromjson(datetime.datetime, '2015-08-13T11:38:09.496475')) # parse complex n = args.fromjson(NestedObj, {'o': {'id': 1234, 'name': 'an object'}}) self.assertIsInstance(n.o, Obj) self.assertEqual(1234, n.o.id) self.assertEqual('an object', n.o.name) self.assertEqual('foo', n.o.default_field) # parse usertype self.assertEqual(['0', '1', '2', 'three'], args.fromjson(types.listtype, '0,1, 2, three'))