def test_complex_tree(self): tree = etree.fromstring(TEST_TREE) gen = xml.XMPPXMLGenerator(self.buf, short_empty_elements=True) lxml.sax.saxify(tree, gen) tree2 = etree.fromstring(self.buf.getvalue()) self.assertSubtreeEqual(tree, tree2)
def test_assertSubtreeEqual_attr_key_missing(self): t1 = etree.fromstring("<foo a='1'/>") t2 = etree.fromstring("<foo />") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2) with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2, ignore_surplus_attr=True)
def test_assertSubtreeEqual_allow_surplus_child(self): t1 = etree.fromstring("<foo />") t2 = etree.fromstring("<foo><bar/></foo>") self.assertSubtreeEqual(t1, t2, ignore_surplus_children=True) t1 = etree.fromstring("<foo><bar/></foo>") t2 = etree.fromstring("<foo><bar/><bar/><fnord /></foo>") self.assertSubtreeEqual(t1, t2, ignore_surplus_children=True)
def test_no_duplicate_auto_assignments_of_prefixes(self): gen = xml.XMPPXMLGenerator(self.buf) gen.startDocument() gen.startPrefixMapping("ns0", "uri:foo") gen.startElementNS(("uri:bar", "foo"), None, None) gen.startElementNS(("uri:foo", "foo"), None, None) gen.endElementNS(("uri:foo", "foo"), None) gen.endElementNS(("uri:bar", "foo"), None) gen.endPrefixMapping("ns0") gen.endDocument() self.assertSubtreeEqual( etree.fromstring("<foo xmlns='uri:bar'>" "<foo xmlns='uri:foo' />" "</foo>"), etree.fromstring(self.buf.getvalue()), )
def test_store_and_retrieve_xml_unregistered(self): tree = etree.fromstring('<example xmlns="urn:example:unregistered">' '<payload xmlns="urn:example:unregistered">' 'foobar' '</payload>' '</example>') query = private_xml.xso.Query(None) query.unregistered_payload.append(tree) iq = aioxmpp.IQ( type_=aioxmpp.IQType.SET, payload=query, ) yield from self.client.send(iq) query.unregistered_payload[0].clear() iq = aioxmpp.IQ( type_=aioxmpp.IQType.GET, payload=query, ) retrieved = yield from self.client.send(iq) self.assertEqual(len(retrieved.unregistered_payload), 1) self.assertEqual(retrieved.unregistered_payload[0].tag, "{urn:example:unregistered}example") self.assertEqual(len(retrieved.unregistered_payload[0]), 1) self.assertEqual(retrieved.unregistered_payload[0][0].tag, "{urn:example:unregistered}payload") self.assertEqual(retrieved.unregistered_payload[0][0].text, "foobar")
def test_element_path(self): el = etree.fromstring("<foo><bar><baz /></bar>" "<subfoo />" "<bar><baz /></bar></foo>") baz1 = el[0][0] subfoo = el[1] baz2 = el[2][0] self.assertEqual("/foo", element_path(el)) self.assertEqual("/foo/bar[0]/baz[0]", element_path(baz1)) self.assertEqual("/foo/subfoo[0]", element_path(subfoo)) self.assertEqual("/foo/bar[1]/baz[0]", element_path(baz2))
def test_store_and_retrieve_xml_unregistered(self): tree = etree.fromstring( '<example xmlns="urn:example:unregistered">' '<payload xmlns="urn:example:unregistered">' 'foobar' '</payload>' '</example>' ) query = private_xml.xso.Query(None) query.unregistered_payload.append(tree) iq = aioxmpp.IQ( type_=aioxmpp.IQType.SET, payload=query, ) yield from self.client.send(iq) query.unregistered_payload[0].clear() iq = aioxmpp.IQ( type_=aioxmpp.IQType.GET, payload=query, ) retrieved = yield from self.client.send(iq) self.assertEqual(len(retrieved.unregistered_payload), 1) self.assertEqual( retrieved.unregistered_payload[0].tag, "{urn:example:unregistered}example" ) self.assertEqual(len(retrieved.unregistered_payload[0]), 1) self.assertEqual( retrieved.unregistered_payload[0][0].tag, "{urn:example:unregistered}payload" ) self.assertEqual( retrieved.unregistered_payload[0][0].text, "foobar" )
def test_element_path(self): el = etree.fromstring("<foo><bar><baz /></bar>" "<subfoo />" "<bar><baz /></bar></foo>") baz1 = el[0][0] subfoo = el[1] baz2 = el[2][0] self.assertEqual( "/foo", element_path(el)) self.assertEqual( "/foo/bar[0]/baz[0]", element_path(baz1)) self.assertEqual( "/foo/subfoo[0]", element_path(subfoo)) self.assertEqual( "/foo/bar[1]/baz[0]", element_path(baz2))
def test_assertSubtreeEqual_text_no_join_text_parts_is_strict(self): t1 = etree.fromstring("<foo><a/>text1</foo>") t2 = etree.fromstring("<foo>text1<a/></foo>") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2, join_text_parts=False)
def test_assertSubtreeEqual_text_including_tails(self): t1 = etree.fromstring("<foo>t<a/>ext1</foo>") t2 = etree.fromstring("<foo>te<a/>xt1</foo>") self.assertSubtreeEqual(t1, t2)
def test_assertSubtreeEqual_text(self): t1 = etree.fromstring("<foo>text1</foo>") t2 = etree.fromstring("<foo>text2</foo>") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2)
def test_assertSubtreeEqual_strict_ordering(self): t1 = etree.fromstring("<foo><bar/><baz/></foo>") t2 = etree.fromstring("<foo><baz/><bar/></foo>") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2, strict_ordering=True)
def test_assertSubtreeEqual_attr_value_mismatch_allow_surplus(self): t1 = etree.fromstring("<foo a='1'/>") t2 = etree.fromstring("<foo a='1' b='2'/>") self.assertSubtreeEqual(t1, t2, ignore_surplus_attr=True)
def test_assertSubtreeEqual_forbid_reordering_of_same(self): t1 = etree.fromstring("<foo><bar a='1' /><bar a='2' /></foo>") t2 = etree.fromstring("<foo><bar a='2' /><bar a='1' /></foo>") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2)
def test_assertSubtreeEqual_allow_relative_reordering(self): t1 = etree.fromstring("<foo><bar/><baz/></foo>") t2 = etree.fromstring("<foo><baz/><bar/></foo>") self.assertSubtreeEqual(t1, t2)
def test_assertSubtreeEqual_surplus_child(self): t1 = etree.fromstring("<foo><bar/></foo>") t2 = etree.fromstring("<foo><bar/><bar/></foo>") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2)
def test_assertSubtreeEqual_attr_value_mismatch(self): t1 = etree.fromstring("<foo a='1'/>") t2 = etree.fromstring("<foo a='2'/>") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2)
def test_assertSubtreeEqual_tag(self): t1 = etree.fromstring("<foo />") t2 = etree.fromstring("<bar />") with self.assertRaisesRegex(AssertionError, "tag mismatch"): self.assertSubtreeEqual(t1, t2)
def test_assertSubtreeEqual_missing_child(self): t1 = etree.fromstring("<foo><bar/></foo>") t2 = etree.fromstring("<foo />") with self.assertRaises(AssertionError): self.assertSubtreeEqual(t1, t2)