From aba3ad327b862ac05c84a8d86b13566409225c3d Mon Sep 17 00:00:00 2001 From: Eric Dattore Date: Wed, 12 Dec 2018 12:23:06 -0800 Subject: [PATCH] Add database pool integration tests. --- .../codegen/tests/ui-fail/database-types.rs | 4 ++ .../tests/ui-fail/database-types.stderr | 8 ++- contrib/lib/src/databases.rs | 2 +- contrib/lib/tests/databases.rs | 49 +++++++++++++++++++ site/guide/6-state.md | 2 +- 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/contrib/codegen/tests/ui-fail/database-types.rs b/contrib/codegen/tests/ui-fail/database-types.rs index 5ee4ac3d..0cee2fe2 100644 --- a/contrib/codegen/tests/ui-fail/database-types.rs +++ b/contrib/codegen/tests/ui-fail/database-types.rs @@ -7,4 +7,8 @@ struct Unknown; struct A(Unknown); //~^ ERROR Unknown: rocket_contrib::databases::Poolable +#[database("foo")] +struct B(Vec); +//~^ ERROR Vec: rocket_contrib::databases::Poolable + fn main() { } diff --git a/contrib/codegen/tests/ui-fail/database-types.stderr b/contrib/codegen/tests/ui-fail/database-types.stderr index 179ce508..29c55825 100644 --- a/contrib/codegen/tests/ui-fail/database-types.stderr +++ b/contrib/codegen/tests/ui-fail/database-types.stderr @@ -4,6 +4,12 @@ error[E0277]: the trait bound `Unknown: rocket_contrib::databases::Poolable` is 7 | struct A(Unknown); | ^^^^^^^ the trait `rocket_contrib::databases::Poolable` is not implemented for `Unknown` -error: aborting due to previous error +error[E0277]: the trait bound `std::vec::Vec: rocket_contrib::databases::Poolable` is not satisfied + --> $DIR/database-types.rs:11:10 + | +11 | struct B(Vec); + | ^^^^^^^^ the trait `rocket_contrib::databases::Poolable` is not implemented for `std::vec::Vec` + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0277`. diff --git a/contrib/lib/src/databases.rs b/contrib/lib/src/databases.rs index 07d28ed6..26b356a1 100644 --- a/contrib/lib/src/databases.rs +++ b/contrib/lib/src/databases.rs @@ -338,7 +338,7 @@ //! [Diesel]: https://diesel.rs //! [`redis::Connection`]: https://docs.rs/redis/0.9.0/redis/struct.Connection.html //! [`rusted_cypher::GraphClient`]: https://docs.rs/rusted_cypher/1.1.0/rusted_cypher/graph/struct.GraphClient.html -//! [`rusqlite::Connection`]: https://docs.rs/rusqlite/0.13.0/rusqlite/struct.Connection.html +//! [`rusqlite::Connection`]: https://docs.rs/rusqlite/0.14.0/rusqlite/struct.Connection.html //! [`diesel::SqliteConnection`]: http://docs.diesel.rs/diesel/prelude/struct.SqliteConnection.html //! [`postgres::Connection`]: https://docs.rs/postgres/0.15.2/postgres/struct.Connection.html //! [`diesel::PgConnection`]: http://docs.diesel.rs/diesel/pg/struct.PgConnection.html diff --git a/contrib/lib/tests/databases.rs b/contrib/lib/tests/databases.rs index 7da70f06..cdffb61e 100644 --- a/contrib/lib/tests/databases.rs +++ b/contrib/lib/tests/databases.rs @@ -11,3 +11,52 @@ mod databases_tests { #[database("bar")] struct PrimaryDb(diesel::PgConnection); } + +#[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; + + #[database("test_db")] + struct SqliteDb(pub rusqlite::Connection); + + #[test] + fn deref_mut_impl_present() { + let mut test_db: BTreeMap = BTreeMap::new(); + let mut test_db_opts: BTreeMap = 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 rocket = rocket::custom(config).attach(SqliteDb::fairing()); + let mut conn = SqliteDb::get_one(&rocket).expect("unable to get connection"); + + // 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(); + let _: i32 = tx.query_row("SELECT 1", &[], |row| row.get(0)).expect("get row"); + tx.commit().expect("committed transaction"); + } + + #[test] + fn deref_impl_present() { + let mut test_db: BTreeMap = BTreeMap::new(); + let mut test_db_opts: BTreeMap = 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 rocket = rocket::custom(config).attach(SqliteDb::fairing()); + let conn = SqliteDb::get_one(&rocket).expect("unable to get connection"); + let _: i32 = conn.query_row("SELECT 1", &[], |row| row.get(0)).expect("get row"); + } +} diff --git a/site/guide/6-state.md b/site/guide/6-state.md index 00a75120..1fc517ad 100644 --- a/site/guide/6-state.md +++ b/site/guide/6-state.md @@ -193,7 +193,7 @@ Presently, Rocket provides built-in support for the following databases: [Diesel]: https://diesel.rs [`redis::Connection`]: https://docs.rs/redis/0.9.0/redis/struct.Connection.html [`rusted_cypher::GraphClient`]: https://docs.rs/rusted_cypher/1.1.0/rusted_cypher/graph/struct.GraphClient.html -[`rusqlite::Connection`]: https://docs.rs/rusqlite/0.13.0/rusqlite/struct.Connection.html +[`rusqlite::Connection`]: https://docs.rs/rusqlite/0.14.0/rusqlite/struct.Connection.html [`diesel::SqliteConnection`]: http://docs.diesel.rs/diesel/prelude/struct.SqliteConnection.html [`postgres::Connection`]: https://docs.rs/postgres/0.15.2/postgres/struct.Connection.html [`diesel::PgConnection`]: http://docs.diesel.rs/diesel/pg/struct.PgConnection.html