# Path: data_catalog/search.py
# class DataSetSearchResource(DataCatalogResource):
# class IndexConnectionError(Exception):
# class DataSetSearch(DataCatalogModel):
#     def __init__(self):
#     def get(self):
#     def __init__(self):
#     def search(self, query, org_uuid_list, dataset_filtering, is_admin):
#     def _extract_metadata(es_query_result):
#     def get_params_from_request_args(args):
#     SEARCH_ERROR_MESSAGE = 'Searching in the index failed'
#     INVALID_QUERY_ERROR_MESSAGE = SEARCH_ERROR_MESSAGE + ': invalid query.'
#     NO_CONNECTION_ERROR_MESSAGE = SEARCH_ERROR_MESSAGE + ': failed to connect to ElasticSearch.'
# 
# Path: tests/base_test.py
# class DataCatalogTestCase(unittest.TestCase):
#     def setUp(self):
#         setup_fake_env()
#         self._config = DCConfig()
# 
#         self.app = data_catalog.app._create_app(self._config)
#         self.app.config['TESTING'] = True
#         self.client = self.app.test_client()
# 
#         self.mocked_authenticate = disable_authentication(self.client)
# 
#     def tearDown(self):
#         clean_fake_env()


import json
import unittest
import flask
from elasticsearch.exceptions import RequestError, ConnectionError
from mock import patch, MagicMock
from data_catalog.search import DataSetSearch, InvalidQueryError, IndexConnectionError
from tests.base_test import DataCatalogTestCase
            index=self._config.elastic.elastic_index,
            doc_type=self._config.elastic.elastic_metadata_type,
            body=TRANSLATED_QUERY)

    def test_search_invalidQuery_invalidQueryErrorRaised(self):
        self._mock_es_search.side_effect = RequestError
        with self.assertRaises(InvalidQueryError):
            self._search_obj.search('an invalid query string', self.fake_org_id, False, False)

    def test_search_noIndexConnection_connectionErrorRaised(self):
        self._mock_es_search.side_effect = ConnectionError
        with self.assertRaises(IndexConnectionError):
            self._search_obj.search('some query string', self.fake_org_id, False, False)

    @patch.object(DataSetSearch, 'search')
    def test_restSearch_withQuery_queryPassedToSearch(self, mock_search):
        flask.g.org_uuid_list = '[orgid001]'
        flask.g.is_admin = False
        test_query = 'fake data catalog query'
        mock_search.return_value = dict(self.test_es_search_results)

        response = self.client.get('{}?query={}&orgs=orgid001'.format('/rest/datasets', test_query))
        org_uuid_list = '[orgid001]'
        self.assertEqual(200, response.status_code)
        self.assertDictEqual(self.test_es_search_results, json.loads(response.data))
        mock_search.assert_called_once_with(test_query, org_uuid_list, None, False)

    @patch.object(DataSetSearch, 'search')
    def test_restSearch_invalidQuery_400Returned(self, mock_search):
        flask.g.is_admin = False

# Path: data_catalog/search.py
# class DataSetSearchResource(DataCatalogResource):
# class IndexConnectionError(Exception):
# class DataSetSearch(DataCatalogModel):
#     def __init__(self):
#     def get(self):
#     def __init__(self):
#     def search(self, query, org_uuid_list, dataset_filtering, is_admin):
#     def _extract_metadata(es_query_result):
#     def get_params_from_request_args(args):
#     SEARCH_ERROR_MESSAGE = 'Searching in the index failed'
#     INVALID_QUERY_ERROR_MESSAGE = SEARCH_ERROR_MESSAGE + ': invalid query.'
#     NO_CONNECTION_ERROR_MESSAGE = SEARCH_ERROR_MESSAGE + ': failed to connect to ElasticSearch.'
# 
# Path: tests/base_test.py
# class DataCatalogTestCase(unittest.TestCase):
#     def setUp(self):
#         setup_fake_env()
#         self._config = DCConfig()
# 
#         self.app = data_catalog.app._create_app(self._config)
#         self.app.config['TESTING'] = True
#         self.client = self.app.test_client()
# 
#         self.mocked_authenticate = disable_authentication(self.client)
# 
#     def tearDown(self):
#         clean_fake_env()


# Path: tests/test_search.py
import json
import unittest
import flask
from elasticsearch.exceptions import RequestError, ConnectionError
from mock import patch, MagicMock
from data_catalog.search import DataSetSearch, InvalidQueryError, IndexConnectionError
from tests.base_test import DataCatalogTestCase

            index=self._config.elastic.elastic_index,
            doc_type=self._config.elastic.elastic_metadata_type,
            body=TRANSLATED_QUERY)

    def test_search_invalidQuery_invalidQueryErrorRaised(self):
        self._mock_es_search.side_effect = RequestError
        with self.assertRaises(InvalidQueryError):
            self._search_obj.search('an invalid query string', self.fake_org_id, False, False)

    def test_search_noIndexConnection_connectionErrorRaised(self):
        self._mock_es_search.side_effect = ConnectionError
        with self.assertRaises(IndexConnectionError):
            self._search_obj.search('some query string', self.fake_org_id, False, False)

    @patch.object(DataSetSearch, 'search')
    def test_restSearch_withQuery_queryPassedToSearch(self, mock_search):
        flask.g.org_uuid_list = '[orgid001]'
        flask.g.is_admin = False
        test_query = 'fake data catalog query'
        mock_search.return_value = dict(self.test_es_search_results)

        response = self.client.get('{}?query={}&orgs=orgid001'.format('/rest/datasets', test_query))
        org_uuid_list = '[orgid001]'
        self.assertEqual(200, response.status_code)
        self.assertDictEqual(self.test_es_search_results, json.loads(response.data))
        mock_search.assert_called_once_with(test_query, org_uuid_list, None, False)

    @patch.object(DataSetSearch, 'search')
    def test_restSearch_invalidQuery_400Returned(self, mock_search):
        flask.g.is_admin = False

        mock_search.side_effect = InvalidQueryError