Skip to content

Commit 6e8b384

Browse files
authored
feat: Modified populate script to accept non-integers as feed_reference (#990)
1 parent 01a95c0 commit 6e8b384

File tree

5 files changed

+76
-18
lines changed

5 files changed

+76
-18
lines changed

api/src/scripts/populate_db_gtfs.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ def process_feed_references(self, session: "Session"):
126126
gtfs_rt_feed = self.query_feed_by_stable_id(session, stable_id, "gtfs_rt")
127127
static_reference = self.get_safe_value(row, "static_reference", "")
128128
if static_reference:
129-
gtfs_stable_id = f"mdb-{int(float(static_reference))}"
129+
try:
130+
gtfs_stable_id = f"mdb-{int(float(static_reference))}"
131+
except ValueError:
132+
gtfs_stable_id = static_reference
130133
gtfs_feed = self.query_feed_by_stable_id(session, gtfs_stable_id, "gtfs")
131134
already_referenced_ids = {ref.id for ref in gtfs_feed.gtfs_rt_feeds}
132135
if gtfs_feed and gtfs_rt_feed.id not in already_referenced_ids:
@@ -150,8 +153,9 @@ def process_redirects(self, session: "Session"):
150153
comments = raw_comments.split("|") if raw_comments is not None else []
151154
if len(redirects_ids) != len(comments) and len(comments) > 0:
152155
self.logger.warning(f"Number of redirect ids and redirect comments differ for feed {stable_id}")
153-
for mdb_source_id in redirects_ids:
154-
if len(mdb_source_id) == 0:
156+
for redirect_id in redirects_ids:
157+
redirect_id = redirect_id.strip() if redirect_id else ""
158+
if len(redirect_id) == 0:
155159
# since there is a 1:1 correspondence between redirect ids and comments, skip also the comment
156160
comments = comments[1:]
157161
continue
@@ -160,9 +164,9 @@ def process_redirects(self, session: "Session"):
160164
else:
161165
comment = ""
162166
try:
163-
target_stable_id = f"mdb-{int(float(mdb_source_id.strip()))}"
167+
target_stable_id = f"mdb-{int(float(redirect_id))}"
164168
except ValueError:
165-
target_stable_id = mdb_source_id.strip()
169+
target_stable_id = redirect_id
166170
target_feed = self.query_feed_by_stable_id(session, target_stable_id, None)
167171
if not target_feed:
168172
self.logger.warning(f"Could not find redirect target feed {target_stable_id} for feed {stable_id}")
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
mdb_source_id,data_type,entity_type,location.country_code,location.subdivision_name,location.municipality,provider,is_official,name,note,feed_contact_email,static_reference,urls.direct_download,urls.authentication_type,urls.authentication_info,urls.api_key_parameter_name,urls.latest,urls.license,location.bounding_box.minimum_latitude,location.bounding_box.maximum_latitude,location.bounding_box.minimum_longitude,location.bounding_box.maximum_longitude,location.bounding_box.extracted_on,status,features,redirect.id,redirect.comment
1+
mdb_source_id,data_type,entity_type,location.country_code,location.subdivision_name,location.municipality,provider,is_offical,name,note,feed_contact_email,static_reference,urls.direct_download,urls.authentication_type,urls.authentication_info,urls.api_key_parameter_name,urls.latest,urls.license,location.bounding_box.minimum_latitude,location.bounding_box.maximum_latitude,location.bounding_box.minimum_longitude,location.bounding_box.maximum_longitude,location.bounding_box.extracted_on,status,features,redirect.id,redirect.comment
22
40,gtfs,,CA,Ontario,London,London Transit Commission,TRUE,,,[email protected],,http://www.londontransit.ca/gtfsfeed/google_transit.zip,0,,,https://storage.googleapis.com/storage/v1/b/mdb-latest/o/ca-ontario-london-transit-commission-gtfs-2.zip?alt=media,https://www.londontransit.ca/open-data/ltcs-open-data-terms-of-use/,42.905244,43.051188,-81.36311,-81.137591,2022-02-22T19:51:34+00:00,inactive,,,
3-
50,gtfs,,CA,Ontario,Barrie,ZBarrie Transit,FALSE,,,,,http://www.myridebarrie.ca/gtfs/Google_transit.zip,,,,https://storage.googleapis.com/storage/v1/b/mdb-latest/o/ca-ontario-barrie-transit-gtfs-3.zip?alt=media,https://www.barrie.ca/services-payments/transportation-parking/barrie-transit/barrie-gtfs,44.3218044,44.42020676,-79.74063237,-79.61089569,2022-03-01T22:43:25+00:00,deprecated,,,
3+
50,gtfs,,CA,Ontario,Barrie,ZBarrie Transit,FALSE,,,,,http://www.myridebarrie.ca/gtfs/Google_transit.zip,,,,https://storage.googleapis.com/storage/v1/b/mdb-latest/o/ca-ontario-barrie-transit-gtfs-3.zip?alt=media,https://www.barrie.ca/services-payments/transportation-parking/barrie-transit/barrie-gtfs,44.3218044,44.42020676,-79.74063237,-79.61089569,2022-03-01T22:43:25+00:00,deprecated,,40|mdb-702,Some|Comment
44
702,gtfs,,CA,[British Columbia,Whistler],BC Transit (Whistler Transit System),,,,,,http://whistler.mapstrat.com/current/google_transit.zip,,,,https://storage.googleapis.com/storage/v1/b/mdb-latest/o/ca-british-columbia-bc-transit-whistler-transit-system-gtfs-702.zip?alt=media,https://www.bctransit.com/open-data/terms-of-use,50.077122,50.159071,-123.043635,-122.926836,2022-03-16T22:05:05+00:00,development,,,
55
1562,gtfs-rt,sa,CA,BC,Vancouver,Vancouver-Transit(éèàçíóúČ),,Realtime(ŘŤÜÎ),,,40,http://foo.org/google_transit.zip,0,,,,,,,,,,active,,10,
6+
1563,gtfs-rt,tu,US,SomeState,SomeCity,SomeCity Bus,,RT,,,mdb-50,http://bar.com,0,,,,,,,,,,inactive,,10,

api/tests/integration/test_feeds_api.py

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,23 +657,23 @@ def test_filter_by_subdivision_and_municipality(client):
657657
@pytest.mark.parametrize(
658658
"values",
659659
[
660-
{"response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
660+
{"response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562", "mdb-1563"]},
661661
{"entity_types": "vp", "response_code": 200, "expected_feed_ids": ["mdb-1561"]},
662662
{"entity_types": "sa,vp", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
663-
{"entity_types": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
663+
{"entity_types": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562", "mdb-1563"]},
664664
{"entity_types": "not_valid", "response_code": 422},
665665
{"country_code": "CA", "response_code": 200, "expected_feed_ids": ["mdb-1562"]},
666666
{"country_code": "CA", "entity_types": "sa,vp", "response_code": 200, "expected_feed_ids": ["mdb-1562"]},
667-
{"country_code": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
667+
{"country_code": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562", "mdb-1563"]},
668668
{"provider": "no-found-provider", "response_code": 200, "expected_feed_ids": []},
669669
{"provider": "transit", "response_code": 200, "expected_feed_ids": ["mdb-1562"]},
670-
{"provider": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
670+
{"provider": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562", "mdb-1563"]},
671671
{"producer_url": "foo.org", "response_code": 200, "expected_feed_ids": ["mdb-1562"]},
672-
{"producer_url": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
672+
{"producer_url": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562", "mdb-1563"]},
673673
{"subdivision_name": "bc", "response_code": 200, "expected_feed_ids": ["mdb-1562"]},
674-
{"subdivision_name": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
674+
{"subdivision_name": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562", "mdb-1563"]},
675675
{"municipality": "vanco", "response_code": 200, "expected_feed_ids": ["mdb-1562"]},
676-
{"municipality": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562"]},
676+
{"municipality": "", "response_code": 200, "expected_feed_ids": ["mdb-1561", "mdb-1562", "mdb-1563"]},
677677
],
678678
ids=[
679679
"all_none",
@@ -723,3 +723,56 @@ def test_gtfs_rt_filters(client, values):
723723
assert len(feeds) == len(values["expected_feed_ids"])
724724
if len(values["expected_feed_ids"]) != 0:
725725
assert any(feed["id"] in values["expected_feed_ids"] for feed in feeds)
726+
727+
728+
@pytest.mark.parametrize(
729+
"values",
730+
[
731+
{"stable_id": "mdb-1562", "target_id": "mdb-40"},
732+
{"stable_id": "mdb-1563", "target_id": "mdb-50"},
733+
],
734+
ids=[
735+
"integer_static_reference",
736+
"string_static_reference",
737+
],
738+
)
739+
def test_gtfs_rt_reference(client, values):
740+
"""Test /v1/gtfs_rt_feeds to make sure it returns the proper feed_reference for integer and string references."""
741+
742+
stable_id = values["stable_id"] if "stable_id" in values else None
743+
response = client.request(
744+
"GET",
745+
f"/v1/gtfs_rt_feeds/{stable_id}",
746+
headers=authHeaders,
747+
)
748+
assert response.status_code == 200
749+
feed = response.json()
750+
assert feed["feed_references"][0] == values["target_id"]
751+
752+
753+
def test_gtfs_redirect(client):
754+
"""Test that a returned feed contains the proper redirects for integer or string redirects."""
755+
756+
response = client.request(
757+
"GET",
758+
"/v1/feeds/mdb-50",
759+
headers=authHeaders,
760+
)
761+
assert response.status_code == 200
762+
feed = response.json()
763+
assert feed["redirects"][0]["comment"] == "Some"
764+
assert feed["redirects"][1]["comment"] == "Comment"
765+
# spreadsheet contains '40|mdb-702'
766+
assert feed["redirects"][0]["target_id"] == "mdb-40"
767+
assert feed["redirects"][1]["target_id"] == "mdb-702"
768+
769+
response = client.request(
770+
"GET",
771+
"/v1/feeds/mdb-1562",
772+
headers=authHeaders,
773+
)
774+
assert response.status_code == 200
775+
feed = response.json()
776+
assert feed["redirects"][0]["comment"] == ""
777+
# spreadsheet contains '10'
778+
assert feed["redirects"][0]["target_id"] == "mdb-10"

api/tests/integration/test_search_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def test_search_feeds_provider_one_feed(client: TestClient, search_query: str):
133133
[
134134
("gtfs", 10),
135135
("not_valid_gtfs", 0),
136-
("gtfs_rt", 2),
136+
("gtfs_rt", 3),
137137
],
138138
)
139139
def test_search_feeds_filter_data_type(client: TestClient, data_type: str, expected_results_total: int):
@@ -176,8 +176,8 @@ def test_search_feeds_filter_data_type(client: TestClient, data_type: str, expec
176176
[
177177
("active", 9),
178178
("not_valid_status", 0),
179-
("inactive", 1),
180-
("active,inactive", 10),
179+
("inactive", 2),
180+
("active,inactive", 11),
181181
],
182182
)
183183
def test_search_feeds_filter_status(client: TestClient, status: str, expected_results_total: int):

functions-python/export_csv/src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def export_and_upload_csv(request=None):
114114

115115
csv_file_path = csv_default_file_path
116116
export_csv(csv_file_path)
117-
upload_file_to_storage(csv_file_path, "sources_v2.csv")
117+
upload_file_to_storage(csv_file_path, "feeds_v2.csv")
118118

119119
logging.info("Export successful")
120120
return "Export successful"

0 commit comments

Comments
 (0)