|
| 1 | +"""Tests for the _should_force_select safety net.""" |
| 2 | + |
| 3 | +from typing import Any, cast |
| 4 | + |
| 5 | +from sqlspec import SQL, ProcessedState |
| 6 | +from sqlspec.adapters.bigquery import bigquery_statement_config |
| 7 | +from sqlspec.driver import CommonDriverAttributesMixin |
| 8 | + |
| 9 | + |
| 10 | +class _DummyDriver(CommonDriverAttributesMixin): |
| 11 | + """Minimal driver to expose _should_force_select for testing.""" |
| 12 | + |
| 13 | + __slots__ = () |
| 14 | + |
| 15 | + def __init__(self) -> None: |
| 16 | + super().__init__(connection=None, statement_config=bigquery_statement_config) |
| 17 | + |
| 18 | + |
| 19 | +class _CursorWithStatementType: |
| 20 | + """Cursor exposing a statement_type attribute.""" |
| 21 | + |
| 22 | + def __init__(self, statement_type: str | None) -> None: |
| 23 | + self.statement_type = statement_type |
| 24 | + self.description = None |
| 25 | + |
| 26 | + |
| 27 | +class _CursorWithDescription: |
| 28 | + """Cursor exposing a description attribute.""" |
| 29 | + |
| 30 | + def __init__(self, has_description: bool) -> None: |
| 31 | + self.description = [("col",)] if has_description else None |
| 32 | + self.statement_type = None |
| 33 | + |
| 34 | + |
| 35 | +def _make_unknown_statement(sql_text: str = "select 1") -> "SQL": |
| 36 | + stmt = SQL(sql_text) |
| 37 | + cast("Any", stmt)._processed_state = ProcessedState( |
| 38 | + compiled_sql=sql_text, execution_parameters={}, operation_type="UNKNOWN" |
| 39 | + ) |
| 40 | + return stmt |
| 41 | + |
| 42 | + |
| 43 | +def _make_select_statement(sql_text: str = "select 1") -> "SQL": |
| 44 | + stmt = SQL(sql_text) |
| 45 | + cast("Any", stmt)._processed_state = ProcessedState( |
| 46 | + compiled_sql=sql_text, execution_parameters={}, operation_type="SELECT" |
| 47 | + ) |
| 48 | + return stmt |
| 49 | + |
| 50 | + |
| 51 | +def test_force_select_uses_statement_type_select() -> None: |
| 52 | + driver = _DummyDriver() |
| 53 | + stmt = _make_unknown_statement() |
| 54 | + cursor = _CursorWithStatementType("SELECT") |
| 55 | + |
| 56 | + assert cast("Any", driver)._should_force_select(stmt, cursor) is True |
| 57 | + |
| 58 | + |
| 59 | +def test_force_select_uses_description_when_unknown() -> None: |
| 60 | + driver = _DummyDriver() |
| 61 | + stmt = _make_unknown_statement() |
| 62 | + cursor = _CursorWithDescription(True) |
| 63 | + |
| 64 | + assert cast("Any", driver)._should_force_select(stmt, cursor) is True |
| 65 | + |
| 66 | + |
| 67 | +def test_force_select_false_when_no_metadata() -> None: |
| 68 | + driver = _DummyDriver() |
| 69 | + stmt = _make_unknown_statement() |
| 70 | + cursor = _CursorWithDescription(False) |
| 71 | + |
| 72 | + assert cast("Any", driver)._should_force_select(stmt, cursor) is False |
| 73 | + |
| 74 | + |
| 75 | +def test_force_select_ignored_when_operation_known() -> None: |
| 76 | + driver = _DummyDriver() |
| 77 | + stmt = _make_select_statement() |
| 78 | + cursor = _CursorWithDescription(True) |
| 79 | + |
| 80 | + assert cast("Any", driver)._should_force_select(stmt, cursor) is False |
0 commit comments