Skip to content

Commit 8706c6d

Browse files
author
Luiko Czub
committed
new method _callServer(methodAPI, argsAPI=None)
calls server method METHODAPI with error handling and returns the responds lczub#2
1 parent 5fe4a0b commit 8706c6d

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

src/testlink/testlinkapi.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,42 @@
99
import xmlrpclib
1010

1111
from testlinkhelper import TestLinkHelper, VERSION
12+
import testlinkerrors
1213

1314

1415
class TestLinkAPIClient(object):
1516

16-
__slots__ = ['server', 'devKey', 'stepsList']
17+
__slots__ = ['server', 'devKey', 'stepsList', '_server_url']
1718

1819
__VERSION__ = VERSION
1920

2021
def __init__(self, server_url, devKey):
2122
self.server = xmlrpclib.Server(server_url)
2223
self.devKey = devKey
2324
self.stepsList = []
24-
25+
self._server_url = server_url
26+
27+
def _callServer(self, methodAPI, argsAPI=None):
28+
""" call server method METHODAPI with error handling and returns the
29+
responds """
30+
31+
response = None
32+
try:
33+
if argsAPI is None:
34+
response = getattr(self.server.tl, methodAPI)()
35+
else:
36+
response = getattr(self.server.tl, methodAPI)(argsAPI)
37+
except (IOError, xmlrpclib.ProtocolError), msg:
38+
new_msg = 'problems connecting the TestLink Server %s\n%s' %\
39+
(self._server_url, msg)
40+
raise testlinkerrors.TLConnectionError(new_msg)
41+
except xmlrpclib.Fault, msg:
42+
new_msg = 'problems calling the API method %s\n%s' %\
43+
(methodAPI, msg)
44+
raise testlinkerrors.TLAPIError(new_msg)
45+
46+
return response
47+
2548
#
2649
# BUILT-IN API CALLS
2750
#

src/testlink/testlinkerrors.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
# see https://github.com/orenault/TestLink-API-Python-client/issues/4
88

99
class TestLinkError(Exception):
10-
""" Basic error handler
10+
""" Basic error
1111
Return message pass as argument
1212
"""
13-
def __init__(self, msg):
14-
self.__msg = msg
13+
# def __init__(self, msg):
14+
# self.__msg = msg
15+
#
16+
# def __str__(self):
17+
# return self.__msg
18+
19+
class TLConnectionError(TestLinkError):
20+
""" Connection error
21+
- wrong url? - server not reachable? """
22+
23+
class TLAPIError(TestLinkError):
24+
""" API error
25+
- wrong method name ? - misssing required args? """
1526

16-
def __str__(self):
17-
return self.__msg
1827

src/testlink/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
# Licensed under ???
77
# see https://github.com/orenault/TestLink-API-Python-client/issues/4
88

9-
VERSION = '0.4.0-Alpha'
9+
VERSION = '0.4.0-Alpha_error-handling'
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#! /usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
# Copyright 2012 Luiko Czub, TestLink-API-Python-client developers
5+
#
6+
# Licensed under ???
7+
# see https://github.com/orenault/TestLink-API-Python-client/issues/4
8+
9+
# this test requires an online TestLink Server, which connection parameters
10+
# are defined in environment variables
11+
# TESTLINK_API_PYTHON_DEVKEY and TESTLINK_API_PYTHON_DEVKEY
12+
13+
import unittest
14+
from testlink import TestLinkAPIClient, TestLinkHelper
15+
from testlink import testlinkerrors
16+
17+
18+
class TestLinkAPIcallServerTestCase(unittest.TestCase):
19+
""" TestCases for TestLinkAPICleint._callServer() """
20+
21+
22+
def test_callServer_noArgs(self):
23+
""" test _callServer() - calling method with no args """
24+
25+
client = TestLinkHelper().connect(TestLinkAPIClient)
26+
response = client._callServer('sayHello')
27+
self.assertEqual('Hello!', response)
28+
29+
def test_callServer_withArgs(self):
30+
""" test _callServer() - calling method with args """
31+
32+
client = TestLinkHelper().connect(TestLinkAPIClient)
33+
response = client._callServer('repeat', {'str' : 'some arg'})
34+
self.assertEqual('You said: some arg', response)
35+
36+
def test_callServer_ProtocollError(self):
37+
""" test _callServer() - Server raises ProtocollError """
38+
39+
server_url = TestLinkHelper()._server_url
40+
bad_server_url = server_url.split('xmlrpc.php')[0]
41+
client = TestLinkHelper(bad_server_url).connect(TestLinkAPIClient)
42+
def a_func(api_client): api_client._callServer('sayHello')
43+
self.assertRaises(testlinkerrors.TLConnectionError, a_func, client)
44+
45+
def test_callServer_socketError(self):
46+
""" test _callServer() - Server raises a socket Error (IOError) """
47+
48+
bad_server_url = 'http://111.222.333.4/testlink/lib/api/xmlrpc.php'
49+
client = TestLinkHelper(bad_server_url).connect(TestLinkAPIClient)
50+
def a_func(api_client): api_client._callServer('sayHello')
51+
self.assertRaises(testlinkerrors.TLConnectionError, a_func, client)
52+
53+
def test_callServer_FaultError(self):
54+
""" test _callServer() - Server raises Fault Error """
55+
56+
client = TestLinkHelper().connect(TestLinkAPIClient)
57+
def a_func(api_client): api_client._callServer('sayGoodBye')
58+
self.assertRaises(testlinkerrors.TLAPIError, a_func, client)
59+
60+
if __name__ == "__main__":
61+
#import sys;sys.argv = ['', 'Test.testName']
62+
unittest.main()

test/utest/testlinkhelpertest.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# Licensed under ???
77
# see https://github.com/orenault/TestLink-API-Python-client/issues/4
88

9+
# this test works WITHOUT an online TestLink Server
10+
# no calls are send to a TestLink Server
11+
912
import unittest, os
1013
from testlink import TestLinkHelper
1114

0 commit comments

Comments
 (0)