def setupDna(self, dedupFieldName="__key__.dedup", dedupSortFieldName="__key__.date", dedupByDefault=True): self.observer = CallTrace('observer', emptyGeneratorMethods=['executeComposedQuery']) self.response = LuceneResponse() def executeComposedQuery(*args, **kwargs): raise StopIteration(self.response) yield self.observer.methods['executeComposedQuery'] = executeComposedQuery self.tree = be( (Observable(), (ConvertToComposedQuery( resultsFrom='defaultCore', matches=[( dict(core='defaultCore', uniqueKey='keyDefault'), dict(core='otherCore', key='keyOther') ), ( dict(core='defaultCore', uniqueKey='key1'), dict(core='aDifferentKore', key='key2') ) ], dedupFieldName=dedupFieldName, dedupSortFieldName=dedupSortFieldName, dedupByDefault=dedupByDefault, drilldownFieldnamesTranslate=lambda name: 'prefix.' + name if name == 'toBePrefixed' else name, ), (self.observer,) ) ) )
def testXTermDrilldown(self): self.response = LuceneResponse(drilldownData=[ dict(fieldname='prefix:field1', terms=[dict(term='term1', count=1)]), dict(fieldname='unknownJoinName.field', terms=[]), dict(fieldname='normal:drilldown', terms=[]), dict(fieldname='prefix:field2', terms=[dict(term='term2', count=1)]), ]) response = retval(self.tree.any.executeQuery( cqlAbstractSyntaxTree=parseCQL('*'), extraArguments={}, facets=[ dict(fieldname='otherCore.prefix:field2', maxTerms=7), dict(fieldname='normal:drilldown', maxTerms=11), dict(fieldname='otherCore.prefix:field1', maxTerms=9), dict(fieldname='unknownJoinName.field', maxTerms=12) ], someKwarg='someValue')) self.assertEquals(['executeComposedQuery'], self.observer.calledMethodNames()) cq = self.observer.calledMethods[0].kwargs['query'] cq.validate() self.assertEquals(set(['defaultCore', 'otherCore']), cq.cores) self.assertEquals('keyDefault', cq.keyName('defaultCore', 'otherCore')) self.assertEquals('keyOther', cq.keyName('otherCore', 'defaultCore')) self.assertEquals([dict(fieldname='prefix:field2', path=[], maxTerms=7), dict(fieldname='prefix:field1', path=[], maxTerms=9)], cq.facetsFor('otherCore')) self.assertEquals([dict(fieldname='normal:drilldown', path=[], maxTerms=11), dict(fieldname='unknownJoinName.field', path=[], maxTerms=12)], cq.facetsFor('defaultCore')) self.assertEquals([ dict(fieldname='otherCore.prefix:field2', terms=[dict(term='term2', count=1)]), dict(fieldname='normal:drilldown', terms=[]), dict(fieldname='otherCore.prefix:field1', terms=[dict(term='term1', count=1)]), dict(fieldname='unknownJoinName.field', terms=[]) ], response.drilldownData)
def drilldownFieldnames(self, path=None, limit=50, **kwargs): args = dict(limit=limit) if path: args["dim"] = path[0] args["path"] = path[1:] args = urlencode(args, doseq=True) fieldnames = (yield self._connect().send(path='/drilldownFieldnames/?{}'.format(args))) raise StopIteration(LuceneResponse(total=len(fieldnames), hits=fieldnames)) yield
def httppost(*args, **kwargs): raise StopIteration('HTTP/1.0 200 Ok\r\n\r\n%s' % LuceneResponse( total=5, hits=[Hit("1"), Hit("2"), Hit("3"), Hit("4"), Hit("5")]).asJson()) yield
def testJson(self): response = LuceneResponse(total=3, hits=['1', '2', '3']) response.drilldownData = [{'terms': [], 'fieldname': 'field'}] response2 = LuceneResponse.fromJson(response.asJson()) self.assertEquals(3, response2.total) self.assertEquals(['1', '2', '3'], response2.hits) self.assertEquals([{ 'terms': [], 'fieldname': 'field' }], response2.drilldownData)
def luceneResponseFromDict(responseDict): hits = [Hit(**hit) for hit in responseDict['hits']] response = LuceneResponse(total=responseDict["total"], queryTime=responseDict["queryTime"], hits=hits, drilldownData=[]) if "totalWithDuplicates" in responseDict: response.totalWithDuplicates = responseDict['totalWithDuplicates'] if "drilldownData" in responseDict: response.drilldownData = responseDict['drilldownData'] if "suggestions" in responseDict: response.suggestions = responseDict['suggestions'] if "times" in responseDict: response.times = responseDict['times'] return response
def prefixSearch(self, fieldname, prefix, showCount=False, limit=10, **kwargs): jsonDict = JsonDict( fieldname=fieldname, prefix=prefix, limit=limit, ) args = urlencode(dict(fieldname=fieldname, prefix=prefix, limit=limit)) responseDict = (yield self._connect().send(jsonDict=jsonDict, path='/prefixSearch/?{}'.format(args))) hits = [((term, count) if showCount else term) for term, count in sorted(responseDict, key=lambda t: t[1], reverse=True)] response = LuceneResponse(total=len(hits), hits=hits) raise StopIteration(response) yield
def testHierarchicalXTermDrilldown(self): self.response = LuceneResponse(drilldownData=[ dict(fieldname='field1', path=['field2'], terms=[dict(term='term1', count=1)]), ]) response = retval(self.tree.any.executeQuery( cqlAbstractSyntaxTree=parseCQL('*'), extraArguments={}, facets=[ dict(fieldname='otherCore.field1>field2', maxTerms=10), ], someKwarg='someValue')) self.assertEquals(['executeComposedQuery'], self.observer.calledMethodNames()) cq = self.observer.calledMethods[0].kwargs['query'] cq.validate() self.assertEquals([{'fieldname': 'field1', 'path': ['field2'], 'maxTerms': 10}], cq.facetsFor('otherCore')) self.assertEquals([ dict(fieldname='otherCore.field1', path=['field2'], terms=[dict(term='term1', count=1)]), ], response.drilldownData)
def fieldnames(self, **kwargs): fieldnames = (yield self._connect().send(path='/fieldnames/')) raise StopIteration(LuceneResponse(total=len(fieldnames), hits=fieldnames)) yield
def fieldnames(**kwargs): raise StopIteration(LuceneResponse(total=2, hits=['aap', 'noot'])) yield
def prefixSearch(**kwargs): raise StopIteration(LuceneResponse(total=2, hits=['aap', 'noot'])) yield
def executeQuery(**kwargs): raise StopIteration(LuceneResponse(total=2, hits=['aap', 'noot'])) yield
def httppost(*args, **kwargs): raise StopIteration( 'HTTP/1.0 200 Ok\r\n\r\n%s' % LuceneResponse(total=2, hits=["field0", "field1"]).asJson()) yield
def httppost(*args, **kwargs): raise StopIteration('HTTP/1.0 200 Ok\r\n\r\n%s' % LuceneResponse( total=5, hits=["1", "2", "3", "4", "5"]).asJson()) yield