| Crates.io | db-testkit |
| lib.rs | db-testkit |
| version | 0.2.0 |
| created_at | 2024-12-30 23:00:40.214722+00 |
| updated_at | 2024-12-30 23:00:40.214722+00 |
| description | A test toolkit for database testing in Rust |
| homepage | |
| repository | https://github.com/auser/testkit |
| max_upload_size | |
| id | 1499563 |
| size | 163,569 |
A Rust library for managing test databases with support for PostgreSQL, MySQL, and SQLite. It provides an easy way to create isolated database instances for testing, with automatic cleanup and connection pooling.
Add this to your Cargo.toml:
[dependencies]
db-testkit = { version = "0.1.0", features = ["postgres"] } # or other backends
Available features:
postgres - Native PostgreSQL supportmysql - MySQL supportsqlx-postgres - SQLx PostgreSQL supportsqlx-sqlite - SQLite supportuse db_testkit::with_test_db;
#[tokio::test]
async fn test_with_postgres() {
with_test_db(|db| async move {
let test_user = db.test_user.clone();
// Setup database
db.setup(|mut conn| async move {
conn.execute(
"CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT NOT NULL,
name TEXT NOT NULL
)"
).await?;
// Insert test data
conn.execute(
"INSERT INTO users (email, name) VALUES ($1, $2)",
&[&test_user, "Test User"],
).await?;
Ok(())
})
.await?;
Ok(())
})
.await;
}
use db_testkit::with_sqlite_test_db;
#[tokio::test]
async fn test_with_sqlite() {
with_sqlite_test_db(|db| async move {
let test_user = db.test_user.clone();
// Setup database
db.setup(|mut conn| async move {
conn.execute(
"CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
name TEXT NOT NULL
)"
).await?;
// Insert test data
conn.execute(
"INSERT INTO users (email, name) VALUES (?, ?)",
&[&test_user, "Test User"],
).await?;
Ok(())
})
.await?;
Ok(())
})
.await;
}
Create a .env file in your project root:
# For PostgreSQL
DATABASE_URL=postgres://user:password@localhost:5432/postgres
# For MySQL
DATABASE_URL=mysql://user:password@localhost:3306/mysql
# For SQLite
DATABASE_URL=/path/to/sqlite/databases
Contributions are welcome! Here's how you can help:
git checkout -b feature-namecargo test --all-featuresgit commit -m 'Add feature'git push origin feature-name.env.example to .env and configure your database URLs.envrc.example to .envrc and configure your environment variables in developmentcargo test --all-featuresLicensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.