Rocket

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit aba3ad327b862ac05c84a8d86b13566409225c3d
parent 3f58ea692f8e540b5d8c4ddc9e3fb1cda8eac0b3
Author: Eric Dattore <eric.dattore@gmail.com>
Date:   Wed, 12 Dec 2018 12:23:06 -0800

Add database pool integration tests.

Diffstat:
Mcontrib/codegen/tests/ui-fail/database-types.rs | 4++++
Mcontrib/codegen/tests/ui-fail/database-types.stderr | 8+++++++-
Mcontrib/lib/src/databases.rs | 2+-
Mcontrib/lib/tests/databases.rs | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Msite/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 @@ -7,4 +7,8 @@ struct Unknown; struct A(Unknown); //~^ ERROR Unknown: rocket_contrib::databases::Poolable +#[database("foo")] +struct B(Vec<i32>); +//~^ ERROR Vec<i32>: 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 @@ -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<i32>: rocket_contrib::databases::Poolable` is not satisfied + --> $DIR/database-types.rs:11:10 + | +11 | struct B(Vec<i32>); + | ^^^^^^^^ the trait `rocket_contrib::databases::Poolable` is not implemented for `std::vec::Vec<i32>` + +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 @@ -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 @@ -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<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 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<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 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 @@ -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