惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
P
Proofpoint News Feed
小众软件
小众软件
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
Martin Fowler
Martin Fowler
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
李成银的技术随笔
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
阮一峰的网络日志
阮一峰的网络日志
Hacker News: Ask HN
Hacker News: Ask HN
C
CERT Recently Published Vulnerability Notes
H
Help Net Security
The GitHub Blog
The GitHub Blog
S
Security Archives - TechRepublic
AWS News Blog
AWS News Blog
Project Zero
Project Zero
Security Latest
Security Latest
P
Privacy International News Feed
T
Troy Hunt's Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
Intezer
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
I
InfoQ
P
Proofpoint News Feed
C
Cisco Blogs
aimingoo的专栏
aimingoo的专栏
T
ThreatConnect
Recorded Future
Recorded Future
P
Palo Alto Networks Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
V2EX
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
G
GRAHAM CLULEY
F
Future of Privacy Forum
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
Engineering at Meta
Engineering at Meta

DEV Community

Google AI Studio Just Changed the Shape of App Development If you struggle to learn then this is for you. Best AI Agent Security & Guardrails Tools in 2026: LLM Guard vs NeMo vs Guardrails AI Building Dynamic RBAC in React 19: From Permission Strings to Component-Level Access Control How to Build a Self-Hosted AI Code Review Tool in Python Why We Switched from React to HTMX in Production: A 200-Site Case Study Gemma-Loom: The Intent-Based Virtual Machine (IVM) for Edge Sovereignty Java实习海投攻略:3天300个沟通,我是怎么拿到面试的 I Deployed Netflix's Web Server in 30 Seconds (And So Can You) - Docker Project 1 Debugging Android 14 WebRTC Disconnects on a coturn Relay Path 1/30 Days System Design Question FAQ Schema Markup Generators: What They Actually Do (and What They Don't Tell You) How a pure-TypeScript flex layout engine closed the last WASM-Yoga gap Spot instances as GitHub Actions runners Agents Need Receipts, Not Just Better Prompts readmegen — Generate beautiful README.md in seconds (12 templates, open source) When AI Reads Blueprints: The Hidden Attack Surface of Multimodal Engineering Intelligence Simplicity scales — complexity kills side projects AI does exactly what you ask — that's the problem How a model upgrade silently broke our extraction prompt (and how we caught it) The Best Form Backend for Static Sites in 2026 # ⛽ I Built a Cross-Platform Fuel Finder with React & Supabase: The Indie Dev Journey The 11 Major Cloud Service Providers in 2025 Membangun Karya Visual: Mengintip Fasilitas Multimedia dan Studio Kreatif Amikom What Is IOPS? Visualizing Database Design: From Interactive Canvas to Drizzle, Prisma, and SQL in Real-time A tool to make your GitHub README impossible to ignore 🚀 Zero-Downtime Blue-Green and IP-Based Canary Deployments on ECS Fargate I reproduced a Claude Code RCE. The bug pattern is everywhere. We Replaced Our RAG Pipeline With Persistent KV Cache. Here's What We Found. Jenkins CI/CD Pipeline for a Dockerized Node.js Application: Manual Trigger vs Automatic Trigger Using GitHub Webhooks How to Stream Live Forex Rates to Google Sheets API: A Complete Guide Small Models Will Beat Giant Models (And Most People Haven’t Realized Why Yet) How I Built 5 Linux Automation Scripts on AWS EC2 I built TokenPatch to measure AI coding cost per applied patch I built a Chrome extension to stop squinting at the web Producer audit clean, six tests red Conversa — A Multi-Agent AI Platform Powered by Gemma 4 Build a Real Agent in 15 Minutes with Gemini's New Managed Agents API What I Actually Build: AI Systems That Ship, Not Demos That Impress The Box Ticked While You Read This: LinkedIn, AI Training, and the Switch You Did Not Flip Investasi Masa Depan: Mengintip Fasilitas Laboratorium Komputer Kelas Dunia di Yogyakarta I Cancelled My $20 Claude Cowork Plan After a Week With OpenWork Stop Reviewing Every Line of AI Code - Build the Trust Stack Instead How To Build an Image Cropper in Browser (Simple Steps) I built a macOS disk cleaner for developers and just launched it would love feedback Membangun Kompetensi dan Relasi: Mengapa Ekosistem Kampus Itu Penting I Built an AI That Decides Which AI to Talk To — Running 24/7 From My Living Room Codex Team Usage SOP How to Actually Become a Programmer: The Hard Part Nobody Wants to Explain Building a Production-Style Multi-Tool AI Agent with Python, Flask, React & Gemini AI The Caretaker Sandbox: An Offline-First Visual Playground & Template Engine powered by Gemma 4 # Building Instagram OSINT Projects with HikerAPI Your AI can read. Gemma 4 can see The Battle of the Senior Dev: Why AI Gives You Wings But Only If You're Ready to Pilot HiDream Raw Output Failed Tried Dev-2604 VRAM Math Killed It Won with a Prompt Enhancer Instead I Finally Finished a Project I Abandoned — And GitHub Copilot Helped Me Ship It SafeSMS: On-Device Threat Detection with Gemma 4 E4B, no internet required I Built OpenKap — A Loom Alternative for Small Teams Who Just Want to Ship Gemma 4 is Here: The Dawn of Local Multimodal Reasoning Offline-First Flutter: How We Built a CRM That Manages 100K+ Leads With No Internet Memory for Agents: When Vectors Meet Graphs, Bugs Drop 4 The Rise of Production-Grade AI Infrastructure I ran my idea-validation product through its own validator. The verdict was PIVOT. We Built an Agent Commerce API. Google I/O 2026 Changed Our 3-Month Roadmap in 24 Hours. "My Partner's Memory Was Full. I Didn't Know — Until We Tried to Talk." I’m a Front End Web Developer Learning Machine Learning From Scratch Laravel Waiting Request I Built a Chrome Extension to Track How Long You Actually Spend on Each Tab Why Google Can't See Your React Breadcrumbs (And the 4-Line Fix) AI Travel Assistant Powered by Gemma 4; With Streaming, Image Input, and Visual Recommendation Cards Microsoft tried to kill the printer driver. Healthcare said no. The Blueprint Beneath the Blueprint: Designing Data Model and Choosing Its Database REST APIs vs Webhooks in Telecom Billing - Which One Actually Makes Sense? Accounting Made Simple: AI-Powered Financial Insights of Japanese Companies with Gemma 4 The append-only AST trick that makes Flutter AI chat actually smooth Designing the Future of Payments — Why XML Still Matters in the Age of APIs From Legacy to Live — Reviving XMLPayments with GitHub Copilot Two Weeks Into Learning Solana XMLPayments — The Hidden Backbone of Modern Financial Orchestration AI Agents in Practice — Read from the beginning Reviving My Gemma Agentic Framework: From Prototype to Polished Repo Smart Contracts Demand Better Infrastructure: Building on contract.dev Self-Hosted LLM Tool Calling: Forge and the Build-vs-Buy Decision ORA-00072 오류 원인과 해결 방법 완벽 가이드 OpenWA for CTOs: Self-Hosted WhatsApp Gateway Trade-Offs NotebookLM Automation With notebooklm-py: Useful, But Classify Data First Docker v29.5.x Operator Upgrade Checklist Coding-Agent Instruction Design: The CLAUDE.md File That Prevents Rework When I Finally Realized My Runtime Was Holding Me Back GnokeOps: Host Your Own AI House Party The Death of Static Rate Limiters: Why Your Java Virtual Threads Need BBR-Style Adaptive Concurrency AI Agents in Practice — Part 2: What Makes Something an Agent Stop scattering LLM SDK/API calls across your codebase. Here is the 2-file rule that fixed mine Beyond Prompts: Structuring AI Workflows for Real Frontend Engineering From an Abandoned Hackathon Project to an AI Study Workspace 🚀 Terraform with AI: Build AWS Infra (Cursor + MCP) What If AI Didn’t Need the Internet? 750,000 Chips, 140 Trillion Tokens: The Math Behind DeepSeek's Permanent Price Cut You're Renting Someone Else's Compute — And It's Costing You More Than You Think
Testing FastAPI + SQLAlchemy with Real PostgreSQL Fixtures: No More Mocking Misery
Ugur Aslim · 2026-05-23 · via DEV Community

