temp_mongo/
lib.rs

1//! Easy temporary MongoDB instance for unit tests.
2//!
3//! Use the [`TempMongo`] struct to get a [`mongodb::Client`] that is connected to a temporary MongoDB instance.
4//! All state of the spawned MongoDB instance is stored in a temporary directory, which will be cleaned up automatically (unless disabled).
5//!
6//! On Unix platforms, the client is connected over a Unix socket.
7//! Windows support is planned by picking a free TCP port on the loopback adapter.
8//!
9//! # Example
10//!
11//! See the [example in the repository](https://github.com/rocsys/temp-mongo/blob/main/examples/example.rs) for a more detailed example using [`assert2`](https://crates.io/crates/assert2).
12//! ```
13//! # #[cfg_attr(feature = "tokio-runtime", tokio::main)]
14//! # #[cfg_attr(feature = "async-std-runtime", async_std::main)]
15//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
16//! use temp_mongo::TempMongo;
17//! use mongodb::bson::doc;
18//!
19//! let mongo = TempMongo::new().await?;
20//! println!("Using temporary directory: {}", mongo.directory().display());
21//!
22//! let client = mongo.client();
23//! let collection = client.database("test").collection("animals");
24//!
25//! collection.insert_one(doc! { "species": "dog", "cute": "yes", "scary": "usually not" }, None).await?;
26//! collection.insert_one(doc! { "species": "T-Rex", "cute": "maybe", "scary": "yes" }, None).await?;
27//! # Ok(())
28//! # }
29
30#![warn(missing_docs)]
31
32mod error;
33mod temp_mongo;
34mod util;
35
36pub use error::Error;
37pub use temp_mongo::TempMongo;
38pub use temp_mongo::TempMongoBuilder;