Skip to content

crates_io_test_db: Improve README #11469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 92 additions & 10 deletions crates/crates_io_test_db/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,97 @@
# crates_io_test_db

This package contains the code necessary to create test databases for testing
purposes.
A database testing infrastructure for creating isolated, ephemeral PostgreSQL test databases with automatic cleanup.

`TestDatabase::new()` can be used to create a new test database, based on a
template database, which is lazily created the first time it is needed.
## Overview

The databases are created based on the `TEST_DATABASE_URL` environment variable,
which should be set to a valid database URL. The template database will then be
created with a similar name and `_template` suffix, while the test databases
will use random suffixes.
This crate provides a robust foundation for testing database operations in the crates.io project. It creates isolated test databases that are automatically cleaned up after use, enabling reliable and concurrent testing without interference between test runs.

Note that the template database will be created with applied database migrations,
so if you need an empty database, this is not the right tool for you.
## Key Features

- **Automatic Cleanup**: Test databases are automatically dropped when the `TestDatabase` instance goes out of scope
- **Template Optimization**: Uses a shared template database with pre-applied migrations for fast test database creation
- **Concurrent Testing**: Supports parallel test execution with isolated databases
- **Connection Pooling**: Built-in connection pooling for improved performance

## Usage

### Basic Usage

```rust,ignore
use crates_io_test_db::TestDatabase;

#[tokio::test]
async fn test_database_operations() {
// Create a new test database with all migrations applied
let test_db = TestDatabase::new();

// Get an async connection
let mut conn = test_db.async_connect().await;

// Perform your database operations...
// Database is automatically cleaned up when test_db is dropped
}
```

### Empty Database

If you need a database without migrations applied:

```rust,no_run
use crates_io_test_db::TestDatabase;

#[test]
fn test_with_empty_database() {
// Create an empty database without migrations
let test_db = TestDatabase::empty();
let mut conn = test_db.connect();

// Database starts completely empty
}
```

### Getting Database URL

```rust,no_run
use crates_io_test_db::TestDatabase;

#[test]
fn test_with_database_url() {
let test_db = TestDatabase::new();
let url = test_db.url();

// Use the URL with your preferred database client
}
```

## Environment Setup

### Required Environment Variable

Set the `TEST_DATABASE_URL` environment variable to a valid PostgreSQL connection string:

```bash
export TEST_DATABASE_URL="postgres://username:password@localhost/test_db_base"
```

### Example Configuration

For local development, you might use:

```bash
export TEST_DATABASE_URL="postgres://postgres:postgres@localhost/crates_io_test"
```

The crate will automatically create:
- A template database: `crates_io_test_template` (with all migrations applied)
- Individual test databases: `crates_io_test_<random_suffix>` (created from template)

## How It Works

### Template Database Pattern

1. **Lazy Initialization**: On first use, creates a singleton template database with all migrations applied
2. **Fast Cloning**: New test databases are created using PostgreSQL's `CREATE DATABASE ... TEMPLATE` command
3. **Automatic Cleanup**: Each test database is automatically dropped when the `TestDatabase` instance is dropped

This approach significantly reduces test setup time by avoiding migration runs for each individual test.