Skip to content

Commit faa5035

Browse files
author
Luiko Czub
committed
decorator decoApiCallAddDevKey registers argument 'devKey' lczub#8
1 parent 77184ef commit faa5035

File tree

4 files changed

+71
-13
lines changed

4 files changed

+71
-13
lines changed

src/testlink/testlinkapigeneric.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import xmlrpclib
2121
from functools import wraps
2222
from .testlinkhelper import TestLinkHelper, VERSION
23-
import testlinkerrors, testlinkargs
23+
#from .testlinkargs import registerMethod, registerArgOptional, getMethodsWithPositionalArgs
24+
from .testlinkargs import *
25+
import testlinkerrors #, testlinkargs
2426

2527

2628
# # Default Definition which (python) API-Method expects which positional arguments
@@ -68,7 +70,7 @@ def decoApiCallWithoutArgs(methodAPI):
6870
""" Decorator for calling server methods without arguments """
6971

7072
# register methods without positional and optional arguments
71-
testlinkargs.registerMethod(methodAPI.__name__)
73+
registerMethod(methodAPI.__name__)
7274

7375
@wraps(methodAPI)
7476
def wrapperWithoutArgs(self):
@@ -89,8 +91,7 @@ def decoApiCallWithArgs(methodAPI):
8991
""" Decorator for calling a server method with arguments """
9092

9193
# register methods positional and optional arguments
92-
testlinkargs.registerMethod(methodAPI.__name__,
93-
argNamesPositional, argNamesOptional)
94+
registerMethod(methodAPI.__name__, argNamesPositional, argNamesOptional)
9495
# define the method server call
9596
@wraps(methodAPI)
9697
def wrapperWithArgs(self, *argsPositional, **argsOptional):
@@ -101,6 +102,8 @@ def wrapperWithArgs(self, *argsPositional, **argsOptional):
101102

102103
def decoApiCallAddDevKey(methodAPI):
103104
""" Decorator to expand parameter list with devKey"""
105+
# register additional optional argument devKey
106+
registerArgOptional(methodAPI.__name__, 'devKey')
104107
@wraps(methodAPI)
105108
def wrapperAddDevKey(self, *argsPositional, **argsOptional):
106109
if not ('devKey' in argsOptional):
@@ -186,7 +189,7 @@ def __init__(self, server_url, devKey, **args):
186189
verbose, allow_none)
187190
self.devKey = devKey
188191
self._server_url = server_url
189-
self._positionalArgNames = testlinkargs.getMethodsWithPositionalArgs()
192+
self._positionalArgNames = getMethodsWithPositionalArgs()
190193

191194

192195

src/testlink/testlinkargs.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#
1818
# ------------------------------------------------------------------------
1919

20+
from .testlinkerrors import TLArgError
21+
22+
2023
__doc__ = """ This modul is used as a 'singleton' to register the supported
2124
TestLink API methods and there (positional and optional) arguments """
2225

@@ -31,10 +34,19 @@
3134
# mandatory Args
3235
_apiMethodsArgs = {}
3336

34-
def resetRegister():
37+
def _resetRegister():
3538
" clears all entries in _apiMethodsArgs"
3639
_apiMethodsArgs.clear()
3740

