|
| 1 | +from contextlib import contextmanager |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | + |
| 7 | +from deepnote_toolkit.sql.duckdb_sql import ( |
| 8 | + _get_duckdb_connection, |
| 9 | + _set_sample_size, |
| 10 | + _set_scan_all_frames, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@contextmanager |
| 15 | +def fresh_duckdb_connection(): |
| 16 | + import deepnote_toolkit.sql.duckdb_sql as duckdb_sql_module |
| 17 | + |
| 18 | + duckdb_sql_module._DEEPNOTE_DUCKDB_CONNECTION = None |
| 19 | + conn = _get_duckdb_connection() |
| 20 | + |
| 21 | + try: |
| 22 | + yield conn |
| 23 | + finally: |
| 24 | + conn.close() |
| 25 | + duckdb_sql_module._DEEPNOTE_DUCKDB_CONNECTION = None |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="function") |
| 29 | +def duckdb_connection(): |
| 30 | + with fresh_duckdb_connection() as conn: |
| 31 | + yield conn |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("extension_name", ["spatial", "excel"]) |
| 35 | +def test_extension_installed_and_loadable(duckdb_connection, extension_name): |
| 36 | + result = duckdb_connection.execute( |
| 37 | + f"SELECT installed FROM duckdb_extensions() WHERE extension_name = '{extension_name}'" |
| 38 | + ).fetchone() |
| 39 | + |
| 40 | + assert ( |
| 41 | + result is not None |
| 42 | + ), f"{extension_name} extension should be found in duckdb_extensions()" |
| 43 | + assert result[0] is True, f"{extension_name} extension should be installed" |
| 44 | + |
| 45 | + loaded_result = duckdb_connection.execute( |
| 46 | + f"SELECT loaded FROM duckdb_extensions() WHERE extension_name = '{extension_name}'" |
| 47 | + ).fetchone() |
| 48 | + assert loaded_result[0] is True, f"{extension_name} extension should be loaded" |
| 49 | + |
| 50 | + |
| 51 | +def test_connection_singleton_pattern(): |
| 52 | + conn1 = _get_duckdb_connection() |
| 53 | + conn2 = _get_duckdb_connection() |
| 54 | + |
| 55 | + assert conn1 is conn2, "Connection should be a singleton" |
| 56 | + |
| 57 | + |
| 58 | +def test_set_sample_size(duckdb_connection): |
| 59 | + _set_sample_size(duckdb_connection, 50000) |
| 60 | + result = duckdb_connection.execute( |
| 61 | + "SELECT value FROM duckdb_settings() WHERE name = 'pandas_analyze_sample'" |
| 62 | + ).fetchone() |
| 63 | + assert int(result[0]) == 50000 |
| 64 | + |
| 65 | + |
| 66 | +def test_set_scan_all_frames(duckdb_connection): |
| 67 | + _set_scan_all_frames(duckdb_connection, False) |
| 68 | + result = duckdb_connection.execute( |
| 69 | + "SELECT value FROM duckdb_settings() WHERE name = 'python_scan_all_frames'" |
| 70 | + ).fetchone() |
| 71 | + assert result[0] == "false" |
| 72 | + |
| 73 | + _set_scan_all_frames(duckdb_connection, True) |
| 74 | + result = duckdb_connection.execute( |
| 75 | + "SELECT value FROM duckdb_settings() WHERE name = 'python_scan_all_frames'" |
| 76 | + ).fetchone() |
| 77 | + assert result[0] == "true" |
| 78 | + |
| 79 | + |
| 80 | +@mock.patch("deepnote_toolkit.sql.duckdb_sql.import_extension") |
| 81 | +def test_connection_returns_successfully_when_import_extension_fails( |
| 82 | + mock_import_extension, |
| 83 | +): |
| 84 | + mock_import_extension.side_effect = Exception("Failed to import extension") |
| 85 | + |
| 86 | + with fresh_duckdb_connection() as conn: |
| 87 | + assert conn is not None |
| 88 | + result = conn.execute( |
| 89 | + "SELECT extension_name, loaded FROM duckdb_extensions()" |
| 90 | + ).df() |
| 91 | + assert result is not None |
| 92 | + # check that spatial and excel extensions are not loaded as import extension failed |
| 93 | + result = result[result["extension_name"].isin(["spatial", "excel"])] |
| 94 | + assert all(result["loaded"]) is False |
| 95 | + |
| 96 | + |
| 97 | +@mock.patch("duckdb.DuckDBPyConnection.load_extension") |
| 98 | +def test_connection_returns_successfully_when_load_extension_fails(mock_load_extension): |
| 99 | + mock_load_extension.side_effect = Exception("Failed to load extension") |
| 100 | + |
| 101 | + with fresh_duckdb_connection() as conn: |
| 102 | + assert conn is not None |
| 103 | + result = conn.execute( |
| 104 | + "SELECT extension_name, loaded FROM duckdb_extensions()" |
| 105 | + ).df() |
| 106 | + assert result is not None |
| 107 | + # check that spatial and excel extensions are not loaded as import extension failed |
| 108 | + result = result[result["extension_name"].isin(["spatial", "excel"])] |
| 109 | + assert all(result["loaded"]) is False |
| 110 | + |
| 111 | + |
| 112 | +def test_excel_extension_roundtrip(duckdb_connection, tmp_path): |
| 113 | + test_data = pd.DataFrame( |
| 114 | + { |
| 115 | + "id": [1, 2, 3], |
| 116 | + "name": ["Alice", "Bob", "Charlie"], |
| 117 | + "score": [95.5, 87.3, 91.2], |
| 118 | + } |
| 119 | + ) |
| 120 | + duckdb_connection.register("test_table", test_data) |
| 121 | + excel_path = tmp_path / "test_data.xlsx" |
| 122 | + duckdb_connection.execute( |
| 123 | + f"COPY test_table TO '{excel_path}' WITH (FORMAT xlsx, HEADER true)" |
| 124 | + ) |
| 125 | + duckdb_connection.unregister("test_table") |
| 126 | + |
| 127 | + assert excel_path.exists(), "Excel file should be created" |
| 128 | + |
| 129 | + # read with spatial extension |
| 130 | + result = duckdb_connection.execute(f"SELECT * FROM st_read('{excel_path}')").df() |
| 131 | + diff = test_data.compare(result) |
| 132 | + assert diff.empty, "Data should be the same" |
| 133 | + |
| 134 | + # read with excel extension |
| 135 | + result = duckdb_connection.execute(f"SELECT * FROM read_xlsx('{excel_path}')").df() |
| 136 | + diff = test_data.compare(result) |
| 137 | + assert diff.empty, "Data should be the same" |
0 commit comments