def testUnion(self): class TypeA(object): _xobjMeta = xobj2.XObjMetadata(tag='typea', attributes=dict(vala=int)) class TypeB(object): _xobjMeta = xobj2.XObjMetadata(tag='typeb', attributes=dict(valb=int)) class Top(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('items', [ TypeA, TypeB, ]) ]) s = _xml('union', '<top>\n' ' <typea vala="1"/>\n' ' <typeb valb="2"/>\n' ' <typea vala="3"/>\n' ' <typeb valb="4"/>\n' ' <typea vala="5"/>\n' '</top>\n') d = xobj2.Document.fromxml(s, rootNodes = [ Top ]) self.failUnlessEqual(d.root.items[0].vala, 1) self.failUnlessEqual(d.root.items[1].valb, 2) self.failUnlessEqual(d.root.items[2].vala, 3) self.failUnlessEqual(d.root.items[3].valb, 4) self.failUnlessEqual(d.root.items[4].vala, 5) self.assertXMLEquals(d.toxml(xml_declaration = False), s) d = xobj2.Document.fromxml('<top/>', rootNodes = [ Top ]) self.failUnlessEqual(d.root.items, [])
def testComplexMultiParse(self): """ Test parsing multiple xml documents with one set of classes using more complex types. """ class Top(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('foo', [ str ]) ]) class Document(xobj2.Document): def __init__(self, *args, **kwargs): kwargs['rootNodes'] = [ Top ] xobj2.Document.__init__(self, *args, **kwargs) xmlTextA = _xml('complex-multi-V2', '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n' '<top>\n' ' <foo>a</foo>\n' ' <foo>b</foo>\n' '</top>\n') docA = Document.fromxml(xmlTextA) xmlTextB = _xml('complex-multi-V2', '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n' '<top>\n' ' <foo>A</foo>\n' ' <foo>B</foo>\n' '</top>\n') xmlB = StringIO(xmlTextB) docB = Document.fromxml(xmlB) self.failUnlessEqual([ x for x in docA.root.foo ], [ 'a', 'b' ]) self.failUnlessEqual([ x for x in docB.root.foo ], [ 'A', 'B' ])
def testExplicitNamespaces(self): s = _xml('explicitns', '<top xmlns="http://somens.xsd" xmlns:ns="http://somens.xsd">\n' ' <element ns:attr="foo"/>\n' '</top>\n' ) d = xobj2.Document.fromxml(s) self.failUnlessEqual(d.rootName, 'ns_top') self.failUnlessEqual(d.root.ns_element.ns_attr, 'foo') s2 = d.toxml(xml_declaration = False) expecteds2 = ( '<ns:top xmlns:ns="http://somens.xsd">\n' ' <ns:element ns:attr="foo"/>\n' '</ns:top>\n' ) self.failUnlessEqual(s2, expecteds2)
def testNamespaces(self): xmlString = _xml('namespaces-V2', """\ <top xmlns="http://this" xmlns:other="http://other/other" xmlns:other2="http://other/other2"> <local/> <other:tag other:val="1"/> <other2:tag val="2"/> </top>""") o = xobj2.Document.fromxml(xmlString) assert(o.root.other_tag.other_val == '1') assert(o.root.other2_tag.val == '2') self.assertXmlEqual(o.toxml(xml_declaration = False), xmlString) o = xobj2.Document() o.nameSpaceMap = { 'other3' : 'http://other/other2' } o.parse(xmlString) assert(o.root.other_tag.other_val == '1') assert(o.root.other3_tag.val == '2') newXmlString = xmlString.replace("other2:", "other3:") newXmlString = newXmlString.replace(":other2", ":other3") self.assertXmlEqual(o.toxml(xml_declaration = False), newXmlString)
def testIdInNamespace(self): s = _xml('id-in-ns1-V2', '<ns:top xmlns:ns="http://somens.xsd">\n' ' <ns:ref ns:other="theid"/>\n' ' <ns:item ns:id="theid" ns:val="value"/>\n' '</ns:top>\n') xml = StringIO(s) class Ref(object): _xobjMeta = xobj2.XObjMetadata( attributes=dict(ns_other = xobj2.XIDREF)) class Top(object): _xobjMeta = xobj2.XObjMetadata( tag = 'ns_top', elements = [ xobj2.Field('ns_ref', Ref) ]) d = xobj2.Document(rootNodes = [ Top ]) d.nameSpaceMap.update(ns="http://somens.xsd") d = xobj2.Document.fromxml(xml, rootNodes = [ Top ]) self.failUnlessEqual(d.root.ns_ref.ns_other, d.root.ns_item) s2 = d.toxml(xml_declaration = False) self.assertXMLEquals(s, s2)
def testIntElement(self): xml = _xml('intelement', '<top><anint>5</anint></top>') doc = xobj2.Document.fromxml(xml) self.failUnlessEqual(doc.root.anint, '5')
def testId(self): s = _xml('id1-V2', '<top>\n' ' <ref other="theid"/>\n' ' <item id="theid" val="value"/>\n' '</top>\n') class Ref(xobj2.XObj): _xobjMeta = xobj2.XObjMetadata( attributes = dict(other = xobj2.XIDREF)) class Top(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('ref', Ref) ]) d = xobj2.Document.fromxml(s, rootNodes = [ Top ]) self.failUnlessEqual(d.root.ref.other, d.root.item) s2 = d.toxml(xml_declaration = False) self.assertXMLEquals(s, s2) # now test if the id is called something else s = _xml('id2', '<top>\n' ' <item anid="theid" val="value"/>\n' ' <ref other="theid"/>\n' '</top>\n') xml = StringIO(s) e = self.failUnlessRaises(xobj2.XObjIdNotFound, xobj2.Document.fromxml, xml, rootNodes = [ Top ]) self.failUnlessEqual(str(e), "XML ID 'theid' not found in document") class Item(xobj2.XObj): _xobjMeta = xobj2.XObjMetadata(attributes = dict(anid=xobj2.XID)) class Top(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('item', Item), xobj2.Field('ref', Ref), ]) d = xobj2.Document.fromxml(xml, rootNodes = [ Top ]) assert(d.root.ref.other == d.root.item) s2 = d.toxml(xml_declaration = False) self.assertXMLEquals(s2, s) class Top(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('item', Item), xobj2.Field('ref', Ref), ]) # test outputing an idref w/o a corresponding id t = Top() t.item = Item() t.item.anid = 'foo' t.ref = Ref() t.ref.other = Item() t.ref.other.anid = 'bar' d = xobj2.Document() d.root = t e = self.failUnlessRaises(xobj2.UnmatchedIdRef, d.toxml, xml_declaration = False) self.failUnlessEqual(str(e), 'Unmatched idref values during XML creation for id(s): bar') t.ref.other = t.item s = d.toxml(xml_declaration = False) self.assertXMLEquals(s, '<top>\n' ' <item anid="foo"/>\n' ' <ref other="foo"/>\n' '</top>\n') # and test if the id isn't defined properly class Top(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', attributes = dict(ref=xobj2.XIDREF)) class Ref(object): _xobjMeta = xobj2.XObjMetadata( attributes = dict(blah=str)) d = xobj2.Document() d.root = Top() d.root.ref = Ref() e = self.failUnlessRaises(xobj2.XObjSerializationException, d.toxml) self.assertEquals(str(e), 'No id found for element referenced by ref') class Ref(object): _xobjMeta = xobj2.XObjMetadata( attributes = dict(blah=xobj2.XID)) d.root.ref = Ref() e = self.failUnlessRaises(xobj2.XObjSerializationException, d.toxml) self.assertEquals(str(e), 'Empty ID field blah for element referenced by ref')
def testComplexParse(self): xmlText = _xml('complex-V2', """\ <?xml version='1.0' encoding='UTF-8'?> <top> <prop> <subprop subattr="1">asdf</subprop> <subprop subattr="2">fdsa</subprop> </prop> <simple>simple</simple> </top>""") xml = StringIO(xmlText) o = xobj2.Document.fromxml(xml) # --- self.assertXMLEquals(o.toxml(), xmlText) # --- self.assertEqual(o.root.__class__.__name__, 'top_XObj_Type') self.assertEqual(type(o.root.prop.subprop), types.ListType) self.assertEqual(o.root.prop.subprop, ['asdf', 'fdsa']) asdf = o.root.prop.subprop[0] self.assertEqual(asdf.__class__.__name__, 'subprop_XObj_Type') self.assertEqual(o.root.prop.__class__.__name__, 'prop_XObj_Type') for i in range(2): self.assertEqual(o.root.prop.subprop[i].__class__.__name__, 'subprop_XObj_Type') assert(o.root.prop.subprop[0].__class__ == o.root.prop.subprop[1].__class__) # --- class SubpropClass(object): __slots__ = [ 'subattr', 'unused', ] _xobjMeta = xobj2.XObjMetadata( attributes = dict(subattr=int, unused=str)) class PropClass(object): _xobjMeta = xobj2.XObjMetadata( elements = [ xobj2.Field('subprop', [ SubpropClass ]) ], ) subprop = [ SubpropClass ] class SimpleClass(xobj2.XObj): pass class TopClass(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('unused', str), xobj2.Field('prop', PropClass), xobj2.Field('simple', [ SimpleClass ]), ], ) o = xobj2.Document.fromxml(xml, rootNodes = [ TopClass ]) self.assertEqual(o.root.prop.subprop[1].subattr, 2) self.assertEqual(o.root.unused, None) self.assertEqual(o.root.prop.subprop[0].unused, None) self.assertEqual(o.root.simple[0], 'simple') # --- # asdf/fdsa have been dropped becuase SubpropClass has slots # that don't include _xobjText # the complex class PropClass xmlOutText = """\ <?xml version='1.0' encoding='UTF-8'?> <top> <prop> <subprop subattr="1"/> <subprop subattr="2"/> </prop> <simple>simple</simple> </top>""" self.assertXMLEquals(o.toxml(), xmlOutText)
def testSimpleParse(self): xml = _xml('simple-V2', """ <top attr1="anattr" attr2="another"> <!-- comment --> <prop>something</prop> <subelement subattr="2"> <subsub>1</subsub> </subelement> </top>""", asFile = True) doc = xobj2.Document.fromxml(xml) self.assertEqual(doc.root.__class__.__name__, 'top_XObj_Type') self.assertEqual(doc.root.attr1, 'anattr') self.assertEqual(doc.root.attr2, 'another') self.assertEqual(doc.root.prop, 'something') self.assertEqual(doc.root.subelement.subattr, '2') self.assertEqual(doc.root.subelement.__class__.__name__, 'subelement_XObj_Type') self.failUnlessStartsWith(repr(doc), '<xobj.xobj2.Document object') self.failUnlessStartsWith(repr(doc.root), '<xobj.xobj2.top_XObj_Type object') self.failUnlessStartsWith(repr(doc.root.subelement), '<xobj.xobj2.subelement_XObj_Type object') self.assertEqual(repr(doc.root.attr1), "'anattr'") # --- class SubelementClass(object): _xobjMeta = xobj2.XObjMetadata( attributes=dict(subattr=int)) class TopClass(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('subelement', SubelementClass), xobj2.Field('unused', str), xobj2.Field('attr1', str), ]) doc = xobj2.Document(rootNodes = [ TopClass ]) o = doc.parse(xml) self.assertEqual(o.root.subelement.subattr, 2) self.assertEqual(o.root.unused, None) # --- class SubelementClass(object): _xobjMeta = xobj2.XObjMetadata( attributes=dict(subattr=int), elements = [ xobj2.Field('subsub', [ int ]) ]) class TopClass(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('subelement', SubelementClass), xobj2.Field('unused', str), xobj2.Field('attr1', str), xobj2.Field('prop', str), ]) o = xobj2.Document.fromxml(xml, rootNodes=[TopClass]) self.assertEqual(o.root.subelement.subattr, 2 ) self.assertEqual(o.root.subelement.subsub, [ 1 ] ) self.assertEqual(o.root.prop, 'something') # --- class TopClass(object): _xobjMeta = xobj2.XObjMetadata( tag = 'top', elements = [ xobj2.Field('subelement', [ SubelementClass ]), xobj2.Field('unused', str), xobj2.Field('attr1', str), xobj2.Field('prop', [ str ]), ]) o = xobj2.Document.fromxml(xml, rootNodes=[TopClass]) self.assertEqual(o.root.subelement[0].subattr, 2 ) self.assertEqual(o.root.subelement[0].subsub, [ 1 ] ) self.assertEqual(o.root.prop, [ 'something' ])