From 04de0449db7ede2d71d3254e368fb19f186c91c9 Mon Sep 17 00:00:00 2001 From: maxachis Date: Sat, 1 Jun 2024 21:57:31 -0400 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20fix(middleware):=20Update=20?= =?UTF-8?q?INSERT=5FLOG=5FQUERY=20to=20remove=20redundant=20datetime=20fie?= =?UTF-8?q?ld?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Removed the redundant 'datetime_of_request' field from the INSERT_LOG_QUERY in the quick_search_query.py file in the middleware directory - This fixes an issue where unnecessary data was being inserted into the quick_search_query_logs table - The query now only includes essential fields for logging search queries and results --- middleware/quick_search_query.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/middleware/quick_search_query.py b/middleware/quick_search_query.py index 10681244..8213a65b 100644 --- a/middleware/quick_search_query.py +++ b/middleware/quick_search_query.py @@ -52,7 +52,7 @@ """ -INSERT_LOG_QUERY = "INSERT INTO quick_search_query_logs (search, location, results, result_count, created_at, datetime_of_request) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{4}')" +INSERT_LOG_QUERY = "INSERT INTO quick_search_query_logs (search, location, results, result_count) VALUES ('{0}', '{1}', '{2}', '{3}')" def unaltered_search_query( @@ -148,14 +148,11 @@ def quick_search_query( "data": data_source_matches_converted, } - current_datetime = datetime.datetime.now() - datetime_string = current_datetime.strftime("%Y-%m-%d %H:%M:%S") - query_results = json.dumps(data_sources["data"]).replace("'", "") cursor.execute( INSERT_LOG_QUERY.format( - search, location, query_results, data_sources["count"], datetime_string + search, location, query_results, data_sources["count"] ), ) conn.commit() From 6a8a2cf3077365463dc824133e49f7bdb9ff294b Mon Sep 17 00:00:00 2001 From: maxachis Date: Sat, 1 Jun 2024 21:57:56 -0400 Subject: [PATCH 2/5] Update timestamp field name in query --- tests/helper_functions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/helper_functions.py b/tests/helper_functions.py index 70cd249c..f0a4c94b 100644 --- a/tests/helper_functions.py +++ b/tests/helper_functions.py @@ -151,8 +151,8 @@ def get_most_recent_quick_search_query_log( """ cursor.execute( """ - SELECT RESULT_COUNT, DATETIME_OF_REQUEST FROM QUICK_SEARCH_QUERY_LOGS WHERE - search = %s AND location = %s ORDER BY DATETIME_OF_REQUEST DESC LIMIT 1 + SELECT RESULT_COUNT, CREATED_AT FROM QUICK_SEARCH_QUERY_LOGS WHERE + search = %s AND location = %s ORDER BY CREATED_AT DESC LIMIT 1 """, (search, location), ) From 1c0a8a0cb8847818acee12502a7930acc4e4fe28 Mon Sep 17 00:00:00 2001 From: maxachis Date: Sat, 1 Jun 2024 21:58:10 -0400 Subject: [PATCH 3/5] Refactor code in resources/QuickSearch.py - Removed redundant psycopg2_connection initialization and query duplication to improve code readability and efficiency. --- resources/QuickSearch.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/resources/QuickSearch.py b/resources/QuickSearch.py index 7b1c70b1..c6ef448b 100644 --- a/resources/QuickSearch.py +++ b/resources/QuickSearch.py @@ -40,12 +40,6 @@ def get(self, search: str, location: str) -> Dict[str, Any]: search, location, self.psycopg2_connection ) - if data_sources["count"] == 0: - self.psycopg2_connection = initialize_psycopg2_connection() - data_sources = quick_search_query( - search, location, self.psycopg2_connection - ) - if data_sources["count"] == 0: return { "count": 0, From 24a9a57aa7cd5be49bb56b5419fcab27f7157d4b Mon Sep 17 00:00:00 2001 From: maxachis Date: Sat, 1 Jun 2024 21:58:47 -0400 Subject: [PATCH 4/5] Fix bug in test_search_tokens.py --- tests/integration/test_search_tokens.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/test_search_tokens.py b/tests/integration/test_search_tokens.py index 45d0b39b..bf346ede 100644 --- a/tests/integration/test_search_tokens.py +++ b/tests/integration/test_search_tokens.py @@ -22,8 +22,8 @@ def test_search_tokens_get( assert response.status_code == 200 data = response.json.get("data") assert ( - data["count"] == 1 + len(data) == 1 ), "Quick Search endpoint response should return only one entry" - entry = data["data"][0] + entry = data[0] assert entry["agency_name"] == "Agency A" assert entry["airtable_uid"] == "SOURCE_UID_1" From dcacd66192a795262712386d326174492375df5f Mon Sep 17 00:00:00 2001 From: maxachis Date: Sat, 1 Jun 2024 21:58:56 -0400 Subject: [PATCH 5/5] Add tests for quick search functionality in test_QuickSearch.py" --- tests/resources/test_QuickSearch.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/resources/test_QuickSearch.py diff --git a/tests/resources/test_QuickSearch.py b/tests/resources/test_QuickSearch.py new file mode 100644 index 00000000..586b3cdb --- /dev/null +++ b/tests/resources/test_QuickSearch.py @@ -0,0 +1,38 @@ +from unittest.mock import patch, MagicMock + +import pytest + +from tests.helper_functions import check_response_status + +patch("middleware.security.api_required", lambda x: x).start() +from tests.fixtures import client_with_mock_db + +@pytest.fixture +def mock_quick_search_query(monkeypatch): + mock = MagicMock() + monkeypatch.setattr("resources.QuickSearch.quick_search_query", mock) + return mock + + +def test_get_quick_search_results_found(client_with_mock_db, mock_quick_search_query): + mock_quick_search_query.return_value = { + "count": "1", + "data": [{"id": "test_id", "name": "test_name"}], + } + response = client_with_mock_db.client.get("/quick-search/test_search/test_location") + check_response_status(response, 200) + response_json = response.json + assert response_json["data"]["data"] == [{'id': 'test_id', 'name': 'test_name'}] + assert response_json["data"]["count"] == '1' + assert response_json["message"] == "Results for search successfully retrieved" + +def test_get_quick_search_results_not_found(client_with_mock_db, mock_quick_search_query): + mock_quick_search_query.return_value = { + "count": 0, + "data": [], + } + response = client_with_mock_db.client.get("/quick-search/test_search/test_location") + check_response_status(response, 404) + response_json = response.json + assert response_json["count"] == 0 + assert response_json["message"] == "No results found. Please considering requesting a new data source."