def test_response_from_search_request(self): body = get_data('search.json') resp = SolrResponse(body=body, status=200) self.assertTrue(resp.is_ok()) self.assertEqual(len(resp.docs), 3) self.assertEqual(resp.num_found, 3) self.assertEqual(resp.start, 0)
def setUp(self): super(SolrMockupTestCase, self).setUp() assert self.schema is not None, 'A path for Solr schema is needed' conn = MagicMock(name='SolrConnection') conn.get = MagicMock(name='get', return_value=SolrResponse( body=self.schema, status=200, )) manager = MagicMock(name='SolrConnectionManager') manager.connection = conn manager.schema = SolrSchema(manager) solr = getUtility(ISolrSearch) solr._manager = manager assert self.search is not None, 'A path for Solr search is needed' solr.search = MagicMock(name='search', return_value=SolrResponse( body=self.search, status=200, )) self.solr = solr self.source = UsersContactsInboxesSource(self.portal)
def test_response_from_bad_request(self): body = get_data('bad_request.json') resp = SolrResponse(body=body, status=400) self.assertFalse(resp.is_ok()) self.assertEqual(resp.http_status, 400) self.assertEqual(resp.status, 400) self.assertEqual(resp[u'error'][u'code'], 400)
def mock_solr(self, response_file): conn = MagicMock(name='SolrConnection') schema_resp = assets.load('solr_schema.json') conn.get = MagicMock(name='get', return_value=SolrResponse(body=schema_resp, status=200)) manager = MagicMock(name='SolrConnectionManager') manager.connection = conn manager.schema = SolrSchema(manager) solr = getUtility(ISolrSearch) solr._manager = manager search_resp = assets.load(response_file) solr.search = MagicMock(name='search', return_value=SolrResponse(body=search_resp, status=200)) return solr
def setUp(self): super(TestSolrSearch, self).setUp() self.search = self.portal.unrestrictedTraverse('@@search') conn = MagicMock(name='SolrConnection') schema_resp = open(os.path.join( os.path.dirname(__file__), 'data', 'solr_schema.json'), 'r').read() conn.get = MagicMock(name='get', return_value=SolrResponse( body=schema_resp, status=200)) manager = MagicMock(name='SolrConnectionManager') manager.connection = conn manager.schema = SolrSchema(manager) solr = getUtility(ISolrSearch) solr._manager = manager search_resp = open(os.path.join( os.path.dirname(__file__), 'data', 'solr_search.json'), 'r').read() solr.search = MagicMock(name='search', return_value=SolrResponse( body=search_resp, status=200)) self.solr = solr
def setUp(self): super(TestListingEndpointWithSolr, self).setUp() # Mock Solr connection self.conn = Mock(name='SolrConnection') self.conn.search.return_value = SolrResponse() self.solr = getUtility(ISolrSearch) self._manager = self.solr._manager self.solr._manager = Mock(name='SolrConnectionManager') self.solr.manager.connection = self.conn
def setUp(self): super(TestSolrSearch, self).setUp() conn = MagicMock(name='SolrConnection') schema_resp = open(os.path.join( pkg_path, 'opengever', 'base', 'tests', 'data', 'solr_schema.json'), 'r').read() conn.get = MagicMock(name='get', return_value=SolrResponse( body=schema_resp, status=200)) manager = MagicMock(name='SolrConnectionManager') manager.connection = conn manager.schema = SolrSchema(manager) solr = getUtility(ISolrSearch) solr._manager = manager search_resp = open(os.path.join( pkg_path, 'opengever', 'base', 'tests', 'data', 'solr_search.json'), 'r').read() solr.search = MagicMock(name='search', return_value=SolrResponse( body=search_resp, status=200)) self.solr = solr self.source = GeverCatalogTableSource( BaseCatalogListingTab(self.portal, self.request), self.request)
def mock_solr(self, response_file=None, response_json=None): conn = MagicMock(name='SolrConnection') schema_resp = assets.load('solr_schema.json') conn.get = MagicMock(name='get', return_value=SolrResponse( body=schema_resp, status=200)) manager = MagicMock(name='SolrConnectionManager') manager.connection = conn manager.schema = SolrSchema(manager) solr = getUtility(ISolrSearch) solr._manager = manager if response_file: search_resp = assets.load(response_file) else: # Make sure status is in header if u'responseHeader' not in response_json: response_json[u'responseHeader'] = {} if 'status' not in response_json[u'responseHeader']: response_json[u'responseHeader']['status'] = 0 search_resp = json.dumps(response_json) solr.search = MagicMock(name='search', return_value=SolrResponse( body=search_resp, status=200)) return solr
def test_contentlisting_returns_og_types(self): body = """{ "responseHeader": {"status": 0}, "response": { "numFound": 1, "start": 0, "docs": [{ "UID": "85bed8c49f6d4f8b841693c6a7c6cff1", "Title": "My Item" }] } }""" resp = SolrResponse(body=body, status=200) listing = OGSolrContentListing(resp) self.assertTrue(isinstance(listing[0], OGSolrContentListingObject)) self.assertTrue(isinstance(listing[0].doc, OGSolrDocument))
def test_response_from_exception(self): exception = socket.error(61, 'Connection refused') resp = SolrResponse(exception=exception) self.assertFalse(resp.is_ok()) self.assertEqual(resp.exception, exception)
def test_response_from_schema_request(self): body = get_data('schema.json') resp = SolrResponse(body=body, status=200) self.assertTrue(resp.is_ok()) self.assertIn('schema', resp)
def test_response_from_search_with_highlighting(self): body = get_data('highlighting.json') resp = SolrResponse(body=body, status=200) self.assertTrue(resp.is_ok()) self.assertIn('highlighting', resp)
def test_response_from_bad_body(self): resp = SolrResponse(body='bad', status=200) self.assertFalse(resp.is_ok()) self.assertEqual(resp.status, -1)