Skip to main content

nafta/
lib.rs

1//! # Nafta
2//!
3//! Creates temporary SQLite database for testing using diesel.
4//! 
5//! ```toml
6//! [dev-dependencies]
7//! nafta = "0.1"
8//! ```
9//!
10//!  # Minimal example
11//! 
12//! ```rust
13//! // Creates empty SQLite database in temporary folder
14//! let test_db = nafta::sqlite::TestDb::new();
15//! // Work with the conn
16//! let conn = test_db.conn();
17//!
18//! // You can check that database is removed
19//! let path = test_db.db_path.clone();
20//! // Necessary to drop anything which can block file
21//! drop(conn); 
22//! // Dropping `test_db` to check it was really removed
23//! drop(test_db);
24//! // sleep added due to problem on windows/github actions randomly failed #9
25//! std::thread::sleep(std::time::Duration::from_millis(100));
26//! assert!(!path.exists()); // Neccessary to test if path was removed
27//! ```
28//!
29//! # Example with migration
30//!
31//! ```ignore
32//! // Database
33//! extern crate diesel;
34//! 
35//! #[macro_use]
36//! extern crate diesel_migrations;
37//! 
38//! // This macro from `diesel_migrations` defines an `embedded_migrations` module
39//! // containing a function named `run`. This allows the example to be run and
40//! // tested without any outside setup of the database.
41//! embed_migrations!("migrations");
42//! 
43//! #[cfg(test)]
44//! mod tests {
45//!
46    //!     #[test]
47//!     async fn test_get_user() {
48//!         let test_db = nafta::sqlite::TestDb::new();
49//!         let conn = test_db
50//!             .conn()
51//!             .expect("Not possible to get pooled connection");
52//! 
53//!         embedded_migrations::run(&conn).expect("Migration not possible to run");
54//! 
55//!         // Test
56//!         let all_user = db::users::get_all_users(test_db.pool);
57//!         assert!(all_user.is_ok());
58//!     }
59//! }
60//! ```
61
62pub mod sqlite;