# 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