Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept #[pg_test(expected = "string")] #1570

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pgrx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ pub fn pg_guard(_attr: TokenStream, item: TokenStream) -> TokenStream {

/// `#[pg_test]` functions are test functions (akin to `#[test]`), but they run in-process inside
/// Postgres during `cargo pgrx test`.
///
/// This can be combined with test attributes like [`#[should_panic(expected = "..")]`][expected].
///
/// [expected]: https://doc.rust-lang.org/reference/attributes/testing.html#the-should_panic-attribute
#[proc_macro_attribute]
pub fn pg_test(attr: TokenStream, item: TokenStream) -> TokenStream {
let mut stream = proc_macro2::TokenStream::new();
let args = parse_extern_attributes(proc_macro2::TokenStream::from(attr.clone()));

let mut expected_error = None;
args.into_iter().for_each(|v| {
if let ExternArgs::Error(message) = v {
if let ExternArgs::ShouldPanic(message) = v {
expected_error = Some(message)
}
});
Expand Down
14 changes: 8 additions & 6 deletions pgrx-sql-entity-graph/src/extern_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum ExternArgs {
ParallelSafe,
ParallelUnsafe,
ParallelRestricted,
Error(String),
ShouldPanic(String),
Schema(String),
Name(String),
Cost(String),
Expand All @@ -47,7 +47,7 @@ impl core::fmt::Display for ExternArgs {
ExternArgs::SecurityDefiner => write!(f, "SECURITY DEFINER"),
ExternArgs::SecurityInvoker => write!(f, "SECURITY INVOKER"),
ExternArgs::ParallelRestricted => write!(f, "PARALLEL RESTRICTED"),
ExternArgs::Error(_) => Ok(()),
ExternArgs::ShouldPanic(_) => Ok(()),
ExternArgs::NoGuard => Ok(()),
ExternArgs::Schema(_) => Ok(()),
ExternArgs::Name(_) => Ok(()),
Expand All @@ -72,7 +72,7 @@ impl ToTokens for ExternArgs {
ExternArgs::ParallelSafe => tokens.append(format_ident!("ParallelSafe")),
ExternArgs::ParallelUnsafe => tokens.append(format_ident!("ParallelUnsafe")),
ExternArgs::ParallelRestricted => tokens.append(format_ident!("ParallelRestricted")),
ExternArgs::Error(_s) => {
ExternArgs::ShouldPanic(_s) => {
tokens.append_all(
quote! {
Error(String::from("#_s"))
Expand Down Expand Up @@ -143,15 +143,15 @@ pub fn parse_extern_attributes(attr: TokenStream) -> HashSet<ExternArgs> {
"parallel_safe" => args.insert(ExternArgs::ParallelSafe),
"parallel_unsafe" => args.insert(ExternArgs::ParallelUnsafe),
"parallel_restricted" => args.insert(ExternArgs::ParallelRestricted),
"error" => {
"error" | "expected" => {
let _punc = itr.next().unwrap();
let literal = itr.next().unwrap();
let message = literal.to_string();
let message = unescape::unescape(&message).expect("failed to unescape");

// trim leading/trailing quotes around the literal
let message = message[1..message.len() - 1].to_string();
args.insert(ExternArgs::Error(message.to_string()))
args.insert(ExternArgs::ShouldPanic(message.to_string()))
}
"schema" => {
let _punc = itr.next().unwrap();
Expand Down Expand Up @@ -201,6 +201,8 @@ mod tests {
let ts = proc_macro2::TokenStream::from_str(s).unwrap();

let args = parse_extern_attributes(ts);
assert!(args.contains(&ExternArgs::Error("syntax error at or near \"THIS\"".to_string())));
assert!(
args.contains(&ExternArgs::ShouldPanic("syntax error at or near \"THIS\"".to_string()))
);
}
}
14 changes: 7 additions & 7 deletions pgrx-sql-entity-graph/src/pg_extern/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum Attribute {
ParallelSafe,
ParallelUnsafe,
ParallelRestricted,
Error(syn::LitStr),
ShouldPanic(syn::LitStr),
Schema(syn::LitStr),
Name(syn::LitStr),
Cost(syn::Expr),
Expand Down Expand Up @@ -76,8 +76,8 @@ impl Attribute {
Attribute::ParallelRestricted => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::ParallelRestricted }
}
Attribute::Error(s) => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Error(String::from(#s)) }
Attribute::ShouldPanic(s) => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::ShouldPanic(String::from(#s)) }
}
Attribute::Schema(s) => {
quote! { ::pgrx::pgrx_sql_entity_graph::ExternArgs::Schema(String::from(#s)) }
Expand Down Expand Up @@ -125,8 +125,8 @@ impl ToTokens for Attribute {
Attribute::ParallelRestricted => {
quote! { parallel_restricted }
}
Attribute::Error(s) => {
quote! { error = #s }
Attribute::ShouldPanic(s) => {
quote! { expected = #s }
}
Attribute::Schema(s) => {
quote! { schema = #s }
Expand Down Expand Up @@ -166,10 +166,10 @@ impl Parse for Attribute {
"parallel_safe" => Self::ParallelSafe,
"parallel_unsafe" => Self::ParallelUnsafe,
"parallel_restricted" => Self::ParallelRestricted,
"error" => {
"error" | "expected" => {
let _eq: Token![=] = input.parse()?;
let literal: syn::LitStr = input.parse()?;
Self::Error(literal)
Attribute::ShouldPanic(literal)
}
"schema" => {
let _eq: Token![=] = input.parse()?;
Expand Down
6 changes: 3 additions & 3 deletions pgrx-tests/src/tests/array_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mod tests {
assert_eq!(sum, Ok(Some(6)));
}

#[pg_test(error = "attempt to add with overflow")]
#[pg_test(expected = "attempt to add with overflow")]
fn test_sum_array_i32_overflow() -> Result<Option<i64>, pgrx::spi::Error> {
Spi::get_one::<i64>(
"SELECT sum_array(a) FROM (SELECT array_agg(s) a FROM generate_series(1, 1000000) s) x;",
Expand All @@ -241,7 +241,7 @@ mod tests {
assert_eq!(sum, Ok(Some(6f32)));
}

#[pg_test(error = "array contains NULL")]
#[pg_test(expected = "array contains NULL")]
fn test_array_deny_nulls() -> Result<(), spi::Error> {
Spi::run("SELECT iterate_array_with_deny_null(ARRAY[1,2,3, NULL]::int[])")
}
Expand Down Expand Up @@ -273,7 +273,7 @@ mod tests {
Ok(())
}

#[pg_test(error = "array contains NULL")]
#[pg_test(expected = "array contains NULL")]
fn test_serde_serialize_array_i32_deny_null() -> Result<Option<Json>, pgrx::spi::Error> {
Spi::get_one::<Json>(
"SELECT serde_serialize_array_i32_deny_null(ARRAY[1, 2, 3, null, 4, 5])",
Expand Down
3 changes: 2 additions & 1 deletion pgrx-tests/src/tests/fn_call_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ mod tests {
assert_eq!(Err(FnCallError::InvalidIdentifier(String::from(stupid_name))), result)
}

#[pg_test(error = "it worked")]
#[pg_test]
#[should_panic(expected = "it worked")]
fn fn_raises_error() {
use std::sync::atomic::AtomicBool;

Expand Down
Loading