def test_prune_etree_function(self): root = ElementTree.XML('<A id="0"><B/><C/><D/></A>') self.assertFalse(prune_etree(root, lambda x: x.tag == 'C')) self.assertListEqual([e.tag for e in root.iter()], ['A', 'B', 'D']) self.assertEqual(root.attrib, {'id': '0'}) root = ElementTree.XML('<A id="1"><B/><C/><D/></A>') self.assertTrue(prune_etree(root, lambda x: x.tag != 'C')) self.assertListEqual([e.tag for e in root.iter()], ['A']) self.assertEqual(root.attrib, {'id': '1'}) class SelectorClass: tag = 'C' @classmethod def class_method(cls, elem): return elem.tag == cls.tag def method(self, elem): return elem.tag != self.tag selector = SelectorClass() root = ElementTree.XML('<A id="0"><B/><C/><D/></A>') self.assertFalse(prune_etree(root, selector.class_method)) self.assertListEqual([e.tag for e in root.iter()], ['A', 'B', 'D']) self.assertEqual(root.attrib, {'id': '0'}) root = ElementTree.XML('<A id="1"><B/><C/><D/></A>') self.assertTrue(prune_etree(root, selector.method)) self.assertListEqual([e.tag for e in root.iter()], ['A']) self.assertEqual(root.attrib, {'id': '1'})
def test_prune_etree(self): root = ElementTree.XML('<a><b1><c1/><c2/></b1><b2/><b3><c3/></b3></a>') prune_etree(root, selector=lambda x: x.tag == 'b1') self.assertListEqual([e.tag for e in root.iter()], ['a', 'b2', 'b3', 'c3']) root = ElementTree.XML('<a><b1><c1/><c2/></b1><b2/><b3><c3/></b3></a>') prune_etree(root, selector=lambda x: x.tag.startswith('c')) self.assertListEqual([e.tag for e in root.iter()], ['a', 'b1', 'b2', 'b3'])
def test_wsdl_message(self): wsdl_document = Wsdl11Document(WSDL_DOCUMENT_EXAMPLE) with self.assertRaises(WsdlParseError) as ctx: wsdl_document._parse_messages() self.assertIn("duplicated message 'tns:GetLastTradePriceInput'", str(ctx.exception)) elem = ElementTree.XML( '<message xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' name="GetLastTradePriceInput">\n' ' <part name="body" element="xsd1:unknown"/>\n' '</message>') with self.assertRaises(WsdlParseError) as ctx: WsdlMessage(elem, wsdl_document) self.assertIn('missing schema element', str(ctx.exception)) elem[0].attrib['element'] = 'xsd1:TradePriceRequest' wsdl_message = WsdlMessage(elem, wsdl_document) self.assertEqual(list(wsdl_message.parts), ['body']) elem.append(elem[0]) with self.assertRaises(WsdlParseError) as ctx: WsdlMessage(elem, wsdl_document) self.assertIn("duplicated part 'body'", str(ctx.exception)) elem[0].attrib['type'] = 'xsd1:TradePriceRequest' with self.assertRaises(WsdlParseError) as ctx: WsdlMessage(elem, wsdl_document) self.assertIn("ambiguous binding", str(ctx.exception)) del elem[0].attrib['name'] wsdl_message = WsdlMessage(elem, wsdl_document) self.assertEqual(wsdl_message.parts, {}) elem = ElementTree.XML( '<message xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:xs="http://www.w3.org/2001/XMLSchema"\n' ' name="GetLastTradePriceInput">\n' ' <part name="body" type="xs:string"/>\n' '</message>') with self.assertRaises(WsdlParseError) as ctx: WsdlMessage(elem, wsdl_document) self.assertIn('missing schema type', str(ctx.exception)) wsdl_document.namespaces['xs'] = "http://www.w3.org/2001/XMLSchema" wsdl_message = WsdlMessage(elem, wsdl_document) self.assertEqual(list(wsdl_message.parts), ['body']) del elem[0].attrib['type'] with self.assertRaises(WsdlParseError) as ctx: WsdlMessage(elem, wsdl_document) self.assertEqual("missing both 'type' and 'element' attributes", str(ctx.exception))
def test_iter_location_hints(self): elem = ElementTree.XML( """<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.com/xmlschema/ns-A import-case4a.xsd"/>""" ) self.assertListEqual( list(etree_iter_location_hints(elem)), [('http://example.com/xmlschema/ns-A', 'import-case4a.xsd')]) elem = ElementTree.XML( """<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema.xsd"/>""") self.assertListEqual(list(etree_iter_location_hints(elem)), [('', 'schema.xsd')])
def test_wsdl_soap_header_bindings(self): wsdl_document = Wsdl11Document(WSDL_DOCUMENT_EXAMPLE) elem = ElementTree.XML( '<header xmlns="http://schemas.xmlsoap.org/wsdl/soap/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' message="tns:SubscribeToQuotes"\n' ' part="subscribeheader" use="literal"/>') with self.assertRaises(WsdlParseError) as ctx: SoapHeader(elem, wsdl_document) self.assertIn('unknown message', str(ctx.exception)) elem.attrib['message'] = 'tns:GetLastTradePriceInput' with self.assertRaises(WsdlParseError) as ctx: SoapHeader(elem, wsdl_document) self.assertIn("missing message part 'subscribeheader'", str(ctx.exception)) elem.attrib['part'] = 'body' soap_header = SoapHeader(elem, wsdl_document) message_name = '{http://example.com/stockquote.wsdl}GetLastTradePriceInput' self.assertIs(wsdl_document.messages[message_name], soap_header.message) self.assertIs(wsdl_document.messages[message_name].parts['body'], soap_header.part) del elem.attrib['part'] soap_header = SoapHeader(elem, wsdl_document) self.assertIs(wsdl_document.messages[message_name], soap_header.message) self.assertIsNone(soap_header.part) elem = ElementTree.XML( '<header xmlns="http://schemas.xmlsoap.org/wsdl/soap/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' message="tns:GetLastTradePriceInput"\n' ' part="body" use="literal">\n' ' <headerfault message="tns:GetLastTradePriceInput"\n' ' part="body" use="literal"/>\n' '</header>') soap_header = SoapHeader(elem, wsdl_document) message = wsdl_document.messages[message_name] self.assertIs(message, soap_header.message) self.assertIs(message.parts['body'], soap_header.part) self.assertEqual(len(soap_header.faults), 1) self.assertIs(message, soap_header.faults[0].message) self.assertIs(message.parts['body'], soap_header.faults[0].part)
def test_element_string_serialization(self): self.assertRaises(TypeError, etree_tostring, '<element/>') elem = ElementTree.Element('element') self.assertEqual(etree_tostring(elem), '<element />') self.assertEqual(etree_tostring(elem, xml_declaration=True), '<element />') self.assertEqual(etree_tostring(elem, encoding='us-ascii'), b'<element />') self.assertEqual(etree_tostring(elem, encoding='us-ascii', indent=' '), b' <element />') self.assertEqual(etree_tostring(elem, encoding='us-ascii', xml_declaration=True), b'<?xml version="1.0" encoding="us-ascii"?>\n<element />') self.assertEqual(etree_tostring(elem, encoding='ascii'), b"<?xml version='1.0' encoding='ascii'?>\n<element />") self.assertEqual(etree_tostring(elem, encoding='ascii', xml_declaration=False), b'<element />') self.assertEqual(etree_tostring(elem, encoding='utf-8'), b'<element />') self.assertEqual(etree_tostring(elem, encoding='utf-8', xml_declaration=True), b'<?xml version="1.0" encoding="utf-8"?>\n<element />') self.assertEqual(etree_tostring(elem, encoding='iso-8859-1'), b"<?xml version='1.0' encoding='iso-8859-1'?>\n<element />") self.assertEqual(etree_tostring(elem, encoding='iso-8859-1', xml_declaration=False), b"<element />") self.assertEqual(etree_tostring(elem, method='html'), '<element></element>') self.assertEqual(etree_tostring(elem, method='text'), '') root = ElementTree.XML('<root>\n' ' text1\n' ' <elem>text2</elem>\n' '</root>') self.assertEqual(etree_tostring(root, method='text'), '\n text1\n text2')
def test_wsdl_and_soap_faults(self): example5_file_with_fault = casepath( 'features/wsdl/wsdl11_example5_with_fault.wsdl') wsdl_document = Wsdl11Document(example5_file_with_fault) port_type_name = '{http://example.com/stockquote.wsdl}StockQuotePortType' self.assertListEqual(list(wsdl_document.port_types), [port_type_name]) port_type = wsdl_document.port_types[port_type_name] operation = port_type.operations[('GetTradePrices', None, None)] message_name = '{http://example.com/stockquote.wsdl}FaultMessage' message = wsdl_document.messages[message_name] self.assertIs(operation.faults['fault'].message, message) elem = ElementTree.XML( '<binding xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"\n' ' name="StockQuoteBinding" type="tns:StockQuotePortType"\n>' ' <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>\n' ' <operation name="GetTradePrices">' ' <input/>' ' <output/>' ' <fault><soap:fault name="exception"/></fault>' ' </operation>' '</binding>') with self.assertRaises(WsdlParseError) as ctx: WsdlBinding(elem, wsdl_document) self.assertIn("missing fault 'exception'", str(ctx.exception))
def test_wsdl_port_type(self): wsdl_document = Wsdl11Document(WSDL_DOCUMENT_EXAMPLE) with self.assertRaises(WsdlParseError) as ctx: wsdl_document._parse_port_types() self.assertIn("duplicated port type 'tns:StockQuotePortType'", str(ctx.exception)) elem = ElementTree.XML( '<portType xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' name="StockQuotePortType">\n' ' <operation name="GetLastTradePrice">\n' ' <input message="tns:GetLastTradePriceInput"/>\n' ' <output message="tns:GetLastTradePriceOutput"/>\n' ' </operation>\n' '</portType>') wsdl_port_type = WsdlPortType(elem, wsdl_document) self.assertEqual(list(wsdl_port_type.operations), [('GetLastTradePrice', None, None)]) elem.append(elem[0]) # Duplicate operation ... with self.assertRaises(WsdlParseError) as ctx: WsdlPortType(elem, wsdl_document) self.assertIn('duplicated operation', str(ctx.exception)) del elem[0].attrib['name'] wsdl_port_type = WsdlPortType(elem, wsdl_document) self.assertEqual(list(wsdl_port_type.operations), [])
def test_wsdl_missing_message_reference(self): wsdl_document = Wsdl11Document(WSDL_DOCUMENT_EXAMPLE) elem = ElementTree.XML( '<input xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' message="tns:unknown"/>') with self.assertRaises(WsdlParseError) as ctx: WsdlInput(elem, wsdl_document) self.assertIn('unknown message', str(ctx.exception)) elem = ElementTree.XML( '<input xmlns="http://schemas.xmlsoap.org/wsdl/"/>') input_op = WsdlInput(elem, wsdl_document) self.assertIsNone(input_op.message)
def test_etree_getpath(self): root = ElementTree.XML('<a><b1><c1/><c2/></b1><b2/><b3><c3/></b3></a>') self.assertEqual(etree_getpath(root, root), '.') self.assertEqual(etree_getpath(root[0], root), './b1') self.assertEqual(etree_getpath(root[2][0], root), './b3/c3') self.assertEqual(etree_getpath(root[0], root, parent_path=True), '.') self.assertEqual(etree_getpath(root[2][0], root, parent_path=True), './b3') self.assertIsNone(etree_getpath(root, root[0])) self.assertIsNone(etree_getpath(root[0], root[1])) self.assertIsNone(etree_getpath(root, root, parent_path=True))
def test_validation_error(self): elem = ElementTree.XML('<foo/>') with self.assertRaises(XMLSchemaValidationError): self.schema.validation_error('strict', 'Test error', obj=elem) self.assertIsInstance(self.schema.validation_error('lax', 'Test error'), XMLSchemaValidationError) self.assertIsInstance(self.schema.validation_error('lax', 'Test error'), XMLSchemaValidationError) self.assertIsInstance(self.schema.validation_error('skip', 'Test error'), XMLSchemaValidationError) error = self.schema.validation_error('lax', 'Test error') self.assertIsNone(error.obj) self.assertEqual(self.schema.validation_error('lax', error, obj=10).obj, 10)
def test_wsdl_service(self): wsdl_document = Wsdl11Document(WSDL_DOCUMENT_EXAMPLE) with self.assertRaises(WsdlParseError) as ctx: wsdl_document._parse_services() self.assertIn("duplicated service 'tns:StockQuoteService'", str(ctx.exception)) elem = ElementTree.XML( '<service xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"\n' ' name="StockQuoteService"\n>' ' <port name="StockQuotePort" binding="tns:StockQuoteBinding">' ' <soap:address location="http://example.com/stockquote"/>' ' </port>' '</service>') wsdl_service = WsdlService(elem, wsdl_document) binding_name = '{http://example.com/stockquote.wsdl}StockQuoteBinding' binding = wsdl_document.bindings[binding_name] self.assertIs(wsdl_service.ports['StockQuotePort'].binding, binding) elem[0].attrib['binding'] = 'tns:unknown' with self.assertRaises(WsdlParseError) as ctx: WsdlService(elem, wsdl_document) self.assertIn('unknown binding', str(ctx.exception)) del elem[0].attrib['binding'] wsdl_service = WsdlService(elem, wsdl_document) self.assertIsNone(wsdl_service.ports['StockQuotePort'].binding) self.assertEqual(wsdl_service.ports['StockQuotePort'].soap_location, 'http://example.com/stockquote') del elem[0][0] wsdl_service = WsdlService(elem, wsdl_document) self.assertIsNone(wsdl_service.ports['StockQuotePort'].soap_location) elem.append(elem[0]) with self.assertRaises(WsdlParseError) as ctx: WsdlService(elem, wsdl_document) self.assertIn('duplicated port', str(ctx.exception)) del elem[0].attrib['name'] wsdl_service = WsdlService(elem, wsdl_document) self.assertEqual(wsdl_service.ports, {})
def test_etree_iterpath(self): root = ElementTree.XML('<a><b1><c1/><c2/></b1><b2/><b3><c3/></b3></a>') items = list(etree_iterpath(root)) self.assertListEqual(items, [(root, '.'), (root[0], './b1'), (root[0][0], './b1/c1'), (root[0][1], './b1/c2'), (root[1], './b2'), (root[2], './b3'), (root[2][0], './b3/c3')]) self.assertListEqual(items, list(etree_iterpath(root, tag='*'))) self.assertListEqual(items, list(etree_iterpath(root, path=''))) self.assertListEqual(items, list(etree_iterpath(root, path=None))) self.assertListEqual(list(etree_iterpath(root, path='/')), [(root, '/'), (root[0], '/b1'), (root[0][0], '/b1/c1'), (root[0][1], '/b1/c2'), (root[1], '/b2'), (root[2], '/b3'), (root[2][0], '/b3/c3')])
def test_setattr(self): schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root" type="xs:integer"/> </xs:schema>""") root = ElementTree.XML('<root a="10"/>') with self.assertRaises(XMLSchemaValidationError) as ctx: schema.validate(root) self.assertIsInstance(ctx.exception.source, XMLResource) self.assertFalse(ctx.exception.source.is_lazy()) resource = XMLResource(io.StringIO('<root a="10"/>'), lazy=True) with self.assertRaises(XMLSchemaValidationError) as ctx: schema.validate(resource) self.assertIsInstance(ctx.exception.source, XMLResource) self.assertTrue(ctx.exception.source.is_lazy()) self.assertIsNone(ctx.exception.elem) self.assertEqual(ctx.exception.source, resource) self.assertEqual(ctx.exception.path, '/root')
def test_wsdl_operation(self): wsdl_document = Wsdl11Document(WSDL_DOCUMENT_EXAMPLE) elem = ElementTree.XML( '<operation xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' name="GetLastTradePrice">\n' ' <input name="input" message="tns:GetLastTradePriceInput"/>\n' ' <output message="tns:GetLastTradePriceOutput"/>\n' '</operation>') wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertEqual(wsdl_operation.key, ('GetLastTradePrice', 'input', None)) self.assertEqual(wsdl_operation.transmission, 'request-response') elem[1].attrib['name'] = 'output' wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertEqual(wsdl_operation.key, ('GetLastTradePrice', 'input', 'output')) # Check the missing of soap bindings self.assertIsNone(wsdl_operation.soap_operation) self.assertIsNone(wsdl_operation.soap_action) self.assertIsNone(wsdl_operation.soap_style) elem = ElementTree.XML( '<operation xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' name="GetLastTradePrice">\n' ' <output name="send" message="tns:GetLastTradePriceOutput"/>\n' ' <input name="receive" message="tns:GetLastTradePriceInput"/>\n' '</operation>') wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertEqual(wsdl_operation.key, ('GetLastTradePrice', 'receive', 'send')) self.assertEqual(wsdl_operation.transmission, 'solicit-response') elem = ElementTree.XML( '<operation xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' name="GetLastTradePrice">\n' ' <input message="tns:GetLastTradePriceInput"/>\n' '</operation>') wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertEqual(wsdl_operation.key, ('GetLastTradePrice', None, None)) self.assertEqual(wsdl_operation.transmission, 'one-way') elem = ElementTree.XML( '<operation xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' name="GetLastTradePrice">\n' ' <output message="tns:GetLastTradePriceOutput"/>\n' '</operation>') wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertEqual(wsdl_operation.key, ('GetLastTradePrice', None, None)) self.assertEqual(wsdl_operation.transmission, 'notification') # Only for testing code, with faults is better to add specific messages. elem = ElementTree.XML( '<operation xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:tns="http://example.com/stockquote.wsdl"\n' ' name="GetLastTradePrice">\n' ' <input message="tns:GetLastTradePriceInput"/>\n' ' <fault message="tns:GetLastTradePriceInput"/>\n' '</operation>') wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertEqual(wsdl_operation.faults, {}) # not inserted if name is missing ... elem[1].attrib['name'] = 'foo' wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertEqual(list(wsdl_operation.faults), ['foo']) message_name = '{http://example.com/stockquote.wsdl}GetLastTradePriceInput' message = wsdl_document.messages[message_name] self.assertIs(wsdl_operation.faults['foo'].message, message) elem.append(elem[1]) # create a fake duplicated fault with self.assertRaises(WsdlParseError) as ctx: WsdlOperation(elem, wsdl_document) self.assertIn("duplicated fault 'foo'", str(ctx.exception)) elem.clear() wsdl_operation = WsdlOperation(elem, wsdl_document) self.assertIsNone(wsdl_operation.input) self.assertIsNone(wsdl_operation.output) self.assertIsNone(wsdl_operation.transmission) self.assertEqual(wsdl_operation.faults, {})
def test_etree_elements_assert_equal(self): e1 = ElementTree.XML('<a><b1>text<c1 a="1"/></b1>\n<b2/><b3/></a>\n') e2 = ElementTree.XML('<a><b1>text<c1 a="1"/></b1>\n<b2/><b3/></a>\n') self.assertIsNone(etree_elements_assert_equal(e1, e1)) self.assertIsNone(etree_elements_assert_equal(e1, e2)) e2 = lxml.etree.XML('<a><b1>text<c1 a="1"/></b1>\n<b2/><b3/></a>\n') self.assertIsNone(etree_elements_assert_equal(e1, e2)) e2 = ElementTree.XML( '<a><b1>text<c1 a="1"/></b1>\n<b2/><b3/><b4/></a>\n') with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2) self.assertIn("has lesser children than <Element 'a'", str(ctx.exception)) e2 = ElementTree.XML('<a><b1>text <c1 a="1"/></b1>\n<b2/><b3/></a>\n') self.assertIsNone(etree_elements_assert_equal(e1, e2, strict=False)) with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2) self.assertIn("texts differ: 'text' != 'text '", str(ctx.exception)) e2 = ElementTree.XML( '<a><b1>text<c1 a="1"/></b1>\n<b2>text</b2><b3/></a>\n') with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2, strict=False) self.assertIn("texts differ: None != 'text'", str(ctx.exception)) e2 = ElementTree.XML('<a><b1>text<c1 a="1"/></b1>\n<b2/><b3/></a>') self.assertIsNone(etree_elements_assert_equal(e1, e2)) e2 = ElementTree.XML('<a><b1>text<c1 a="1"/></b1><b2/><b3/></a>\n') self.assertIsNone(etree_elements_assert_equal(e1, e2, strict=False)) with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2) self.assertIn(r"tails differ: '\n' != None", str(ctx.exception)) e2 = ElementTree.XML('<a><b1>text<c1 a="1 "/></b1>\n<b2/><b3/></a>\n') self.assertIsNone(etree_elements_assert_equal(e1, e2, strict=False)) with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2) self.assertIn("attributes differ: {'a': '1'} != {'a': '1 '}", str(ctx.exception)) e2 = ElementTree.XML('<a><b1>text<c1 a="2 "/></b1>\n<b2/><b3/></a>\n') with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2, strict=False) self.assertIn("attribute 'a' values differ: '1' != '2'", str(ctx.exception)) e2 = ElementTree.XML( '<a><!--comment--><b1>text<c1 a="1"/></b1>\n<b2/><b3/></a>\n') self.assertIsNone(etree_elements_assert_equal(e1, e2)) self.assertIsNone( etree_elements_assert_equal(e1, e2, skip_comments=False)) e2 = lxml.etree.XML( '<a><!--comment--><b1>text<c1 a="1"/></b1>\n<b2/><b3/></a>\n') self.assertIsNone(etree_elements_assert_equal(e1, e2)) e1 = ElementTree.XML('<a><b1>+1</b1></a>') e2 = ElementTree.XML('<a><b1>+ 1 </b1></a>') self.assertIsNone(etree_elements_assert_equal(e1, e2, strict=False)) e1 = ElementTree.XML('<a><b1>+1</b1></a>') e2 = ElementTree.XML('<a><b1>+1.1 </b1></a>') with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2, strict=False) self.assertIn("texts differ: '+1' != '+1.1 '", str(ctx.exception)) e1 = ElementTree.XML('<a><b1>1</b1></a>') e2 = ElementTree.XML('<a><b1>true </b1></a>') self.assertIsNone(etree_elements_assert_equal(e1, e2, strict=False)) self.assertIsNone(etree_elements_assert_equal(e2, e1, strict=False)) e2 = ElementTree.XML('<a><b1>false </b1></a>') with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2, strict=False) self.assertIn("texts differ: '1' != 'false '", str(ctx.exception)) e1 = ElementTree.XML('<a><b1> 0</b1></a>') self.assertIsNone(etree_elements_assert_equal(e1, e2, strict=False)) self.assertIsNone(etree_elements_assert_equal(e2, e1, strict=False)) e2 = ElementTree.XML('<a><b1>true </b1></a>') with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2, strict=False) self.assertIn("texts differ: ' 0' != 'true '", str(ctx.exception)) e1 = ElementTree.XML('<a><b1>text<c1 a="1"/></b1>\n<b2/><b3/></a>\n') e2 = ElementTree.XML( '<a><b1>text<c1 a="1"/>tail</b1>\n<b2/><b3/></a>\n') with self.assertRaises(AssertionError) as ctx: etree_elements_assert_equal(e1, e2, strict=False) self.assertIn("tails differ: None != 'tail'", str(ctx.exception))
def test_rel_xpath_boolean(self): root = ElementTree.XML('<A><B><C/></B></A>') el = root[0] self.assertTrue(Selector('boolean(C)').iter_select(el)) self.assertFalse(next(Selector('boolean(D)').iter_select(el)))
def test_children_validation_error(self): schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b1" type="xs:string"/> <xs:element name="b2" type="xs:string"/> <xs:element name="b3" type="xs:string" minOccurs="2" maxOccurs="3"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/><b2/><b3/><b3/><b3/><b3/></a>') lines = str(ctx.exception).split('\n') self.assertEqual( lines[2], "Reason: Unexpected child with tag 'b3' at position 6.") self.assertEqual(lines[-2], "Path: /a") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/><b2/><b3/></a>') lines = str(ctx.exception).split('\n') self.assertEqual( lines[2][:51], "Reason: The content of element 'a' is not complete.") self.assertEqual(lines[-2], "Path: /a") root = ElementTree.XML('<a><b1/><b2/><b2/><b3/><b3/><b3/></a>') validator = schema.elements['a'].type.content with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: raise XMLSchemaChildrenValidationError(validator, root, 2, validator[1], 2) lines = str(ctx.exception).split('\n') self.assertTrue( lines[2].endswith("occurs 2 times but the maximum is 1.")) schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b1" type="xs:string"/> <xs:any/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/></a>') lines = str(ctx.exception).split('\n') self.assertTrue( lines[2].endswith("Tag from \'##any\' namespace/s expected.")) schema = XMLSchema(""" <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b1" type="xs:string"/> <xs:choice> <xs:any namespace="tns0" processContents="lax"/> <xs:element name="b2" type="xs:string"/> </xs:choice> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>""") with self.assertRaises(XMLSchemaChildrenValidationError) as ctx: schema.validate('<a><b1/></a>') lines = str(ctx.exception).split('\n') self.assertTrue(lines[2].endswith("Tag 'b2' expected."))
def test_wsdl_binding(self): wsdl_document = Wsdl11Document(WSDL_DOCUMENT_EXAMPLE) with self.assertRaises(WsdlParseError) as ctx: wsdl_document._parse_bindings() self.assertIn("duplicated binding 'tns:StockQuoteBinding'", str(ctx.exception)) elem = ElementTree.XML( '<binding xmlns="http://schemas.xmlsoap.org/wsdl/"\n' ' xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"\n' ' name="StockQuoteBinding" type="tns:StockQuotePortType"\n>' ' <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>\n' ' <operation name="GetLastTradePrice">' ' <input/>' ' <output/>' ' <fault><soap:fault name=""/></fault>' ' </operation>' '</binding>') wsdl_binding = WsdlBinding(elem, wsdl_document) self.assertEqual(wsdl_binding.port_type, list(wsdl_document.port_types.values())[0]) self.assertEqual(list(wsdl_binding.operations), [('GetLastTradePrice', None, None)]) del elem[1][0] # remove <input/> wsdl_binding = WsdlBinding(elem, wsdl_document) self.assertEqual(wsdl_binding.port_type, list(wsdl_document.port_types.values())[0]) self.assertEqual(list(wsdl_binding.operations), [('GetLastTradePrice', None, None)]) del elem[1][0] # remove <output/> wsdl_binding = WsdlBinding(elem, wsdl_document) self.assertEqual(wsdl_binding.port_type, list(wsdl_document.port_types.values())[0]) self.assertEqual(list(wsdl_binding.operations), [('GetLastTradePrice', None, None)]) elem[1][0].attrib[ 'name'] = 'unknown' # set an unknown name to <fault/> with self.assertRaises(WsdlParseError) as ctx: WsdlBinding(elem, wsdl_document) self.assertIn("missing fault 'unknown'", str(ctx.exception)) del elem[1][0] # remove <fault/> elem.append(elem[1]) with self.assertRaises(WsdlParseError) as ctx: WsdlBinding(elem, wsdl_document) self.assertIn("duplicated operation 'GetLastTradePrice'", str(ctx.exception)) del elem[2] elem[1].attrib['name'] = 'unknown' with self.assertRaises(WsdlParseError) as ctx: WsdlBinding(elem, wsdl_document) self.assertIn("operation 'unknown' not found", str(ctx.exception)) del elem[1].attrib['name'] wsdl_binding = WsdlBinding(elem, wsdl_document) self.assertEqual(wsdl_binding.operations, {}) elem.attrib['type'] = 'tns:unknown' with self.assertRaises(WsdlParseError) as ctx: WsdlBinding(elem, wsdl_document) self.assertIn("missing port type", str(ctx.exception)) del elem[0] with self.assertRaises(WsdlParseError) as ctx: WsdlBinding(elem, wsdl_document) self.assertIn("missing soap:binding element", str(ctx.exception))
def test_tostring(self): cars_dump = str(self.schema.elements['cars'].tostring()) self.assertEqual(len(cars_dump.split('\n')), 7) self.assertIn('name="car" type="vh:vehicleType"', cars_dump) self.assertIsInstance(ElementTree.XML(cars_dump), ElementTree.Element)