|
| 1 | +# |
| 2 | +# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +import pytest |
| 6 | +import re |
| 7 | +from unittest.mock import Mock, patch |
| 8 | +from snowflake.snowpark._internal.data_source.drivers.base_driver import BaseDriver |
| 9 | +from snowflake.snowpark._internal.data_source.datasource_reader import DataSourceReader |
| 10 | +from snowflake.snowpark._internal.data_source.utils import DBMS_TYPE |
| 11 | +from snowflake.snowpark.types import StructType, StructField, StringType |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + "cursor_fails,conn_fails", |
| 16 | + [ |
| 17 | + (True, False), # cursor.close() fails |
| 18 | + (False, True), # connection.close() fails |
| 19 | + (True, True), # both fail |
| 20 | + ], |
| 21 | +) |
| 22 | +def test_close_error_handling(cursor_fails, conn_fails): |
| 23 | + """Test that errors during cursor/connection close are handled gracefully.""" |
| 24 | + # Setup mock driver |
| 25 | + mock_create_connection = Mock() |
| 26 | + driver = BaseDriver(mock_create_connection, DBMS_TYPE.UNKNOWN) |
| 27 | + |
| 28 | + # Setup mocks |
| 29 | + mock_conn = Mock() |
| 30 | + mock_cursor = Mock() |
| 31 | + mock_conn.cursor.return_value = mock_cursor |
| 32 | + mock_create_connection.return_value = mock_conn |
| 33 | + |
| 34 | + # Configure failures |
| 35 | + |
| 36 | + if conn_fails: |
| 37 | + mock_conn.close.side_effect = Exception("Connection close failed") |
| 38 | + if cursor_fails: |
| 39 | + mock_cursor.close.side_effect = Exception("Cursor close failed") |
| 40 | + |
| 41 | + # Mock schema inference to succeed |
| 42 | + expected_schema = StructType([StructField("test_col", StringType())]) |
| 43 | + driver.infer_schema_from_description = Mock(return_value=expected_schema) |
| 44 | + |
| 45 | + # Test - should succeed despite close errors and log the failure |
| 46 | + with patch( |
| 47 | + "snowflake.snowpark._internal.data_source.drivers.base_driver.logger" |
| 48 | + ) as mock_logger: |
| 49 | + result = driver.infer_schema_from_description_with_error_control( |
| 50 | + "test_table", False |
| 51 | + ) |
| 52 | + |
| 53 | + assert result == expected_schema |
| 54 | + |
| 55 | + # Use regex to match the log message flexibly |
| 56 | + mock_logger.debug.assert_called() |
| 57 | + args, kwargs = mock_logger.debug.call_args |
| 58 | + assert re.search(r"Failed to close", args[0]) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "cursor_fails,conn_fails", |
| 63 | + [ |
| 64 | + (True, False), # cursor.close() fails |
| 65 | + (False, True), # connection.close() fails |
| 66 | + (True, True), # both fail |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_datasource_reader_close_error_handling(cursor_fails, conn_fails): |
| 70 | + """Test that DataSourceReader handles cursor/connection close errors gracefully.""" |
| 71 | + # Setup mocks |
| 72 | + mock_create_connection = Mock() |
| 73 | + expected_schema = StructType([StructField("test_col", StringType())]) |
| 74 | + |
| 75 | + # Mock driver, connection, and cursor |
| 76 | + mock_driver = Mock() |
| 77 | + mock_conn = Mock() |
| 78 | + mock_cursor = Mock() |
| 79 | + |
| 80 | + # Configure the mock chain |
| 81 | + mock_driver.prepare_connection.return_value = mock_conn |
| 82 | + mock_driver.create_connection.return_value = mock_conn |
| 83 | + mock_conn.cursor.return_value = mock_cursor |
| 84 | + mock_cursor.fetchall.return_value = [("test_data",)] |
| 85 | + |
| 86 | + # Configure failures |
| 87 | + if cursor_fails: |
| 88 | + mock_cursor.close.side_effect = Exception("Cursor close failed") |
| 89 | + if conn_fails: |
| 90 | + mock_conn.close.side_effect = Exception("Connection close failed") |
| 91 | + |
| 92 | + # Create mock driver class that returns our mock driver |
| 93 | + mock_driver_class = Mock(return_value=mock_driver) |
| 94 | + |
| 95 | + # Create reader with the mock driver class |
| 96 | + reader = DataSourceReader( |
| 97 | + driver_class=mock_driver_class, |
| 98 | + create_connection=mock_create_connection, |
| 99 | + schema=expected_schema, |
| 100 | + dbms_type=DBMS_TYPE.UNKNOWN, |
| 101 | + fetch_size=0, # Use 0 to trigger fetchall() path |
| 102 | + query_timeout=0, |
| 103 | + session_init_statement=None, |
| 104 | + fetch_merge_count=1, |
| 105 | + ) |
| 106 | + |
| 107 | + # Test - should succeed despite close errors and log the failure |
| 108 | + with patch( |
| 109 | + "snowflake.snowpark._internal.data_source.datasource_reader.logger" |
| 110 | + ) as mock_logger: |
| 111 | + # Consume the generator to trigger execution of the finally block |
| 112 | + list(reader.read("SELECT * FROM test")) |
| 113 | + |
| 114 | + mock_logger.debug.assert_called() |
| 115 | + args, kwargs = mock_logger.debug.call_args |
| 116 | + assert re.search(r"Failed to close", args[0]) |
0 commit comments