|
3 | 3 | import pytest
|
4 | 4 | from httpx import AsyncClient
|
5 | 5 |
|
6 |
| -from app.crud.geostore import get_gadm_geostore |
| 6 | +from app.crud.geostore import get_gadm_geostore, get_gadm_geostore_id |
7 | 7 | from app.errors import RecordNotFoundError
|
8 | 8 |
|
9 | 9 |
|
10 | 10 | @pytest.mark.asyncio
|
11 | 11 | async def test_get_gadm_geostore_generates_correct_sql_for_country_lookup(
|
12 |
| - async_client: AsyncClient |
| 12 | + async_client: AsyncClient |
13 | 13 | ):
|
14 | 14 | provider = "gadm"
|
15 | 15 | version = "4.1"
|
@@ -45,7 +45,7 @@ async def test_get_gadm_geostore_generates_correct_sql_for_country_lookup(
|
45 | 45 |
|
46 | 46 | @pytest.mark.asyncio
|
47 | 47 | async def test_get_gadm_geostore_generates_correct_sql_for_region_lookup(
|
48 |
| - async_client: AsyncClient |
| 48 | + async_client: AsyncClient |
49 | 49 | ):
|
50 | 50 | provider = "gadm"
|
51 | 51 | version = "4.1"
|
@@ -80,10 +80,9 @@ async def test_get_gadm_geostore_generates_correct_sql_for_region_lookup(
|
80 | 80 | assert actual_sql == expected_sql
|
81 | 81 |
|
82 | 82 |
|
83 |
| - |
84 | 83 | @pytest.mark.asyncio
|
85 | 84 | async def test_get_gadm_geostore_generates_correct_sql_for_subregion_lookup(
|
86 |
| - async_client: AsyncClient |
| 85 | + async_client: AsyncClient |
87 | 86 | ):
|
88 | 87 | provider = "gadm"
|
89 | 88 | version = "4.1"
|
@@ -117,3 +116,109 @@ async def test_get_gadm_geostore_generates_correct_sql_for_subregion_lookup(
|
117 | 116 |
|
118 | 117 | assert mock_get_first_row.called is True
|
119 | 118 | assert actual_sql == expected_sql
|
| 119 | + |
| 120 | + |
| 121 | +class TestGadmGeostoreIDLookup: |
| 122 | + @pytest.mark.asyncio |
| 123 | + async def test_get_gadm_geostore_id_generates_correct_sql_for_country_lookup( |
| 124 | + async_client: AsyncClient |
| 125 | + ): |
| 126 | + provider = "gadm" |
| 127 | + version = "4.1" |
| 128 | + adm_level = 0 |
| 129 | + country_id = "MEX" |
| 130 | + |
| 131 | + with patch("app.crud.geostore.get_first_row") as mock_get_first_row: |
| 132 | + mock_get_first_row.return_value = None |
| 133 | + try: |
| 134 | + _ = await get_gadm_geostore_id( |
| 135 | + provider, version, adm_level, country_id, None, None |
| 136 | + ) |
| 137 | + except RecordNotFoundError: |
| 138 | + pass |
| 139 | + |
| 140 | + expected_sql = ( |
| 141 | + "SELECT gfw_geostore_id " |
| 142 | + '\nFROM gadm_administrative_boundaries."v4.1.64" \n' |
| 143 | + "WHERE adm_level='0' AND gid_0='MEX'" |
| 144 | + ) |
| 145 | + |
| 146 | + actual_sql = str( |
| 147 | + mock_get_first_row.call_args.args[0].compile( |
| 148 | + compile_kwargs={"literal_binds": True} |
| 149 | + ) |
| 150 | + ) |
| 151 | + |
| 152 | + assert mock_get_first_row.called is True |
| 153 | + assert actual_sql == expected_sql |
| 154 | + |
| 155 | + |
| 156 | + @pytest.mark.asyncio |
| 157 | + async def test_get_gadm_geostore_id_generates_correct_sql_for_region_lookup( |
| 158 | + async_client: AsyncClient |
| 159 | + ): |
| 160 | + provider = "gadm" |
| 161 | + version = "4.1" |
| 162 | + adm_level = 1 |
| 163 | + country = "MEX" |
| 164 | + region = "5" |
| 165 | + |
| 166 | + with patch("app.crud.geostore.get_first_row") as mock_get_first_row: |
| 167 | + mock_get_first_row.return_value = None |
| 168 | + try: |
| 169 | + _ = await get_gadm_geostore_id( |
| 170 | + provider, version, adm_level, country, region, None |
| 171 | + ) |
| 172 | + except RecordNotFoundError: |
| 173 | + pass |
| 174 | + |
| 175 | + expected_sql = ( |
| 176 | + "SELECT gfw_geostore_id " |
| 177 | + '\nFROM gadm_administrative_boundaries."v4.1.64" \n' |
| 178 | + r"WHERE adm_level='1' AND gid_1 LIKE 'MEX.5\__'" |
| 179 | + ) |
| 180 | + |
| 181 | + actual_sql = str( |
| 182 | + mock_get_first_row.call_args.args[0].compile( |
| 183 | + compile_kwargs={"literal_binds": True} |
| 184 | + ) |
| 185 | + ) |
| 186 | + |
| 187 | + assert mock_get_first_row.called is True |
| 188 | + assert actual_sql == expected_sql |
| 189 | + |
| 190 | + |
| 191 | + @pytest.mark.asyncio |
| 192 | + async def test_get_gadm_geostore_id_generates_correct_sql_for_subregion_lookup( |
| 193 | + async_client: AsyncClient |
| 194 | + ): |
| 195 | + provider = "gadm" |
| 196 | + version = "4.1" |
| 197 | + adm_level = 2 |
| 198 | + country = "MEX" |
| 199 | + region = "5" |
| 200 | + subregion = "2" |
| 201 | + |
| 202 | + with patch("app.crud.geostore.get_first_row") as mock_get_first_row: |
| 203 | + mock_get_first_row.return_value = None |
| 204 | + try: |
| 205 | + _ = await get_gadm_geostore_id( |
| 206 | + provider, version, adm_level, country, region, subregion |
| 207 | + ) |
| 208 | + except RecordNotFoundError: |
| 209 | + pass |
| 210 | + |
| 211 | + expected_sql = ( |
| 212 | + "SELECT gfw_geostore_id " |
| 213 | + '\nFROM gadm_administrative_boundaries."v4.1.64" \n' |
| 214 | + r"WHERE adm_level='2' AND gid_2 LIKE 'MEX.5.2\__'" |
| 215 | + ) |
| 216 | + |
| 217 | + actual_sql = str( |
| 218 | + mock_get_first_row.call_args.args[0].compile( |
| 219 | + compile_kwargs={"literal_binds": True} |
| 220 | + ) |
| 221 | + ) |
| 222 | + |
| 223 | + assert mock_get_first_row.called is True |
| 224 | + assert actual_sql == expected_sql |
0 commit comments