Ugur Aslim

Testing FastAPI + SQLAlchemy with Real PostgreSQL Fixtures: No More Mocking Misery

I spent three months debugging why a query worked in tests but failed in production. The culprit? I'd mocked the entire database layer.

Mocks are seductive. They're fast, isolated, and you control everything. But they're also liars. They hide migration bugs, concurrency issues, transaction edge cases, and the thousand small ways your ORM behaves differently under real database constraints. I'd rather catch those bugs in CI than at 2 AM in production.

This post shows you how to test FastAPI + SQLAlchemy against real PostgreSQL instances—cheaply, quickly, and with perfect isolation. No mocking the data layer. No false confidence.

Why Real Databases Beat Mocks

Before diving into code, let me be direct about why I changed my mind.

Mocks hide real problems. When you mock session.query(), you're not testing how SQLAlchemy constructs queries, handles N+1 problems, or manages transactions. You're testing your test setup.

Migrations are invisible to mocks. I once added a NOT NULL constraint in a migration. Tests passed (mocked). Production broke (real database). Now I run real migrations in every test database.

Concurrency and locks don't exist in mocks. Row-level locking, deadlock conditions, and transaction isolation levels are real problems in multi-tenant systems. Mocks can't catch them.

Your ORM behaves differently than you expect. SQLAlchemy's relationship loading, cascade deletes, and lazy vs. eager loading have edge cases. Real tests find them.

