Add database pool integration tests.

This commit is contained in:
Eric Dattore 2018-12-12 12:23:06 -08:00 committed by Sergio Benitez
parent 3f58ea692f
commit aba3ad327b
5 changed files with 62 additions and 3 deletions

View File

@ -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() { }

View File

@ -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`.

View File

@ -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

View File

@ -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");
}
}

View File

@ -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