-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sqlwool.py
455 lines (385 loc) · 18.1 KB
/
test_sqlwool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# tests/test_sqlwool.py
import sqlite3
from pathlib import Path
from unittest import mock
import pytest
from sqwool import (
Connection,
ExtensionsManager,
PlatformInfo,
SQLiteSystemInfo,
connect,
)
class TestPlatformInfo:
@mock.patch("platform.system")
@mock.patch("platform.machine")
def test_get_platform_info_macos_arm(self, mock_machine, mock_system):
mock_system.return_value = "Darwin"
mock_machine.return_value = "arm64"
expected = {"system": "macos", "arch": "arm64", "extensions_dir": "macos-arm64"}
assert PlatformInfo.get_platform_info() == expected
@mock.patch("platform.system")
@mock.patch("platform.machine")
def test_get_platform_info_windows_x64(self, mock_machine, mock_system):
mock_system.return_value = "Windows"
mock_machine.return_value = "AMD64"
expected = {"system": "windows", "arch": "x64", "extensions_dir": "win-x64"}
assert PlatformInfo.get_platform_info() == expected
@mock.patch("platform.system")
@mock.patch("platform.machine")
def test_get_platform_info_linux_arm64(self, mock_machine, mock_system):
mock_system.return_value = "Linux"
mock_machine.return_value = "aarch64"
expected = {"system": "linux", "arch": "arm64", "extensions_dir": "linux-arm64"}
assert PlatformInfo.get_platform_info() == expected
@mock.patch("platform.system")
@mock.patch("platform.machine")
def test_get_platform_info_unsupported(self, mock_machine, mock_system):
mock_system.return_value = "UnknownOS"
mock_machine.return_value = "unknown"
with pytest.raises(RuntimeError) as excinfo:
PlatformInfo.get_platform_info()
assert "Unsupported platform" in str(excinfo.value)
@mock.patch.object(PlatformInfo, "get_platform_info")
def test_is_supported_platform_true(self, mock_get_info):
mock_get_info.return_value = {
"system": "linux",
"arch": "x86",
"extensions_dir": "linux-x86",
}
assert PlatformInfo.is_supported_platform() is True
@mock.patch.object(
PlatformInfo, "get_platform_info", side_effect=RuntimeError("Unsupported")
)
def test_is_supported_platform_false(self, mock_get_info):
assert PlatformInfo.is_supported_platform() is False
@pytest.fixture
def manager():
with mock.patch("sqwool.PlatformInfo.get_platform_info") as mock_get_info:
mock_get_info.return_value = {
"system": "linux",
"arch": "x86",
"extensions_dir": "linux-x86",
}
return ExtensionsManager(base_dir=Path("/fake/extensions"))
@pytest.fixture
def manager_factory():
"""
Fixture that returns a factory function to create ExtensionsManager instances
with a specified allowlist.
"""
with mock.patch("sqwool.PlatformInfo.get_platform_info") as mock_get_info:
mock_get_info.return_value = {
"system": "linux",
"arch": "x86",
"extensions_dir": "linux-x86",
}
def _create_manager(allowlist=None, base_dir=Path("/fake/extensions")):
"""
Factory function to create an ExtensionsManager instance.
Args:
allowlist (List[str], optional): List of allowed extensions.
base_dir (Path, optional): Base directory for extensions.
Returns:
ExtensionsManager: Configured ExtensionsManager instance.
"""
return ExtensionsManager(base_dir=base_dir, allowlist=allowlist)
return _create_manager
class TestExtensionsManager:
def test_initialization(self, manager):
assert manager.platform_info["system"] == "linux"
assert manager.platform_info["arch"] == "x86"
assert manager.platform_info["extensions_dir"] == "linux-x86"
assert manager.base_dir == Path("/fake/extensions")
assert manager.platform_dir == Path("/fake/extensions/linux-x86")
assert manager.extension_patterns["linux"] == ".so"
@mock.patch("os.makedirs")
def test_setup_directories(self, mock_makedirs, manager):
manager.setup_directories()
expected_dirs = [
Path("/fake/extensions"),
Path("/fake/extensions/win-x64"),
Path("/fake/extensions/linux-x86"),
Path("/fake/extensions/linux-arm64"),
Path("/fake/extensions/macos-x86"),
Path("/fake/extensions/macos-arm64"),
]
calls = [mock.call(dir, exist_ok=True) for dir in expected_dirs]
mock_makedirs.assert_has_calls(calls, any_order=True)
assert mock_makedirs.call_count == len(expected_dirs)
def test_should_load_all_allowlist(self, manager_factory):
# Define the allowlist to load all extensions
allowlist = ["__ALL__"]
# Initialize ExtensionsManager with the "__ALL__" allowlist
manager = manager_factory(allowlist=allowlist)
# Define a sample extension path
ext = Path("uuid.so")
# Assert that the extension should be loaded
assert manager._should_load(ext) is True
def test_should_load_allowed_extension(self, manager_factory):
# Define the allowlist with a specific extension
allowlist = ["UUID"]
# Initialize ExtensionsManager with the specific allowlist
manager = manager_factory(allowlist=allowlist)
# Define a sample extension path
ext = Path("uuid.so")
# Assert that the extension should be loaded
assert manager._should_load(ext) is True
def test_should_load_allowed_extension_case_insensitive(self, manager_factory):
# Define the allowlist with lowercase extension name
allowlist = ["uuid"]
# Initialize ExtensionsManager with the specific allowlist
manager = manager_factory(allowlist=allowlist)
# Define a sample extension path with uppercase name
ext = Path("UUID.so")
# Assert that the extension should be loaded (case-insensitive)
assert manager._should_load(ext) is True
def test_should_not_load_disallowed_extension(self, manager_factory):
# Define an allowlist that does not include "malicious"
allowlist = ["UUID"]
# Initialize ExtensionsManager with the specific allowlist
manager = manager_factory(allowlist=allowlist)
# Define a sample extension path that is not allowed
ext = Path("malicious.so")
# Assert that the extension should NOT be loaded
assert manager._should_load(ext) is False
@mock.patch("sqwool.core.Path.exists", return_value=True)
def test_validate_extension_valid(self, mock_exists, manager):
ext = Path("uuid.so")
assert manager.validate_extension(ext) is True
def test_validate_extension_invalid_suffix(self, manager):
ext = Path("uuid.dll")
assert manager.validate_extension(ext) is False
@mock.patch("sqwool.core.Path.exists", return_value=False)
def test_validate_extension_nonexistent_file(self, mock_exists, manager):
ext = Path("/nonexistent/uuid.so")
assert manager.validate_extension(ext) is False
class TestSQLiteSystemInfo:
@mock.patch("platform.system")
def test_get_sqlite_info_macos(self, mock_system):
mock_system.return_value = "Darwin"
info = SQLiteSystemInfo.get_sqlite_info()
assert info["system"] == "darwin"
assert info["can_load_extensions"] is False
assert info["reason"] == "Default macOS SQLite3 build has extensions disabled"
assert info["solution"] == "Use homebrew sqlite: brew install sqlite3"
@mock.patch("platform.system")
def test_get_sqlite_info_linux(self, mock_system):
mock_system.return_value = "Linux"
info = SQLiteSystemInfo.get_sqlite_info()
assert info["system"] == "linux"
assert info["can_load_extensions"] is True
assert info["reason"] == "System SQLite build supports extensions"
assert info["solution"] is None
@mock.patch("platform.system")
@mock.patch("os.path.exists")
@mock.patch("subprocess.run")
def test_find_sqlite_binary_homebrew(self, mock_run, mock_exists, mock_system):
mock_system.return_value = "Darwin"
# Mock Homebrew paths
def side_effect(path):
return path in ["/opt/homebrew/opt/sqlite/bin/sqlite3"]
mock_exists.side_effect = side_effect
binary = SQLiteSystemInfo.find_sqlite_binary()
assert binary == "/opt/homebrew/opt/sqlite/bin/sqlite3"
@mock.patch("platform.system")
@mock.patch("os.path.exists", return_value=False)
@mock.patch("subprocess.run")
def test_find_sqlite_binary_which(self, mock_run, mock_exists, mock_system):
mock_system.return_value = "Linux"
mock_run.return_value = mock.Mock(returncode=0, stdout="/usr/bin/sqlite3\n")
binary = SQLiteSystemInfo.find_sqlite_binary()
assert binary == "/usr/bin/sqlite3"
@mock.patch("platform.system")
@mock.patch("os.path.exists", return_value=False)
@mock.patch("subprocess.run", side_effect=Exception("Error"))
def test_find_sqlite_binary_not_found(self, mock_run, mock_exists, mock_system):
mock_system.return_value = "Linux"
binary = SQLiteSystemInfo.find_sqlite_binary()
assert binary is None
class TestConnection:
@pytest.fixture
def connection(self):
# Patch sqlite3.connect to return a mock Connection
with mock.patch("sqlite3.connect") as mock_sqlite_connect:
mock_conn = mock.Mock(spec=Connection)
mock_sqlite_connect.return_value = mock_conn
conn = Connection(":memory:")
return conn
def test_init_extensions_enabled(self, connection, manager_factory):
with mock.patch.object(connection, "enable_load_extension") as mock_enable:
# Patch ExtensionsManager methods and os.makedirs
with (
mock.patch.object(
ExtensionsManager, "get_platform_extensions", return_value=[]
),
mock.patch.object(
ExtensionsManager, "validate_extension", return_value=True
),
mock.patch("os.makedirs") as mock_makedirs,
):
# Initialize extensions with manager_factory
manager = manager_factory(allowlist=["__ALL__"])
connection.extensions_manager = manager
connection.init_extensions(extensions_dir="/fake/extensions")
# Assert that 'enable_load_extension' was called correctly
mock_enable.assert_called_with(True)
# Assert that extensions were enabled
assert connection._extensions_enabled is True
# Define expected calls to 'os.makedirs'
expected_calls = [
mock.call(Path("/fake/extensions"), exist_ok=True),
mock.call(Path("/fake/extensions/win-x64"), exist_ok=True),
mock.call(Path("/fake/extensions/linux-x86"), exist_ok=True),
mock.call(Path("/fake/extensions/linux-arm64"), exist_ok=True),
mock.call(Path("/fake/extensions/macos-x86"), exist_ok=True),
mock.call(Path("/fake/extensions/macos-arm64"), exist_ok=True),
]
# Assert that 'os.makedirs' was called with all expected directories
mock_makedirs.assert_has_calls(expected_calls, any_order=True)
# Optionally, assert that 'os.makedirs' was called the correct number of times
assert mock_makedirs.call_count == len(expected_calls)
def test_init_extensions_disabled(self, connection, manager_factory):
with mock.patch.object(
connection,
"enable_load_extension",
side_effect=sqlite3.OperationalError("Disabled"),
):
with mock.patch(
"sqwool.PlatformInfo.get_platform_info",
return_value={
"system": "linux",
"arch": "x86",
"extensions_dir": "linux-x86",
},
):
with (
mock.patch("logging.warning") as mock_log,
mock.patch("os.makedirs") as mock_makedirs,
):
connection.init_extensions(extensions_dir="/fake/extensions")
# Assert that a warning was logged
mock_log.assert_called()
# Assert that extensions were not enabled
assert connection._extensions_enabled is False
# Define expected calls to 'os.makedirs'
expected_calls = [
mock.call(Path("/fake/extensions"), exist_ok=True),
mock.call(Path("/fake/extensions/win-x64"), exist_ok=True),
mock.call(Path("/fake/extensions/linux-x86"), exist_ok=True),
mock.call(Path("/fake/extensions/linux-arm64"), exist_ok=True),
mock.call(Path("/fake/extensions/macos-x86"), exist_ok=True),
mock.call(Path("/fake/extensions/macos-arm64"), exist_ok=True),
]
# Assert that 'os.makedirs' was called with all expected directories
mock_makedirs.assert_has_calls(expected_calls, any_order=True)
# Optionally, assert that 'os.makedirs' was called the correct number of times
assert mock_makedirs.call_count == len(expected_calls)
def test_load_platform_extensions_success(self, connection, manager_factory):
with mock.patch.object(connection, "_extensions_enabled", True):
with (
mock.patch.object(
ExtensionsManager,
"get_platform_extensions",
return_value=[Path("uuid.so")],
),
mock.patch.object(
ExtensionsManager, "validate_extension", return_value=True
),
mock.patch.object(connection, "load_extension") as mock_load_ext,
):
connection.extensions_manager = manager_factory(allowlist=["UUID"])
connection._load_platform_extensions()
mock_load_ext.assert_called_with("uuid.so")
def test_load_platform_extensions_invalid_extension(
self, connection, manager_factory
):
with mock.patch.object(connection, "_extensions_enabled", True):
with (
mock.patch.object(
ExtensionsManager,
"get_platform_extensions",
return_value=[Path("malicious.dll")],
),
mock.patch.object(
ExtensionsManager, "validate_extension", return_value=False
),
):
with mock.patch("logging.warning") as mock_log:
connection.extensions_manager = manager_factory(
allowlist=["__ALL__"]
)
connection._load_platform_extensions()
mock_log.assert_called_with(
"Invalid extension for platform: malicious.dll"
)
def test_load_platform_extensions_load_failure(self, connection, manager_factory):
with mock.patch.object(connection, "_extensions_enabled", True):
with (
mock.patch.object(
ExtensionsManager,
"get_platform_extensions",
return_value=[Path("uuid.so")],
),
mock.patch.object(
ExtensionsManager, "validate_extension", return_value=True
),
mock.patch.object(
connection,
"load_extension",
side_effect=sqlite3.Error("Load failed"),
),
):
with mock.patch("logging.warning") as mock_log:
connection.extensions_manager = manager_factory(allowlist=["UUID"])
connection._load_platform_extensions()
mock_log.assert_called_with(
"Failed to load extension uuid.so: Load failed"
)
def test_extensions_supported_true(self, connection):
connection._extensions_enabled = True
assert connection.extensions_supported is True
def test_extensions_supported_false(self, connection):
connection._extensions_enabled = False
assert connection.extensions_supported is False
class TestConnectFunction:
@mock.patch("sqwool.Connection")
@mock.patch("sqlite3.connect")
def test_connect_returns_custom_connection(
self, mock_sqlite_connect, mock_custom_conn
):
mock_sqlite_connect.return_value = mock_custom_conn
conn = connect("test.db")
mock_sqlite_connect.assert_called_with(
database="test.db",
timeout=5.0,
detect_types=0,
isolation_level=None,
check_same_thread=True,
factory=Connection,
cached_statements=128,
uri=False,
)
mock_custom_conn.init_extensions.assert_called_with(
extensions_dir=None, allowlist=None
)
assert conn == mock_custom_conn
@mock.patch("sqwool.Connection")
@mock.patch("sqlite3.connect")
def test_connect_with_extensions_dir(self, mock_sqlite_connect, mock_custom_conn):
mock_sqlite_connect.return_value = mock_custom_conn
conn = connect("test.db", extensions_dir="/fake/extensions")
mock_sqlite_connect.assert_called_with(
database="test.db",
timeout=5.0,
detect_types=0,
isolation_level=None,
check_same_thread=True,
factory=Connection,
cached_statements=128,
uri=False,
)
mock_custom_conn.init_extensions.assert_called_with(
extensions_dir="/fake/extensions", allowlist=None
)
assert conn == mock_custom_conn