Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test(hooks): harden raw-SQL hook coverage with branch-level tests (#3545)

LearningCircuit3e352ffeeaba
diff --git a/tests/hooks/test_custom_checks_raw_sql.py b/tests/hooks/test_custom_checks_raw_sql.py
index 89b0b3b11e..be4b18049b 100644
--- a/tests/hooks/test_custom_checks_raw_sql.py
+++ b/tests/hooks/test_custom_checks_raw_sql.py
@@ -22,9 +22,10 @@ HOOK_SCRIPT = (
def _run_hook(content: str, filename: str) -> subprocess.CompletedProcess:
- """Write content to a temp file with a specific basename and run the hook."""
+ """Write content to a temp file with a specific basename (may include subdirs) and run the hook."""
with tempfile.TemporaryDirectory() as d:
path = Path(d) / filename
+ path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
return subprocess.run(
[sys.executable, str(HOOK_SCRIPT), str(path)],
@@ -155,3 +156,140 @@ class TestAllowsLegitimateUsage:
"auth_db.py",
)
assert result.returncode == 0
+
+
+class TestExecuteCallVariants:
+ """Every execute-call variant in db_execute_patterns must be flagged.
+
+ Locks in current behavior so a refactor of the pattern list can't silently
+ drop coverage for one of the call styles.
+ """
+
+ def test_cursor_executemany_flagged(self):
+ result = _run_hook(
+ 'cursor.executemany("INSERT INTO users VALUES (?)", rows)\n',
+ "service.py",
+ )
+ assert result.returncode == 1
+
+ def test_conn_execute_flagged(self):
+ result = _run_hook(
+ 'conn.execute("DELETE FROM users WHERE id = 1")\n',
+ "service.py",
+ )
+ assert result.returncode == 1
+
+ def test_connection_execute_flagged(self):
+ result = _run_hook(
+ 'connection.execute("SELECT * FROM users")\n',
+ "service.py",
+ )
+ assert result.returncode == 1
+
+
+class TestExemptionBranches:
+ """Every branch of _is_raw_sql_exempt must be covered.
+
+ If any branch silently regresses (e.g. DB_UTIL_FILES entry removed,
+ _test.py suffix check dropped), one of these will fail.
+ """
+
+ def test_tests_directory_path_exempted(self):
+ """Files under a /tests/ directory are exempt even without test_ prefix."""
+ result = _run_hook(
+ 'cursor.execute("DELETE FROM users")\n',
+ "tests/helpers.py",
+ )
+ assert result.returncode == 0
+
+ def test_underscore_test_suffix_exempted(self):
+ """The `_test.py` suffix (Go-style) is an exempted test convention."""
+ result = _run_hook(
+ 'cursor.execute("DELETE FROM users")\n',
+ "model_test.py",
+ )
+ assert result.returncode == 0
+
+ def test_alembic_filename_exempted(self):
+ result = _run_hook(
+ 'cursor.execute("CREATE TABLE foo (id INT)")\n',
+ "alembic_env.py",
+ )
+ assert result.returncode == 0
+
+ def test_sqlcipher_utils_exempted(self):
+ result = _run_hook(
+ 'cursor.execute("SELECT * FROM users")\n',
+ "sqlcipher_utils.py",
+ )
+ assert result.returncode == 0
+
+ def test_backup_service_exempted(self):
+ result = _run_hook(
+ 'cursor.execute("SELECT * FROM users")\n',
+ "backup_service.py",
+ )
+ assert result.returncode == 0
+
+ def test_encrypted_db_exempted(self):
+ result = _run_hook(
+ 'cursor.execute("SELECT * FROM users")\n',
+ "encrypted_db.py",
+ )
+ assert result.returncode == 0
+
+
+class TestSkipLogic:
+ """Lines that start with comments / docstrings / are empty must be skipped.
+
+ The skip block at the top of the per-line loop is load-bearing: without it,
+ any file explaining SQL in comments would be flagged. Lock in its behavior.
+ """
+
+ def test_full_line_comment_with_sql_not_flagged(self):
+ """A comment line containing SQL keywords must not be flagged."""
+ result = _run_hook(
+ '# Example: cursor.execute("SELECT * FROM users")\n',
+ "service.py",
+ )
+ assert result.returncode == 0
+
+ def test_docstring_line_with_sql_not_flagged(self):
+ """A line that starts with triple-quotes must be skipped."""
+ result = _run_hook(
+ '"""cursor.execute(\'SELECT * FROM users\')"""\n',
+ "service.py",
+ )
+ assert result.returncode == 0
+
+ def test_single_quote_docstring_line_not_flagged(self):
+ result = _run_hook(
+ "'''SELECT * FROM users'''\n",
+ "service.py",
+ )
+ assert result.returncode == 0
+
+
+class TestOrmPatternsNotFlagged:
+ """Legitimate ORM builder chains must never trigger raw-SQL detection."""
+
+ def test_filter_by_not_flagged(self):
+ result = _run_hook(
+ "session.query(User).filter_by(id=1).all()\n",
+ "service.py",
+ )
+ assert result.returncode == 0
+
+ def test_join_order_by_not_flagged(self):
+ result = _run_hook(
+ "session.query(User).join(Role).order_by(User.id).all()\n",
+ "service.py",
+ )
+ assert result.returncode == 0
+
+ def test_group_by_not_flagged(self):
+ result = _run_hook(
+ "session.query(User).group_by(User.role).all()\n",
+ "service.py",
+ )
+ assert result.returncode == 0