def setUp(self): provideUtility(SolrConnectionConfig(), ISolrConnectionConfig) self.mngr = SolrConnectionManager() self.mngr.setHost(active=True) self.conn = self.mngr.getConnection() self.search = Search() self.search.manager = self.mngr
def testFacetDependencies(self): cfg = SolrConnectionConfig() provideUtility(cfg, ISolrConnectionConfig) # dependency info can be set via the configuration utility... cfg.facets = ['foo:bar'] context = Dummy() request = {} view = DummyView(context, request) self.assertEqual(facetParameters(view), (['foo:bar'], dict(foo=['bar']))) # overridden on the context context.facet_fields = ['bar:foo'] self.assertEqual(facetParameters(view), (['bar:foo'], dict(bar=['foo']))) # and via the request request['facet.field'] = ['foo:bar', 'bar:foo'] self.assertEqual( facetParameters(view), (['foo:bar', 'bar:foo'], dict(foo=['bar'], bar=['foo'])) ) # white space shouldn't matter request['facet.field'] = ['foo : bar', 'bar :foo'] self.assertEqual( facetParameters(view), (['foo : bar', 'bar :foo'], dict(foo=['bar'], bar=['foo'])) ) # clean up... getGlobalSiteManager().unregisterUtility(cfg, ISolrConnectionConfig)
def test_fallback_search_solr(self): """Should work as test_fallback_search, but based on the native solr search utility """ pc = api.portal.get_tool("portal_catalog") mock_results = SolrResponse() mock_results.response = pc({"path": {"query": "/plone/en-de"}}) mock_search = MagicMock(return_value=mock_results) mock_search.getManager = lambda: SolrConnectionManager(active=True) from zope.interface import alsoProvides from plone.indexer.interfaces import IIndexableObject alsoProvides(mock_search, IIndexableObject) sm = self.portal.getSiteManager() sm.unregisterUtility(provided=ISearch) sm.unregisterUtility(provided=ISolrConnectionConfig) sm.registerUtility(component=SolrConnectionConfig(), provided=ISolrConnectionConfig) sm.registerUtility(component=mock_search, provided=ISearch) lf_search_view = self.portal.restrictedTraverse( "@@language-fallback-search") results = lf_search_view.search_solr("path_parents:/plone/events") self.assertEqual( set([x.getPath() for x in results]), set([ '/plone/en-de', '/plone/en-de/en-event', '/plone/en/notrans-event' ])) mock_search.search.assert_called_with( "path_parents:/plone/events +Language:en OR all OR de")
def setUp(self): provideUtility(SolrConnectionConfig(), ISolrConnectionConfig) self.mngr = SolrConnectionManager() self.mngr.setHost(active=True) conn = self.mngr.getConnection() fakehttp(conn, getData('schema.xml')) # fake schema response self.mngr.getSchema() # read and cache the schema self.proc = SolrIndexProcessor(self.mngr)
def setUp(self): provideUtility(SolrConnectionConfig(), ISolrConnectionConfig) self.mngr = SolrConnectionManager() self.mngr.setHost(active=True) self.conn = self.mngr.getConnection() self.proc = SolrIndexProcessor(self.mngr) self.log = [] # catch log messages... def logger(*args): self.log.extend(args) logger_indexer.warning = logger
def setUp(self): provideUtility(SolrConnectionConfig(), ISolrConnectionConfig) self.mngr = SolrConnectionManager() self.mngr.setHost(active=True) conn = self.mngr.getConnection() fakehttp(conn, getData('plone_schema.xml')) # fake schema response self.mngr.getSchema() # read and cache the schema self.search = Search() self.search.manager = self.mngr # Patch buildQuery method self.search.buildQuery = buildQuery.__get__(self.search, self.search.__class__)
def testFilterQuerySubstitution(self): def optimize(**params): query = dict(a='a:23', b='b:42', c='c:(23 42)') optimizeQueryParameters(query, params) return query, params # first test without the configuration utility self.assertEqual(optimize(), (dict(a='a:23', b='b:42', c='c:(23 42)'), dict())) # now unconfigured... config = SolrConnectionConfig() provideUtility(config, ISolrConnectionConfig) self.assertEqual(optimize(), (dict(a='a:23', b='b:42', c='c:(23 42)'), dict())) config.filter_queries = ['a'] self.assertEqual(optimize(), (dict(b='b:42', c='c:(23 42)'), dict(fq=['a:23']))) self.assertEqual( optimize(fq='x:13'), (dict(b='b:42', c='c:(23 42)'), dict(fq=['x:13', 'a:23']))) self.assertEqual( optimize(fq=['x:13', 'y:17']), (dict(b='b:42', c='c:(23 42)'), dict(fq=['x:13', 'y:17', 'a:23']))) config.filter_queries = ['a', 'c'] self.assertEqual(optimize(), (dict(b='b:42'), dict(fq=['a:23', 'c:(23 42)']))) self.assertEqual( optimize(fq='x:13'), (dict(b='b:42'), dict(fq=['x:13', 'a:23', 'c:(23 42)']))) self.assertEqual( optimize(fq=['x:13', 'y:17']), (dict(b='b:42'), dict(fq=['x:13', 'y:17', 'a:23', 'c:(23 42)']))) # also test substitution of combined filter queries config.filter_queries = ['a c'] self.assertEqual(optimize(), (dict(b='b:42'), dict(fq=['a:23 c:(23 42)']))) config.filter_queries = ['a c', 'b'] self.assertEqual(optimize(), ({ '*': '*:*' }, dict(fq=['a:23 c:(23 42)', 'b:42']))) # for multiple matches the first takes precedence config.filter_queries = ['a', 'a c', 'b'] self.assertEqual(optimize(), (dict(c='c:(23 42)'), dict(fq=['a:23', 'b:42']))) # parameters not contained in the query must not be converted config.filter_queries = ['a nonexisting', 'b'] self.assertEqual(optimize(), (dict(a='a:23', c='c:(23 42)'), dict(fq=['b:42'])))
def testLocalConnections(self): provideUtility(SolrConnectionConfig(), ISolrConnectionConfig) mngr = SolrConnectionManager(active=True) proc = SolrIndexProcessor(mngr) mngr.setHost(active=True) schema = getData('schema.xml') log = [] def runner(): fakehttp(mngr.getConnection(), schema) # fake schema response # read and cache the schema mngr.getSchema() response = getData('add_response.txt') # fake add response output = fakehttp(mngr.getConnection(), response) # indexing sends data proc.index(Foo(id='500', name='python test doc')) mngr.closeConnection() log.append(str(output)) log.append(proc) log.append(mngr.getConnection()) # after the runner was set up, another thread can be created and # started; its output should contain the proper indexing request, # whereas the main thread's connection remain idle; the latter # cannot be checked directly, but the connection object would raise # an exception if it was used to send a request without setting up # a fake response beforehand... thread = Thread(target=runner) thread.start() thread.join() conn = mngr.getConnection() # get this thread's connection fakehttp(conn, schema) # fake schema response mngr.getSchema() # read and cache the schema mngr.closeConnection() mngr.setHost(active=False) self.assertEqual(len(log), 3) self.assertEqual(sortFields(log[0]), getData('add_request.txt')) self.failUnless(isinstance(log[1], SolrIndexProcessor)) self.failUnless(isinstance(log[2], SolrConnection)) self.failUnless(isinstance(proc, SolrIndexProcessor)) self.failUnless(isinstance(conn, SolrConnection)) self.assertEqual(log[1], proc) # processors should be the same... self.assertNotEqual(log[2], conn) # but not the connections
def testFacetParameters(self): context = Dummy() request = {} # with nothing set up, no facets will be returned self.assertEqual(facetParameters(context, request), ([], {})) # setting up the regular config utility should give the default value cfg = SolrConnectionConfig() provideUtility(cfg, ISolrConnectionConfig) self.assertEqual(facetParameters(context, request), ([], {})) # so let's set it... cfg.facets = ['foo'] self.assertEqual(facetParameters(context, request), (['foo'], {})) # override the setting on the context context.facet_fields = ['bar'] self.assertEqual(facetParameters(context, request), (['bar'], {})) # and again via the request request['facet.field'] = ['foo', 'bar'] self.assertEqual(facetParameters(context, request), (['foo', 'bar'], {})) # clean up... getGlobalSiteManager().unregisterUtility(cfg, ISolrConnectionConfig)
def setUp(self): self.config = SolrConnectionConfig() provideUtility(self.config, ISolrConnectionConfig)
def testUnavailableSchema(self): provideUtility(SolrConnectionConfig(), ISolrConnectionConfig) search = Search() search.manager = SolrConnectionManager() self.assertEqual(search.buildQuery('foo'), {}) self.assertEqual(search.buildQuery(name='foo'), {})
def setUp(self): provideUtility(SolrConnectionConfig(), ISolrConnectionConfig) self.foo = Foo(id='500', name='python test doc') self.schema_request = 'GET /solr/admin/file/?file=schema.xml'