def test_creating_subelements(self): # Test creating a subelement as a dictionary element = xml.create_root_xml_element("root", {"root": 5}) xml.create_subelement(element, "subelement", {"subelement": "value"}) exp = '<root xmlns="http://docs.openstack.org/database/api/v1.0" ' \ 'root="5"><subelement subelement="value"/></root>' self.assertEqual(exp, etree.tostring(element)) # Test creating a subelement as a list element = xml.create_root_xml_element("root", {"root": { "value": "nested" }}) xml.create_subelement(element, "subelement", [{"subelement": "value"}]) exp = '<root xmlns="http://docs.openstack.org/database/api/v1.0">' \ '<root value="nested"/><subelement><subelement subelement=' \ '"value"/></subelement></root>' self.assertEqual(exp, etree.tostring(element)) # Test creating a subelement as a string (should raise TypeError) element = xml.create_root_xml_element("root", {"root": "value"}) try: xml.create_subelement(element, "subelement", ["value"]) self.fail("TypeError exception expected") except TypeError: pass
def test_create_root_xml_element(self): # Test creating when name is not in REQUEST_AS_LIST element = xml.create_root_xml_element("root", {"root": "value"}) exp = '<root xmlns="http://docs.openstack.org/database/api/v1.0" ' \ 'root="value"/>' self.assertEqual(exp, etree.tostring(element)) # Test creating when name is in REQUEST_AS_LIST element = xml.create_root_xml_element("users", []) exp = '<users xmlns="http://docs.openstack.org/database/api/v1.0"/>' self.assertEqual(exp, etree.tostring(element))
def test_creating_subelements(self): # Test creating a subelement as a dictionary element = xml.create_root_xml_element("root", {"root": 5}) xml.create_subelement(element, "subelement", {"subelement": "value"}) exp = '<root xmlns="http://docs.openstack.org/database/api/v1.0" ' \ 'root="5"><subelement subelement="value"/></root>' self.assertEqual(exp, etree.tostring(element)) # Test creating a subelement as a list element = xml.create_root_xml_element("root", {"root": {"value": "nested"}}) xml.create_subelement(element, "subelement", [{"subelement": "value"}]) exp = '<root xmlns="http://docs.openstack.org/database/api/v1.0">' \ '<root value="nested"/><subelement><subelement subelement=' \ '"value"/></subelement></root>' self.assertEqual(exp, etree.tostring(element)) # Test creating a subelement as a string (should raise TypeError) element = xml.create_root_xml_element("root", {"root": "value"}) try: xml.create_subelement(element, "subelement", ["value"]) self.fail("TypeError exception expected") except TypeError: pass