2018-10-07 00:24:11 +00:00
|
|
|
#[cfg(all(feature = "diesel_sqlite_pool", feature = "diesel_postgres_pool"))]
|
2018-07-21 22:11:08 +00:00
|
|
|
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 rocket_contrib::database;
|
2020-09-03 05:41:31 +00:00
|
|
|
use rocket_contrib::databases::rusqlite;
|
2018-12-12 20:23:06 +00:00
|
|
|
|
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);
|
|
|
|
|
2020-07-11 18:17:43 +00:00
|
|
|
// Test to ensure that multiple databases of the same type can be used
|
|
|
|
#[database("test_db_2")]
|
|
|
|
struct SqliteDb2(pub rusqlite::Connection);
|
2018-12-12 20:23:06 +00:00
|
|
|
|
2020-06-14 15:57:53 +00:00
|
|
|
#[rocket::async_test]
|
2020-07-11 18:17:43 +00:00
|
|
|
async fn test_db() {
|
2020-09-03 05:41:31 +00:00
|
|
|
use rocket::figment::{Figment, util::map};
|
|
|
|
|
|
|
|
let options = map!["url" => ":memory:"];
|
|
|
|
let config = Figment::from(rocket::Config::default())
|
|
|
|
.merge(("databases", map!["test_db" => &options]))
|
|
|
|
.merge(("databases", map!["test_db_2" => &options]));
|
|
|
|
|
2020-10-22 10:27:04 +00:00
|
|
|
let rocket = rocket::custom(config)
|
2020-09-03 05:41:31 +00:00
|
|
|
.attach(SqliteDb::fairing())
|
|
|
|
.attach(SqliteDb2::fairing());
|
|
|
|
|
2020-10-22 10:27:04 +00:00
|
|
|
let conn = SqliteDb::get_one(&rocket).await
|
2020-09-03 05:41:31 +00:00
|
|
|
.expect("unable to get connection");
|
2020-07-11 18:17:43 +00:00
|
|
|
|
|
|
|
// Rusqlite's `transaction()` method takes `&mut self`; this tests that
|
|
|
|
// the &mut method can be called inside the closure passed to `run()`.
|
|
|
|
conn.run(|conn| {
|
|
|
|
let tx = conn.transaction().unwrap();
|
2020-10-22 10:27:04 +00:00
|
|
|
let _: i32 = tx.query_row(
|
|
|
|
"SELECT 1", &[] as &[&dyn ToSql], |row| row.get(0)
|
|
|
|
).expect("get row");
|
|
|
|
|
2020-07-11 18:17:43 +00:00
|
|
|
tx.commit().expect("committed transaction");
|
|
|
|
}).await;
|
2018-12-12 20:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 04:05:31 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "databases")]
|
|
|
|
#[cfg(test)]
|
|
|
|
mod drop_runtime_test {
|
|
|
|
use r2d2::{ManageConnection, Pool};
|
|
|
|
use rocket_contrib::databases::{database, Poolable, PoolResult};
|
|
|
|
use tokio::runtime::Runtime;
|
|
|
|
|
|
|
|
struct ContainsRuntime(Runtime);
|
|
|
|
struct TestConnection;
|
|
|
|
|
|
|
|
impl ManageConnection for ContainsRuntime {
|
|
|
|
type Connection = TestConnection;
|
|
|
|
type Error = std::convert::Infallible;
|
|
|
|
|
|
|
|
fn connect(&self) -> Result<Self::Connection, Self::Error> {
|
|
|
|
Ok(TestConnection)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Poolable for TestConnection {
|
|
|
|
type Manager = ContainsRuntime;
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn pool(_db_name: &str, _rocket: &rocket::Rocket) -> PoolResult<Self> {
|
|
|
|
let manager = ContainsRuntime(tokio::runtime::Runtime::new().unwrap());
|
|
|
|
Ok(Pool::builder().build(manager)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[database("test_db")]
|
|
|
|
struct TestDb(TestConnection);
|
|
|
|
|
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_drop_runtime() {
|
|
|
|
use rocket::figment::{Figment, util::map};
|
|
|
|
|
|
|
|
let config = Figment::from(rocket::Config::default())
|
|
|
|
.merge(("databases", map!["test_db" => map!["url" => ""]]));
|
|
|
|
|
|
|
|
let rocket = rocket::custom(config).attach(TestDb::fairing());
|
|
|
|
drop(rocket);
|
|
|
|
}
|
|
|
|
}
|