4 个版本
0.1.3 | 2020年5月10日 |
---|---|
0.1.2 | 2020年5月8日 |
0.1.1 | 2020年5月8日 |
0.1.0 | 2020年5月7日 |
#494 in 测试
8KB
56 行
Nafta
使用 diesel 创建临时 SQLite 数据库进行测试。
[dev-dependencies]
nafta = "0.1"
最小示例
// Creates empty SQLite database in temporary folder
let test_db = nafta::sqlite::TestDb::new();
// Work with the connetion
let conn = test_db.conn();
// You can check that database file was really removed
let path = test_db.db_path.clone();
// Necessary to drop anything which can block file
drop(conn);
// Dropping `test_db` to check it was really removed
drop(test_db);
assert!(!path.exists());
带迁移的示例
// Database
extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
// This macro from `diesel_migrations` defines an `embedded_migrations` module
// containing a function named `run`. This allows the example to be run and
// tested without any outside setup of the database.
embed_migrations!("migrations");
#[cfg(test)]
mod tests {
#[test]
async fn test_get_user() {
let test_db = nafta::sqlite::TestDb::new();
let conn = test_db
.conn()
.expect("Not possible to get pooled connection");
// Database migration
embedded_migrations::run(&conn).expect("Migration not possible to run");
// Example method to get all users
let all_user = db::users::get_all_users(test_db.pool);
assert!(all_user.is_ok());
}
}
在 Windows 上构建
请参阅 Windows 的构建批处理文件: .github/install-sqlite.bat
依赖项
~25–33MB
~567K SLoC