use super::super::function::function_from_definition;
fn source(name: &str) -> String {
let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../fixtures/postgres/catalog/function-contracts")
.join(format!("{name}.sql"));
std::fs::read_to_string(path).unwrap()
}
#[test]
fn saved_function_literals_decode_bodies_without_reading_header_noise() {
for (name, expected) in [
("adjacent", "SELECT 1"),
("escape_controls", "SELECT\n\t\r\u{8}\u{c}\\'it's"),
("escape_invalid", "xZ|u|U|u12|\u{7}|Ā"),
("unicode_long", "SELECT 😀, !"),
("unicode_invalid", "SELECT !xyz, +xyz"),
("escape_long", "😀"),
("atomic_nested", " BEGIN SELECT 1; END; SELECT 2; "),
("return_expression", " n + 1\n"),
] {
let definition = source(name);
let function = function_from_definition("f()", &definition);
assert_eq!(function.body.as_deref(), Some(expected), "{name}");
assert_eq!(function.language.as_deref(), Some("sql"), "{name}");
let (start, end) = function.body_span.expect("source body span");
assert!(start < end && end <= definition.len(), "{name}");
}
}
#[test]
fn invalid_or_unclosed_function_literals_leave_the_body_unknown() {
for name in [
"unicode_bad_escape",
"unicode_hex_escape",
"unicode_space_escape",
"unclosed_dollar",
"unclosed_escape",
] {
let function = function_from_definition("f()", &source(name));
assert_eq!(function.body, None, "{name}");
assert_eq!(function.body_span, None, "{name}");
assert_eq!(function.language.as_deref(), Some("sql"), "{name}");
}
}
#[test]
fn invalid_quoted_routine_languages_are_unknown() {
for name in [
"unclosed_language",
"unclosed_language_with_doubled_quote",
"empty_language",
] {
let definition = source(name);
let function = function_from_definition("f()", definition.trim_end());
assert_eq!(function.language, None, "{name}");
}
}
#[test]
fn routine_named_return_keeps_its_real_body_and_language() {
let procedural = function_from_definition("return()", &source("keyword_routine_name"));
assert_eq!(procedural.language.as_deref(), Some("plpgsql"));
assert_eq!(procedural.body.as_deref(), Some("BEGIN RETURN 1; END"));
let standard = function_from_definition("return()", &source("keyword_inline_return"));
assert_eq!(standard.language.as_deref(), Some("sql"));
assert_eq!(standard.body.as_deref(), Some(" 1;\n"));
}
#[test]
fn inline_sql_body_delimiters_ignore_keywords_used_as_identifiers() {
for (name, expected) in [
("keyword_result_type", " 1;\n"),
("qualified_keyword_result_type", " 2;\n"),
("keyword_routine_setting", " 3;\n"),
("language_routine_setting", " 4;\n"),
("language_result_type", " 5;\n"),
(
"atomic_keyword_columns",
"\n SELECT begin + if + loop + CASE WHEN true THEN 1 ELSE 0 END FROM jobs;\n",
),
] {
let function = function_from_definition("f()", &source(name));
assert_eq!(function.language.as_deref(), Some("sql"), "{name}");
assert_eq!(function.body.as_deref(), Some(expected), "{name}");
}
}
#[test]
fn quoted_function_keys_and_planner_settings_preserve_distinct_contracts() {
let function = function_from_definition(
"\"odd\"\"(name\"(integer,text)",
&source("quoted_signature"),
);
assert_eq!(function.name, "\"odd\"\"(name\"");
assert_eq!(function.signature.as_deref(), Some("integer,text"));
assert!(function.return_contract.contains("out value integer"));
assert!(function.return_contract.contains("inout label text"));
assert_eq!(function.body.as_deref(), Some("SELECT 1, 'a'"));
let planned = function_from_definition("f()", &source("planner_settings"));
assert_eq!(planned.planner, "support public.estimate cost 3.5 rows 8");
assert_eq!(planned.parallel, "safe");
assert_eq!(planned.volatility, "volatile");
assert_eq!(planned.return_contract, "setof integer");
}
#[test]
fn malformed_unicode_escape_clauses_and_dollar_tags_do_not_invent_a_body() {
for name in [
"unicode_empty_escape",
"unicode_missing_escape",
"unicode_unquoted_escape",
"unicode_unclosed_literal",
"invalid_dollar_tag",
] {
let function = function_from_definition("f()", &source(name));
assert_eq!(function.body, None, "{name}");
assert_eq!(function.language.as_deref(), Some("sql"), "{name}");
}
}
#[test]
fn settings_and_output_types_do_not_capture_unrelated_planner_words() {
let settings = function_from_definition("f()", &source("set_equals"));
assert_eq!(settings.planner, "cost 2 support schema.estimate");
assert_eq!(settings.return_contract, "int");
let malformed = function_from_definition("f()", &source("malformed_planner"));
assert!(malformed.planner.is_empty());
assert_eq!(malformed.body.as_deref(), Some("SELECT 1"));
let outputs = function_from_definition("f(numeric)", &source("output_types"));
assert!(outputs.return_contract.contains("out total numeric"));
assert!(outputs.return_contract.contains("inout tags text"));
assert_eq!(outputs.body.as_deref(), Some("SELECT 1"));
}