|
1 | 1 | import typing as t |
2 | 2 |
|
| 3 | +import duckdb # noqa: TID253 |
3 | 4 | import pandas as pd # noqa: TID253 |
4 | 5 | import pytest |
5 | 6 | from pytest_mock.plugin import MockerFixture |
@@ -126,25 +127,77 @@ def test_drop_catalog_motherduck(make_mocked_engine_adapter: t.Callable) -> None |
126 | 127 | assert to_sql_calls(adapter) == ['DROP DATABASE IF EXISTS "foo" CASCADE'] |
127 | 128 |
|
128 | 129 |
|
129 | | -@pytest.fixture |
130 | | -def ducklake_conn(duck_conn): |
131 | | - """Load DuckLake without reinstalling it when a previous test already did. |
| 130 | +class DuckLakeInstallState: |
| 131 | + def __init__(self) -> None: |
| 132 | + self.ready = False |
| 133 | + |
| 134 | + |
| 135 | +_DUCKLAKE_STATE = DuckLakeInstallState() |
| 136 | + |
132 | 137 |
|
133 | | - On Windows, install_extension moves a file into the extension directory. |
134 | | - A ducklake.dll that is still mapped from an earlier test in this process |
135 | | - makes that move fail with "Access is denied". Installing only when |
136 | | - duckdb_extensions() says it is absent, then calling load_extension, avoids |
137 | | - the second install while the DLL is still mapped. |
| 138 | +def _disable_extension_auto_install(conn: duckdb.DuckDBPyConnection) -> None: |
| 139 | + # LOAD of a missing extension otherwise downloads and moves the file, the |
| 140 | + # same Windows failure as install_extension. |
| 141 | + for setting in ("autoinstall_known_extensions", "autoload_known_extensions"): |
| 142 | + try: |
| 143 | + conn.execute(f"SET {setting} = false") |
| 144 | + except duckdb.Error: |
| 145 | + continue |
| 146 | + |
| 147 | + |
| 148 | +def ensure_ducklake( |
| 149 | + conn: duckdb.DuckDBPyConnection, state: t.Optional[DuckLakeInstallState] = None |
| 150 | +) -> None: |
| 151 | + """Load DuckLake, installing it at most once per process. |
| 152 | +
|
| 153 | + install_extension replaces ducklake.duckdb_extension. On Windows that move |
| 154 | + fails with "Access is denied" while a DLL loaded by an earlier connection |
| 155 | + in this process is still mapped. A new connection can still report the |
| 156 | + extension as not installed, so the install decision cannot be made from |
| 157 | + duckdb_extensions() on every test. |
138 | 158 | """ |
139 | | - installed = duck_conn.execute( |
140 | | - "SELECT installed FROM duckdb_extensions() WHERE extension_name = 'ducklake'" |
141 | | - ).fetchone() |
142 | | - if not installed or not installed[0]: |
143 | | - duck_conn.install_extension("ducklake") |
144 | | - duck_conn.load_extension("ducklake") |
| 159 | + holder = _DUCKLAKE_STATE if state is None else state |
| 160 | + _disable_extension_auto_install(conn) |
| 161 | + if holder.ready: |
| 162 | + conn.load_extension("ducklake") |
| 163 | + return |
| 164 | + try: |
| 165 | + conn.load_extension("ducklake") |
| 166 | + except duckdb.Error: |
| 167 | + conn.install_extension("ducklake") |
| 168 | + conn.load_extension("ducklake") |
| 169 | + holder.ready = True |
| 170 | + |
| 171 | + |
| 172 | +@pytest.fixture |
| 173 | +def ducklake_conn(duck_conn): |
| 174 | + ensure_ducklake(duck_conn) |
145 | 175 | return duck_conn |
146 | 176 |
|
147 | 177 |
|
| 178 | +def test_ensure_ducklake_loads_without_install_when_already_present(mocker: MockerFixture): |
| 179 | + conn = mocker.Mock() |
| 180 | + state = DuckLakeInstallState() |
| 181 | + |
| 182 | + ensure_ducklake(conn, state) |
| 183 | + ensure_ducklake(conn, state) |
| 184 | + |
| 185 | + conn.install_extension.assert_not_called() |
| 186 | + assert conn.load_extension.call_args_list == [mocker.call("ducklake"), mocker.call("ducklake")] |
| 187 | + |
| 188 | + |
| 189 | +def test_ensure_ducklake_installs_once_when_load_fails(mocker: MockerFixture): |
| 190 | + conn = mocker.Mock() |
| 191 | + conn.load_extension.side_effect = [duckdb.IOException("missing"), None, None] |
| 192 | + state = DuckLakeInstallState() |
| 193 | + |
| 194 | + ensure_ducklake(conn, state) |
| 195 | + ensure_ducklake(conn, state) |
| 196 | + |
| 197 | + conn.install_extension.assert_called_once_with("ducklake") |
| 198 | + assert conn.load_extension.call_count == 3 |
| 199 | + |
| 200 | + |
148 | 201 | @pytest.mark.xdist_group("ducklake_extension") |
149 | 202 | def test_ducklake_partitioning(adapter: EngineAdapter, ducklake_conn, tmp_path): |
150 | 203 | catalog = "a_ducklake_db" |
|
0 commit comments