Skip to content

Python · testing

no-raw-sql-in-tests

python:no-raw-sql-in-tests

Tests should seed records through store or service methods instead of raw SQL inserts.

Code
SARJ036
Default
error
Fix
none
Languages
python

Why

Raw inserts bypass the validation, defaults, events, and invariants exercised by production writes.

Fix

Create test records through the owning store or service API.

Before / after

Executed by this rule’s unit tests.

Before

Test seeds data with raw SQL

tests/test_call_store.py · focus
async def test_create(conn):
    await conn.execute("INSERT INTO call (id) VALUES (1)")

After

Test seeds data through the store API

tests/test_call_store.py · focus
async def test_create(store):
    await store.insert(make_call())

Limits

  • Only literal `INSERT INTO` statements passed to known execution methods in test files are reported.
  • Fixtures in `conftest.py`, migrations, dynamic SQL, reads, updates, and cleanup statements are excluded.