Skip to content

Commit

Permalink
Merge pull request #24 from Police-Data-Accessibility-Project/mc_issu…
Browse files Browse the repository at this point in the history
…e_290_remove_datetime_of_request

Mc issue 290 remove datetime of request
  • Loading branch information
maxachis authored Jun 2, 2024
2 parents d9dd825 + dcacd66 commit 3cfa69b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
7 changes: 2 additions & 5 deletions middleware/quick_search_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 0 additions & 6 deletions resources/QuickSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_search_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
38 changes: 38 additions & 0 deletions tests/resources/test_QuickSearch.py
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit 3cfa69b

Please sign in to comment.