def test_many(self): class SomeComplexModel(ComplexModel): s = Unicode i = Integer v = [ SomeComplexModel(s='a', i=1), SomeComplexModel(s='b', i=2), SomeComplexModel(s='c', i=3), SomeComplexModel(s='d', i=4), SomeComplexModel(s='e', i=5), ] class SomeService(ServiceBase): @rpc(_returns=Array(SomeComplexModel)) def get(ctx): return v desc = SomeService.public_methods['get'] ctx = FakeContext(out_object=[v], descriptor=desc) ostr = ctx.out_stream = StringIO() XmlDocument(Application([SomeService], __name__)) \ .serialize(ctx, XmlDocument.RESPONSE) elt = etree.fromstring(ostr.getvalue()) print(etree.tostring(elt, pretty_print=True)) assert elt.xpath('x:getResult/x:SomeComplexModel/x:i/text()', namespaces={'x': __name__}) == ['1', '2', '3', '4', '5'] assert elt.xpath('x:getResult/x:SomeComplexModel/x:s/text()', namespaces={'x': __name__}) == ['a', 'b', 'c', 'd', 'e']
def test_one(self): class SomeComplexModel(ComplexModel): s = Unicode i = Integer v = SomeComplexModel(s='a', i=1), class SomeService(ServiceBase): @rpc(_returns=SomeComplexModel) def get(ctx): return v desc = SomeService.public_methods['get'] ctx = FakeContext(out_object=v, descriptor=desc) ostr = ctx.out_stream = BytesIO() XmlDocument(Application([SomeService], __name__)) \ .serialize(ctx, XmlDocument.RESPONSE) elt = etree.fromstring(ostr.getvalue()) print(etree.tostring(elt, pretty_print=True)) assert elt.xpath('x:getResult/x:i/text()', namespaces={'x': __name__}) == ['1'] assert elt.xpath('x:getResult/x:s/text()', namespaces={'x': __name__}) == ['a']
def get_object_as_yaml(o, cls, ignore_wrappers=False, complex_as=dict, encoding='utf8', polymorphic=False): prot = YamlDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) ctx = FakeContext(out_document=[prot._object_to_doc(cls,o)]) prot.create_out_string(ctx, encoding) return ''.join(ctx.out_string)
def yaml_loads(s, cls, protocol=YamlDocument, ignore_wrappers=False, **kwargs): if s is None: return None if s == '': return None prot = protocol(ignore_wrappers=ignore_wrappers, **kwargs) ctx = FakeContext(in_string=[s]) prot.create_in_document(ctx) return prot._doc_to_object(cls, ctx.in_document, validator=prot.validator)
def json_loads(s, cls, protocol=JsonDocument, **kwargs): if s is None: return None if s == '': return None prot = protocol(**kwargs) ctx = FakeContext(in_string=[s]) prot.create_in_document(ctx) return prot._doc_to_object(cls, ctx.in_document, validator=prot.validator)
def get_object_as_json(o, cls, ignore_wrappers=True, complex_as=list, encoding='utf8'): prot = JsonDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as) ctx = FakeContext(out_document=[prot._object_to_doc(cls, o)]) prot.create_out_string(ctx, encoding) return ''.join(ctx.out_string)
def get_object_as_json(o, cls=None, ignore_wrappers=True, complex_as=list, encoding='utf8', polymorphic=False): if cls is None: cls = o.__class__ prot = JsonDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) ctx = FakeContext(out_document=[prot._object_to_doc(cls, o)]) prot.create_out_string(ctx, encoding) return b''.join(ctx.out_string)
def get_object_as_msgpack(o, cls=None, ignore_wrappers=False, complex_as=dict, encoding='utf8', polymorphic=False): if cls is None: cls = o.__class__ prot = MessagePackDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) ctx = FakeContext(out_document=[prot._object_to_doc(cls,o)]) prot.create_out_string(ctx, encoding) return ''.join(ctx.out_string)
def yaml_loads(s, cls, protocol=YamlDocument, ignore_wrappers=False, **kwargs): prot = protocol(ignore_wrappers=ignore_wrappers, **kwargs) ctx = FakeContext(in_string=[s]) prot.create_in_document(ctx) return prot._doc_to_object(cls, ctx.in_document)
def json_loads(s, cls, protocol=JsonDocument, encoding=None, **kwargs): prot = protocol(**kwargs) ctx = FakeContext(in_string=[s]) prot.create_in_document(ctx) return prot._doc_to_object(cls, ctx.in_document)