The trade-off isn't as bad as it sounds. Docker + pytest fixtures make spinning up isolated PostgreSQL databases fast—we're talking milliseconds for setup, and modern CI can parallelize tests across multiple database instances.

The Setup: Docker Compose + Pytest Fixtures

I use a minimal Docker Compose setup that spins up PostgreSQL and tears it down cleanly between test runs.

docker-compose.test.yml:

version: '3.8'
services:
  postgres-test:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: test_user
      POSTGRES_PASSWORD: test_password
      POSTGRES_DB: test_db
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U test_user"]
      interval: 1s
      timeout: 5s
      retries: 10
    tmpfs:
      - /var/lib/postgresql/data

Enter fullscreen mode Exit fullscreen mode

The tmpfs line is crucial—it stores the database in RAM, making writes blazingly fast. Perfect for tests.

conftest.py — this is where the magic happens:

import pytest
import subprocess
import time
from sqlalchemy import create_engine, text
from sqlalchemy.pool import NullPool
from sqlalchemy.orm import sessionmaker, Session

# Import your models
from app.models import Base
from app.database import get_db


@pytest.fixture(scope="session")
def docker_compose():
    """Spin up Docker Compose once per test session."""
    subprocess.run(
        ["docker-compose", "-f", "docker-compose.test.yml", "up", "-d"],
        check=True,
        cwd=".",
    )

    # Wait for postgres to be ready
    max_retries = 30
    for i in range(max_retries):
        try:
            engine = create_engine(
                "postgresql://test_user:test_password@localhost:5433/test_db"
            )
            with engine.connect() as conn:
                conn.execute(text("SELECT 1"))
            break
        except Exception as e:
            if i == max_retries - 1:
                raise
            time.sleep(0.5)

    yield

    # Tear down
    subprocess.run(
        ["docker-compose", "-f", "docker-compose.test.yml", "down"],
        check=True,
        cwd=".",
    )


@pytest.fixture
def db_session(docker_compose):
    """Create a fresh database session for each test."""
    # Create engine with NullPool—don't reuse connections between tests
    engine = create_engine(
        "postgresql://test_user:test_password@localhost:5433/test_db",
        poolclass=NullPool,
    )

    # Run migrations (I use Alembic; you could also use Base.metadata.create_all)
    subprocess.run(
        ["alembic", "upgrade", "head"],
        check=True,
        cwd=".",
    )

    SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
    session = SessionLocal()

    yield session

    # Cleanup: rollback any uncommitted transactions
    session.rollback()
    session.close()

    # Drop all tables between tests for true isolation
    Base.metadata.drop_all(engine)


