def testCollectLog(self): handler = SruHandler(enableCollectLog=True) observer = CallTrace('observer', emptyGeneratorMethods=['echoedExtraRequestData', 'extraResponseData']) __callstack_var_logCollector__ = dict() times = [1, 2.5, 3.5] def timeNow(): return times.pop(0) handler._timeNow = timeNow def executeQuery(**kwargs): response = Response(total=0, hits=[]) response.queryTime=5 raise StopIteration(response) yield observer.methods['executeQuery'] = executeQuery handler.addObserver(observer) arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema') consume(handler.searchRetrieve(sruArguments=arguments, **arguments)) self.assertEquals({ 'sru': { 'handlingTime': [Decimal('2.500')], 'queryTime': [Decimal('1.500')], 'indexTime': [Decimal('0.005')], 'numberOfRecords': [0], 'arguments': [{ 'startRecord': 11, 'query': 'query', 'recordPacking': 'string', 'maximumRecords': 15, 'recordSchema': 'schema', }], } }, __callstack_var_logCollector__)
def testCollectLogWhenIndexRaisesError(self): handler = SruHandler(enableCollectLog=True) observer = CallTrace('observer', emptyGeneratorMethods=['echoedExtraRequestData', 'extraResponseData', 'additionalDiagnosticDetails']) __callstack_var_logCollector__ = dict() times = [1] def timeNow(): return times.pop(0) handler._timeNow = timeNow def executeQuery(**kwargs): raise Exception('Sorry') yield observer.methods['executeQuery'] = executeQuery handler.addObserver(observer) arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema') consume(handler.searchRetrieve(sruArguments=arguments, **arguments)) self.assertEquals({ 'sru': { 'arguments': [{ 'startRecord': 11, 'query': 'query', 'recordPacking': 'string', 'maximumRecords': 15, 'recordSchema': 'schema', }], } }, __callstack_var_logCollector__)
def testQueryTimeInExtraResponse(self): handler = SruHandler(includeQueryTimes=True) observer = CallTrace('observer', emptyGeneratorMethods=['echoedExtraRequestData', 'extraResponseData']) times = [1, 2.5, 3.5] def timeNow(): return times.pop(0) handler._timeNow = timeNow def executeQuery(**kwargs): response = Response(total=0, hits=[]) response.queryTime=5 raise StopIteration(response) yield observer.methods['executeQuery'] = executeQuery handler.addObserver(observer) arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema') result = "".join(compose(handler.searchRetrieve(sruArguments=arguments, **arguments))) sruResponse = parse(StringIO(result)) extraResponseData = sruResponse.xpath('/srw:searchRetrieveResponse/srw:extraResponseData', namespaces={'srw':"http://www.loc.gov/zing/srw/"})[0] self.assertEqualsWS("""<srw:extraResponseData %(xmlns_srw)s %(xmlns_diag)s %(xmlns_xcql)s %(xmlns_dc)s %(xmlns_meresco_srw)s> <querytimes xmlns="http://meresco.org/namespace/timing"> <sruHandling>PT2.500S</sruHandling> <sruQueryTime>PT1.500S</sruQueryTime> <index>PT0.005S</index> </querytimes> </srw:extraResponseData>""" % namespaces, lxmltostring(extraResponseData)) queryTimes = lxmltostring(extraResponseData.xpath('//ti:querytimes', namespaces={'ti':"http://meresco.org/namespace/timing"})[0]) assertValid(queryTimes, join(schemasPath, 'timing-20120827.xsd')) self.assertEquals(['executeQuery', 'echoedExtraRequestData', 'extraResponseData', 'handleQueryTimes'], observer.calledMethodNames()) self.assertEquals({'sru': Decimal("2.500"), 'queryTime': Decimal("1.500"), 'index': Decimal("0.005")}, observer.calledMethods[3].kwargs)