Best Python code snippet using autotest_python
test_serviceHandler.py
Source:test_serviceHandler.py
...35 36 def findServiceEndpoint(self, name):37 self._foundServiceEndpoint=True38 return jsonrpc.ServiceHandler.findServiceEndpoint(self, name)39 def invokeServiceEndpoint(self, meth, params):40 self._invokedEndpoint=True41 return jsonrpc.ServiceHandler.invokeServiceEndpoint(self, meth, params)42 def translateResult(self, result, error, id_):43 self._resultTranslated=True44 return jsonrpc.ServiceHandler.translateResult(self, result, error, id_)45class TestServiceHandler(unittest.TestCase):46 def setUp(self):47 self.service = Service()48 49 def tearDown(self):50 pass51 def test_RequestProcessing(self):52 handler = Handler(self.service)53 json=jsonrpc.dumps({"method":"echo", 'params':['foobar'], 'id':''})54 55 result = handler.handleRequest(json)56 self.assert_(handler._requestTranslated)57 self.assert_(handler._foundServiceEndpoint)58 self.assert_(handler._invokedEndpoint)59 self.assert_(handler._resultTranslated)60 def test_translateRequest(self):61 handler = Handler(self.service)62 json=jsonrpc.dumps({"method":"echo", 'params':['foobar'], 'id':''})63 req = handler.translateRequest(json)64 self.assertEquals(req['method'], "echo")65 self.assertEquals(req['params'],['foobar'])66 self.assertEquals(req['id'],'')67 def test_findServiceEndpoint(self):68 handler = Handler(self.service)69 self.assertRaises(jsonrpc.ServiceMethodNotFound, handler.findServiceEndpoint, "notfound")70 self.assertRaises(jsonrpc.ServiceMethodNotFound, handler.findServiceEndpoint, "not_a_serviceMethod")71 meth = handler.findServiceEndpoint("echo")72 self.assertEquals(self.service.echo, meth)73 def test_invokeEndpoint(self):74 handler = Handler(self.service)75 meth = handler.findServiceEndpoint("echo")76 rslt = handler.invokeServiceEndpoint(meth, ['spam'])77 self.assertEquals(rslt, 'spam')78 def test_translateResults(self):79 handler=Handler(self.service)80 data=handler.translateResult("foobar", None, "spam")81 self.assertEquals(jsonrpc.loads(data), {"result":"foobar","id":"spam","error":None})82 def test_translateError(self):83 handler=Handler(self.service)84 exc = Exception()85 data=handler.translateResult(None, exc, "id")86 self.assertEquals(jsonrpc.loads(data), {"result":None,"id":"id","error":{"name":"Exception", "message":""}})87 def test_translateUnencodableResults(self):88 handler=Handler(self.service)89 data=handler.translateResult(self, None, "spam")90 self.assertEquals(jsonrpc.loads(data), {"result":None,"id":"spam","error":{"name":"JSONEncodeException", "message":"Result Object Not Serializable"}})...
serviceHandler.py
Source:serviceHandler.py
...54 except Exception, e:55 err = e56 if err == None:57 try:58 result = self.invokeServiceEndpoint(meth, args)59 except Exception, e:60 err = e61 resultdata = self.translateResult(result, err, id_)62 return resultdata63 def translateRequest(self, data):64 try:65 req = loads(data)66 except:67 raise ServiceRequestNotTranslatable(data)68 return req69 70 def findServiceEndpoint(self, name):71 try:72 meth = getattr(self.service, name)73 if getattr(meth, "IsServiceMethod"):74 return meth75 else:76 raise ServiceMethodNotFound(name)77 except AttributeError:78 raise ServiceMethodNotFound(name)79 def invokeServiceEndpoint(self, meth, args):80 return meth(*args)81 def translateResult(self, rslt, err, id_):82 if err != None:83 err = {"name": err.__class__.__name__, "message":err.message}84 rslt = None85 try:86 data = dumps({"result":rslt,"id":id_,"error":err})87 except Exception, e:88 err = {"name": "Exception", "message":"Result Object Not Serializable"}89 data = dumps({"result":None, "id":id_,"error":err})90 ...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!