3 releases

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

#653 in Database interfaces

Download history 1/week @ 2024-01-23 6/week @ 2024-01-30 1/week @ 2024-02-13 7/week @ 2024-02-20 18/week @ 2024-02-27 9/week @ 2024-03-05 8/week @ 2024-03-12 5/week @ 2024-03-26 41/week @ 2024-04-02

59 downloads per month

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

~27–41MB
~841K SLoC