Skip to content

Commit 4668790

Browse files
committed
Use dolar sign quoting to avoid PostgreSQL complaining about apostrophes.
1 parent 849d68a commit 4668790

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

app/routes/thematic/geoencoder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ def _admin_boundary_lookup_sql(
145145

146146
sql = (
147147
f"SELECT gid_0, gid_1, gid_2, {name_fields[0]}, {name_fields[1]}, {name_fields[2]}"
148-
f" FROM {dataset} WHERE {match_name_fields[0]}='{country_name}'"
148+
f" FROM {dataset} WHERE {match_name_fields[0]}=$country${country_name}$country$"
149149
)
150150
if region_name is not None:
151-
sql += f" AND {match_name_fields[1]}='{region_name}'"
151+
sql += f" AND {match_name_fields[1]}=$region${region_name}$region$"
152152
if subregion_name is not None:
153-
sql += f" AND {match_name_fields[2]}='{subregion_name}'"
153+
sql += f" AND {match_name_fields[2]}=$subregion${subregion_name}$subregion$"
154154

155155
sql += f" AND adm_level='{adm_level}'"
156156

tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def test__admin_boundary_lookup_sql_country() -> None:
6969
)
7070
assert sql == (
7171
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
72-
" WHERE country='some_country' AND adm_level='0'"
72+
" WHERE country=$country$some_country$country$ AND adm_level='0'"
7373
)
7474

7575

@@ -80,8 +80,8 @@ async def test__admin_boundary_lookup_sql_country_region() -> None:
8080
)
8181
assert sql == (
8282
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
83-
" WHERE country='some_country'"
84-
" AND name_1='some_region'"
83+
" WHERE country=$country$some_country$country$"
84+
" AND name_1=$region$some_region$region$"
8585
" AND adm_level='1'"
8686
)
8787

@@ -93,9 +93,9 @@ async def test__admin_boundary_lookup_sql_all() -> None:
9393
)
9494
assert sql == (
9595
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
96-
" WHERE country='some_country'"
97-
" AND name_1='some_region'"
98-
" AND name_2='some_subregion'"
96+
" WHERE country=$country$some_country$country$"
97+
" AND name_1=$region$some_region$region$"
98+
" AND name_2=$subregion$some_subregion$subregion$"
9999
" AND adm_level='2'"
100100
)
101101

@@ -107,9 +107,23 @@ async def test__admin_boundary_lookup_sql_all_normalized() -> None:
107107
)
108108
assert sql == (
109109
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
110-
" WHERE country_normalized='some_country'"
111-
" AND name_1_normalized='some_region'"
112-
" AND name_2_normalized='some_subregion'"
110+
" WHERE country_normalized=$country$some_country$country$"
111+
" AND name_1_normalized=$region$some_region$region$"
112+
" AND name_2_normalized=$subregion$some_subregion$subregion$"
113+
" AND adm_level='2'"
114+
)
115+
116+
117+
@pytest.mark.asyncio
118+
async def test__admin_boundary_lookup_sql_no_single_quotes() -> None:
119+
sql = _admin_boundary_lookup_sql(
120+
2, False, "some_dataset", "Côte d'Ivoire", "some_region", "some_subregion"
121+
)
122+
assert sql == (
123+
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
124+
" WHERE country=$country$Côte d'Ivoire$country$"
125+
" AND name_1=$region$some_region$region$"
126+
" AND name_2=$subregion$some_subregion$subregion$"
113127
" AND adm_level='2'"
114128
)
115129

0 commit comments

Comments
 (0)