From 8f6c5be314913c6a049c7efb4e2bed4cd570c94e Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 5 Oct 2026 09:38:56 +0200 Subject: [PATCH] Query plan gate --- pyproject.toml | 2 +- tests/unit/test_query_plans.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_query_plans.py diff --git a/pyproject.toml b/pyproject.toml index 190564d..476732c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "molodetz" -version = "1.0.2" +version = "1.0.3" description = "Molodetz, a calm community blog roll." readme = "README.md" requires-python = ">=3.12" diff --git a/tests/unit/test_query_plans.py b/tests/unit/test_query_plans.py new file mode 100644 index 0000000..d58d78a --- /dev/null +++ b/tests/unit/test_query_plans.py @@ -0,0 +1,27 @@ +# retoor +import sqlite3 + +import pytest + +from tests.conftest import TEST_DATA_DIR + +QUERIES = [ + ("idx_posts_published", "SELECT * FROM posts WHERE topic = 'rol' AND status = 'published' AND deleted_at IS NULL ORDER BY published_at DESC LIMIT 25"), + ("idx_posts_slug", "SELECT * FROM posts WHERE slug = 'x'"), + ("idx_sessions_token", "SELECT * FROM sessions WHERE token = 'x'"), + ("idx_users_api_key", "SELECT * FROM users WHERE api_key = 'x'"), + ("idx_media_items_kind", "SELECT * FROM media_items WHERE kind = 'flyer' AND deleted_at IS NULL ORDER BY position, id"), + ("idx_notifications_user", "SELECT * FROM notifications WHERE user_uid = 'x' ORDER BY created_at DESC LIMIT 50"), + ("idx_audit_log_created", "SELECT uid FROM audit_log WHERE created_at < '2020-01-01'"), +] + + +@pytest.mark.parametrize("index,sql", QUERIES, ids=[item[0] for item in QUERIES]) +def test_index_is_used_without_temp_btree(app_server, index, sql): + connection = sqlite3.connect(TEST_DATA_DIR / "molodetz.db") + try: + plan = " | ".join(row[-1] for row in connection.execute(f"EXPLAIN QUERY PLAN {sql}")) + finally: + connection.close() + assert f"USING INDEX {index}" in plan or f"USING COVERING INDEX {index}" in plan, plan + assert "TEMP B-TREE" not in plan, plan