Rocket/contrib/lib/tests/databases.rs

62 lines
2.5 KiB
Rust
Raw Normal View History

2018-10-07 00:24:11 +00:00
#[cfg(all(feature = "diesel_sqlite_pool", feature = "diesel_postgres_pool"))]
mod databases_tests {
use rocket_contrib::databases::{database, diesel};
#[database("foo")]
struct TempStorage(diesel::SqliteConnection);
#[database("bar")]
struct PrimaryDb(diesel::PgConnection);
}
2018-12-12 20:23:06 +00:00
#[cfg(all(feature = "databases", feature = "sqlite_pool"))]
#[cfg(test)]
mod rusqlite_integration_test {
use std::collections::BTreeMap;
use rocket::config::{Config, Environment, Value};
use rocket_contrib::databases::rusqlite;
use rocket_contrib::database;
2019-06-13 02:17:59 +00:00
use rusqlite::types::ToSql;
2019-01-27 15:52:03 +00:00
2018-12-12 20:23:06 +00:00
#[database("test_db")]
struct SqliteDb(pub rusqlite::Connection);
#[rocket::async_test]
async fn deref_mut_impl_present() {
2018-12-12 20:23:06 +00:00
let mut test_db: BTreeMap<String, Value> = BTreeMap::new();
let mut test_db_opts: BTreeMap<String, Value> = BTreeMap::new();
test_db_opts.insert("url".into(), Value::String(":memory:".into()));
test_db.insert("test_db".into(), Value::Table(test_db_opts));
let config = Config::build(Environment::Development)
.extra("databases", Value::Table(test_db))
.finalize()
.unwrap();
let mut rocket = rocket::custom(config).attach(SqliteDb::fairing());
let mut conn = SqliteDb::get_one(rocket.inspect().await).expect("unable to get connection");
2018-12-12 20:23:06 +00:00
// Rusqlite's `transaction()` method takes `&mut self`; this tests the
// presence of a `DerefMut` trait on the generated connection type.
let tx = conn.transaction().unwrap();
2019-01-27 15:52:03 +00:00
let _: i32 = tx.query_row("SELECT 1", &[] as &[&dyn ToSql], |row| row.get(0)).expect("get row");
2018-12-12 20:23:06 +00:00
tx.commit().expect("committed transaction");
}
#[rocket::async_test]
async fn deref_impl_present() {
2018-12-12 20:23:06 +00:00
let mut test_db: BTreeMap<String, Value> = BTreeMap::new();
let mut test_db_opts: BTreeMap<String, Value> = BTreeMap::new();
test_db_opts.insert("url".into(), Value::String(":memory:".into()));
test_db.insert("test_db".into(), Value::Table(test_db_opts));
let config = Config::build(Environment::Development)
.extra("databases", Value::Table(test_db))
.finalize()
.unwrap();
let mut rocket = rocket::custom(config).attach(SqliteDb::fairing());
let conn = SqliteDb::get_one(rocket.inspect().await).expect("unable to get connection");
2019-01-27 15:52:03 +00:00
let _: i32 = conn.query_row("SELECT 1", &[] as &[&dyn ToSql], |row| row.get(0)).expect("get row");
2018-12-12 20:23:06 +00:00
}
}