Skip to content

Commit

Permalink
Update typeahead_location tests and correct minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxachis committed Nov 14, 2024
1 parent ba79166 commit c5a5baa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
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
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
32 changes: 16 additions & 16 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,26 +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
"""
agency_id = 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": [
{
"id": agency_id,
"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

0 comments on commit c5a5baa

Please sign in to comment.