def testSetHandler(self): api = iApi() assert len(api._apiHandlers) == 0 NS = "namespace" def myHandler(): pass api.setHandler(NS, myHandler) assert NS in api._apiHandlers assert len(api._apiHandlers) == 1
def testGetHandlerWhenNoneSet(self): api = iApi() assert len(api._apiHandlers) == 0 NS = "namespace" try: api.getHandler(NS) except UnsupportedApiError, e: assert e.ns() == NS
def testSetInvalidTransportDataReceiveListener(self): api = iApi() myListener = object() try: api.transportDataReceiveListener = myListener except ApiParamError, e: assert e.item == myListener assert iIpcTransportDataReceiveListener in e.allowedTypes
def testGethandler(self): api = iApi() assert len(api._apiHandlers) == 0 NS = "namespace" def myHandler(): pass api.setHandler(NS, myHandler) assert len(api._apiHandlers) == 1 handler = api.getHandler(NS) assert handler == myHandler
def testInvalidIpc(self): ipc = object() api = iApi() try: api.ipc = ipc except ApiParamError, e: assert e.item == ipc assert iIpcTransport in e.allowedTypes assert len(e.allowedTypes) == 1
def testSetInvalidHandler(self): api = iApi() assert len(api._apiHandlers) == 0 def myHandler(): pass try: api.setHandler(None, myHandler) except ApiParamError, e: assert e.item == None assert basestring in e.allowedTypes assert len(e.allowedTypes) == 1
def testGetCatchallHandlerAsFallback(self): api = iApi() assert len(api._apiHandlers) == 0 NS = iApi.CATCHALL def myHandler(): pass api.setHandler(NS, myHandler) assert len(api._apiHandlers) == 1 dNS = "some.namespace" handler = api.getHandler(dNS) assert handler == myHandler
def testSyncFuncRaisesNoResponseRequired(self): api = iApi(ipc=self.myIpc) tId = 1 def func(*args, **kwargs): raise NoResponseRequired(tId) sync = True try: api._handleStandardCall(tId, sync, func) except NoResponseRequired, _e: assert True
def testGetHandlerNsIsNone(self): api = iApi() assert len(api._apiHandlers) == 0 NS = None def myHandler(): pass try: api.getHandler(NS) except ApiParamError, e: assert e.item == NS assert basestring in e.allowedTypes assert len(e.allowedTypes) == 1
def testGetHandlerWithCatchallHandlerAsFallback(self): api = iApi() assert len(api._apiHandlers) == 0 NS = iApi.CATCHALL def myHandler(): pass def myOtherHandler(): pass api.setHandler(NS, myHandler) assert len(api._apiHandlers) == 1 eNS = "hello.world!" api.setHandler(eNS, myOtherHandler) assert len(api._apiHandlers) == 2 handler = api.getHandler(eNS) assert handler == myOtherHandler
def testSyncFuncRaisesCustomException(self): api = iApi(ipc=self.myIpc) eResult = CustomException(123) def func(*args, **kwargs): self.funcData["args"] = args self.funcData["kwargs"] = kwargs raise eResult tId = 1 sync = True try: api._handleStandardCall(tId, sync, func, *self.args, **self.kwargs) except CustomException, e: assert e == eResult
def testSyncFuncReturnsCustomData(self): api = iApi(ipc=self.myIpc) eResult = 123 def func(*args, **kwargs): self.funcData["args"] = args self.funcData["kwargs"] = kwargs return eResult tId = 1 sync = True r = api._handleStandardCall(tId, sync, func, *self.args, **self.kwargs) assert isinstance(r, iApiTransportResponse) assert r.response() == eResult assert self.funcData["args"] == self.args assert self.funcData["kwargs"] == self.kwargs
def _checkNChunks(self, count): api = iApi(ipc=self.myIpc) eResult = CustomException(123) def func(*args, **kwargs): self.funcData["args"] = args self.funcData["kwargs"] = kwargs raise eResult tId = 123 chunks = [] for i in range(1, count + 1): chunks.append([i for i in range(1, i + 1)]) combinerMethod = ("hello", "world!") try: api._returnChunks(tId, chunks, combinerMethod, lambda x, index: MyResult(x[index], index)) except NoResponseRequired, _e: assert True
def testAsyncFuncReturnsCustomData(self): api = iApi(ipc=self.myIpc) eResult = 123 def func(*args, **kwargs): self.funcData["args"] = args self.funcData["kwargs"] = kwargs return eResult tId = 1 sync = False api._handleStandardCall(tId, sync, func, *self.args, **self.kwargs) assert self.myIpc.dataSentLock.acquire(timeout=1) assert len(self.myIpc.dataSent) == 1 assert self.myIpc.dataSent[0][0] == tId d = self.myIpc.dataSent[0][1] assert isinstance(d, ApiTransportResponse) assert d.response() == eResult assert self.funcData["args"] == self.args assert self.funcData["kwargs"] == self.kwargs
def testSetTransportStateChangeListener(self): api = iApi() myListener = MyTransportListener() api.transportStateChangeListener = myListener assert api.transportStateChangeListener == myListener
def testSetTransportDataReceiveListener(self): api = iApi() myListener = MyTransportListener() api.transportDataReceiveListener = myListener assert api.transportDataReceiveListener == myListener
def testValidIpc(self): ipc = MyTransport() api = iApi() api.ipc = ipc assert api.ipc == ipc
def setUp(self): self.myIpc = MyTransport() self.args = (345, "hello", "world!") self.kwargs = {"i.am":"an.arg"} self.funcData = {} self.myApi = iApi(ipc=self.myIpc)
def testGetEventsToHandle(self, api=iApi()): api = iApi() events = api.getEventsToHandle() assert isinstance(events, list) assert len(events) == 0