@pytest.fixture
def client(db_session):
    """Provide a FastAPI test client with dependency injection."""
    from fastapi.testclient import TestClient
    from app.main import app

    def override_get_db():
        yield db_session

    app.dependency_overrides[get_db] = override_get_db

    return TestClient(app)

Enter fullscreen mode Exit fullscreen mode

Notice the key decisions:

  • NullPool: Each test gets a fresh connection. No connection reuse surprises.
  • drop_all() after each test: Complete isolation. No shared state.
  • Alembic migrations run in every test: You catch migration bugs immediately.

Real Test Example: Catching What Mocks Miss

Let's test a user creation endpoint with a unique email constraint:

from app.models import User
from datetime import datetime


def test_create_user_success(client, db_session):
    """Test successful user creation."""
    response = client.post(
        "/users",
        json={"email": "alice@example.com", "name": "Alice"},
    )

    assert response.status_code == 201
    assert response.json()["email"] == "alice@example.com"

    # Verify it's actually in the database
    user = db_session.query(User).filter_by(email="alice@example.com").first()
    assert user is not None
    assert user.name == "Alice"


def test_create_duplicate_email_fails(client, db_session):
    """Test that duplicate emails are rejected.

    This catches:
    - Whether your database constraint actually exists
    - Whether your error handling returns the right status code
    - Whether the transaction rolled back properly
    """
    # Insert first user
    client.post(
        "/users",
        json={"email": "bob@example.com", "name": "Bob"},
    )

    # Try to insert duplicate
    response = client.post(
        "/users",
        json={"email": "bob@example.com", "name": "Bob 2"},
    )

    assert response.status_code == 409  # Conflict
    assert "already exists" in response.json()["detail"].lower()

    # Verify only one user exists
    count = db_session.query(User).filter_by(email="bob@example.com").count()
    assert count == 1


def test_n_plus_one_query_detection(client, db_session):
    """Catch N+1 queries against real PostgreSQL."""
    from sqlalchemy import event

    # Track queries
    queries = []

    @event.listens_for(engine, "before_cursor_execute")
    def receive_before_cursor_execute(conn, cursor, statement, params, context, executemany):
        queries.append(statement)

    # Create users with posts
    for i in range(5):
        user = User(email=f"user{i}@example.com", name=f"User {i}")
        db_session.add(user)
    db_session.commit()

    # Fetch users and their posts efficiently
    users = db_session.query(User).options(joinedload(User.posts)).all()

    # Against a real database, you can verify query patterns
    # Mocks can't catch these subtle performance bugs
    assert len(queries) < 10  # Rough check; adjust for your schema

Enter fullscreen mode Exit fullscreen mode

That last test is impossible to write meaningfully against mocks. With real PostgreSQL, you actually catch N+1 queries, missing indexes, and transaction issues.

Gotcha: Test Database Concurrency

Here's what burned me: I created a test for concurrent writes, but because each test got its own db_session, I wasn't actually testing multiple concurrent connections.

Solution: When you need true concurrency tests, use separate database sessions:

def test_concurrent_writes(db_session):
    """Test actual concurrent database writes."""
    from concurrent.futures import ThreadPoolExecutor
    from app.database import SessionLocal

    def create_user(email: str):
        session = SessionLocal()
        try:
            user = User(email=email, name=email.split("@")[0])
            session.add(user)
            session.commit()
            return user.id
        finally:
            session.close()

    # Run 10 writes concurrently
    with ThreadPoolExecutor(max_workers=10) as executor:
        futures = [
            executor.submit(create_user, f"user{i}@example.com")
            for i in range(10)
        ]
        results = [f.result() for f in futures]

    # Verify all succeeded
    assert len(set(results)) == 10  # All IDs unique

Enter fullscreen mode Exit fullscreen mode

This actually stress-tests your database and connection pool. Mocks wouldn't catch connection pool exhaustion or deadlocks.

Speed: It's Faster Than You Think

People worry real database tests are slow. They're not—especially with tmpfs and good fixture design:

  • Database spin-up: ~2s (once per session)
  • Per-test setup: ~50ms
  • Typical test execution: ~100ms

A 100-test suite runs in under 15 seconds locally. In CI with parallelization