|
1 | 1 | # crates_io_test_db
|
2 | 2 |
|
3 |
| -This package contains the code necessary to create test databases for testing |
4 |
| -purposes. |
| 3 | +A database testing infrastructure for creating isolated, ephemeral PostgreSQL test databases with automatic cleanup. |
5 | 4 |
|
6 |
| -`TestDatabase::new()` can be used to create a new test database, based on a |
7 |
| -template database, which is lazily created the first time it is needed. |
| 5 | +## Overview |
8 | 6 |
|
9 |
| -The databases are created based on the `TEST_DATABASE_URL` environment variable, |
10 |
| -which should be set to a valid database URL. The template database will then be |
11 |
| -created with a similar name and `_template` suffix, while the test databases |
12 |
| -will use random suffixes. |
| 7 | +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. |
13 | 8 |
|
14 |
| -Note that the template database will be created with applied database migrations, |
15 |
| -so if you need an empty database, this is not the right tool for you. |
| 9 | +## Key Features |
| 10 | + |
| 11 | +- **Automatic Cleanup**: Test databases are automatically dropped when the `TestDatabase` instance goes out of scope |
| 12 | +- **Template Optimization**: Uses a shared template database with pre-applied migrations for fast test database creation |
| 13 | +- **Concurrent Testing**: Supports parallel test execution with isolated databases |
| 14 | +- **Connection Pooling**: Built-in connection pooling for improved performance |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Basic Usage |
| 19 | + |
| 20 | +```rust,ignore |
| 21 | +use crates_io_test_db::TestDatabase; |
| 22 | +
|
| 23 | +#[tokio::test] |
| 24 | +async fn test_database_operations() { |
| 25 | + // Create a new test database with all migrations applied |
| 26 | + let test_db = TestDatabase::new(); |
| 27 | + |
| 28 | + // Get an async connection |
| 29 | + let mut conn = test_db.async_connect().await; |
| 30 | + |
| 31 | + // Perform your database operations... |
| 32 | + // Database is automatically cleaned up when test_db is dropped |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Empty Database |
| 37 | + |
| 38 | +If you need a database without migrations applied: |
| 39 | + |
| 40 | +```rust,no_run |
| 41 | +use crates_io_test_db::TestDatabase; |
| 42 | +
|
| 43 | +#[test] |
| 44 | +fn test_with_empty_database() { |
| 45 | + // Create an empty database without migrations |
| 46 | + let test_db = TestDatabase::empty(); |
| 47 | + let mut conn = test_db.connect(); |
| 48 | + |
| 49 | + // Database starts completely empty |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Getting Database URL |
| 54 | + |
| 55 | +```rust,no_run |
| 56 | +use crates_io_test_db::TestDatabase; |
| 57 | +
|
| 58 | +#[test] |
| 59 | +fn test_with_database_url() { |
| 60 | + let test_db = TestDatabase::new(); |
| 61 | + let url = test_db.url(); |
| 62 | + |
| 63 | + // Use the URL with your preferred database client |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Environment Setup |
| 68 | + |
| 69 | +### Required Environment Variable |
| 70 | + |
| 71 | +Set the `TEST_DATABASE_URL` environment variable to a valid PostgreSQL connection string: |
| 72 | + |
| 73 | +```bash |
| 74 | +export TEST_DATABASE_URL="postgres://username:password@localhost/test_db_base" |
| 75 | +``` |
| 76 | + |
| 77 | +### Example Configuration |
| 78 | + |
| 79 | +For local development, you might use: |
| 80 | + |
| 81 | +```bash |
| 82 | +export TEST_DATABASE_URL="postgres://postgres:postgres@localhost/crates_io_test" |
| 83 | +``` |
| 84 | + |
| 85 | +The crate will automatically create: |
| 86 | +- A template database: `crates_io_test_template` (with all migrations applied) |
| 87 | +- Individual test databases: `crates_io_test_<random_suffix>` (created from template) |
| 88 | + |
| 89 | +## How It Works |
| 90 | + |
| 91 | +### Template Database Pattern |
| 92 | + |
| 93 | +1. **Lazy Initialization**: On first use, creates a singleton template database with all migrations applied |
| 94 | +2. **Fast Cloning**: New test databases are created using PostgreSQL's `CREATE DATABASE ... TEMPLATE` command |
| 95 | +3. **Automatic Cleanup**: Each test database is automatically dropped when the `TestDatabase` instance is dropped |
| 96 | + |
| 97 | +This approach significantly reduces test setup time by avoiding migration runs for each individual test. |
0 commit comments