Skip to content

Commit f7fb396

Browse files
authored
fix: Added some tests for the populate script (#1065)
1 parent a6e654e commit f7fb396

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

api/src/scripts/populate_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, filepaths):
5454
filepaths = [filepaths]
5555

5656
for filepath in filepaths:
57-
new_df = pandas.read_csv(filepath, low_memory=False)
57+
new_df = pandas.read_csv(filepath, comment="#", low_memory=False)
5858
self.df = pandas.concat([self.df, new_df])
5959

6060
self.filter_data()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
3+
import pytest
4+
from fastapi import FastAPI
5+
from fastapi.testclient import TestClient
6+
7+
from shared.database.database import Database
8+
from main import app as application
9+
from tests.test_utils.database import populate_database
10+
11+
12+
@pytest.fixture(scope="package")
13+
def app() -> FastAPI:
14+
application.dependency_overrides = {}
15+
return application
16+
17+
18+
@pytest.fixture(scope="package")
19+
def test_database():
20+
# Restrict the tests to the test database
21+
os.environ["FEEDS_DATABASE_URL"] = "postgresql://postgres:postgres@localhost:54320/MobilityDatabaseTest"
22+
23+
current_path = os.path.dirname(os.path.abspath(__file__))
24+
25+
data_dirs = [current_path + "/../../test_data", current_path + "/../test_data", current_path + "/test_data"]
26+
with populate_database(Database(), data_dirs) as db:
27+
yield db
28+
29+
30+
@pytest.fixture(scope="package")
31+
def client(app, test_database) -> TestClient:
32+
return TestClient(app)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The contents of this file will be added to the database after the sources_test.csv of the parent directories.
2+
# So we can replicate some feeds here that are a slight change from the ones in the parent source_test.csv
3+
# and see the effect.
4+
# We want to make sure the is_official flag is handled properly in that case.
5+
# Normally changing to True or False should change the original value but changing to an empty cell should not
6+
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
7+
# For mdb-40, change is_official from TRUE to empty. Should retain True
8+
40,gtfs,,CA,Ontario,London,London Transit Commission,,,,[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,,,
9+
# For mdb-50, change is_official from FALSE to empty. Should retain False
10+
50,gtfs,,CA,Ontario,Barrie,ZBarrie Transit,,,,,,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
11+
# For mdb-1562, change is_official from FALSE to TRUE. Should change to True
12+
1562,gtfs-rt,sa,CA,BC,Vancouver,Vancouver-Transit(éèàçíóúČ),TRUE,Realtime(ŘŤÜÎ),,,40,http://foo.org/google_transit.zip,0,,,,,,,,,,active,,10,
13+
# For mdb-1563, change is_official from TRUE to FALSE. Should change to False
14+
1563,gtfs-rt,tu,US,SomeState,SomeCity,SomeCity Bus,FALSE,RT,,,mdb-50,http://bar.com,0,,,,,,,,,,inactive,,10,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# coding: utf-8
2+
import pytest
3+
from fastapi.testclient import TestClient
4+
5+
from tests.test_utils.token import authHeaders
6+
7+
# Test the populate script. The assumption is that the populate script is called to populate the database for these
8+
# tests, so we can check if certain behaviours are correct.
9+
10+
11+
@pytest.mark.parametrize(
12+
"values",
13+
[
14+
{
15+
"feed_id": "mdb-40",
16+
"expected_official": True,
17+
"assert_fail_message": "mdb-40, changed is_official from TRUE to empty. Should retain True in the DB",
18+
},
19+
{
20+
"feed_id": "mdb-50",
21+
"expected_official": False,
22+
"assert_fail_message": "mdb-50, changed is_official from FALSE to empty. Should retain False in the DB",
23+
},
24+
{
25+
"feed_id": "mdb-1562",
26+
"expected_official": True,
27+
"assert_fail_message": "mdb-1562, changed is_official from FALSE to TRUE. Should change to True in the DB",
28+
},
29+
{
30+
"feed_id": "mdb-1563",
31+
"expected_official": False,
32+
"assert_fail_message": "mdb-1563, changed is_official from TRUE to FALSE. Should change to False in the DB",
33+
},
34+
],
35+
ids=[
36+
"official_change_true_to_empty",
37+
"official_change_false_to_empty",
38+
"official_change_false_to_true",
39+
"official_change_true_to_false",
40+
],
41+
)
42+
def test_is_official_overwrite(client: TestClient, values):
43+
"""Test case for feeds_gtfs_id_get with a non-existent feed"""
44+
feed_id = values["feed_id"]
45+
expected_official = values["expected_official"]
46+
47+
response = client.request(
48+
"GET",
49+
"/v1/feeds/{id}".format(id=feed_id),
50+
headers=authHeaders,
51+
)
52+
53+
assert response.status_code == 200
54+
json_response = response.json()
55+
assert json_response["official"] is expected_official, values["assert_fail_message"]

0 commit comments

Comments
 (0)