Skip to content

Commit

Permalink
fix: empty search query with no additional parameter returns HTTP 500 (
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgamez authored Aug 19, 2024
1 parent 7af2035 commit c34ba47
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
1 change: 0 additions & 1 deletion api/src/feeds/impl/models/basic_feed_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ class Config:
Enabling `from_orm` method to create a model instance from a SQLAlchemy row object."""

from_attributes = True
orm_mode = True
16 changes: 13 additions & 3 deletions api/src/feeds/impl/models/search_feed_item_result_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,30 @@ def from_orm_gtfs_rt(cls, feed_search_row):

@classmethod
def _translate_locations(cls, feed_search_row):
"""Translate location information in the feed search row."""
"""Translate location information in the feed search row.
This method modifies the locations in the feed search row in place."""
if feed_search_row.locations is None:
return
country_translations = cls._create_translation_dict(feed_search_row.country_translations)
subdivision_translations = cls._create_translation_dict(feed_search_row.subdivision_name_translations)
municipality_translations = cls._create_translation_dict(feed_search_row.municipality_translations)

for location in feed_search_row.locations:
location["country"] = country_translations.get(location["country"], location["country"])
if location["country"] is None:
location["country"] = pycountry.countries.get(alpha_2=location["country_code"]).name
if location["country"] is None or len(location["country"]) == 0:
location["country"] = SearchFeedItemResultImpl.resolve_country_by_code(location)
location["subdivision_name"] = subdivision_translations.get(
location["subdivision_name"], location["subdivision_name"]
)
location["municipality"] = municipality_translations.get(location["municipality"], location["municipality"])

@classmethod
def resolve_country_by_code(cls, location):
"""Resolve country name by country code.
If the country code is not found, return the original country name."""
country = pycountry.countries.get(alpha_2=location["country_code"])
return country.name if country else location["country"]

@staticmethod
def _create_translation_dict(translations):
"""Helper method to create a translation dictionary."""
Expand Down
62 changes: 62 additions & 0 deletions api/tests/unittest/models/test_search_feed_item_result_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from feeds.impl.models.search_feed_item_result_impl import SearchFeedItemResultImpl
from feeds_gen.models.latest_dataset import LatestDataset
from feeds_gen.models.location import Location
from feeds_gen.models.source_info import SourceInfo

fake = Faker()
Expand Down Expand Up @@ -127,3 +128,64 @@ def test_from_orm(self):
item = copy.deepcopy(search_item)
item.data_type = "unknown"
SearchFeedItemResultImpl.from_orm(item)

def test_from_orm_locations_country_provided(self):
"""Test that the country is not replaced with the translation."""
item = copy.deepcopy(search_item)
item.data_type = "gtfs"
item.locations = [
{
"country_code": "CA",
"country": "CanadaNotReplaced",
"subdivision_name": "subdivision_name",
"municipality": "municipality",
}
]
result = SearchFeedItemResultImpl.from_orm(item)
assert result.data_type == "gtfs"
assert result.locations == [
Location(
country_code="CA",
country="CanadaNotReplaced",
subdivision_name="subdivision_name",
municipality="municipality",
)
]

def test_from_orm_locations_country_missing(self):
"""Test that the country is not replaced with the translation."""
item = copy.deepcopy(search_item)
item.data_type = "gtfs"
item.locations = [
{
"country_code": "CA",
"country": "",
"subdivision_name": "subdivision_name",
"municipality": "municipality",
}
]
result = SearchFeedItemResultImpl.from_orm(item)
assert result.data_type == "gtfs"
assert result.locations == [
Location(
country_code="CA", country="Canada", subdivision_name="subdivision_name", municipality="municipality"
)
]

def test_from_orm_locations_country_invalid_code(self):
"""Test that the country is not replaced with the translation."""
item = copy.deepcopy(search_item)
item.data_type = "gtfs"
item.locations = [
{
"country_code": "XY",
"country": "",
"subdivision_name": "subdivision_name",
"municipality": "municipality",
}
]
result = SearchFeedItemResultImpl.from_orm(item)
assert result.data_type == "gtfs"
assert result.locations == [
Location(country_code="XY", country="", subdivision_name="subdivision_name", municipality="municipality")
]

0 comments on commit c34ba47

Please sign in to comment.