osa / tests / test_client.py

#!/usr/bin/env python
# test_client.py - test Client class, part of osa.
# Copyright 2013 Sergey Bozhenkov, boz at ipp.mpg.de
# Licensed under GPLv3 or later, see the COPYING file.

import sys
for x in sys.path:
    if x.find("osa") != -1:
        sys.path.remove(x)
sys.path.append("../")

import unittest
import xml.etree.cElementTree as etree
from osa.client import *
from osa.wsdl import *
from osa.method import *
from osa.xmltypes import *
import sys
if sys.version_info.major < 3:
    from urllib2 import urlopen, HTTPError, URLError
else:
    from urllib.request import urlopen, HTTPError, URLError
    basestring = str

wsdl_url = 'http://lxpowerboz:88/services/python/HelloWorldService?wsdl'

class TestClient(unittest.TestCase):
    def setUp(self):
        self.client = Client("test.wsdl")
    def tearDown(self):
        self.client = None
    def test_init(self):
        self.assertEqual(self.client.names, ["service HelloWorldService",])
        for t in ("Person", "Name", "echoString", "sayHello"):
            self.assertTrue(hasattr(self.client.types, t))
            self.assertEqual(type(getattr(self.client.types, t)), ComplexTypeMeta)
        for method in ("testMe", "giveMessage", "echoString", "sayHello", "faultyThing"):
            self.assertTrue(hasattr(self.client.service, method))
            self.assertEqual(type(getattr(self.client.service, method)), Method)

    def test_giveMessage(self):
        try:
            urlopen("http://lxpowerboz:88")
        except HTTPError:
            pass
        except URLError:
            return
        res = self.client.service.giveMessage()
        self.assertTrue(isinstance(res, basestring))

    def test_echoString(self):
        try:
            urlopen("http://lxpowerboz:88")
        except HTTPError:
            pass
        except URLError:
            return
        self.assertEqual('my message 1', self.client.service.echoString('my message 1'))

    def test_sayHello(self):
        try:
            urlopen("http://lxpowerboz:88")
        except HTTPError:
            pass
        except URLError:
            return
        n = self.client.types.Name()
        n.firstName = "first"
        n.lastName = "last"
        p = self.client.types.Person()
        p.name = n
        p.age = 30
        p.weight = 80
        p.height = 175
        self.assertEqual(['Hello, first\n']*5, self.client.service.sayHello(p, 5))

    def test_faultyThing(self):
        try:
            urlopen("http://lxpowerboz:88")
        except HTTPError:
            pass
        except URLError:
            return
        try:
            self.client.service.faultyThing()
        except RuntimeError as e:
            self.assertFalse(str(e).find('4u!') == -1)


if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.