Skip to content

feat: add isolated integration test database lifecycle#108

Merged
3m1n3nc3 merged 1 commit into
learnault:mainfrom
ciynthia:feat/70-isolated-integration-test-db
Jul 20, 2026
Merged

feat: add isolated integration test database lifecycle#108
3m1n3nc3 merged 1 commit into
learnault:mainfrom
ciynthia:feat/70-isolated-integration-test-db

Conversation

@ciynthia

Copy link
Copy Markdown
Contributor

Overview

This PR adds a complete isolated integration test database lifecycle system to learnault-api. It provides safe PostgreSQL setup, migrations, factories, cleanup, and parallelism support for integration tests.

Related Issue

Closes #70

Changes

🛡️ Database Safety Guard

  • [ADD] tests/helpers/guard.ts
  • Validates DATABASE_URL against production keywords and host patterns
  • Rejects URLs containing production, prod, AWS RDS, Azure, Heroku, Render, Fly, Railway
  • Warns on localhost:5432 without a _test database name

🗄️ Database Lifecycle Management

  • [ADD] tests/helpers/db.ts
  • Per-worker PostgreSQL schema creation for safe parallel execution
  • Schema teardown with DROP SCHEMA ... CASCADE
  • Global migration application via prisma db push once per run
  • Truncate-all-tables utility for cleanup

🏭 Test Data Factories

  • [ADD] tests/helpers/factories.ts
  • buildUser / createUser — User factory with unique email/username
  • buildModule / createModule — Module factory
  • buildCompletion / createCompletion — Completion factory
  • createUserWithModule — Combined factory for common test setup

🔄 Test Isolation

  • [ADD] `tests/helpers/isolation.ts'
  • withIsolation — Wraps test logic in a Prisma transaction for automatic rollback
  • createIsolatedTest — Convenience wrapper for isolated test functions

⚙️ Configuration & Setup

  • [ADD] tests/globalSetup.ts — Runs migrations once before all test workers
  • [MODIFY] tests/setup.ts — Integrates guard validation + per-worker schema creation
  • [MODIFY] vitest.config.js — Added globalSetup configuration
  • [MODIFY] package.json — Added test:integration and test:integration:watch scripts

🧪 Self-Tests

  • [ADD] tests/integration/guard.test.ts — 4 tests verifying production URL rejection
  • [ADD] tests/integration/factories.test.ts — 7 tests verifying factory output
  • [ADD] tests/integration/cleanup.test.ts — 5 tests verifying schema/URL utilities
  • [ADD] tests/integration/isolation.test.ts — 5 tests (3 DB-dependent skipped gracefully)

Verification Results

npm run lint ✅ completed
npm test ✅ 397 passed, 3 skipped (no DB available)
npm run test:integration ✅ 18 passed, 3 skipped
Acceptance Criteria Status
Unsafe database targets are rejected
Migrations applied once per run
Factories produce correct data
Tests do not leak records across cases
Cleanup survives failures
Self-tests pass

- Add production database safety guard (tests/helpers/guard.ts)
- Add database lifecycle management (tests/helpers/db.ts)
- Add test data factories (tests/helpers/factories.ts)
- Add transaction isolation helpers (tests/helpers/isolation.ts)
- Add global setup for running migrations once per run
- Update test setup to integrate guard and per-worker schema
- Add integration self-tests for guard, factories, cleanup, isolation
- Update vitest config and package.json scripts
@3m1n3nc3
3m1n3nc3 merged commit 95b54eb into learnault:main Jul 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Add Isolated Integration Test Database Lifecycle

2 participants