def test_equal(self): class SubObj(xmlmap.XmlObject): baz = xmlmap.StringField('baz') class XmlObj(xmlmap.XmlObject): bar = xmlmap.NodeField('bar', SubObj) bar_list = xmlmap.NodeListField('bar', SubObj) generic = xmlmap.NodeField('bar', xmlmap.XmlObject) obj = xmlmap.load_xmlobject_from_string(TestXsl.FIXTURE_TEXT, XmlObj) self.assertTrue(obj == obj, 'xmlobject identity equals obj == obj should return True') self.assertFalse(obj.bar != obj.bar, 'xmlobject identity not-equals obj != obj should return False') self.assertTrue(obj.bar == obj.bar_list[0], 'xmlobject equal should return True for objects pointing at same document node') self.assertFalse(obj.bar != obj.bar_list[0], 'xmlobject not equal should return False for objects pointing at same document node') self.assertTrue(obj.bar != obj.bar_list[1], 'xmlobject not equal should return True for objects pointing at different nodes') self.assertFalse(obj.bar == obj.bar_list[1], 'xmlobject equal should return False for object pointing at different nodes') obj2 = xmlmap.load_xmlobject_from_string(TestXsl.FIXTURE_TEXT, XmlObj) self.assertTrue(obj == obj2, 'two different xmlobjects that serialize the same should be considered equal') # compare to None self.assertTrue(obj != None, 'xmlobject not equal to None should return True') self.assertFalse(obj.bar == None, 'xmlobject equal None should return False') # FIXME: is this really what we want? # should different xmlobject classes pointing at the same node be considered equal? self.assertTrue(obj.generic == obj.bar, 'different xmlobject classes pointing at the same node are considered equal')
def test_serializeDocument(self): obj = xmlmap.load_xmlobject_from_string(TestXmlObjectStringInit.VALID_XML) xmlstr = obj.serializeDocument() self.assert_("encoding='UTF-8'" in xmlstr, "XML generated by serializeDocument should include xml character encoding") self.assert_('<!DOCTYPE a' in xmlstr, "XML generated by serializeDocument should include DOCTYPE declaration")
def test_load_from_string_with_classname(self): """Test using shortcut to initialize named XmlObject class from string""" class TestObject(xmlmap.XmlObject): pass obj = xmlmap.load_xmlobject_from_string(TestXsl.FIXTURE_TEXT, TestObject) self.assert_(isinstance(obj, TestObject))
def test_isvalid(self): # attempting schema-validation on an xmlobject with no schema should raise an exception self.assertRaises(Exception, self.obj.schema_valid) # generic validation with no schema -- assumed True self.assertTrue(self.obj.is_valid()) # very simple xsd schema and valid/invalid xml taken from lxml docs: # http://codespeak.net/lxml/validation.html#xmlschema xsd = '''<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="a" type="AType"/> <xsd:complexType name="AType"> <xsd:sequence> <xsd:element name="b" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:schema> ''' FILE = tempfile.NamedTemporaryFile(mode="w") FILE.write(xsd) FILE.flush() valid_xml = '<a><b></b></a>' invalid_xml = '<a foo="1"><c></c></a>' class TestSchemaObject(xmlmap.XmlObject): XSD_SCHEMA = FILE.name valid = xmlmap.load_xmlobject_from_string(valid_xml, TestSchemaObject) self.assertTrue(valid.is_valid()) self.assertTrue(valid.schema_valid()) invalid = xmlmap.load_xmlobject_from_string(invalid_xml, TestSchemaObject) self.assertFalse(invalid.is_valid()) invalid.is_valid() self.assertEqual(2, len(invalid.validation_errors())) # do schema validation at load time valid = xmlmap.load_xmlobject_from_string(valid_xml, TestSchemaObject, validate=True) self.assert_(isinstance(valid, TestSchemaObject)) self.assertRaises(etree.XMLSyntaxError, xmlmap.load_xmlobject_from_string, invalid_xml, TestSchemaObject, validate=True) FILE.close()
def test_load_from_string_with_validation(self): self.assertRaises(Exception, xmlmap.load_xmlobject_from_string, self.INVALID_XML, validate=True) # fixture with no doctype also causes a validation error self.assertRaises(Exception, xmlmap.load_xmlobject_from_string, TestXsl.FIXTURE_TEXT, validate=True) obj = xmlmap.load_xmlobject_from_string(self.VALID_XML) self.assert_(isinstance(obj, xmlmap.XmlObject))
def testSchemaField(self): # very simple xsd schema and valid/invalid xml based on the one from lxml docs: # http://codespeak.net/lxml/validation.html#xmlschema xsd = '''<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="a" type="AType"/> <xsd:complexType name="AType"> <xsd:sequence> <xsd:element name="b" type="BType" /> </xsd:sequence> </xsd:complexType> <xsd:simpleType name="BType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="c"/> <xsd:enumeration value="d"/> <xsd:enumeration value="e"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> ''' FILE = tempfile.NamedTemporaryFile(mode="w") FILE.write(xsd) FILE.flush() valid_xml = '<a><b>some text</b></a>' class TestSchemaObject(xmlmap.XmlObject): XSD_SCHEMA = FILE.name txt = xmlmap.SchemaField('/a/b', 'BType', required=True) valid = xmlmap.load_xmlobject_from_string(valid_xml, TestSchemaObject) self.assertEqual('some text', valid.txt, 'schema field value is accessible as text') self.assert_(isinstance(valid._fields['txt'], xmlmap.StringField), 'txt SchemaField with base string in schema initialized as StringField') self.assertEqual(['', 'c', 'd', 'e'], valid._fields['txt'].choices, 'txt SchemaField has choices based on restriction enumeration in schema') # check required self.assertTrue(valid._fields['txt'].required) FILE.close()
def test_load_from_string(self): """Test using shortcut to initialize XmlObject from string""" obj = xmlmap.load_xmlobject_from_string(TestXsl.FIXTURE_TEXT) self.assert_(isinstance(obj, xmlmap.XmlObject))
def test__string(self): self.assertEqual('42 13', self.obj.__string__()) # convert xml with unicode content obj = xmlmap.load_xmlobject_from_string(u'<text>unicode \u2026</text>') self.assertEqual('unicode …', obj.__string__())
def setUp(self): self.obj = xmlmap.load_xmlobject_from_string(TestXsl.FIXTURE_TEXT)