Commits

Anonymous committed 0740679

Add test suite.

Comments (0)

Files changed (12)

osa/__init__.pyc

Binary file added.

osa/client.pyc

Binary file added.

osa/methods.pyc

Binary file added.

osa/soap.pyc

Binary file added.

osa/wsdl.pyc

Binary file added.
         element.text = self.value.isoformat('T')
 
     def from_xml(self, element):
-        return datetime.strptime('2011-08-02T17:00:01.000122',
-                                        '%Y-%m-%dT%H:%M:%S.%f')
+        if not(element.text):
+            return None
+        text = element.text
+        return datetime.strptime(text, '%Y-%m-%dT%H:%M:%S.%f')

osa/xmltypes.pyc

Binary file added.
+Tests for the library.
+
+Folder serializers - contains tests for simple and complex XML types.
+These test are standalone and do not require any service.
+
+The following services, however, require a simple HelloWorldService to
+be available.
+test_wsdl - test WSDL to Python conversion
+test_client - test method calling

tests/serializers/test_complex.py

+#!/usr/bin/env python
+import sys
+for x in sys.path:
+    if x.find("osa") != -1:
+        sys.path.remove(x)
+sys.path.append("../../")
+
+import datetime
+import unittest
+
+import xml.etree.cElementTree as etree
+
+from osa.xmltypes import *
+from osa.soap import *
+
+ns_test = 'test_namespace'
+
+Address = ComplexTypeMeta('Address', (), {
+                "_children":[
+                        {'name':"street", "type":XMLString, "min":1, "max": 1},
+                        {'name':"city", "type":XMLString, "min":1, "max": 1},
+                        {'name':"zip", "type":XMLInteger, "min":1, "max": 1},
+                        {'name':"since", "type":XMLDateTime, "min":0, "max": 1},
+                        {'name':"lattitude", "type":XMLDouble, "min":1, "max": 1},
+                        {'name':"longitude", "type":XMLDouble, "min":1, "max": 1},
+                        ], "__doc__": "an address info"})
+Person = ComplexTypeMeta('Person', (), {
+                "_children":[
+                        {'name':"name", "type":XMLString, "min":0, "max": 1},
+                        {'name':"birthdate", "type":XMLDateTime, "min":0, "max": 1},
+                        {'name':"age", "type":XMLInteger, "min":0, "max": 1},
+                        {'name':"addresses", "type":Address, "min":0, "max": 'unbounded'},
+                        {'name':"titles", "type":XMLString, "min":0, "max": 'unbounded'},
+                        ], "__doc__": "a person info"})
+
+Employee = ComplexTypeMeta('Employee', (Person,), {
+                "_children":[
+                        {'name':"id", "type":XMLInteger, "min":1, "max": 1},
+                        {'name':"salary", "type":XMLDouble, "min":1, "max": 1},
+                        ], "__doc__": "an employee info"})
+
+Level2 = ComplexTypeMeta('Level2', (), {
+                "_children":[
+                        {'name':"arg1", "type":XMLString, "min":1, "max": 1},
+                        {'name':"arg2", "type":XMLDouble, "min":1, "max": 1},
+                        ], "__doc__": "don't know"})
+
+Level3 = ComplexTypeMeta('Level3', (), {
+                "_children":[
+                        {'name':"arg1", "type":XMLInteger, "min":1, "max": 1},
+                        ], "__doc__": "don't know"})
+Level4 = ComplexTypeMeta('Level4', (), {
+                "_children":[
+                        {'name':"arg1", "type":XMLString, "min":1, "max": 1},
+                        ], "__doc__": "don't know"})
+
+Level1 = ComplexTypeMeta('Level1', (), {
+                "_children":[
+                        {'name':"level2", "type":Level2, "min":1, "max": 1},
+                        {'name':"level3", "type":Level3, "min":0, "max": 'unbouneded'},
+                        {'name':"level4", "type":Level4, "min":0, "max": 'unbouneded'},
+                        ], "__doc__": "don't know"})
+
+class TestClassSerializer(unittest.TestCase):
+    def test_simple_class(self):
+        a = Address()
+        a.street = '123 happy way'
+        a.city = 'badtown'
+        a.zip = 32
+        a.lattitude = 4.3
+        a.longitude = 88.0
+
+        element = etree.Element('test')
+        a.to_xml( element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+        self.assertEquals(5, len(element.getchildren()))
+
+        r = Address().from_xml(element)
+
+        self.assertEquals(a.street, r.street)
+        self.assertEquals(a.city, r.city)
+        self.assertEquals(a.zip, r.zip)
+        self.assertEquals(a.lattitude, r.lattitude)
+        self.assertEquals(a.longitude, r.longitude)
+        self.assertEquals(a.since, r.since)
+
+    def test_nested_class(self):
+        p = Person()
+        element = etree.Element('test')
+        p.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+
+        self.assertEquals(None, p.name)
+        self.assertEquals(None, p.birthdate)
+        self.assertEquals(None, p.age)
+        self.assertEquals(None, p.addresses)
+
+    def test_complex_class(self):
+        l = Level1()
+        l.level2 = Level2()
+        l.level2.arg1 = 'abcd'
+        l.level2.arg2 = 1.0/3.0
+        l.level3 = []
+        l.level4 = []
+
+        for i in range(0, 100):
+            a = Level3()
+            a.arg1 = i
+            l.level3.append(a)
+
+        for i in range(0, 4):
+            a = Level4()
+            a.arg1 = str(i)
+            l.level4.append(a)
+
+        element = etree.Element('test')
+        l.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+        l1 = Level1().from_xml(element)
+
+        self.assertEquals(l1.level2.arg1, l.level2.arg1)
+        self.assertEquals(l1.level2.arg2, l.level2.arg2)
+        self.assertEquals(len(l1.level4), len(l.level4))
+        self.assertEquals(len(l1.level3), len(l.level3))
+        for i in range(100):
+            self.assertEquals(l1.level3[i].arg1, l.level3[i].arg1)
+        for i in range(4):
+            self.assertEquals(l1.level4[i].arg1, l.level4[i].arg1)
+
+
+    def test_any(self):
+        a = Address()
+        a.street = '123 happy way'
+        a.city = 'badtown'
+        a.zip = 32
+        a.lattitude = 4.3
+        a.longitude = 88.0
+
+        element = etree.Element('test')
+        a.to_xml( element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+        element.set("{%s}type" %NS_XSI, 'Address')
+
+        XMLAny._types = {'Person':Person, 'Address':Address, 'Level4':Level4,
+                        'Level3':Level3, 'Level2': Level2, 'Level1':Level1}
+
+        r = XMLAny().from_xml(element)
+        self.assertTrue(isinstance(r, Address))
+
+        self.assertEquals(a.street, r.street)
+        self.assertEquals(a.city, r.city)
+        self.assertEquals(a.zip, r.zip)
+        self.assertEquals(a.lattitude, r.lattitude)
+        self.assertEquals(a.longitude, r.longitude)
+        self.assertEquals(a.since, r.since)
+
+if __name__ == '__main__':
+    unittest.main()

tests/serializers/test_primitive.py

+#!/usr/bin/env python
+
+import sys
+for x in sys.path:
+    if x.find("osa") != -1:
+        sys.path.remove(x)
+sys.path.append("../../")
+
+import datetime
+import unittest
+
+import xml.etree.cElementTree as etree
+
+from osa.xmltypes import *
+
+ns_test = 'test_namespace'
+
+class TestPrimitive(unittest.TestCase):
+    def test_string(self):
+        s = XMLString("value")
+        element = etree.Element('test')
+        s.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element=element[0]
+
+        self.assertEquals(element.text, 'value')
+        value = XMLString().from_xml(element)
+        self.assertEquals(value, 'value')
+
+    def test_datetime(self):
+        d = XMLDateTime(datetime.now())
+
+        element = etree.Element('test')
+        d.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+
+        self.assertEquals(element.text, d.value.isoformat())
+        dt = XMLDateTime().from_xml(element)
+        self.assertEquals(d.value, dt)
+
+    def test_date(self):
+        x = datetime.now()
+        x = x.date()
+        d = XMLDate(x)
+
+        element = etree.Element('test')
+        d.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+
+        self.assertEquals(element.text, d.value.isoformat())
+        dt = XMLDate().from_xml(element)
+        self.assertEquals(d.value, dt)
+
+    def test_integer(self):
+        integer = XMLInteger(12)
+
+        element = etree.Element('test')
+        integer.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+
+        self.assertEquals(element.text, '12')
+        value = XMLInteger().from_xml(element)
+        self.assertEquals(value, integer)
+
+    def test_large_integer(self):
+        integer = XMLInteger(128375873458473)
+
+        element = etree.Element('test')
+        integer.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+
+        self.assertEquals(element.text, '128375873458473')
+        value = XMLInteger().from_xml(element)
+        self.assertEquals(value, integer)
+
+    def test_float(self):
+        f = XMLDouble(1.0/3.0)
+        element = etree.Element('test')
+        f.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+
+        self.assertEquals(element.text, repr(f))
+
+        f2 = XMLDouble().from_xml(element)
+        self.assertEquals(f2, f)
+
+    def test_unicode(self):
+        s = XMLString(u'\x34\x55\x65\x34')
+        self.assertEquals(4, len(s))
+        element = etree.Element('test')
+        s.to_xml(element, "{%s}%s" %(ns_test, "atach"))
+        element = element[0]
+        value = XMLString().from_xml(element)
+        self.assertEquals(value, s)
+
+    def test_boolean(self):
+        b = etree.Element('test')
+        XMLBoolean(True).to_xml(b, "{%s}%s" %(ns_test, "atach"))
+        b = b[0]
+        self.assertEquals('true', b.text)
+
+        b = etree.Element('test')
+        XMLBoolean(0).to_xml(b, "{%s}%s" %(ns_test, "atach"))
+        b = b[0]
+        self.assertEquals('false', b.text)
+
+        b = etree.Element('test')
+        XMLBoolean(1).to_xml(b, "{%s}%s" %(ns_test, "atach"))
+        b = b[0]
+        self.assertEquals('true', b.text)
+
+        b = XMLBoolean().from_xml(b)
+        self.assertEquals(b, True)
+
+        b = etree.Element('test')
+        XMLBoolean(False).to_xml(b, "{%s}%s" %(ns_test, "atach"))
+        b = b[0]
+        self.assertEquals('false', b.text)
+
+        b = XMLBoolean().from_xml(b)
+        self.assertEquals(b, False)
+
+        b = etree.Element('test')
+        b.text = ''
+        b = XMLBoolean().from_xml(b)
+        self.assertEquals(b, None)
+
+if __name__ == '__main__':
+    unittest.main()

tests/test_client.py

+#!/usr/bin/env python
+
+import sys
+for x in sys.path:
+    if x.find("osa") != -1:
+        sys.path.remove(x)
+sys.path.append("../")
+
+import datetime
+import unittest
+
+import xml.etree.cElementTree as etree
+
+from osa.client import Client
+from osa.wsdl import WSDLParser, _primmap
+from osa.methods import Method
+from osa.xmltypes import *
+
+wsdl_url = 'http://lxpowerboz:88/services/python/HelloWorldService?wsdl'
+
+class TestClient(unittest.TestCase):
+    def test_init(self):
+        cl = Client(wsdl_url)
+        self.assertEquals(cl.wsdl_url, wsdl_url)
+        self.assertEquals(cl.names, ["HelloWorldService",])
+        for t in ("Person", "Name", "echoString", "sayHello"):
+            self.assertTrue(hasattr(cl.types, t))
+            self.assertEquals(type(getattr(cl.types, t)), ComplexTypeMeta)
+            self.assertTrue(t in cl.types.anyType._types.keys())
+        for method in ("testMe", "giveMessage", "echoString", "sayHello", "faultyThing"):
+            self.assertTrue(hasattr(cl.service, method))
+            self.assertEquals(type(getattr(cl.service, method)), Method)
+
+    def test_testme(self):
+        #note: giveMessage is broken at the server side
+        cl = Client(wsdl_url)
+        res = cl.service.testMe()
+        self.assertEqual(type(res), cl.types.testMeResponse)
+
+    def test_echoString(self):
+        cl = Client(wsdl_url)
+
+        self.assertEquals('my message 1', cl.service.echoString('my message 1'))
+        self.assertEquals('my message 2', cl.service.echoString(msg = 'my message 2'))
+        m = cl.types.echoString()
+        m.msg = 'my message 3'
+        self.assertEquals('my message 3', cl.service.echoString(m))
+
+    def test_sayHello(self):
+        cl = Client(wsdl_url)
+
+        n = cl.types.Name()
+        n.firstName = "first"
+        n.lastName = "last"
+        p = cl.types.Person()
+        p.name = n
+        p.age = 30
+        p.weight = 80
+        p.height = 175
+
+        self.assertEquals(['Hello, first']*5, cl.service.sayHello(p, 5))
+        self.assertEquals(['Hello, first']*8, cl.service.sayHello(person = p, times = 8))
+        m = cl.types.sayHello()
+        m.person = p
+        m.times = 10
+        self.assertEquals(['Hello, first']*10, cl.service.sayHello(m))
+
+    def test_faultyThing(self):
+        cl = Client(wsdl_url)
+        self.assertRaises(RuntimeError, cl.service.faultyThing)
+        try:
+            cl.service.faultyThing()
+        except RuntimeError as e:
+            self.assertFalse(str(e).find('4u!') == -1)
+
+
+
+
+if __name__ == '__main__':
+    unittest.main()

tests/test_wsdl.py

+#!/usr/bin/env python
+
+import sys
+for x in sys.path:
+    if x.find("osa") != -1:
+        sys.path.remove(x)
+sys.path.append("../")
+
+import datetime
+import unittest
+
+import xml.etree.cElementTree as etree
+
+from osa.wsdl import WSDLParser, _primmap
+from osa.methods import Method
+from osa.xmltypes import *
+
+wsdl_url = 'http://lxpowerboz:88/services/python/HelloWorldService?wsdl'
+
+class TestWSDL(unittest.TestCase):
+    def test_reading(self):
+        w = WSDLParser(wsdl_url)
+        self.assertEquals(w.wsdl_url, wsdl_url)
+        self.assertEquals(w.tns, 'helloworldservice.soaplib.boz.hgw.ipp.mpg.de')
+        self.assertEquals(type(w.wsdl), type(etree.Element('root')))
+
+    def test_service_name(self):
+        w = WSDLParser(wsdl_url)
+        self.assertEquals(w.get_service_names(), ["HelloWorldService"])
+
+    def test_conversions(self):
+        w = WSDLParser(wsdl_url)
+        types = w.get_types(_primmap)
+        self.assertTrue('Name' in types.keys())
+        self.assertTrue('Person' in types.keys())
+        self.assertTrue('echoString' in types.keys())
+        self.assertTrue('faultyThing' in types.keys())
+        self.assertTrue('testMe' in types.keys())
+        self.assertTrue('sayHello' in types.keys())
+        self.assertTrue('echoStringResponse' in types.keys())
+        self.assertTrue('faultyThingResponse' in types.keys())
+        self.assertTrue('testMeResponse' in types.keys())
+        self.assertTrue('sayHelloResponse' in types.keys())
+        self.assertTrue(types['Name'], 'firstName')
+        self.assertTrue(types['Name'], 'lastName')
+        self.assertTrue(types['Person'], 'name')
+        self.assertTrue(types['Person'], 'age')
+        self.assertTrue(types['Person'], 'height')
+        self.assertTrue(types['Person'], 'weight')
+        self.assertTrue(types['Person']._children[0]['name'] == "name")
+        self.assertTrue(types['Person']._children[2]['name'] == "age")
+        self.assertTrue(types['Person']._children[0]['type'] == types['Name'])
+        self.assertTrue(types['Person']._children[2]['type'] == types['int'])
+        self.assertTrue(types['sayHello'], 'person')
+        self.assertTrue(types['sayHello'], 'times')
+        self.assertTrue(types['sayHello']._children[0]['name'] == "person")
+        self.assertTrue(types['sayHello']._children[1]['name'] == "times")
+        self.assertTrue(types['sayHello']._children[0]['type'] == types['Person'])
+        self.assertTrue(types['sayHello']._children[1]['type'] == types['int'])
+        methods = w.get_methods(types)
+        self.assertTrue('testMe' in methods.keys())
+        self.assertTrue('giveMessage' in methods.keys())
+        self.assertTrue('echoString' in methods.keys())
+        self.assertTrue('sayHello' in methods.keys())
+        self.assertTrue('faultyThing' in methods.keys())
+        self.assertTrue(isinstance(methods['testMe'], Method))
+        self.assertTrue(isinstance(methods['giveMessage'], Method))
+        self.assertTrue(isinstance(methods['echoString'], Method))
+        self.assertTrue(isinstance(methods['sayHello'], Method))
+        self.assertTrue(isinstance(methods['faultyThing'], Method))
+
+    def test_methods(self):
+        w = WSDLParser(wsdl_url)
+        types = w.get_types(_primmap)
+        methods = w.get_methods(types)
+
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        methods['testMe'].input.to_xml(_body = root)
+        self.assertEquals(len(root), 1)
+
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        methods['giveMessage'].input.to_xml(_body = root)
+        self.assertEquals(len(root), 1)
+
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        methods['faultyThing'].input.to_xml(_body = root)
+        self.assertEquals(len(root), 1)
+
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        methods['echoString'].input.to_xml(msg = 'message', _body = root)
+        self.assertEquals(len(root), 1)
+        self.assertEquals('message', XMLString().from_xml(root[0][0]))
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        methods['echoString'].input.to_xml('message', _body = root)
+        self.assertEquals(len(root), 1)
+        self.assertEquals('message', XMLString().from_xml(root[0][0]))
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        mm = types['echoString']()
+        mm.msg = 'message'
+        methods['echoString'].input.to_xml(mm, _body = root)
+        self.assertEquals(len(root), 1)
+        self.assertEquals('message', XMLString().from_xml(root[0][0]))
+
+        n = types['Name']()
+        n.firstName = "first"
+        n.lastName = "last"
+        p = types['Person']()
+        p.name = n
+        p.age = 30
+        p.weight = 80
+        p.height = 175
+        t = 5
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        methods['sayHello'].input.to_xml(person = p, times = t, _body = root)
+        self.assertEquals(len(root), 1)
+        self.assertEquals(len(root[0]), 2)
+        rp = types['Person']().from_xml(root[0][0])
+        rt = types['int']().from_xml(root[0][1])
+        self.assertEquals(rt, t)
+        self.assertEquals(rp.age, p.age)
+        self.assertEquals(rp.weight, p.weight)
+        self.assertEquals(rp.height, p.height)
+        self.assertEquals(rp.name.firstName, p.name.firstName)
+        self.assertEquals(rp.name.lastName, p.name.lastName)
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        methods['sayHello'].input.to_xml( p, t, _body = root)
+        self.assertEquals(len(root), 1)
+        self.assertEquals(len(root[0]), 2)
+        rp = types['Person']().from_xml(root[0][0])
+        rt = types['int']().from_xml(root[0][1])
+        self.assertEquals(rt, t)
+        self.assertEquals(rp.age, p.age)
+        self.assertEquals(rp.weight, p.weight)
+        self.assertEquals(rp.height, p.height)
+        self.assertEquals(rp.name.firstName, p.name.firstName)
+        self.assertEquals(rp.name.lastName, p.name.lastName)
+        root = etree.Element('root')
+        self.assertEquals(len(root), 0)
+        mm = types['sayHello']()
+        mm.person = p
+        mm.times = t
+        methods['sayHello'].input.to_xml(mm, _body = root)
+        self.assertEquals(len(root), 1)
+        self.assertEquals(len(root[0]), 2)
+        rp = types['Person']().from_xml(root[0][0])
+        rt = types['int']().from_xml(root[0][1])
+        self.assertEquals(rt, t)
+        self.assertEquals(rp.age, p.age)
+        self.assertEquals(rp.weight, p.weight)
+        self.assertEquals(rp.height, p.height)
+        self.assertEquals(rp.name.firstName, p.name.firstName)
+        self.assertEquals(rp.name.lastName, p.name.lastName)
+
+if __name__ == '__main__':
+    unittest.main()