def test_before_first_root(self): class CM(ComplexModel): i = Integer s = String class CCM(ComplexModel): c = CM i = Integer s = String class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [CCM(c=ccm.c,i=ccm.i, s=ccm.s)] * 2 cb_called = [False] def _cb(ctx, cls, inst, parent, name, **kwargs): assert not cb_called[0] cb_called[0] = True app = Application([SomeService], 'tns', in_protocol=HttpRpc(hier_delim='_'), out_protocol=HtmlMicroFormat( doctype=None, before_first_root=_cb)) server = WsgiApplication(app) call_wsgi_app_kwargs(server, ccm_c_s='abc', ccm_c_i=123, ccm_i=456, ccm_s='def') assert cb_called[0]
def test_before_first_root(self): class CM(ComplexModel): i = Integer s = String class CCM(ComplexModel): c = CM i = Integer s = String class SomeService(Service): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [CCM(c=ccm.c,i=ccm.i, s=ccm.s)] * 2 cb_called = [False] def _cb(ctx, cls, inst, parent, name, **kwargs): assert not cb_called[0] cb_called[0] = True app = Application([SomeService], 'tns', in_protocol=HttpRpc(hier_delim='_'), out_protocol=HtmlMicroFormat( doctype=None, before_first_root=_cb)) server = WsgiApplication(app) call_wsgi_app_kwargs(server, ccm_c_s='abc', ccm_c_i=123, ccm_i=456, ccm_s='def') assert cb_called[0]
def test_complex_array(self): class CM(ComplexModel): i = Integer s = String class CCM(ComplexModel): c = CM i = Integer s = String class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [CCM(c=ccm.c, i=ccm.i, s=ccm.s)] * 2 app = Application([SomeService], 'tns', in_protocol=HttpRpc(hier_delim='_'), out_protocol=HtmlMicroFormat()) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_c_s='abc', ccm_c_i=123, ccm_i=456, ccm_s='def') # # Here's what this is supposed to return: # # <div class="some_callResponse"><div class="some_callResult"> # <div class="CCM"> # <div class="i">456</div> # <div class="c"> # <div class="i">123</div> # <div class="s">abc</div> # </div> # <div class="s">def</div> # </div> # <div class="CCM"> # <div class="i">456</div> # <div class="c"> # <div class="i">123</div> # <div class="s">abc</div> # </div> # <div class="s">def</div> # </div> # </div></div> # print(out_string) elt = html.fromstring(''.join(out_string)) show(elt, "TestHtmlMicroFormat.test_complex_array") resp = elt.find_class('some_callResponse') assert len(resp) == 1 res = resp[0].find_class('some_callResult') assert len(res) == 1 assert len(res[0].find_class("CCM")) == 2
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) resp = elt.find_class('some_callResponse') assert len(resp) == 1 for i in range(len(elt)): row = elt[i] if i == 0: # check for field names in table header cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="c_i"]') assert len(cell) == 1 assert cell[0].text == 'c_i' cell = row.findall('th[@class="c_s"]') assert len(cell) == 1 assert cell[0].text == 'c_s' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' else: # check for field values in table body cell = row.findall('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.findall('td[@class="c_i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.findall('td[@class="c_s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.findall('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_complex_array(self): class CM(ComplexModel): i = Integer s = String class CCM(ComplexModel): c = CM i = Integer s = String class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [CCM(c=ccm.c,i=ccm.i, s=ccm.s)] * 2 app = Application([SomeService], 'tns', in_protocol=HttpRpc(hier_delim='_'), out_protocol=HtmlMicroFormat()) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_c_s='abc', ccm_c_i=123, ccm_i=456, ccm_s='def') # # Here's what this is supposed to return: # # <div class="some_callResponse"><div class="some_callResult"> # <div class="CCM"> # <div class="i">456</div> # <div class="c"> # <div class="i">123</div> # <div class="s">abc</div> # </div> # <div class="s">def</div> # </div> # <div class="CCM"> # <div class="i">456</div> # <div class="c"> # <div class="i">123</div> # <div class="s">abc</div> # </div> # <div class="s">def</div> # </div> # </div></div> # print(out_string) elt = html.fromstring(out_string) show(elt, "TestHtmlMicroFormat.test_complex_array") resp = elt.find_class('some_callResponse') assert len(resp) == 1 res = resp[0].find_class('some_callResult') assert len(res) == 1 assert len(res[0].find_class("CCM")) == 2
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs( server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) from lxml import etree elt = etree.fromstring(out_string) print(etree.tostring(elt, pretty_print=True)) elt = html.fromstring(out_string) resp = elt.find_class('some_callResponse') assert len(resp) == 1 row, = elt[0] # thead cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' for row in elt[1]: # tbody cell = row.xpath('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.xpath('td[@class="c"]//td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.xpath('td[@class="c"]//td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.xpath('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_complex(self): class SomeService(ServiceBase): @srpc(CCM, _returns=CCM) def some_call(ccm): return ccm app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class', fields_as='rows')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_c_s='abc', ccm_c_i='123', ccm_i='456', ccm_s='def') elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) # Here's what this is supposed to return """ <table class="some_callResponse"> <tr> <th class="i">i</th> <td class="i">456</td> </tr> <tr> <th class="c_i">c_i</th> <td class="c_i">123</td> </tr> <tr> <th class="c_s">c_s</th> <td class="c_s">abc</td> </tr> <tr> <th class="s">s</th> <td class="s">def</td> </tr> </table> """ resp = elt.find_class('some_callResponse') assert len(resp) == 1 assert elt.xpath('//th[@class="i"]/text()')[0] == 'i' assert elt.xpath('//td[@class="i"]/text()')[0] == '456' assert elt.xpath('//th[@class="c_i"]/text()')[0] == 'c_i' assert elt.xpath('//td[@class="c_i"]/text()')[0] == '123' assert elt.xpath('//th[@class="c_s"]/text()')[0] == 'c_s' assert elt.xpath('//td[@class="c_s"]/text()')[0] == 'abc' assert elt.xpath('//th[@class="s"]/text()')[0] == 's' assert elt.xpath('//td[@class="s"]/text()')[0] == 'def'
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application( [SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_type_name_attr=None)) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs( server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) elt = etree.fromstring(out_string) show(elt, 'TestHtmlColumnTable.test_complex_array') elt = html.fromstring(out_string) row, = elt[0] # thead cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' for row in elt[1]: # tbody cell = row.xpath('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.xpath('td[@class="c"]//td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.xpath('td[@class="c"]//td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.xpath('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) from lxml import etree elt = etree.fromstring(out_string) print(etree.tostring(elt, pretty_print=True)) elt = html.fromstring(out_string) resp = elt.find_class('some_callResponse') assert len(resp) == 1 row, = elt[0] # thead cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' for row in elt[1]: # tbody cell = row.xpath('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.xpath('td[@class="c"]//td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.xpath('td[@class="c"]//td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.xpath('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_type_name_attr=None)) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) elt = etree.fromstring(out_string) show(elt, 'TestHtmlColumnTable.test_complex_array') elt = html.fromstring(out_string) row, = elt[0] # thead cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' for row in elt[1]: # tbody cell = row.xpath('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.xpath('td[@class="c"]//td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.xpath('td[@class="c"]//td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.xpath('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application( [SomeService], "tns", in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr="class") ) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_i="456", ccm_s="def", ccm_c_i="123", ccm_c_s="abc") elt = etree.fromstring(out_string) show(elt, "TestHtmlColumnTable.test_complex_array") elt = html.fromstring(out_string) row, = elt[0] # thead cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == "i" cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == "s" for row in elt[1]: # tbody cell = row.xpath('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == "456" cell = row.xpath('td[@class="c"]//td[@class="i"]') assert len(cell) == 1 assert cell[0].text == "123" cell = row.xpath('td[@class="c"]//td[@class="s"]') assert len(cell) == 1 assert cell[0].text == "abc" cell = row.xpath('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == "def"
def test_column_href_string_with_substitution(self): _link = "http://arskom.com.tr/?spyne_test=%s" class C(ComplexModel): c = Unicode(pa={HtmlColumnTable: dict(href=_link)}) class SomeService(ServiceBase): @srpc(_returns=C) def some_call(): return C(c="hello") app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link % "hello"
def test_anyuri_string(self): _link = "http://arskom.com.tr/" class C(ComplexModel): c = AnyUri class SomeService(ServiceBase): @srpc(_returns=Array(C)) def some_call(): return [C(c=_link)] app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) show(elt, "TestHtmlColumnTable.test_anyuri_string") assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link
def test_anyuri_string(self): _link = "http://arskom.com.tr/" class C(ComplexModel): c = AnyUri class SomeService(ServiceBase): @srpc(_returns=C) def some_call(): return C(c=_link) app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class', fields_as='rows')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link
def test_anyuri_uri_value(self): _link = "http://arskom.com.tr/" _text = "Arskom" class C(ComplexModel): c = AnyUri class SomeService(ServiceBase): @srpc(_returns=Array(C)) def some_call(): return [C(c=AnyUri.Value(_link, text=_text))] app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].text == _text assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link
def test_anyuri_string(self): _link = "http://arskom.com.tr/" class C(ComplexModel): c = AnyUri class SomeService(ServiceBase): @srpc(_returns=Array(C)) def some_call(): return [C(c=_link)] app = Application( [SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlColumnTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) show(elt, "TestHtmlColumnTable.test_anyuri_string") assert elt.xpath('//td[@class="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link
def test_with_encoding(self): headers = {'CONTENT_TYPE':'text/plain; charset=utf8'} ret = call_wsgi_app_kwargs(self.app, 'echo_string', headers, s="string") assert ret == 'string'
def test_without_content_type(self): headers = None ret = call_wsgi_app_kwargs(self.app, 'echo_string', headers, s="string") assert ret == 'string'
def test_with_encoding(self): headers = {"CONTENT_TYPE": "text/plain; charset=utf8"} ret = call_wsgi_app_kwargs(self.app, "echo_string", headers, s="string") assert ret == b"string"
def test_row_subprot(self): from lxml.html.builder import E from spyne.protocol.html import HtmlBase from spyne.util.six.moves.urllib.parse import urlencode from spyne.protocol.html import HtmlMicroFormat class SearchProtocol(HtmlBase): def to_parent(self, ctx, cls, inst, parent, name, **kwargs): s = self.to_unicode(cls._type_info['query'], inst.query) q = urlencode({"q": s}) parent.write(E.a("Search %s" % inst.query, href="{}?{}".format(inst.uri, q))) def column_table_gen_header(self, ctx, cls, parent, name): parent.write(E.thead(E.th("Search", **{'class': 'search-link'}))) def column_table_before_row(self, ctx, cls, inst, parent, name,**_): ctxstack = getattr(ctx.protocol[self], 'array_subprot_ctxstack', []) tr_ctx = parent.element('tr') tr_ctx.__enter__() ctxstack.append(tr_ctx) td_ctx = parent.element('td', **{'class': "search-link"}) td_ctx.__enter__() ctxstack.append(td_ctx) ctx.protocol[self].array_subprot_ctxstack = ctxstack def column_table_after_row(self, ctx, cls, inst, parent, name, **kwargs): ctxstack = ctx.protocol[self].array_subprot_ctxstack for elt_ctx in reversed(ctxstack): elt_ctx.__exit__(None, None, None) del ctxstack[:] class Search(ComplexModel): query = Unicode uri = Unicode SearchTable = Array( Search.customize(prot=SearchProtocol()), prot=HtmlColumnTable(field_type_name_attr=None), ) class SomeService(ServiceBase): @srpc(_returns=SearchTable) def some_call(): return [ Search(query='Arskom', uri='https://www.google.com/search'), Search(query='Spyne', uri='https://www.bing.com/search'), ] app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlMicroFormat()) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="search-link"]/a/text()') == \ ['Search Arskom', 'Search Spyne'] assert elt.xpath('//td[@class="search-link"]/a/@href') == [ 'https://www.google.com/search?q=Arskom', 'https://www.bing.com/search?q=Spyne', ] assert elt.xpath('//th[@class="search-link"]/text()') == ["Search"]
def test_complex_array(self): v = [ CM(i=1, s='a'), CM(i=2, s='b'), CM(i=3, s='c'), CM(i=4, s='d'), ] class SomeService(ServiceBase): @srpc(_returns=Array(CM)) def some_call(): return v app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlRowTable()) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) show(html.fromstring(out_string), 'TestHtmlRowTable.test_complex_array') #FIXME: Needs a proper test with xpaths and all. assert out_string == \ '<div xmlns="http://www.w3.org/1999/xhtml">' \ '<table xmlns="http://www.w3.org/1999/xhtml" class="CM">' \ '<tbody>' \ '<tr>' \ '<th class="i">i</th>' \ '<td class="i">1</td>' \ '</tr>' \ '<tr>' \ '<th class="s">s</th>' \ '<td class="s">a</td>' \ '</tr>' \ '</tbody>' \ '</table>' \ '<table xmlns="http://www.w3.org/1999/xhtml" class="CM">' \ '<tbody>' \ '<tr>' \ '<th class="i">i</th>' \ '<td class="i">2</td>' \ '</tr>' \ '<tr>' \ '<th class="s">s</th>' \ '<td class="s">b</td>' \ '</tr>' \ '</tbody>' \ '</table>' \ '<table xmlns="http://www.w3.org/1999/xhtml" class="CM">' \ '<tbody>' \ '<tr>' \ '<th class="i">i</th>' \ '<td class="i">3</td>' \ '</tr>' \ '<tr>' \ '<th class="s">s</th>' \ '<td class="s">c</td>' \ '</tr>' \ '</tbody>' \ '</table>' \ '<table xmlns="http://www.w3.org/1999/xhtml" class="CM">' \ '<tbody>' \ '<tr>' \ '<th class="i">i</th>' \ '<td class="i">4</td>' \ '</tr>' \ '<tr>' \ '<th class="s">s</th>' \ '<td class="s">d</td>' \ '</tr>' \ '</tbody>' \ '</table>' \ '</div>'
def test_complex_array(self): v = [CM(i=1, s="a"), CM(i=2, s="b"), CM(i=3, s="c"), CM(i=4, s="d")] class SomeService(ServiceBase): @srpc(_returns=Array(CM)) def some_call(): return v app = Application([SomeService], "tns", in_protocol=HttpRpc(), out_protocol=HtmlRowTable()) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) show(html.fromstring(out_string), "TestHtmlRowTable.test_complex_array") # FIXME: Needs a proper test with xpaths and all. assert ( out_string == "<div>" '<table class="CM">' "<tbody>" "<tr>" '<th class="i">i</th>' '<td class="i">1</td>' "</tr>" "<tr>" '<th class="s">s</th>' '<td class="s">a</td>' "</tr>" "</tbody>" "</table>" '<table class="CM">' "<tbody>" "<tr>" '<th class="i">i</th>' '<td class="i">2</td>' "</tr>" "<tr>" '<th class="s">s</th>' '<td class="s">b</td>' "</tr>" "</tbody>" "</table>" '<table class="CM">' "<tbody>" "<tr>" '<th class="i">i</th>' '<td class="i">3</td>' "</tr>" "<tr>" '<th class="s">s</th>' '<td class="s">c</td>' "</tr>" "</tbody>" "</table>" '<table class="CM">' "<tbody>" "<tr>" '<th class="i">i</th>' '<td class="i">4</td>' "</tr>" "<tr>" '<th class="s">s</th>' '<td class="s">d</td>' "</tr>" "</tbody>" "</table>" "</div>" )
def test_complex(self): class SomeService(ServiceBase): @srpc(CCM, _returns=CCM) def some_call(ccm): return ccm app = Application( [SomeService], "tns", in_protocol=HttpRpc(hier_delim="_"), out_protocol=HtmlRowTable(field_name_attr="class"), ) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, "some_call", ccm_c_s="abc", ccm_c_i="123", ccm_i="456", ccm_s="def") elt = html.fromstring(out_string) show(elt, "TestHtmlRowTable.test_complex") # Here's what this is supposed to return """ <table class="CCM"> <tbody> <tr> <th class="i">i</th> <td class="i">456</td> </tr> <tr class="c"> <th class="c">c</th> <td class="c"> <table class="c"> <tbody> <tr> <th class="i">i</th> <td class="i">123</td> </tr> <tr> <th class="s">s</th> <td class="s">abc</td> </tr> </tbody> </table> </td> </tr> <tr> <th class="s">s</th> <td class="s">def</td> </tr> </tbody> </table> """ print(html.tostring(elt, pretty_print=True)) resp = elt.find_class("CCM") assert len(resp) == 1 assert elt.xpath('tbody/tr/th[@class="i"]/text()')[0] == "i" assert elt.xpath('tbody/tr/td[@class="i"]/text()')[0] == "456" assert elt.xpath('tbody/tr/td[@class="c"]//th[@class="i"]/text()')[0] == "i" assert elt.xpath('tbody/tr/td[@class="c"]//td[@class="i"]/text()')[0] == "123" assert elt.xpath('tbody/tr/td[@class="c"]//th[@class="s"]/text()')[0] == "s" assert elt.xpath('tbody/tr/td[@class="c"]//td[@class="s"]/text()')[0] == "abc" assert elt.xpath('tbody/tr/th[@class="s"]/text()')[0] == "s" assert elt.xpath('tbody/tr/td[@class="s"]/text()')[0] == "def"
def test_row_subprot(self): from lxml.html.builder import E from spyne.protocol.html import HtmlBase from spyne.util.six.moves.urllib.parse import urlencode from spyne.protocol.html import HtmlMicroFormat class SearchProtocol(HtmlBase): def to_parent(self, ctx, cls, inst, parent, name, **kwargs): s = self.to_unicode(cls._type_info['query'], inst.query) q = urlencode({"q": s}) parent.write( E.a("Search %s" % inst.query, href="{}?{}".format(inst.uri, q))) def column_table_gen_header(self, ctx, cls, parent, name): parent.write( E.thead(E.th("Search", **{'class': 'search-link'}))) def column_table_before_row(self, ctx, cls, inst, parent, name, **_): ctxstack = getattr(ctx.protocol[self], 'array_subprot_ctxstack', []) tr_ctx = parent.element('tr') tr_ctx.__enter__() ctxstack.append(tr_ctx) td_ctx = parent.element('td', **{'class': "search-link"}) td_ctx.__enter__() ctxstack.append(td_ctx) ctx.protocol[self].array_subprot_ctxstack = ctxstack def column_table_after_row(self, ctx, cls, inst, parent, name, **kwargs): ctxstack = ctx.protocol[self].array_subprot_ctxstack for elt_ctx in reversed(ctxstack): elt_ctx.__exit__(None, None, None) del ctxstack[:] class Search(ComplexModel): query = Unicode uri = Unicode SearchTable = Array( Search.customize(prot=SearchProtocol()), prot=HtmlColumnTable(field_type_name_attr=None), ) class SomeService(ServiceBase): @srpc(_returns=SearchTable) def some_call(): return [ Search(query='Arskom', uri='https://www.google.com/search'), Search(query='Spyne', uri='https://www.bing.com/search'), ] app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlMicroFormat()) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) assert elt.xpath('//td[@class="search-link"]/a/text()') == \ ['Search Arskom', 'Search Spyne'] assert elt.xpath('//td[@class="search-link"]/a/@href') == [ 'https://www.google.com/search?q=Arskom', 'https://www.bing.com/search?q=Spyne', ] assert elt.xpath('//th[@class="search-link"]/text()') == ["Search"]