def test_marshal_list_of_lists(self): fields = OrderedDict([ ('foo', flask_restful.fields.Raw), ('fee', flask_restful.fields.List(flask_restful.fields.List( flask_restful.fields.String))) ]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', [['fye'], ['fum']])]) output = flask_restful.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'bar'), ('fee', [['fye'], ['fum']])]) self.assertEquals(output, expected)
def test_marshal_list_of_nesteds(self): fields = { 'foo': flask_restful.fields.Raw, 'fee': flask_restful.fields.List(flask_restful.fields.Nested({ 'fye': flask_restful.fields.String })) } output = flask_restful.marshal([{'foo': 'bar', 'bat': 'baz', 'fee': {'fye': 'fum'}}], fields) self.assertEquals(output, [OrderedDict({'fee': [OrderedDict({'fye': 'fum'})], 'foo': 'bar'})])
def test_marshal_list_of_nesteds(self): fields = OrderedDict([ ('foo', flask_restful.fields.Raw), ('fee', flask_restful.fields.List(flask_restful.fields.Nested({ 'fye': flask_restful.fields.String }))) ]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', {'fye': 'fum'})]) output = flask_restful.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'bar'), ('fee', [OrderedDict([('fye', 'fum')])])]) self.assertEquals(output, expected)
def test_marshal_nested_dict(self): fields = OrderedDict([ ('foo', flask_restful.fields.Raw), ('bar', OrderedDict([ ('a', flask_restful.fields.Raw), ('b', flask_restful.fields.Raw), ])), ]) marshal_fields = OrderedDict([('foo', 'foo-val'), ('bar', 'bar-val'), ('bat', 'bat-val'), ('a', 1), ('b', 2), ('c', 3)]) output = flask_restful.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'foo-val'), ('bar', OrderedDict([('a', 1), ('b', 2)]))]) self.assertEquals(output, expected)
def test_marshal_nested_with_null(self): fields = OrderedDict([('foo', flask_restful.fields.Raw), ('fee', flask_restful.fields.Nested(OrderedDict([ ('fye', flask_restful.fields.String), ('blah', flask_restful.fields.String) ]), allow_null=True))]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', None)]) output = flask_restful.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'bar'), ('fee', None)]) self.assertEquals(output, expected)
def test_allow_null_presents_data(self): fields = OrderedDict([ ('foo', flask_restful.fields.Raw), ('fee', flask_restful.fields.Nested( OrderedDict([ ('fye', flask_restful.fields.String), ('blah', flask_restful.fields.String) ]), allow_null=True)) ]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', {'blah': 'cool'})]) output = flask_restful.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'bar'), ('fee', OrderedDict([('fye', None), ('blah', 'cool')]))]) self.assertEquals(output, expected)
def test_marshal_decorator(self): fields = OrderedDict([('foo', flask_restful.fields.Raw)]) @flask_restful.marshal_with(fields) def try_me(): return OrderedDict([('foo', 'bar'), ('bat', 'baz')]) self.assertEquals(try_me(), {'foo': 'bar'})
def test_marshal_nested_dict(self): fields = { 'foo': flask_restful.fields.Raw, 'bar': { 'a': flask_restful.fields.Raw, 'b': flask_restful.fields.Raw, }, } output = flask_restful.marshal( { 'foo': 'foo-val', 'bar': 'bar-val', 'bat': 'bat-val', 'a': 1, 'b': 2, 'c': 3 }, fields) self.assertEquals( output, OrderedDict({ 'foo': 'foo-val', 'bar': { 'a': 1, 'b': 2 } }))
def test_marshal_decorator_tuple(self): fields = OrderedDict([('foo', flask_restful.fields.Raw)]) @flask_restful.marshal_with(fields) def try_me(): return OrderedDict([('foo', 'bar'), ('bat', 'baz')]), 200, {'X-test': 123} self.assertEquals(try_me(), ({'foo': 'bar'}, 200, {'X-test': 123}))
def setUp(self): self.app = Flask(__name__) self.api = flask_restful.Api(self.app) self.app_401 = Flask(__name__) self.api_401 = flask_restful.Api(self.app, serve_challenge_on_401=True) self.fields = OrderedDict([('foo', flask_restful.fields.Raw)])
def test_marshal_decorator_with_envelope(self): fields = OrderedDict([('foo', flask_restful.fields.Raw)]) @flask_restful.marshal_with(fields, envelope='hey') def try_me(): return OrderedDict([('foo', 'bar'), ('bat', 'baz')]) self.assertEqual(try_me(), {'hey': {'foo': 'bar'}})
def test_marshal_nested_with_non_null_and_no_display_empty(self): fields = OrderedDict([('foo', flask_restful.fields.Raw), ('fee', flask_restful.fields.Nested(OrderedDict([ ('fye', flask_restful.fields.String), ('blah', flask_restful.fields.String) ]), allow_null=False, display_null=False)) ]) marshal_fields = [ OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', None)]) ] output = flask_restful.marshal(marshal_fields, fields, display_null=False) expected = [OrderedDict([('foo', 'bar'), ('fee', OrderedDict([]))])] self.assertEquals(output, expected)
def test_marshal_list(self): fields = { 'foo': flask_restful.fields.Raw, 'fee': flask_restful.fields.List(flask_restful.fields.String) } output = flask_restful.marshal([{'foo': 'bar', 'bat': 'baz', 'fee': ['fye', 'fum']}], fields) self.assertEquals(output, [OrderedDict({'fee': (['fye', 'fum']), 'foo': 'bar'})])
def test_marshal_nested_property(self): class TestObject(object): @property def fee(self): return {'blah': 'cool'} fields = OrderedDict([ ('foo', flask_restful.fields.Raw), ('fee', flask_restful.fields.Nested( OrderedDict([ ('fye', flask_restful.fields.String), ('blah', flask_restful.fields.String) ]), allow_null=True)) ]) obj = TestObject() obj.foo = 'bar' obj.bat = 'baz' output = flask_restful.marshal([obj], fields) expected = [OrderedDict([('foo', 'bar'), ('fee', OrderedDict([('fye', None), ('blah', 'cool')]))])] self.assertEquals(output, expected)
def test_marshal_nested(self): fields = { 'foo': flask_restful.fields.Raw, 'fee': flask_restful.fields.Nested({ 'fye': flask_restful.fields.String, }) } output = flask_restful.marshal([{ 'foo': 'bar', 'bat': 'baz', 'fee': { 'fye': 'fum' } }], fields) self.assertEquals(output, [{ 'fee': OrderedDict({'fye': 'fum'}), 'foo': 'bar' }])
def dispatch_request(self, *args, **kwargs): # Taken from flask #noinspection PyUnresolvedReferences meth = getattr(self, request.method.lower(), None) if meth is None and request.method == 'HEAD': meth = getattr(self, 'get', None) assert meth is not None, 'Unimplemented method %r' % request.method for decorator in self.method_decorators: meth = decorator(meth) resp = meth(*args, **kwargs) # adds default success key if isinstance(resp, dict): if resp.get('success', None) is None: resp['success'] = True elif resp.get('success', True) is False: resp = self.__serialize_errors(resp) elif isinstance(resp, tuple): if resp[0].get('success', True) is False: list(resp)[0] = self.__serialize_errors(resp[0]) resp = tuple(resp) if isinstance(resp, ResponseBase): # There may be a better way to test return resp representations = self.representations or OrderedDict() #noinspection PyUnresolvedReferences mediatype = request.accept_mimetypes.best_match(representations, default=None) if mediatype in representations: data, code, headers = unpack(resp) resp = representations[mediatype](data, code, headers) resp.headers['Content-Type'] = mediatype return resp return resp
def test_marshal(self): fields = OrderedDict([('foo', flask_restful.fields.Raw)]) marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')]) output = flask_restful.marshal(marshal_dict, fields) self.assertEquals(output, {'foo': 'bar'})
def test_marshal_field(self): marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')]) output = flask_restful.marshal(marshal_fields, self.fields) self.assertEquals(output, {'foo': 'bar'})
def test_marshal_tuple(self): fields = OrderedDict({'foo': flask_restful.fields.Raw}) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')]) output = flask_restful.marshal((marshal_fields, ), fields) self.assertEquals(output, [{'foo': 'bar'}])
def try_me(): return OrderedDict([('foo', 'bar'), ('bat', 'baz')]), 200, { 'X-test': 123 }
def try_me(): return OrderedDict([('foo', 'bar'), ('bat', 'baz')])
def test_marshal_with_envelope(self): fields = OrderedDict([('foo', flask_restful.fields.Raw)]) marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')]) output = flask_restful.marshal(marshal_dict, fields, envelope='hey') self.assertEquals(output, {'hey': {'foo': 'bar'}})
def test_marshal_tuple_with_envelope(self): fields = OrderedDict({'foo': flask_restful.fields.Raw}) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')]) output = flask_restful.marshal((marshal_fields,), fields, envelope='hey') self.assertEquals(output, {'hey': [{'foo': 'bar'}]})