aboutsummaryrefslogtreecommitdiff
path: root/tests/test_config.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-17 21:06:05 +0100
committerDennis Fink2026-11-17 21:06:05 +0100
commitc4c17e3e046a627a7d2353c734bc69f05d4fc3b1 (patch)
treeebc343ce994c0c32243e1f8b7e654ee902d714bc /tests/test_config.py
parent5e24e8307f650e72881f747661a2b6e52db15f2d (diff)
downloadwebmentions-ssg-c4c17e3e046a627a7d2353c734bc69f05d4fc3b1.tar.gz
webmentions-ssg-c4c17e3e046a627a7d2353c734bc69f05d4fc3b1.zip
feat(core): add configurable SQLite pragmas
Apply validated SQLite settings through SQLAlchemy's connection event so every SQLite connection and bind receives consistent foreign key, WAL, timeout, and security defaults. Allow deployments to override supported PRAGMAs safely while skipping file-only settings for in-memory databases and leaving other database backends untouched.
Diffstat (limited to 'tests/test_config.py')
-rw-r--r--tests/test_config.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/test_config.py b/tests/test_config.py
index 81ff498..ca87403 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -497,3 +497,64 @@ def test_proxy_fix_can_be_configured_via_environment(
app = create_app(EnvProxyConfig)
assert app.config["PROXY_FIX_TRUSTED_HOPS"] == 1
assert app.wsgi_app.__class__.__name__ == "ProxyFix"
+
+
+def test_sqlite_pragma_overrides_keep_default_settings(tmp_path: Path) -> None:
+ class PartialSQLiteConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "partial-sqlite-test.log")
+ SQLITE_PRAGMAS = {"busy_timeout": 10000, "synchronous": "normal"}
+
+ app = create_app(PartialSQLiteConfig)
+ assert app.config["SQLITE_PRAGMAS"] == {
+ "foreign_keys": True,
+ "journal_mode": "WAL",
+ "busy_timeout": 10000,
+ "trusted_schema": False,
+ "synchronous": "NORMAL",
+ "cache_size": None,
+ "wal_autocheckpoint": None,
+ "journal_size_limit": None,
+ }
+ # The default mapping must not be modified by individual app overrides.
+ assert TestingConfig.SQLITE_PRAGMAS["busy_timeout"] == 5000
+
+
+@pytest.mark.parametrize(
+ "invalid",
+ [
+ "PRAGMA foreign_keys=OFF",
+ None,
+ {"foreign_keys": "OFF"},
+ {"journal_mode": "WAL; DROP TABLE users"},
+ {"journal_mode": "MEMORY"},
+ {"trusted_schema": 0},
+ {"busy_timeout": -1},
+ {"busy_timeout": True},
+ {"cache_size": 2**32},
+ {"wal_autocheckpoint": -1},
+ {"journal_size_limit": -2},
+ {"synchronous": "INVALID"},
+ {"unsupported_pragma": 42},
+ ],
+)
+def test_invalid_sqlite_pragmas_fail_at_startup(
+ tmp_path: Path, invalid: object
+) -> None:
+ class BadSQLiteConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "bad-sqlite-test.log")
+ SQLITE_PRAGMAS = invalid
+
+ with pytest.raises((TypeError, ValueError), match="SQLITE_PRAGMAS"):
+ create_app(BadSQLiteConfig)
+
+
+def test_uppercase_sqlite_pragma_keys_support_environment_convention(
+ tmp_path: Path,
+) -> None:
+ class EnvStyleSQLiteConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "env-style-sqlite-test.log")
+ SQLITE_PRAGMAS = {"CACHE_SIZE": -8192, "SYNCHRONOUS": "normal"}
+
+ app = create_app(EnvStyleSQLiteConfig)
+ assert app.config["SQLITE_PRAGMAS"]["cache_size"] == -8192
+ assert app.config["SQLITE_PRAGMAS"]["synchronous"] == "NORMAL"