def testRequestScopeIsPerRequest(self): class MyObserver(Observable): def handleRequest(self, key, value, *args, **kwargs): self.do.setArg(key, value) yield self.call.getArg() class SetArgObserver(Observable): def setArg(self, key, value): self.ctx.requestScope[key] = value class GetArgObserver(Observable): def getArg(self): return ';'.join( '%s=%s' % (k, v) for k, v in list(self.ctx.requestScope.items())) dna = be((Observable(), (RequestScope(), (MyObserver(), (SetArgObserver(), ), (GetArgObserver(), ))))) result0 = list(compose(dna.all.handleRequest("key0", "value0"))) result1 = list(compose(dna.all.handleRequest("key1", "value1"))) self.assertEqual(['key0=value0'], result0) self.assertEqual(['key1=value1'], result1)
def testRequestScopeForEveryMethod(self): resultByDo = [] class MyObserver(Observable): def someAnyMethod(self, key, value, *args, **kwargs): self.do.setArg(key, value) result = self.call.getArg(key) return result yield def someAllMethod(self, key, value, *args, **kwargs): self.do.setArg(key, value) yield self.call.getArg(key) def someCallMethod(self, key, value, *args, **kwargs): self.do.setArg(key, value) return self.call.getArg(key) def someDoMethod(self, key, value, *args, **kwargs): self.do.setArg(key, value) resultByDo.append(self.call.getArg(key)) class SetArgObserver(Observable): def setArg(self, key, value): self.ctx.requestScope[key] = value class GetArgObserver(Observable): def getArg(self, key): return self.ctx.requestScope[key] dna = be((Observable(), (RequestScope(), (MyObserver(), (SetArgObserver(), ), (GetArgObserver(), ))))) try: next(compose(dna.any.someAnyMethod(key='anykey', value='anyvalue'))) self.fail() except StopIteration as e: self.assertEqual('anyvalue', e.args[0]) self.assertEqual( ['allvalue'], list(compose(dna.all.someAllMethod(key='allkey', value='allvalue')))) dna.do.someDoMethod(key='dokey', value='dovalue') self.assertEqual(['dovalue'], resultByDo) self.assertEqual( 'callvalue', dna.call.someCallMethod(key='callkey', value='callvalue'))
def testEverythingIsPassed(self): usedArgsKwargs = [] class MyObserver(Observable): def handleRequest(innerself, *args, **kwargs): usedArgsKwargs.append((args, kwargs)) yield 'result' dna = be((Observable(), (RequestScope(), (MyObserver(), )))) result = list( compose( dna.all.handleRequest( "an arg", RequestURI='http://www.example.org/path'))) self.assertEqual(['result'], result) self.assertEqual( [(("an arg", ), dict(RequestURI='http://www.example.org/path'))], usedArgsKwargs)
def testRequestScopeIsAvailable(self): class MyObserver(Observable): def handleRequest(self, *args, **kwargs): self.do.setArg() yield self.call.getArg() class SetArgObserver(Observable): def setArg(self): self.ctx.requestScope["arg"] = "value" class GetArgObserver(Observable): def getArg(self): return self.ctx.requestScope["arg"] dna = be((Observable(), (RequestScope(), (MyObserver(), (SetArgObserver(), ), (GetArgObserver(), ))))) result = list(compose(dna.all.handleRequest("a request"))) self.assertEqual(['value'], result)