Crates.io | temp-mongo |
lib.rs | temp-mongo |
version | 0.1.3 |
source | src |
created_at | 2023-12-07 09:46:33.664474 |
updated_at | 2024-01-25 15:25:02.505855 |
description | temporary mongo instance meant for unit tests |
homepage | |
repository | https://github.com/rocsys/temp-mongo |
max_upload_size | |
id | 1060856 |
size | 81,495 |
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.
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?;