diff --git a/database_client/dynamic_query_constructor.py b/database_client/dynamic_query_constructor.py index 27d60f00..ddbd8203 100644 --- a/database_client/dynamic_query_constructor.py +++ b/database_client/dynamic_query_constructor.py @@ -173,6 +173,7 @@ def generate_new_typeahead_agencies_query(search_term: str): WITH combined AS ( SELECT 1 AS sort_order, + id, name, jurisdiction_type, state_iso, @@ -183,6 +184,7 @@ def generate_new_typeahead_agencies_query(search_term: str): UNION ALL SELECT 2 AS sort_order, + id, name, jurisdiction_type, state_iso, @@ -193,6 +195,7 @@ def generate_new_typeahead_agencies_query(search_term: str): AND name NOT ILIKE {search_term} ) SELECT + id, name as display_name, jurisdiction_type, state_iso as state, @@ -201,6 +204,7 @@ def generate_new_typeahead_agencies_query(search_term: str): FROM ( SELECT DISTINCT sort_order, + id, name, jurisdiction_type, state_iso, diff --git a/middleware/schema_and_dto_logic/primary_resource_schemas/data_sources_schemas.py b/middleware/schema_and_dto_logic/primary_resource_schemas/data_sources_schemas.py index dcd2ff37..bc229fbf 100644 --- a/middleware/schema_and_dto_logic/primary_resource_schemas/data_sources_schemas.py +++ b/middleware/schema_and_dto_logic/primary_resource_schemas/data_sources_schemas.py @@ -277,7 +277,7 @@ class DataSourceBaseSchema(Schema): "Airtable UID of the associated data source request" ), ) - broken_source_url_as_of = fields.Date( + broken_source_url_as_of = fields.DateTime( allow_none=True, format="iso", metadata=get_json_metadata("When the url was marked as broken."), diff --git a/middleware/schema_and_dto_logic/primary_resource_schemas/typeahead_suggestion_schemas.py b/middleware/schema_and_dto_logic/primary_resource_schemas/typeahead_suggestion_schemas.py index 8b8e37da..98d9add0 100644 --- a/middleware/schema_and_dto_logic/primary_resource_schemas/typeahead_suggestion_schemas.py +++ b/middleware/schema_and_dto_logic/primary_resource_schemas/typeahead_suggestion_schemas.py @@ -73,6 +73,14 @@ class TypeaheadLocationsResponseSchema(TypeaheadBaseResponseSchema): class TypeaheadAgenciesResponseSchema(TypeaheadBaseResponseSchema): + id = fields.Integer( + required=True, + metadata={ + "source": SourceMappingEnum.JSON, + "description": "The id of the suggestion", + "example": 1, + }, + ) display_name = fields.String( required=True, metadata={ diff --git a/tests/helper_scripts/common_test_data.py b/tests/helper_scripts/common_test_data.py index ee639525..31e92253 100644 --- a/tests/helper_scripts/common_test_data.py +++ b/tests/helper_scripts/common_test_data.py @@ -183,6 +183,7 @@ class TestDataCreatorFlask: def __init__(self, flask_client: FlaskClient): self.flask_client = flask_client + self.tdcdb = TestDataCreatorDBClient() self.request_validator = RequestValidator(flask_client) self.endpoint_caller = EndpointCaller(flask_client) self.db_client = DatabaseClient() @@ -241,9 +242,9 @@ def update_data_request_status(self, data_request_id: int, status: RequestStatus json={"request_status": status.value}, ) - def agency(self, location_info: Optional[dict] = None) -> TestAgencyInfo: - submitted_name = uuid.uuid4().hex - locality_name = uuid.uuid4().hex + def agency(self, location_info: Optional[dict] = None, agency_name: str = "") -> TestAgencyInfo: + submitted_name = self.tdcdb.test_name(agency_name) + locality_name = self.tdcdb.test_name() sample_agency_post_parameters = get_sample_agency_post_parameters( submitted_name=submitted_name, locality_name=locality_name, @@ -261,6 +262,9 @@ def agency(self, location_info: Optional[dict] = None) -> TestAgencyInfo: return TestAgencyInfo(id=json["id"], submitted_name=submitted_name) + def refresh_typeahead_agencies(self): + self.db_client.execute_raw_sql("CALL refresh_typeahead_agencies();") + def update_agency(self, agency_id: int, data_to_update: dict): run_and_validate_request( flask_client=self.flask_client, @@ -309,6 +313,13 @@ def notifications_user(self) -> TestUserSetup: self.flask_client, permissions=[PermissionsEnum.NOTIFICATIONS] ) + def locality(self, locality_name: str = "", state_iso: str = "PA", county_name: str = "Allegheny"): + return self.tdcdb.locality( + locality_name=locality_name, + state_iso=state_iso, + county_name=county_name + ) + def select_only_complex_linked_resources(self): """ Create the following: @@ -354,6 +365,7 @@ def get_sample_agency_post_parameters( "agency_info": generate_test_data_from_schema( schema=AgencyInfoPostSchema(), override={ + "submitted_name": submitted_name, "jurisdiction_type": JurisdictionType.LOCAL.value, } ), diff --git a/tests/helper_scripts/helper_classes/TestDataCreatorDBClient.py b/tests/helper_scripts/helper_classes/TestDataCreatorDBClient.py index 7d2b1677..562c83c9 100644 --- a/tests/helper_scripts/helper_classes/TestDataCreatorDBClient.py +++ b/tests/helper_scripts/helper_classes/TestDataCreatorDBClient.py @@ -76,8 +76,8 @@ def __init__(self): self.db_client: DatabaseClient = DatabaseClient() self.helper = TDCSQLAlchemyHelper() - def test_name(self): - return f"TEST_{uuid.uuid4().hex}" + def test_name(self, midfix: str = ""): + return f"TEST_{midfix}_{uuid.uuid4().hex}" def clear_test_data(self): # Remove test data from data request @@ -113,6 +113,7 @@ def clear_test_data(self): def locality( self, + locality_name: str = "", state_iso: str = "PA", county_name: str = "Allegheny", ) -> int: @@ -121,7 +122,7 @@ def locality( county_id = self.helper.get_county_id( county_name=county_name, state_iso=state_iso ) - locality_name = self.test_name() + locality_name = self.test_name(locality_name) locality_id = self.db_client.create_locality( column_value_mappings={"name": locality_name, "county_id": county_id} ) diff --git a/tests/helper_scripts/helper_functions.py b/tests/helper_scripts/helper_functions.py index 8eef468f..0d92d8a5 100644 --- a/tests/helper_scripts/helper_functions.py +++ b/tests/helper_scripts/helper_functions.py @@ -434,6 +434,8 @@ def setup_get_typeahead_suggestion_test_data(cursor: Optional[psycopg.Cursor] = db_client.execute_raw_sql("CALL refresh_typeahead_agencies();") db_client.execute_raw_sql("CALL refresh_typeahead_locations();") + return agency_id + except sqlalchemy.exc.IntegrityError: pass diff --git a/tests/integration/test_typeahead_suggestions.py b/tests/integration/test_typeahead_suggestions.py index bb16efdf..92dd22bf 100644 --- a/tests/integration/test_typeahead_suggestions.py +++ b/tests/integration/test_typeahead_suggestions.py @@ -2,11 +2,13 @@ TypeaheadAgenciesOuterResponseSchema, TypeaheadLocationsOuterResponseSchema, ) +from tests.helper_scripts.common_test_data import TestDataCreatorFlask from tests.helper_scripts.helper_functions import ( setup_get_typeahead_suggestion_test_data, ) from tests.helper_scripts.run_and_validate_request import run_and_validate_request from tests.conftest import flask_client_with_db +from conftest import test_data_creator_flask, monkeysession def test_typeahead_locations(flask_client_with_db): @@ -50,25 +52,24 @@ def test_typeahead_locations(flask_client_with_db): assert suggestions == expected_suggestions -def test_typeahead_agencies(flask_client_with_db): +def test_typeahead_agencies(test_data_creator_flask: TestDataCreatorFlask): """ Test that GET call to /typeahead/agencies endpoint successfully retrieves data """ - setup_get_typeahead_suggestion_test_data() + tdc = test_data_creator_flask + tdc.clear_test_data() + location_id = tdc.locality(locality_name="Qzy") + agency_id = tdc.agency(agency_name="Qzy").id + tdc.refresh_typeahead_agencies() + json_content = run_and_validate_request( - flask_client=flask_client_with_db, + flask_client=tdc.flask_client, http_method="get", - endpoint="/typeahead/agencies?query=xyl", - expected_json_content={ - "suggestions": [ - { - "display_name": "Xylodammerung Police Agency", - "locality": "Xylodammerung", - "county": "Arxylodon", - "state": "XY", - "jurisdiction_type": "state", - } - ] - }, + endpoint="/typeahead/agencies?query=qzy", expected_schema=TypeaheadAgenciesOuterResponseSchema, ) + assert len(json_content["suggestions"]) > 0 + result = json_content["suggestions"][0] + + assert "Qzy" in result["display_name"] + assert result["id"] == int(agency_id) diff --git a/tests/test_database_client.py b/tests/test_database_client.py index 808ff462..b9a15085 100644 --- a/tests/test_database_client.py +++ b/tests/test_database_client.py @@ -580,7 +580,7 @@ def test_get_typeahead_agencies(live_database_client): setup_get_typeahead_suggestion_test_data(cursor) results = live_database_client.get_typeahead_agencies(search_term="xyl") - assert len(results) == 1 + assert len(results) > 0 assert results[0]["display_name"] == "Xylodammerung Police Agency" assert results[0]["jurisdiction_type"] == "state" assert results[0]["state"] == "XY"