Debian Codegraph @

tests/test_load_extensions.py

raw
1 from datasette.app import Datasette
2 import pytest
3 from pathlib import Path
4
5 # not necessarily a full path - the full compiled path looks like "ext.dylib"
6 # or another suffix, but sqlite will, under the hood, decide which file
7 # extension to use based on the operating system (apple=dylib, windows=dll etc)
8 # this resolves to "./ext", which is enough for SQLite to calculate the rest
9 COMPILED_EXTENSION_PATH = str(Path(__file__).parent / "ext")
10
11
12 # See if ext.c has been compiled, based off the different possible suffixes.
13 def has_compiled_ext():
14 for ext in ["dylib", "so", "dll"]:
15 path = Path(__file__).parent / f"ext.{ext}"
16 if path.is_file():
17 return True
18 return False
19
20
21 @pytest.mark.asyncio
22 @pytest.mark.skipif(not has_compiled_ext(), reason="Requires compiled ext.c")
23 async def test_load_extension_default_entrypoint():
24 # The default entrypoint only loads a() and NOT b() or c(), so those
25 # should fail.
26 ds = Datasette(sqlite_extensions=[COMPILED_EXTENSION_PATH])
27
28 response = await ds.client.get("/_memory.json?sql=select+a()")
29 assert response.status_code == 200
30 assert response.json()["rows"][0][0] == "a"
31
32 response = await ds.client.get("/_memory.json?sql=select+b()")
33 assert response.status_code == 400
34 assert response.json()["error"] == "no such function: b"
35
36 response = await ds.client.get("/_memory.json?sql=select+c()")
37 assert response.status_code == 400
38 assert response.json()["error"] == "no such function: c"
39
40
41 @pytest.mark.asyncio
42 @pytest.mark.skipif(not has_compiled_ext(), reason="Requires compiled ext.c")
43 async def test_load_extension_multiple_entrypoints():
44 # Load in the default entrypoint and the other 2 custom entrypoints, now
45 # all a(), b(), and c() should run successfully.
46 ds = Datasette(
47 sqlite_extensions=[
48 COMPILED_EXTENSION_PATH,
49 (COMPILED_EXTENSION_PATH, "sqlite3_ext_b_init"),
50 (COMPILED_EXTENSION_PATH, "sqlite3_ext_c_init"),
51 ]
52 )
53
54 response = await ds.client.get("/_memory.json?sql=select+a()")
55 assert response.status_code == 200
56 assert response.json()["rows"][0][0] == "a"
57
58 response = await ds.client.get("/_memory.json?sql=select+b()")
59 assert response.status_code == 200
60 assert response.json()["rows"][0][0] == "b"
61
62 response = await ds.client.get("/_memory.json?sql=select+c()")
63 assert response.status_code == 200
64 assert response.json()["rows"][0][0] == "c"