|
| 1 | +"""Function discovery must address the requested catalog independently of session state.""" |
| 2 | + |
| 3 | +from typing import Any, cast |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from sqlbuild.adapter.contract.models import FunctionInfo |
| 8 | +from sqlbuild.adapters.snowflake.classes.snowflake_adapter import SnowflakeAdapter |
| 9 | +from tests.unit.src.sqlbuild.adapters.snowflake._test_types import ( |
| 10 | + SnowflakeFunctionDiscoveryTestCase, |
| 11 | +) |
| 12 | +from tests.unit.src.sqlbuild.adapters.snowflake.helpers import ( |
| 13 | + FakeSnowflakeMetadataConnection, |
| 14 | + FakeSnowflakeMetadataCursor, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "test_case", |
| 20 | + [ |
| 21 | + SnowflakeFunctionDiscoveryTestCase( |
| 22 | + description="explicit database qualifies the information schema", |
| 23 | + database="analytics", |
| 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 | + ), |
| 37 | + ), |
| 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)) |
| 65 | + |
| 66 | + result: tuple[FunctionInfo, ...] = SnowflakeAdapter().list_functions( |
| 67 | + connection=cast(Any, FakeSnowflakeMetadataConnection(cursor)), |
| 68 | + database=test_case.database, |
| 69 | + schemas=test_case.schemas, |
| 70 | + names=test_case.names, |
| 71 | + ) |
| 72 | + |
| 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 |
| 76 | + assert cursor.closed |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + pytest.main([__file__, "-vv"]) |
0 commit comments