temp-mongo

Crates.iotemp-mongo
lib.rstemp-mongo
version0.1.3
sourcesrc
created_at2023-12-07 09:46:33.664474
updated_at2024-01-25 15:25:02.505855
descriptiontemporary mongo instance meant for unit tests
homepage
repositoryhttps://github.com/rocsys/temp-mongo
max_upload_size
id1060856
size81,495
crates-io (github:rocsys:crates-io)

documentation

README

temp-mongo

Easy temporary MongoDB instance for unit tests.

Use the [TempMongo] struct to get a [mongodb::Client] that is connected to a temporary MongoDB instance. All state of the spawned MongoDB instance is stored in a temporary directory, which will be cleaned up automatically (unless disabled).

On Unix platforms, the client is connected over a Unix socket. Windows support is planned by picking a free TCP port on the loopback adapter.

Example

See the example in the repository for a more detailed example using assert2.

use temp_mongo::TempMongo;
use mongodb::bson::doc;

let mongo = TempMongo::new().await?;
println!("Using temporary directory: {}", mongo.directory().display());

let client = mongo.client();
let collection = client.database("test").collection("animals");

collection.insert_one(doc! { "species": "dog", "cute": "yes", "scary": "usually not" }, None).await?;
collection.insert_one(doc! { "species": "T-Rex", "cute": "maybe", "scary": "yes" }, None).await?;
Commit count: 18

cargo fmt