Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mc 515 typeahead agencies include agency #173

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions database_client/dynamic_query_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -201,6 +204,7 @@ def generate_new_typeahead_agencies_query(search_term: str):
FROM (
SELECT DISTINCT
sort_order,
id,
name,
jurisdiction_type,
state_iso,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down
18 changes: 15 additions & 3 deletions tests/helper_scripts/common_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
}
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +113,7 @@ def clear_test_data(self):

def locality(
self,
locality_name: str = "",
state_iso: str = "PA",
county_name: str = "Allegheny",
) -> int:
Expand All @@ -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}
)
Expand Down
2 changes: 2 additions & 0 deletions tests/helper_scripts/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 16 additions & 15 deletions tests/integration/test_typeahead_suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/test_database_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading