Unverified Commit 70cdf1db authored by Tamika Tannis's avatar Tamika Tannis Committed by GitHub

Update search_table endpoint (#400)

* Update search_table endpoint

* Update test docstring
parent 5b32e160
......@@ -11,7 +11,7 @@ from flask.blueprints import Blueprint
from amundsen_application.log.action_log import action_logging
from amundsen_application.api.utils.request_utils import get_query_param, request_search
from amundsen_application.api.utils.search_utils import generate_query_json, map_table_result
from amundsen_application.api.utils.search_utils import generate_query_json, has_filters, map_table_result
from amundsen_application.models.user import load_user, dump_user
LOGGER = logging.getLogger(__name__)
......@@ -20,7 +20,8 @@ REQUEST_SESSION_TIMEOUT_SEC = 3
search_blueprint = Blueprint('search', __name__, url_prefix='/api/search/v0')
SEARCH_TABLE_ENDPOINT = '/search_table'
SEARCH_TABLE_ENDPOINT = '/search'
SEARCH_TABLE_FILTER_ENDPOINT = '/search_table'
SEARCH_USER_ENDPOINT = '/search_user'
......@@ -73,6 +74,8 @@ def _search_table(*, search_term: str, page_index: int, filters: Dict, search_ty
'tables': tables,
}
try:
if has_filters(filters=filters):
try:
query_json = generate_query_json(filters=filters, page_index=page_index, search_term=search_term)
except Exception as e:
......@@ -81,12 +84,16 @@ def _search_table(*, search_term: str, page_index: int, filters: Dict, search_ty
logging.exception(message)
return results_dict
try:
url = app.config['SEARCHSERVICE_BASE'] + SEARCH_TABLE_ENDPOINT
response = request_search(url=url,
url_base = app.config['SEARCHSERVICE_BASE'] + SEARCH_TABLE_FILTER_ENDPOINT
response = request_search(url=url_base,
headers={'Content-Type': 'application/json'},
method='POST',
data=json.dumps(query_json))
else:
url_base = app.config['SEARCHSERVICE_BASE'] + SEARCH_TABLE_ENDPOINT
url = f'{url_base}?query_term={search_term}&page_index={page_index}'
response = request_search(url=url)
status_code = response.status_code
if status_code == HTTPStatus.OK:
results_dict['msg'] = 'Success'
......
......@@ -51,3 +51,10 @@ def generate_query_json(*, filters: Dict = {}, page_index: int, search_term: str
},
'query_term': search_term
}
def has_filters(*, filters: Dict = {}) -> bool:
for category in valid_search_fields:
if filters.get(category) is not None:
return True
return False
......@@ -6,7 +6,7 @@ from http import HTTPStatus
from unittest.mock import patch
from amundsen_application import create_app
from amundsen_application.api.search.v0 import SEARCH_TABLE_ENDPOINT, SEARCH_USER_ENDPOINT
from amundsen_application.api.search.v0 import SEARCH_TABLE_ENDPOINT, SEARCH_TABLE_FILTER_ENDPOINT, SEARCH_USER_ENDPOINT
local_app = create_app('amundsen_application.config.TestConfig', 'tests/templates')
......@@ -45,11 +45,12 @@ MOCK_PARSED_TABLE_RESULTS = [
]
class SearchTableQueryString(unittest.TestCase):
class SearchTable(unittest.TestCase):
def setUp(self) -> None:
self.mock_table_results = MOCK_TABLE_RESULTS
self.expected_parsed_table_results = MOCK_PARSED_TABLE_RESULTS
self.search_service_url = local_app.config['SEARCHSERVICE_BASE'] + SEARCH_TABLE_ENDPOINT
self.search_service_filter_url = local_app.config['SEARCHSERVICE_BASE'] + SEARCH_TABLE_FILTER_ENDPOINT
self.fe_flask_endpoint = '/api/search/v0/table'
def test_fail_if_term_is_none(self) -> None:
......@@ -82,7 +83,10 @@ class SearchTableQueryString(unittest.TestCase):
test_term = 'hello'
test_index = 1
test_search_type = 'test'
responses.add(responses.POST, self.search_service_url, json=self.mock_table_results, status=HTTPStatus.OK)
responses.add(responses.POST,
self.search_service_filter_url,
json=self.mock_table_results,
status=HTTPStatus.OK)
with local_app.test_client() as test:
test.post(self.fe_flask_endpoint,
......@@ -97,17 +101,22 @@ class SearchTableQueryString(unittest.TestCase):
search_type=test_search_type)
@responses.activate
@patch('amundsen_application.api.search.v0.has_filters')
@patch('amundsen_application.api.search.v0.generate_query_json')
def test_calls_generate_query_json(self, mock_generate_query_json) -> None:
def test_calls_generate_query_json(self, mock_generate_query_json, has_filters_mock) -> None:
"""
Test generate_query_json helper method is called with correct arguments
from the request_json
from the request_json if filters exist
:return:
"""
test_filters = {'schema': 'test_schema'}
test_term = 'hello'
test_index = 1
responses.add(responses.POST, self.search_service_url, json=self.mock_table_results, status=HTTPStatus.OK)
responses.add(responses.POST,
self.search_service_filter_url,
json=self.mock_table_results,
status=HTTPStatus.OK)
has_filters_mock.return_value = True
with local_app.test_client() as test:
test.post(self.fe_flask_endpoint,
......@@ -116,6 +125,23 @@ class SearchTableQueryString(unittest.TestCase):
page_index=test_index,
search_term=test_term)
@responses.activate
@patch('amundsen_application.api.search.v0.has_filters')
@patch('amundsen_application.api.search.v0.generate_query_json')
def test_does_not_calls_generate_query_json(self, mock_generate_query_json, has_filters_mock) -> None:
"""
Test generate_query_json helper method is not called if filters do not exist
:return:
"""
test_term = 'hello'
test_index = 1
responses.add(responses.GET, self.search_service_url, json=self.mock_table_results, status=HTTPStatus.OK)
has_filters_mock.return_value = False
with local_app.test_client() as test:
test.post(self.fe_flask_endpoint, json={'term': test_term, 'pageIndex': test_index, 'filters': {}})
mock_generate_query_json.assert_not_called()
@patch('amundsen_application.api.search.v0.generate_query_json')
def test_catch_exception_generate_query_json(self, mock_generate_query_json) -> None:
"""
......@@ -144,7 +170,10 @@ class SearchTableQueryString(unittest.TestCase):
test_filters = {'schema': 'test_schema'}
test_term = 'hello'
test_index = 1
responses.add(responses.POST, self.search_service_url, json=self.mock_table_results, status=HTTPStatus.OK)
responses.add(responses.POST,
self.search_service_filter_url,
json=self.mock_table_results,
status=HTTPStatus.OK)
with local_app.test_client() as test:
response = test.post(self.fe_flask_endpoint,
......@@ -165,7 +194,7 @@ class SearchTableQueryString(unittest.TestCase):
test_filters = {'schema': 'test_schema'}
test_term = 'hello'
test_index = 1
responses.add(responses.POST, self.search_service_url, json={}, status=HTTPStatus.BAD_REQUEST)
responses.add(responses.POST, self.search_service_filter_url, json={}, status=HTTPStatus.BAD_REQUEST)
with local_app.test_client() as test:
response = test.post(self.fe_flask_endpoint,
......@@ -175,7 +204,7 @@ class SearchTableQueryString(unittest.TestCase):
self.assertEqual(data.get('msg'), 'Encountered error: Search request failed')
class SearchUserTest(unittest.TestCase):
class SearchUser(unittest.TestCase):
def setUp(self) -> None:
self.mock_search_user_results = {
'total_results': 1,
......
import unittest
from amundsen_application.api.utils.search_utils import generate_query_json
from amundsen_application.api.utils.search_utils import generate_query_json, has_filters
class SearchUtilsTest(unittest.TestCase):
......@@ -41,3 +41,19 @@ class SearchUtilsTest(unittest.TestCase):
'filters': self.expected_transformed_filters
})
self.assertEqual(query_json.get('query_term'), self.test_term)
def test_has_filters_return_true(self) -> None:
"""
Returns true if called with a dictionary that has values for a valid filter category
:return:
"""
self.assertTrue(has_filters(filters=self.test_filters))
def test_has_filters_return_false(self) -> None:
"""
Returns false if called with a dictionary that has no values for a valid filter category
:return:
"""
test_filters = {'fake_category': {'db1': True}}
self.assertFalse(has_filters(filters=test_filters))
self.assertFalse(has_filters())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment