3 releases

0.1.3 Jan 25, 2024
0.1.2 Dec 7, 2023
0.1.1 Dec 7, 2023

#2136 in Database interfaces

MIT license

24KB
265 lines

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?;

Dependencies

~29–42MB
~794K SLoC