|
6 | 6 |
|
7 | 7 | from sqlbuild.adapter.contract.models import FunctionInfo |
8 | 8 | from sqlbuild.adapters.snowflake.classes.snowflake_adapter import SnowflakeAdapter |
| 9 | +from tests.unit.src.sqlbuild.adapters.snowflake._test_types import ( |
| 10 | + SnowflakeFunctionDiscoveryTestCase, |
| 11 | +) |
9 | 12 | from tests.unit.src.sqlbuild.adapters.snowflake.helpers import ( |
10 | 13 | FakeSnowflakeMetadataConnection, |
11 | 14 | FakeSnowflakeMetadataCursor, |
12 | 15 | ) |
13 | 16 |
|
14 | 17 |
|
15 | | -def test_given_explicit_database_when_listing_functions_then_queries_requested_catalog() -> None: |
16 | | - cursor = FakeSnowflakeMetadataCursor(rows=[("NORMALIZE_ORDER", "PUBLIC", "function")]) |
17 | | - connection = FakeSnowflakeMetadataConnection(cursor) |
18 | | - |
19 | | - result = SnowflakeAdapter().list_functions( |
20 | | - connection=cast(Any, connection), |
21 | | - database="analytics", |
22 | | - schemas=("public",), |
23 | | - names=("normalize_order",), |
24 | | - ) |
25 | | - |
26 | | - assert cursor.executed_sql is not None |
27 | | - assert 'FROM "ANALYTICS".information_schema.functions' in cursor.executed_sql |
28 | | - assert cursor.executed_params == ("PUBLIC", "NORMALIZE_ORDER", "analytics") |
29 | | - assert result == ( |
30 | | - FunctionInfo( |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "test_case", |
| 20 | + [ |
| 21 | + SnowflakeFunctionDiscoveryTestCase( |
| 22 | + description="explicit database qualifies the information schema", |
31 | 23 | database="analytics", |
32 | | - schema="public", |
33 | | - name="normalize_order", |
34 | | - function_type="function", |
| 24 | + schemas=("public",), |
| 25 | + names=("normalize_order",), |
| 26 | + rows=(("NORMALIZE_ORDER", "PUBLIC", "function"),), |
| 27 | + expected_relation='FROM "ANALYTICS".information_schema.functions', |
| 28 | + expected_params=("PUBLIC", "NORMALIZE_ORDER", "analytics"), |
| 29 | + expected_functions=( |
| 30 | + FunctionInfo( |
| 31 | + database="analytics", |
| 32 | + schema="public", |
| 33 | + name="normalize_order", |
| 34 | + function_type="function", |
| 35 | + ), |
| 36 | + ), |
35 | 37 | ), |
36 | | - ) |
37 | | - assert cursor.closed |
38 | | - |
39 | | - |
40 | | -def test_given_database_with_quote_when_listing_functions_then_escapes_identifier() -> None: |
41 | | - cursor = FakeSnowflakeMetadataCursor() |
42 | | - |
43 | | - SnowflakeAdapter().list_functions( |
44 | | - connection=cast(Any, FakeSnowflakeMetadataConnection(cursor)), |
45 | | - database='order"catalog', |
46 | | - schemas=None, |
47 | | - ) |
48 | | - |
49 | | - assert cursor.executed_sql is not None |
50 | | - assert 'FROM "ORDER""CATALOG".information_schema.functions' in cursor.executed_sql |
51 | | - assert cursor.closed |
52 | | - |
53 | | - |
54 | | -def test_given_no_database_when_listing_functions_then_uses_session_catalog() -> None: |
55 | | - cursor = FakeSnowflakeMetadataCursor() |
| 38 | + SnowflakeFunctionDiscoveryTestCase( |
| 39 | + description="database containing a quote is escaped", |
| 40 | + database='order"catalog', |
| 41 | + schemas=None, |
| 42 | + names=None, |
| 43 | + rows=(), |
| 44 | + expected_relation='FROM "ORDER""CATALOG".information_schema.functions', |
| 45 | + expected_params=('order"catalog',), |
| 46 | + expected_functions=(), |
| 47 | + ), |
| 48 | + SnowflakeFunctionDiscoveryTestCase( |
| 49 | + description="omitted database uses the session catalog", |
| 50 | + database=None, |
| 51 | + schemas=None, |
| 52 | + names=None, |
| 53 | + rows=(), |
| 54 | + expected_relation="FROM information_schema.functions", |
| 55 | + expected_params=(), |
| 56 | + expected_functions=(), |
| 57 | + ), |
| 58 | + ], |
| 59 | + ids=lambda case: case.description, |
| 60 | +) |
| 61 | +def test_given_database_when_listing_functions_then_queries_matching_catalog( |
| 62 | + test_case: SnowflakeFunctionDiscoveryTestCase, |
| 63 | +) -> None: |
| 64 | + cursor: FakeSnowflakeMetadataCursor = FakeSnowflakeMetadataCursor(rows=list(test_case.rows)) |
56 | 65 |
|
57 | | - result = SnowflakeAdapter().list_functions( |
| 66 | + result: tuple[FunctionInfo, ...] = SnowflakeAdapter().list_functions( |
58 | 67 | connection=cast(Any, FakeSnowflakeMetadataConnection(cursor)), |
59 | | - database=None, |
60 | | - schemas=None, |
| 68 | + database=test_case.database, |
| 69 | + schemas=test_case.schemas, |
| 70 | + names=test_case.names, |
61 | 71 | ) |
62 | 72 |
|
63 | | - assert cursor.executed_sql is not None |
64 | | - assert "FROM information_schema.functions" in cursor.executed_sql |
65 | | - assert cursor.executed_params == () |
66 | | - assert result == () |
| 73 | + assert test_case.expected_relation in str(cursor.executed_sql) |
| 74 | + assert cursor.executed_params == test_case.expected_params |
| 75 | + assert result == test_case.expected_functions |
67 | 76 | assert cursor.closed |
68 | 77 |
|
69 | 78 |
|
|
0 commit comments