41+
def _getMethodsArgDefinition(methodName):
42+
""" returns argument definition for api methodName """
43+
44+
try:
45+
return _apiMethodsArgs[methodName]
46+
except KeyError:
47+
raise TLArgError('apiMethod %s not registered!' % methodName)
48+
49+
3850
def registerMethod(methodName, apiArgsPositional=[], apiArgsOptional=[],
3951
otherArgsMandatory=[]):
4052
""" extend _apiMethodsArgs with a new definition structure for METHODNAME
@@ -48,7 +60,7 @@ def registerMethod(methodName, apiArgsPositional=[], apiArgsOptional=[],
4860
print 'register args for', methodName
4961

5062
if methodName in _apiMethodsArgs:
51-
raise StandardError('apiMethod %s already registered!' % methodName)
63+
raise TLArgError('apiMethod %s already registered!' % methodName)
5264

5365
allArgs = apiArgsPositional[:]
5466
for argName in apiArgsOptional:
@@ -57,6 +69,13 @@ def registerMethod(methodName, apiArgsPositional=[], apiArgsOptional=[],
5769
_apiMethodsArgs[methodName] = (apiArgsPositional[:], allArgs,
5870
otherArgsMandatory[:])
5971

72+
def registerArgOptional(methodName, argName):
73+
""" Update _apiMethodsArgs[methodName] with additional optional argument """
74+
75+
allArgs = _getMethodsArgDefinition(methodName)[1]
76+
if not argName in allArgs:
77+
allArgs.append(argName)
78+
6079

6180
def getMethodsWithPositionalArgs():
6281
""" returns a dictionary with method names and there positional args """
@@ -66,3 +85,7 @@ def getMethodsWithPositionalArgs():
6685
if argdef[0]:
6786
positionalArgNames[mname] = argdef[0][:]
6887
return positionalArgNames
88+
89+
def getApiArgsForMethod(methodName):
90+
""" returns list with all argument name api methodName """
91+
return _getMethodsArgDefinition(methodName)[1][:]

test/utest/testlinkapigeneric_offline_test.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def test_decoApiCallWithArgs(self):
210210
def DummyMethod(a_api):
211211
"a dummy api method with 3 positional args and 1 optional arg"
212212
pass
213-
posArgs = testlinkapigeneric.testlinkargs.getMethodsWithPositionalArgs()
213+
posArgs = testlinkapigeneric.getMethodsWithPositionalArgs()
214214
print posArgs
215215
self.assertEqual(['Uno', 'due', 'tre'], posArgs['DummyMethod'])
216216

@@ -227,15 +227,20 @@ def orig_funcname2(a_api):
227227

228228
def test_decoApiCallAddDevKey(self):
229229
" decorator test: argsOptional should be extended with devKey"
230+
testlinkapigeneric.registerMethod('a_func')
230231
@testlinkapigeneric.decoApiCallAddDevKey
231232
def a_func(a_api, *argsPositional, **argsOptional):
232233
return argsPositional, argsOptional
233-
234+
# check method argument definition
235+
allArgs = testlinkapigeneric.getApiArgsForMethod('a_func')
236+
self.assertEqual(['devKey'], allArgs)
237+
# check call arguments
234238
response = a_func(self.api)
235239
self.assertEqual({'devKey' : self.api.devKey}, response[1])
236240

237241
def test_noWrapperName_decoApiCallAddDevKey(self):
238242
" decorator test: original function name should be unchanged "
243+
testlinkapigeneric.registerMethod('orig_funcname3')
239244
@testlinkapigeneric.decoApiCallAddDevKey
240245
def orig_funcname3(a_api, *argsPositional, **argsOptional):
241246
"orig doc string"

test/utest/testlinkargstest.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323

2424
import unittest
25-
from testlink.testlinkapigeneric import testlinkargs
25+
# from testlink.testlinkapigeneric import testlinkargs
26+
from testlink import testlinkargs
2627

2728

2829
class testlinkargsTestCase(unittest.TestCase):
@@ -34,13 +35,13 @@ def setUp(self):
3435
# module under test
3536
self.mut = testlinkargs
3637
# reset the args cache
37-
self.mut.resetRegister()
38+
self.mut._resetRegister()
3839

3940

40-
def test_resetRegister(self):
41+
def test__resetRegister(self):
4142
self.mut._apiMethodsArgs['BigBird'] = 'not a Small Bird'
4243
self.assertIsNotNone(self.mut._apiMethodsArgs.get('BigBird'))
43-
self.mut.resetRegister()
44+
self.mut._resetRegister()
4445
self.assertIsNone(self.mut._apiMethodsArgs.get('BigBird'))
4546

4647
def test_registerMethod(self):
@@ -75,6 +76,32 @@ def test_getMethodsWithPositionalArgs(self):
7576
'Method_1pos_2opt' : ['Uno']},
7677
a_def )
7778

79+
def test_registerMethod_ErrorAlreadyDefined(self):
80+
self.mut.registerMethod('DummyMethod', ['Uno', 'due', 'tre'],
81+
['quad','tre'], ['cinque'])
82+
with self.assertRaisesRegexp(testlinkargs.TLArgError,
83+
'DummyMethod already registered'):
84+
self.mut.registerMethod('DummyMethod')
85+
86+
def test_registerArgOptional(self):
87+
self.mut.registerMethod('DummyMethod', ['Uno', 'due', 'tre'],
88+
['quad','tre'], ['cinque'])
89+
self.mut.registerArgOptional('DummyMethod', 'sei')
90+
a_def = self.mut._apiMethodsArgs['DummyMethod']
91+
self.assertEqual((['Uno', 'due', 'tre'],
92+
['Uno', 'due', 'tre', 'quad', 'sei'],
93+
['cinque']), a_def )
94+
95+
def test_registerArgOptional_ErrorUnknownMethod(self):
96+
with self.assertRaisesRegexp(testlinkargs.TLArgError,
97+
'DummyMethod not registered'):
98+
self.mut.registerArgOptional('DummyMethod', 'sei')
99+
100+
def test_getApiArgsForMethod(self):
101+
self.mut.registerMethod('DummyMethod', ['Uno', 'due', 'tre'],
102+
['quad','tre'], ['cinque'])
103+
allArgs = self.mut.getApiArgsForMethod('DummyMethod')
104+
self.assertEqual(['Uno', 'due', 'tre', 'quad'], allArgs )
78105

79106
if __name__ == "__main__":
80107
#import sys;sys.argv = ['', 'Test.testName']

0 commit comments

Comments
 (0)