How to Avoid SELECT * and Mock SQL Column Queries in Python

Mock a SQLite cursor to verify that queries specify explicit columns instead of using SELECT *.

Easy Python 3.9+ Aug 9, 2026 Database scaling & optimization 13 views 0 copies

Python code

26 lines
Python 3.9+
import sqlite3
from unittest.mock import Mock, patch


def get_user_emails(connection):
    """Fetch only the required columns instead of SELECT *."""
    cursor = connection.cursor()
    cursor.execute("SELECT email FROM users")
    return [row[0] for row in cursor.fetchall()]


def test_get_user_emails_specific_columns():
    mock_connection = Mock()
    mock_cursor = mock_connection.cursor.return_value
    mock_cursor.fetchall.return_value = [("alice@example.com",), ("bob@example.com",)]
    
    emails = get_user_emails(mock_connection)
    
    # Verify the exact SQL with explicit column names
    mock_cursor.execute.assert_called_with("SELECT email FROM users")
    assert emails == ["alice@example.com", "bob@example.com"]


if __name__ == "__main__":
    test_get_user_emails_specific_columns()
    print("All tests passed - query explicitly specifies columns, avoiding SELECT *")

Output

stdout
All tests passed - query explicitly specifies columns, avoiding SELECT *

How it works

The test creates a Mock connection and cursor to simulate a database without a real connection. The cursor.execute call is asserted to have the exact SQL string 'SELECT email FROM users', enforcing explicit column selection. The fetchall return value is a list of tuples, each containing a single email, matching the row[0] extraction. This pattern ensures your code avoids SELECT * which can hurt performance and maintainability. Using mocks isolates the query logic from actual database dependencies, making tests fast and reliable.

Common mistakes

  • Forgetting to set `mock_cursor.fetchall.return_value` before calling the function.
  • Using `SELECT *` in the query, defeating the purpose of the test.
  • Not asserting the exact SQL string, allowing silent regression to star selects.

Variations

  1. Use `pytest` with `monkeypatch` to replace `connection.cursor` for integration tests.
  2. Use `moto` or a real test database to verify column selection end-to-end.

Real-world use cases

  • Enforcing query best practices in a code review pipeline to prevent inefficient full-table scans.
  • Unit testing data access functions without spinning up a real database in CI.
  • Refactoring legacy SQL to explicit columns to mitigate schema change breakages.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Database scaling & optimization

Related tutorials and quizzes for this topic.