• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/opal/ZSI/test/test_t8.py

00001 #!/usr/bin/env python
00002 import unittest, sys, types, time
00003 from ZSI import TC, SoapWriter, ParsedSoap, EvaluateException
00004 from ZSI.wstools.Namespaces import SCHEMA, SOAP
00005 
00006 NSDICT = {'tns':'xmlns:tns="urn:a"',
00007     'xsi':'xmlns:xsi="%s"' %SCHEMA.XSI3,
00008     'xsd':'xmlns:xsd="%s"' %SCHEMA.XSD3,
00009     'soap':'xmlns:SOAP-ENC="%s"' %SOAP.ENC,
00010 }
00011 
00012 class AnyTestCase(unittest.TestCase):
00013     "Test Any serialize and parse"
00014 
00015     def check_empty_array(self):
00016         """Empty Array returned as list()
00017         """
00018         data = []
00019         s = str(SoapWriter().serialize(data,TC.Any(aslist=True)))
00020         p = ParsedSoap(s).Parse(TC.Any())
00021         self.failUnless(data==p, 'expecting "%s", got "%s"' %(data,p))
00022 
00023     def check_empty_struct(self):
00024         """Empty Struct is None, maybe dict() makes more sense, but this
00025         is fairly hard to determine if not typed (which is the norm).
00026         """
00027         data = {}
00028         s = str(SoapWriter().serialize(data,TC.Any()))
00029         p = ParsedSoap(s).Parse(TC.Any())
00030         self.failUnless(p==None, 'expecting "%s", got "%s"' %(None,p))
00031 
00032     def check_parse_empty_all(self):
00033         # None
00034         skip = [TC.FPEnumeration, TC.Enumeration, TC.IEnumeration, TC.List, TC.Integer]
00035         for typeclass in filter(lambda c: type(c) in [types.ClassType,type] and not issubclass(c, TC.String) and issubclass(c, TC.SimpleType), TC.__dict__.values()):
00036             if typeclass in skip: continue
00037             tc = typeclass()
00038             sw = SoapWriter()
00039             sw.serialize(None, typecode=tc, typed=True)
00040             soap = str(sw)
00041             ps = ParsedSoap(soap)
00042             parsed = ps.Parse(TC.Any())
00043             self.assertEqual(None, parsed)
00044 
00045     def check_parse_empty_string(self):
00046         # Empty String
00047         typecodes = TC.Any.parsemap.values()
00048         for tc in filter(lambda c: isinstance(c, TC.String), TC.Any.parsemap.values()):
00049             sw = SoapWriter()
00050             sw.serialize("", typecode=tc, typed=True)
00051             soap = str(sw)
00052             ps = ParsedSoap(soap)
00053             parsed = ps.Parse(TC.Any())
00054             self.assertEqual("", parsed)
00055 
00056     def check_builtins(self):
00057         myInt,myLong,myStr,myDate,myFloat = 123,2147483648,\
00058             u"hello", time.gmtime(), 1.0001
00059         orig = [myInt,myLong,myStr,myDate,myFloat]
00060 
00061         sw = SoapWriter()
00062         sw.serialize(orig, typecode=TC.Any(pname="builtins", aslist=True))
00063         
00064         ps = ParsedSoap(str(sw)) 
00065         parsed = ps.Parse(TC.Any())
00066         self.assertEqual(len(orig), len(parsed))
00067 
00068         self.assertEqual(myInt, parsed[0])
00069         self.assertEqual(myLong, parsed[1])
00070         self.assertEqual(myStr, parsed[2])
00071         self.assertEqual(myDate[0:6], parsed[3][0:6])
00072         self.assertEqual(myFloat, parsed[4])
00073         
00074         self.assertEqual(type(myInt), type(parsed[0]))
00075         self.assertEqual(type(myLong), type(parsed[1]))
00076         self.assertEqual(str, type(parsed[2]))
00077         self.assertEqual(tuple, type(parsed[3]))
00078         self.assertEqual(type(myFloat), type(parsed[4]))
00079 
00080     def check_any_nill(self):
00081         result = ['23', {'a' : None, 'b': 5}]
00082         soap = str(SoapWriter().serialize(result, TC.Any(pname="NilRequest", nillable=True, aslist=True)))
00083 
00084         ps = ParsedSoap(soap)
00085         tc = TC.Any(nillable=True)
00086         pyobj = ps.Parse(tc)
00087 
00088     def check_any_compound(self):
00089         # from zsi developer's guide
00090         xml = """
00091 <tns:foo %(tns)s %(xsi)s %(soap)s>
00092     <tns:i xsi:type="SOAP-ENC:integer">12</tns:i>
00093     <tns:name xsi:type="SOAP-ENC:string">Hello world</tns:name>
00094 </tns:foo>""" %NSDICT
00095 
00096         ps = ParsedSoap(xml, envelope=False)
00097         self.failUnless(ps.Parse(TC.Any()) == {'i': 12, 'name': 'Hello world'})
00098         self.failUnless(ps.Parse(TC.Any(aslist=True)) == [12, 'Hello world'])
00099 
00100     def check_any_typed_soap_integer(self):
00101         # from zsi developer's guide
00102         value = 12
00103         d = dict(value=value)
00104         d.update(NSDICT)
00105         xml = """<tns:i xsi:type="SOAP-ENC:integer" %(xsi)s %(soap)s %(tns)s>%(value)d</tns:i>""" %d
00106         ps = ParsedSoap(xml, envelope=False)
00107         self.failUnless(ps.Parse(TC.Any()) == value)
00108 
00109     def check_any_typed_xsd_int(self):
00110         # from zsi developer's guide
00111         value = 12
00112         d = dict(value=value)
00113         d.update(NSDICT)
00114         xml = """<tns:i xsi:type="xsd:int" %(xsi)s %(soap)s %(tns)s %(xsd)s>%(value)d</tns:i>""" %d
00115         ps = ParsedSoap(xml, envelope=False)
00116         self.failUnless(ps.Parse(TC.Any()) == value)
00117 
00118     def check_any_typed_nonNegativeInteger(self):
00119         # from zsi developer's guide
00120         value = 12
00121         d = dict(value=value)
00122         d.update(NSDICT)
00123         xml = """<tns:i xsi:type="xsd:nonNegativeInteger" %(xsi)s %(soap)s %(tns)s %(xsd)s>%(value)d</tns:i>""" %d
00124         ps = ParsedSoap(xml, envelope=False)
00125         self.failUnless(ps.Parse(TC.Any()) == value)
00126 
00127     def check_any_untyped_int(self):
00128         # from zsi developer's guide
00129         d = dict(value=12)
00130         d.update(NSDICT)
00131         xml = """<tns:i %(tns)s>12</tns:i>""" %NSDICT
00132         ps = ParsedSoap(xml, envelope=False)
00133         self.failUnless(int(ps.Parse(TC.Any())) == 12)
00134 
00135     def check_any_dict_list_rpcenc(self):
00136         sw = SoapWriter()
00137         testObj = [{"a":1,"b":2}, {"d":4,"e":5}, {"f":{"x":9}, "g":[6,7.0]}]
00138         typecode = TC.Any(aslist=True)
00139         sw.serialize(testObj, typecode=typecode)
00140         xml = str(sw)
00141         ps = ParsedSoap(xml)
00142         result = TC.Any().parse(ps.body_root, ps)
00143         self.failUnless(result == testObj)
00144 
00145 #
00146 # Creates permutation of test options: "check", "check_any", etc
00147 #
00148 _SEP = '_'
00149 for t in [i[0].split(_SEP) for i in filter(lambda i: callable(i[1]), AnyTestCase.__dict__.items())]:
00150     test = ''
00151     for f in t:
00152         test += f
00153         if globals().has_key(test): test += _SEP; continue
00154         def _closure():
00155             name = test
00156             def _makeTestSuite():
00157                 suite = unittest.TestSuite()
00158                 suite.addTest(unittest.makeSuite(AnyTestCase, name))
00159                 return suite
00160             return _makeTestSuite
00161 
00162         globals()[test] = _closure()
00163         test += _SEP
00164 
00165 makeTestSuite = check
00166 
00167 def main():
00168     unittest.main(defaultTest="makeTestSuite")
00169 
00170 if __name__ == "__main__" : main()
00171 
00172 

Generated on Wed Oct 20 2010 11:12:16 for APBS by  doxygen 1.7.2