|
1 | | -import uuid |
2 | 1 | from unittest.mock import patch |
3 | 2 |
|
4 | 3 | import pytest |
5 | 4 |
|
6 | | -from app.crud.geostore import ( |
7 | | - get_gadm_geostore, |
8 | | - get_gadm_geostore_id, |
9 | | - get_geostore_by_version, |
10 | | -) |
| 5 | +from app.crud.geostore import get_gadm_geostore, get_gadm_geostore_id |
11 | 6 | from app.errors import RecordNotFoundError |
12 | 7 |
|
13 | | -GEOSTORE_ID = uuid.UUID("12345678-1234-5678-1234-567812345678") |
14 | | - |
15 | | - |
16 | | -@pytest.mark.asyncio |
17 | | -async def test_get_geostore_by_version_quotes_dotted_version_string(): |
18 | | - """Regression test: version strings containing dots (e.g. 'v1.11') must be |
19 | | - double-quoted in the generated SQL. |
20 | | - """ |
21 | | - dataset = "umd_tree_cover_loss" |
22 | | - version = "v1.11" |
23 | | - |
24 | | - with patch("app.crud.geostore.db.first") as mock_db_first: |
25 | | - mock_db_first.return_value = None |
26 | | - try: |
27 | | - await get_geostore_by_version(dataset, version, GEOSTORE_ID) |
28 | | - except RecordNotFoundError: |
29 | | - pass |
30 | | - |
31 | | - assert mock_db_first.called, "db.first should have been called" |
32 | | - |
33 | | - actual_sql = str( |
34 | | - mock_db_first.call_args.args[0].compile(compile_kwargs={"literal_binds": True}) |
35 | | - ) |
36 | | - |
37 | | - # The schema and table must both be double-quoted so that PostgreSQL |
38 | | - # treats the dot in "v1.11" as part of the identifier, not a separator. |
39 | | - assert ( |
40 | | - '"umd_tree_cover_loss"."v1.11"' in actual_sql |
41 | | - ), f"Expected schema and table to be double-quoted in SQL, but got:\n{actual_sql}" |
42 | | - |
43 | | - |
44 | | -@pytest.mark.asyncio |
45 | | -async def test_get_geostore_by_version_quotes_undotted_version_string(): |
46 | | - """Ensure that version strings without dots also render correctly and are |
47 | | - quoted, as a sanity check that the fix doesn't break the common case.""" |
48 | | - dataset = "umd_tree_cover_loss" |
49 | | - version = "v1" |
50 | | - |
51 | | - with patch("app.crud.geostore.db.first") as mock_db_first: |
52 | | - mock_db_first.return_value = None |
53 | | - try: |
54 | | - await get_geostore_by_version(dataset, version, GEOSTORE_ID) |
55 | | - except RecordNotFoundError: |
56 | | - pass |
57 | | - |
58 | | - assert mock_db_first.called |
59 | | - |
60 | | - actual_sql = str( |
61 | | - mock_db_first.call_args.args[0].compile(compile_kwargs={"literal_binds": True}) |
62 | | - ) |
63 | | - |
64 | | - assert ( |
65 | | - '"umd_tree_cover_loss"."v1"' in actual_sql |
66 | | - ), f"Expected schema and table to be double-quoted in SQL, but got:\n{actual_sql}" |
67 | | - |
68 | 8 |
|
69 | 9 | @pytest.mark.asyncio |
70 | 10 | async def test_get_gadm_geostore_generates_correct_sql_for_country_lookup(): |
|
0 commit comments