def findmeld_test1(): """ XML namespace unit test - find_meld - one element """ x = xml.Renderer() node = x.parse_xmlstring(xml_test1_in) child = node.findmeld('child') assert child is not None
def global_test7(): """ XML namespace unit test - meld3 - test option write_xmlstring method with option pipeline=True """ x = xml.Renderer() root = x.parse_xmlstring(xml_test2_in) root.findmeld('title').text = 'My document' root.findmeld('form1').set('action', './handler') data = ( {'name': 'Girls', 'description': 'Pretty'}, {'name': 'Boys', 'description': 'Ugly'}, ) children = [] for elt in data: children.append(x.tr([x.td(elt['name']), x.td(elt['description'])], {'class': 'bar'})) root.findmeld('tr').replace(children) root.write_xmlstring(xml_declaration=True, pretty_print=True, pipeline=False).strip() assert root.findmeld('tr') is None assert root.findmeld('content_well') is None
def parse_xmlstring_test4(): """ XML namespace unit test - parse_xmlstring - parse xml tree with fragment flag """ x = xml.Renderer() roots = x.parse_xmlstring(xml_fragments_1, fragment=True, no_leading_text=True) assert roots[0].write_xmlstring() == "<fragment1/>text" assert roots[0].tail == "text" assert roots[1].write_xmlstring() == "<fragment2/>"
def root_test2(): """ XML namespace unit test - root - two elements """ x = xml.Renderer() x << x.node() x << x.node() assert isinstance(x.root, ListType)
def test_global8(): """ XML namespace unit test - create xml """ x = xml.Renderer() x.namespaces = {'meld': 'http://www.plope.com/software/meld3'} data = ( {'name': 'Girls', 'description': 'Pretty'}, {'name': 'Boys', 'description': 'Ugly'}, ) with x.html: with x.head: with x.meta: x << {'content': 'text/html; charset=ISO-8859-1', 'http-equiv': 'content-type'} with x.title.meld_id('title'): x << 'My document' with x.body: with x.div: pass x << x.comment(' empty tag ') with x.div: with x.form.meld_id('form1'): x << {'action': './handler'} with x.table: with x.tbody: with x.tr: x << x.th('Name') << x.th('Description') for elt in data: with x.tr.meld_id('tr'): x << x.td(elt['name']).meld_id('td') << x.td(elt['description']).meld_id('td') with x.input: x << {'type': 'submit', 'name': 'next', 'value': ' Next '} assert [elt.text for elt in x.root.xpath('.//td')] == ['Girls', 'Pretty', 'Boys', 'Ugly'] assert x.root[0][1].text == 'My document' assert x.root.xpath('.//form')[0].attrib['action'] == './handler'
def test_3(): """ database - test with elixir & framework render method """ app = App1() filePath = os.path.join(os.path.dirname(__file__), 'helloworld.csv') reader = csv.reader(open(filePath, 'r')) res = {} for row in reader: res[row[0].decode('utf-8')] = row[1].decode('utf-8') Language(id=row[0].decode('utf-8'), label=row[1].decode('utf-8')) session.flush() for language in Language.query.all(): app.add_language(language) h = xml.Renderer() xmlToTest = component.Component(app).render(h).write_xmlstring( xml_declaration=True, pretty_print=True).strip() f = open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml')) xmlToCompare = f.read() f.close() assert xmlToTest == xmlToCompare
def test1(): """ XSL namespace unit test - simple xsl transformation 1 """ x = xsl.Renderer() x.namespaces = {'xsl': 'http://www.w3.org/1999/XSL/Transform'} x.default_namespace = 'xsl' styleSheet = x.stylesheet( x.output(encoding="utf-8", method="html"), x.template(x.copy(x.apply_templates(select="@*|node()")), match="@*|node()"), x.template(x.element(name="head"), x.element(x.element(x.apply_templates(select="@*|node()"), name="body"), name="html"), match="helloWorlds"), x.template(x.element(x.value_of(select="@language"), ' : ', x.value_of(select="."), name="h1"), match="helloWorld") ) x = xml.Renderer() f = open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml')) root = x.parse_xmlstring(f.read()) f.close() r = root.getroottree().xslt(styleSheet) f = open(os.path.join(os.path.dirname(__file__), 'helloworld.html')) xmlToCompare = f.read() f.close() assert r.__str__().strip() == xmlToCompare
def init_portal_xml_published_ideas(self, url, comp, http_method, request): root = comp.render(xml.Renderer(), model='xml_published_ideas') # Now we need to fix the avatar urls because we can't pass the request object to the component # render function (or did I missed something ?) for node in root.xpath('//avatar'): node.text = request.application_url + node.text raise HTTPOk(content_type='application/xml', body=root.write_xmlstring( xml_declaration=True, encoding='UTF-8'))
def test_parse_xmlstring3(): """ XML namespace unit test - parse_xmlstring - parse fragment xml with fragment flag """ x = xml.Renderer() roots = x.parse_xmlstring(xml_fragments_1, fragment=True) assert roots[0] == 'leading_text' assert roots[1].write_xmlstring() == "<fragment1/>text" assert roots[1].tail == "text" assert roots[2].write_xmlstring() == "<fragment2/>"
def repeat_test1(): """ XML namespace unit test - repeat - Repeat with 2 simple text, use childname argument""" x = xml.Renderer() node = x.parse_xmlstring(xml_test1_in) iterator = node.repeat(['test1', 'test2'], childname='child') for child, value in iterator: child.append_text(value) assert [child.text for child in node.getchildren()] == ['test1', 'test2']
def parse_xml_test1(): """ XML namespace unit test - parse_xmlstring - good encoding """ try: x = xml.Renderer() x.parse_xml(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml')) except UnicodeDecodeError: assert False else: assert True
def test_repeat2(): """ XML namespace unit test - repeat - Repeat with 2 simple text, don't use childname argument""" x = xml.Renderer() node = x.parse_xmlstring(xml_test1_in) child = node.findmeld('child') iterator = child.repeat(['test1', 'test2']) for child, value in iterator: child.append_text(value) assert [child.text for child in node.getchildren()] == ['test1', 'test2']
def test_parse_xmlstring1(): """ XML namespace unit test - parse_xmlstring - good encoding """ try: x = xml.Renderer() f = open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml')) x.parse_xmlstring(f.read()) f.close() except UnicodeDecodeError: assert False else: assert True
def parse_xmlstring_test2(): """ XML namespace unit test - parse_xmlstring - bad encoding """ try: x = xml.Renderer() f = open(os.path.join(os.path.dirname(__file__), 'iso-8859.xml')) x.parse_xml(f, encoding='utf-8') f.close() except ET.XMLSyntaxError: assert True else: assert False
def add_child_test7(): """ XML namespace unit test - add_child - add int In: - <node/> Out: - <node>42</node> """ x = xml.Renderer() node = x.node(42) assert node.text == '42' assert node.write_xmlstring() == '<node>42</node>'
def add_child_test3(): """ XML namespace unit test - add_child - add dictionnary In: - <node/> Out: - <node test="test"/> """ x = xml.Renderer() node = x.node({'test': 'test'}) assert node.attrib == {'test': 'test'} assert node.write_xmlstring() == '<node test="test"/>'
def repeat_test3(): """ XML namespace unit test - repeat - findmeld in repeat loop """ h = xml.Renderer() xhtml_tree_2 = '<div xmlns:meld="http://www.plope.com/software/meld3"><ul><li meld:id="entry"><span meld:id="count">count</span></li></ul></div>' root = h.parse_xmlstring(xhtml_tree_2, fragment=True)[0] for (elem, count) in root.repeat(range(2), 'entry'): elem.findmeld('count').fill(count) h << root assert h.root.write_xmlstring() == '<div xmlns:meld="http://www.plope.com/software/meld3"><ul><li meld:id="entry"><span meld:id="count">0</span></li><li meld:id="entry"><span meld:id="count">1</span></li></ul></div>'
def add_child_test4(): """ XML namespace unit test - add_child - add attribute with keyword In: - <node/> Out: - <node test="test"/> """ x = xml.Renderer() node = x.node(test='test') assert node.attrib == {'test': 'test'} assert node.write_xmlstring() == '<node test="test"/>'
def add_child_test8(): """ XML namespace unit test - add_child - add float In: - <node/> Out: - <node>0.1</node> """ x = xml.Renderer() node = x.node(0.1) assert node.text == '0.1' assert node.write_xmlstring() == '<node>0.1</node>'
def add_child_test10(): """ XML namespace unit test - add_child - add attribute with python keyword In: - <node/> Out: - <node class="test"/> """ x = xml.Renderer() node = x.node(class_='test') assert node.attrib == {'class': 'test'} assert node.write_xmlstring() == '<node class="test"/>'
def replace_test6(): """ XML namespace unit test - replace - replace root node In: - <node/> Out: - <node/> """ x = xml.Renderer() node = x.node() node.replace('test') assert node.write_xmlstring() == '<node/>'
def add_child_test1(): """ XML namespace unit test - add_child - add text In: - <node/> Out: - <node>test</node> """ x = xml.Renderer() node = x.node('test') assert node.text == 'test' assert node.write_xmlstring() == '<node>test</node>'
def test_add_child9(): """ XML namespace unit test - add_child - add dictionnary with python keyword In: - <node/> Out: - <node class="test"/> """ x = xml.Renderer() node = x.node({'class_': 'test'}) assert node.attrib == {'class': 'test'} assert node.write_xmlstring() == '<node class="test"/>'
def global_test3(): """ XML namespace unit test - test parse_xmlstring method """ x = xml.Renderer() f = open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml')) root = x.parse_xmlstring(f.read()) f.close() xmlToTest = root.write_xmlstring(xml_declaration=True, pretty_print=True).strip() f = open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml')) xmlToCompare = f.read() f.close() assert xmlToTest == xmlToCompare
def add_child_test2(): """ XML namespace unit test - add_child - add node In: - <node/> Out: - <node><child/></node> """ x = xml.Renderer() child = x.child() node = x.node(child) assert node.getchildren()[0] == child assert node.write_xmlstring() == '<node><child/></node>'
def append_text_test3(): """ XML namespace unit test - append_text - Append text to node with node child In: - <node><child/></node> Out: - <node><child/>test</node> """ x = xml.Renderer() node = x.node(x.child()) node.append_text('test') assert node.getchildren()[0].tail == 'test' assert node.write_xmlstring() == '<node><child/>test</node>'
def append_text_test1(): """ XML namespace unit test - append_text Append text to an empty node In: - <node/> Out: - <node>test</node> """ x = xml.Renderer() node = x.node() node.append_text('test') assert node.text == 'test' assert node.write_xmlstring() == '<node>test</node>'
def append_text_test2(): """ XML namespace unit test - append_text - Append text to node with text child In: - <node>test1</node> Out: - <node>test1test2</node> """ x = xml.Renderer() node = x.node('test1') node.append_text('test2') assert node.text == 'test1test2' assert node.write_xmlstring() == '<node>test1test2</node>'
def append_text_test4(): """ XML namespace unit test - append_text - Append text to node with text & node children In: - <node>test1<child/></node> Out: - <node>test1<child/>test2</node> """ x = xml.Renderer() node = x.node(['test1', x.child()]) node.append_text('test2') assert node.text == 'test1' assert node.getchildren()[0].tail == 'test2' assert node.write_xmlstring() == '<node>test1<child/>test2</node>'
def global_test2(): """ XML namespace unit test - create xml by functionnal way """ x = xml.Renderer() filePath = os.path.join(os.path.dirname(__file__), 'helloworld.csv') reader = csv.reader(open(filePath, 'r')) root = x.helloWorlds([x.helloWorld(row[1].decode('utf-8'), {'language':row[0].decode('utf-8')}) for row in reader]) xmlToTest = root.write_xmlstring(xml_declaration=True, pretty_print=True).strip() f = open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml')) xmlToCompare = f.read() f.close() assert xmlToTest == xmlToCompare