1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
import unittest
import datetime
from soaplib.etimport import ElementTree
from soaplib.serializers.primitive import *
from soaplib.serializers.clazz import *
from soaplib.service import *
from soaplib.wsgi_soap import *
from soaplib.client import *
from soaplib.util import *
from soaplib.soap import *
from threading import Thread
try:
from wsgiref.simple_server import make_server
except ImportError:
raise Exception("UnitTests require Python >= 2.5")
class Address(ClassSerializer):
class types:
street = String
city = String
zip = Integer
since = DateTime
laditude = Float
longitude = Float
class Person(ClassSerializer):
class types:
name = String
birthdate = DateTime
age = Integer
addresses = Array(Address)
titles = Array(String)
class Request(ClassSerializer):
class types:
param1 = String
param2 = Integer
class Response(ClassSerializer):
class types:
param1 = Float
class TestService(SimpleWSGISoapApp):
@soapmethod(String, Integer, _returns=DateTime)
def a(self, s, i):
return datetime.datetime(1901,12,15)
@soapmethod(Person, String, Integer, _returns=Address)
def b(self, p,s,i):
a = Address()
a.zip = 4444
a.street = 'wsgi way'
a.laditude = 123.3
return a
@soapmethod(Person, _isAsync=True)
def d(self, person):
pass
@soapmethod(Person, _isCallback=True)
def e(self, person):
pass
@soapmethod()
def fault(self):
raise Exception('Testing faults')
class test(unittest.TestCase):
def setUp(self):
self.server = make_server('127.0.0.1', 9191, TestService())
self.server.allow_reuse_address = True
Thread(target=self.server.serve_forever).start()
def tearDown(self):
self.server.shutdown()
del self.server
def test_simple(self):
inMessage = Message('a',[('s',String),('i',Integer)])
outMessage = Message('aResponse',[('retval',DateTime)])
desc = MethodDescriptor('a','a',inMessage,outMessage,'')
client = SimpleSoapClient('127.0.0.1:9191','/',desc)
results = client('abc',54)
self.assertEquals(results,datetime.datetime(1901,12,15))
def test_nested(self):
inMessage = Message('b',[('p',Person),('s',String),('i',Integer)])
outMessage = Message('bResponse',[('retval',Address)])
desc = MethodDescriptor('b','b',inMessage,outMessage,'')
client = SimpleSoapClient('127.0.0.1:9191','/',desc)
p = Person()
p.name = 'wilson'
p.addresses = []
for i in range(0,123):
a = Address()
a.zip = i
p.addresses.append(a)
res = client(p,'abc',123)
self.assertEquals(res.longitude,None)
self.assertEquals(res.zip,4444)
self.assertEquals(res.street,'wsgi way')
def test_async(self):
inMessage = Message('d',[('person',Person)])
outMessage = Message('dResponse',[])
desc = MethodDescriptor('d','d',inMessage,outMessage,'')
client = SimpleSoapClient('127.0.0.1:9191','/',desc)
p = Person()
p.name = 'wilson'
r = client(p)
self.assertEquals(r,None)
def test_fault(self):
inMessage = Message('fault',[])
outMessage = Message('faultResponse',[])
desc = MethodDescriptor('fault','fault',inMessage,outMessage,'')
client = SimpleSoapClient('127.0.0.1:9191','/',desc)
try:
client()
except Fault, f:
self.assertEquals(f.faultcode,'faultFault')
self.assertEquals(f.faultstring,'Testing faults')
self.assertTrue(f.detail.find('client_test.py') > -1)
else:
raise
def _test_callback(self):
inputs = [ParameterDescriptor('person',Person)]
client = SimpleSoapClient('127.0.0.1:9191','/','e',inputs,None)
p = Person()
p.name = 'wilson'
r = client(p)
self.assertEquals(r,None)
def test_service_client(self):
client = ServiceClient('127.0.0.1:9191','/',TestService())
r = client.a('bobo',23)
self.assertEquals(r,datetime.datetime(1901, 12, 15))
p = Person()
p.name = 'wilson'
p.addresses = []
for i in range(0,123):
a = Address()
a.zip = i
p.addresses.append(a)
res = client.b(p,'abc',123)
self.assertEquals(res.longitude,None)
self.assertEquals(res.zip,4444)
self.assertEquals(res.street,'wsgi way')
request = Request()
request.param1 = 'asdf'
p = Person()
p.name = 'wilson'
r = client.d(p)
self.assertEquals(r,None)
p = Person()
p.name = 'wilson'
r = client.e(p)
self.assertEquals(r,None)
def test_suite():
#debug(True)
loader = unittest.TestLoader()
#log_debug(True)
return loader.loadTestsFromTestCase(test)
if __name__== '__main__':
unittest.TextTestRunner().run(test_suite())
|