mirror of https://github.com/rwf2/Rocket.git
Add 'rocket_db_pools' UI tests.
This commit is contained in:
parent
b6324433b4
commit
179be25866
|
@ -19,3 +19,5 @@ quote = "1"
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rocket = { path = "../../../core/lib", default-features = false }
|
rocket = { path = "../../../core/lib", default-features = false }
|
||||||
rocket_db_pools = { path = "../lib", features = ["deadpool_postgres"] }
|
rocket_db_pools = { path = "../lib", features = ["deadpool_postgres"] }
|
||||||
|
trybuild = "1.0"
|
||||||
|
version_check = "0.9"
|
||||||
|
|
|
@ -21,19 +21,23 @@ pub fn derive_database(input: TokenStream) -> TokenStream {
|
||||||
if s.fields.len() == 1 {
|
if s.fields.len() == 1 {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(s.fields.span().error(ONE_UNNAMED_FIELD))
|
Err(s.span().error(ONE_UNNAMED_FIELD))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
.outer_mapper(MapperBuild::new()
|
.outer_mapper(MapperBuild::new()
|
||||||
.struct_map(|_, s| {
|
.struct_map(|_, s| {
|
||||||
let decorated_type = &s.ident;
|
|
||||||
let pool_type = match &s.fields {
|
let pool_type = match &s.fields {
|
||||||
syn::Fields::Unnamed(f) => &f.unnamed[0].ty,
|
syn::Fields::Unnamed(f) => &f.unnamed[0].ty,
|
||||||
_ => unreachable!("Support::TupleStruct"),
|
_ => unreachable!("Support::TupleStruct"),
|
||||||
};
|
};
|
||||||
|
|
||||||
quote_spanned! { s.span() =>
|
let decorated_type = &s.ident;
|
||||||
|
let db_ty = quote_spanned!(decorated_type.span() =>
|
||||||
|
<#decorated_type as rocket_db_pools::Database>
|
||||||
|
);
|
||||||
|
|
||||||
|
quote_spanned! { decorated_type.span() =>
|
||||||
impl From<#pool_type> for #decorated_type {
|
impl From<#pool_type> for #decorated_type {
|
||||||
fn from(pool: #pool_type) -> Self {
|
fn from(pool: #pool_type) -> Self {
|
||||||
Self(pool)
|
Self(pool)
|
||||||
|
@ -61,7 +65,7 @@ pub fn derive_database(input: TokenStream) -> TokenStream {
|
||||||
async fn from_request(
|
async fn from_request(
|
||||||
req: &'r rocket::request::Request<'_>
|
req: &'r rocket::request::Request<'_>
|
||||||
) -> rocket::request::Outcome<Self, Self::Error> {
|
) -> rocket::request::Outcome<Self, Self::Error> {
|
||||||
match #decorated_type::fetch(req.rocket()) {
|
match #db_ty::fetch(req.rocket()) {
|
||||||
Some(db) => rocket::outcome::Outcome::Success(db),
|
Some(db) => rocket::outcome::Outcome::Success(db),
|
||||||
None => rocket::outcome::Outcome::Failure((
|
None => rocket::outcome::Outcome::Failure((
|
||||||
rocket::http::Status::InternalServerError, ()))
|
rocket::http::Status::InternalServerError, ()))
|
||||||
|
@ -71,7 +75,7 @@ pub fn derive_database(input: TokenStream) -> TokenStream {
|
||||||
|
|
||||||
impl rocket::Sentinel for &#decorated_type {
|
impl rocket::Sentinel for &#decorated_type {
|
||||||
fn abort(rocket: &rocket::Rocket<rocket::Ignite>) -> bool {
|
fn abort(rocket: &rocket::Rocket<rocket::Ignite>) -> bool {
|
||||||
#decorated_type::fetch(rocket).is_none()
|
#db_ty::fetch(rocket).is_none()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,7 +95,7 @@ pub fn derive_database(input: TokenStream) -> TokenStream {
|
||||||
_ => unreachable!("Support::TupleStruct"),
|
_ => unreachable!("Support::TupleStruct"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(quote_spanned! { s.span() =>
|
Ok(quote_spanned! { pool_type.span() =>
|
||||||
type Pool = #pool_type;
|
type Pool = #pool_type;
|
||||||
|
|
||||||
const NAME: &'static str = #db_name;
|
const NAME: &'static str = #db_name;
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
../ui-fail/database-syntax.rs
|
|
@ -0,0 +1,109 @@
|
||||||
|
error: invalid value: expected string literal
|
||||||
|
--> $DIR/database-syntax.rs:4:12
|
||||||
|
|
|
||||||
|
4 | #[database(123)]
|
||||||
|
| ^^^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:3:10
|
||||||
|
|
|
||||||
|
3 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: expected key/value `key = value`
|
||||||
|
--> $DIR/database-syntax.rs:8:25
|
||||||
|
|
|
||||||
|
8 | #[database("some-name", "another")]
|
||||||
|
| ^^^^^^^^^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:7:10
|
||||||
|
|
|
||||||
|
7 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: unexpected attribute parameter: `name`
|
||||||
|
--> $DIR/database-syntax.rs:12:25
|
||||||
|
|
|
||||||
|
12 | #[database("some-name", name = "another")]
|
||||||
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:11:10
|
||||||
|
|
|
||||||
|
11 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: enums are not supported
|
||||||
|
--> $DIR/database-syntax.rs:16:1
|
||||||
|
|
|
||||||
|
16 | / #[database("foo")]
|
||||||
|
17 | | enum D { }
|
||||||
|
| |___________^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:15:10
|
||||||
|
|
|
||||||
|
15 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: missing `#[database("name")]` attribute
|
||||||
|
--> $DIR/database-syntax.rs:20:1
|
||||||
|
|
|
||||||
|
20 | struct E(deadpool_postgres::Pool);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:19:10
|
||||||
|
|
|
||||||
|
19 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: struct must have exactly one unnamed field
|
||||||
|
--> $DIR/database-syntax.rs:23:1
|
||||||
|
|
|
||||||
|
23 | / #[database("foo")]
|
||||||
|
24 | | struct F;
|
||||||
|
| |_________^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:22:10
|
||||||
|
|
|
||||||
|
22 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: struct must have exactly one unnamed field
|
||||||
|
--> $DIR/database-syntax.rs:27:1
|
||||||
|
|
|
||||||
|
27 | / #[database("foo")]
|
||||||
|
28 | | struct G(deadpool_postgres::Pool, deadpool_postgres::Pool);
|
||||||
|
| |___________________________________________________________^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:26:10
|
||||||
|
|
|
||||||
|
26 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: named structs are not supported
|
||||||
|
--> $DIR/database-syntax.rs:31:1
|
||||||
|
|
|
||||||
|
31 | / #[database("foo")]
|
||||||
|
32 | | struct H {
|
||||||
|
33 | | foo: deadpool_postgres::Pool,
|
||||||
|
34 | | }
|
||||||
|
| |_^
|
||||||
|
|
|
||||||
|
note: error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:30:10
|
||||||
|
|
|
||||||
|
30 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
= note: this error originates in the derive macro `Database` (in Nightly builds, run with -Z macro-backtrace for more info)
|
|
@ -0,0 +1 @@
|
||||||
|
../ui-fail/database-types.rs
|
|
@ -0,0 +1,21 @@
|
||||||
|
error[E0277]: the trait bound `Unknown: Pool` is not satisfied
|
||||||
|
--> $DIR/database-types.rs:7:10
|
||||||
|
|
|
||||||
|
7 | struct A(Unknown);
|
||||||
|
| ^^^^^^^ the trait `Pool` is not implemented for `Unknown`
|
||||||
|
|
|
||||||
|
::: $WORKSPACE/contrib/db_pools/lib/src/database.rs
|
||||||
|
|
|
||||||
|
| type Pool: Pool;
|
||||||
|
| ---- required by this bound in `rocket_db_pools::Database::Pool`
|
||||||
|
|
||||||
|
error[E0277]: the trait bound `Vec<i32>: Pool` is not satisfied
|
||||||
|
--> $DIR/database-types.rs:11:10
|
||||||
|
|
|
||||||
|
11 | struct B(Vec<i32>);
|
||||||
|
| ^^^^^^^^ the trait `Pool` is not implemented for `Vec<i32>`
|
||||||
|
|
|
||||||
|
::: $WORKSPACE/contrib/db_pools/lib/src/database.rs
|
||||||
|
|
|
||||||
|
| type Pool: Pool;
|
||||||
|
| ---- required by this bound in `rocket_db_pools::Database::Pool`
|
|
@ -0,0 +1 @@
|
||||||
|
../ui-fail/database-syntax.rs
|
|
@ -0,0 +1,111 @@
|
||||||
|
error: invalid value: expected string literal
|
||||||
|
--> $DIR/database-syntax.rs:4:12
|
||||||
|
|
|
||||||
|
4 | #[database(123)]
|
||||||
|
| ^^^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:3:10
|
||||||
|
|
|
||||||
|
3 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: expected key/value `key = value`
|
||||||
|
--> $DIR/database-syntax.rs:8:25
|
||||||
|
|
|
||||||
|
8 | #[database("some-name", "another")]
|
||||||
|
| ^^^^^^^^^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:7:10
|
||||||
|
|
|
||||||
|
7 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: unexpected attribute parameter: `name`
|
||||||
|
--> $DIR/database-syntax.rs:12:25
|
||||||
|
|
|
||||||
|
12 | #[database("some-name", name = "another")]
|
||||||
|
| ^^^^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:11:10
|
||||||
|
|
|
||||||
|
11 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: enums are not supported
|
||||||
|
--> $DIR/database-syntax.rs:16:1
|
||||||
|
|
|
||||||
|
16 | #[database("foo")]
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:15:10
|
||||||
|
|
|
||||||
|
15 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: missing `#[database("name")]` attribute
|
||||||
|
--> $DIR/database-syntax.rs:20:1
|
||||||
|
|
|
||||||
|
20 | struct E(deadpool_postgres::Pool);
|
||||||
|
| ^^^^^^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:19:10
|
||||||
|
|
|
||||||
|
19 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: struct must have exactly one unnamed field
|
||||||
|
--> $DIR/database-syntax.rs:23:1
|
||||||
|
|
|
||||||
|
23 | #[database("foo")]
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:22:10
|
||||||
|
|
|
||||||
|
22 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: struct must have exactly one unnamed field
|
||||||
|
--> $DIR/database-syntax.rs:27:1
|
||||||
|
|
|
||||||
|
27 | #[database("foo")]
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:26:10
|
||||||
|
|
|
||||||
|
26 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: named structs are not supported
|
||||||
|
--> $DIR/database-syntax.rs:31:1
|
||||||
|
|
|
||||||
|
31 | #[database("foo")]
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: [note] error occurred while deriving `Database`
|
||||||
|
--> $DIR/database-syntax.rs:30:10
|
||||||
|
|
|
||||||
|
30 | #[derive(Database)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
|
@ -0,0 +1 @@
|
||||||
|
../ui-fail/database-types.rs
|
|
@ -0,0 +1,21 @@
|
||||||
|
error[E0277]: the trait bound `Unknown: Pool` is not satisfied
|
||||||
|
--> $DIR/database-types.rs:7:10
|
||||||
|
|
|
||||||
|
7 | struct A(Unknown);
|
||||||
|
| ^^^^^^^ the trait `Pool` is not implemented for `Unknown`
|
||||||
|
|
|
||||||
|
::: $WORKSPACE/contrib/db_pools/lib/src/database.rs
|
||||||
|
|
|
||||||
|
| type Pool: Pool;
|
||||||
|
| ---- required by this bound in `rocket_db_pools::Database::Pool`
|
||||||
|
|
||||||
|
error[E0277]: the trait bound `Vec<i32>: Pool` is not satisfied
|
||||||
|
--> $DIR/database-types.rs:11:10
|
||||||
|
|
|
||||||
|
11 | struct B(Vec<i32>);
|
||||||
|
| ^^^ the trait `Pool` is not implemented for `Vec<i32>`
|
||||||
|
|
|
||||||
|
::: $WORKSPACE/contrib/db_pools/lib/src/database.rs
|
||||||
|
|
|
||||||
|
| type Pool: Pool;
|
||||||
|
| ---- required by this bound in `rocket_db_pools::Database::Pool`
|
|
@ -0,0 +1,10 @@
|
||||||
|
#[test]
|
||||||
|
fn ui() {
|
||||||
|
let path = match version_check::is_feature_flaggable() {
|
||||||
|
Some(true) => "ui-fail-nightly",
|
||||||
|
_ => "ui-fail-stable"
|
||||||
|
};
|
||||||
|
|
||||||
|
let t = trybuild::TestCases::new();
|
||||||
|
t.compile_fail(format!("tests/{}/*.rs", path));
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
use rocket_db_pools::{deadpool_postgres, Database};
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database(123)]
|
||||||
|
struct A(deadpool_postgres::Pool);
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("some-name", "another")]
|
||||||
|
struct B(deadpool_postgres::Pool);
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("some-name", name = "another")]
|
||||||
|
struct C(deadpool_postgres::Pool);
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("foo")]
|
||||||
|
enum D { }
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
struct E(deadpool_postgres::Pool);
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("foo")]
|
||||||
|
struct F;
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("foo")]
|
||||||
|
struct G(deadpool_postgres::Pool, deadpool_postgres::Pool);
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("foo")]
|
||||||
|
struct H {
|
||||||
|
foo: deadpool_postgres::Pool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() { }
|
|
@ -0,0 +1,13 @@
|
||||||
|
#[macro_use] extern crate rocket_db_pools;
|
||||||
|
|
||||||
|
struct Unknown;
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("foo")]
|
||||||
|
struct A(Unknown);
|
||||||
|
|
||||||
|
#[derive(Database)]
|
||||||
|
#[database("bar")]
|
||||||
|
struct B(Vec<i32>);
|
||||||
|
|
||||||
|
fn main() { }
|
|
@ -84,6 +84,11 @@ CONTRIB_SYNC_DB_POOLS_CRATE_ROOTS=(
|
||||||
"${CONTRIB_ROOT}/sync_db_pools/codegen"
|
"${CONTRIB_ROOT}/sync_db_pools/codegen"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
CONTRIB_DB_POOLS_CRATE_ROOTS=(
|
||||||
|
"${CONTRIB_ROOT}/db_pools/lib"
|
||||||
|
"${CONTRIB_ROOT}/db_pools/codegen"
|
||||||
|
)
|
||||||
|
|
||||||
ALL_CRATE_ROOTS=(
|
ALL_CRATE_ROOTS=(
|
||||||
"${CORE_HTTP_ROOT}"
|
"${CORE_HTTP_ROOT}"
|
||||||
"${CORE_CODEGEN_ROOT}"
|
"${CORE_CODEGEN_ROOT}"
|
||||||
|
|
|
@ -189,6 +189,9 @@ check_versions_match "${CORE_CRATE_ROOTS[@]}"
|
||||||
echo ":: Ensuring contrib sync_db_pools versions match..."
|
echo ":: Ensuring contrib sync_db_pools versions match..."
|
||||||
check_versions_match "${CONTRIB_SYNC_DB_POOLS_CRATE_ROOTS[@]}"
|
check_versions_match "${CONTRIB_SYNC_DB_POOLS_CRATE_ROOTS[@]}"
|
||||||
|
|
||||||
|
echo ":: Ensuring contrib db_pools versions match..."
|
||||||
|
check_versions_match "${CONTRIB_SYNC_DB_POOLS_CRATE_ROOTS[@]}"
|
||||||
|
|
||||||
echo ":: Ensuring minimum style requirements are met..."
|
echo ":: Ensuring minimum style requirements are met..."
|
||||||
check_style
|
check_style
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue