use super::*;
use NormalizedValue::Int;
fn norm(src: &str) -> NormalizeOutcome {
normalize_statement(src, ParseOptions::default())
}
fn normalized(src: &str) -> Normalized {
match norm(src) {
NormalizeOutcome::Normalized(n) => n,
NormalizeOutcome::NotCacheable => panic!("expected {src:?} to normalize"),
}
}
fn template(src: &str) -> String {
normalized(src).template
}
fn values(src: &str) -> Vec<(String, NormalizedValue)> {
normalized(src).values
}
#[test]
fn keyword_case_stays_as_written() {
assert_ne!(
template("MATCH (n:order) RETURN n"),
template("MATCH (n:Order) RETURN n")
);
assert_ne!(template("RETURN n.match"), template("RETURN n.MATCH"));
assert_ne!(template("RETURN {match: 1}"), template("RETURN {MATCH: 1}"));
assert_ne!(
template("match (a:Person) where a.age > 21 with a limit 5"),
template("MATCH (a:Person) WHERE a.age > 21 WITH a LIMIT 5")
);
assert_ne!(
template("MATCH (a:Person) RETURN a"),
template("MATCH (b:Person) RETURN b")
);
assert_ne!(template("RETURN count"), template("RETURN COUNT"));
assert_ne!(template("RETURN n.true"), template("RETURN n.TRUE"));
}
#[test]
fn typed_literal_placeholders() {
assert_eq!(
template("MATCH (n:User {id: 42}) RETURN n"),
"MATCH ( n : User { id : $__AUTO_INT_0__ } ) RETURN n"
);
assert_eq!(
template("MATCH (n:User {id: 43}) RETURN n"),
template("MATCH (n:User {id: 42}) RETURN n")
);
let int = template("RETURN 42");
let float = template("RETURN 4.2");
let string = template("RETURN '42'");
assert_ne!(int, float);
assert_ne!(int, string);
assert_ne!(float, string);
assert_eq!(
values("RETURN 'hello'"),
vec![(
"__AUTO_STR_0__".into(),
NormalizedValue::Str("hello".into())
)]
);
}
#[test]
fn lists_collapse_to_one_param() {
assert_eq!(template("RETURN [1, 'a', 2.5]"), "RETURN $__AUTO_LIST_0__");
assert_eq!(
values("RETURN [1, 'a', 2.5]"),
vec![(
"__AUTO_LIST_0__".into(),
NormalizedValue::List(vec![
NormalizedValue::Int(1),
NormalizedValue::Str("a".into()),
NormalizedValue::Float(2.5),
])
)]
);
assert_eq!(template("RETURN []"), "RETURN $__AUTO_LIST_0__");
assert_eq!(template("RETURN [[1, 2], [3]]"), "RETURN $__AUTO_LIST_0__");
assert_eq!(
values("RETURN [[1, 2], [3]]"),
vec![(
"__AUTO_LIST_0__".into(),
NormalizedValue::List(vec![
NormalizedValue::List(vec![Int(1), Int(2)]),
NormalizedValue::List(vec![Int(3)]),
])
)]
);
assert_eq!(
template("RETURN [1, 2 + 3]"),
"RETURN [ $__AUTO_INT_0__ , $__AUTO_INT_1__ + $__AUTO_INT_2__ ]"
);
}
#[test]
fn map_key_strings_stay() {
assert_eq!(
template("RETURN {'a': 1}"),
"RETURN { 'a' : $__AUTO_INT_0__ }"
);
assert_ne!(template("RETURN {'a': 1}"), template("RETURN {'b': 1}"));
assert_ne!(template("RETURN {'a': 1}"), template("RETURN {a: 1}"));
}
#[test]
fn load_csv_field_terminator_stays() {
let t = template("LOAD CSV WITH HEADERS FROM 'data.csv' AS row FIELDTERMINATOR ';' RETURN row");
assert!(t.contains("$__AUTO_STR_0__"), "the URL normalizes: {t}");
assert!(t.contains("';'"), "the terminator stays: {t}");
assert_ne!(
template("LOAD CSV FROM 'a.csv' AS r FIELDTERMINATOR ';' RETURN r"),
template("LOAD CSV FROM 'a.csv' AS r FIELDTERMINATOR ',' RETURN r")
);
}
#[test]
fn quantifier_bounds_stay() {
let t = template("MATCH p = (a)-[:KNOWS*1..3]->(b) RETURN p");
assert!(t.contains("* 1 .. 3"), "the bounds stay: {t}");
assert_ne!(
template("MATCH p = (a)-[:KNOWS*1..3]->(b) RETURN p"),
template("MATCH p = (a)-[:KNOWS*2..3]->(b) RETURN p")
);
assert_ne!(template("RETURN 2 * 3"), template("RETURN 2 * 4"));
}
#[test]
fn function_first_arguments_stay() {
let t = template("WHERE date(p.birthday) > date(\"1990-01-01\")");
assert!(
t.contains("'1990-01-01'"),
"the constructor argument stays: {t}"
);
assert!(!t.contains("$__AUTO_STR"), "no placeholder for it: {t}");
assert_ne!(
template("RETURN date(\"1990-01-01\") AS d"),
template("RETURN date(\"1991-01-01\") AS d")
);
assert!(template("RETURN foo(\"x\") AS y").contains("'x'"));
let dt = template("RETURN datetime({year: 2021}) AS d");
assert!(
dt.contains("2021"),
"the map-form argument stays literal: {dt}"
);
assert!(!dt.contains("$__AUTO_INT"), "no placeholder for it: {dt}");
assert!(template("RETURN left('abc', 2) AS s").contains("'abc'"));
assert_eq!(
template("WHERE n.name = 'alice'"),
"WHERE n . name = $__AUTO_STR_0__"
);
assert_eq!(template("RETURN [1, 2]"), "RETURN $__AUTO_LIST_0__");
let list_arg = template("RETURN f([1, 2]) AS x");
assert!(
list_arg.contains("[ 1 , 2 ]"),
"the list argument stays literal, element-wise: {list_arg}"
);
assert!(
!list_arg.contains("$__AUTO_LIST"),
"no whole-group placeholder inside a call: {list_arg}"
);
}
#[test]
fn parameters_render_with_dollar() {
assert_eq!(
template("MATCH (n {id: $uid}) RETURN n"),
"MATCH ( n { id : $uid } ) RETURN n"
);
assert_ne!(
template("MATCH (n {id: $uid}) RETURN n"),
template("MATCH (n {id: uid}) RETURN n")
);
assert_ne!(
template("MATCH (n {id: $uid}) RETURN n"),
template("MATCH (n {id: $other}) RETURN n")
);
}
#[test]
fn quoted_identifiers_render_quoted() {
let t = template("MATCH (`a b`) RETURN `a b`");
assert!(t.contains("`a b`"), "the quoted name stays quoted: {t}");
assert_ne!(t, template("MATCH (a) RETURN a"));
assert_ne!(template("RETURN `MATCH`"), template("RETURN MATCH"));
}
#[test]
fn interpolated_strings_bail_the_statement() {
assert!(matches!(
norm("RETURN s'a{x}' AS s"),
NormalizeOutcome::NotCacheable
));
}
#[test]
fn obfuscated_literal_renders_as_written() {
let gated = ParseOptions {
obfuscated_literals: true,
..ParseOptions::default()
};
let t = match normalize_statement("RETURN ******", gated) {
NormalizeOutcome::Normalized(n) => n.template,
NormalizeOutcome::NotCacheable => panic!("expected the gated form to normalize"),
};
assert_eq!(t, "RETURN ******");
}
#[test]
fn auto_param_prefix_in_statement_bails() {
assert!(matches!(
norm("RETURN $__AUTO_INT_0__"),
NormalizeOutcome::NotCacheable
));
assert!(matches!(
norm("RETURN $__AUTO_LIST_9__"),
NormalizeOutcome::NotCacheable
));
assert!(matches!(
norm("RETURN $__auto_x__"),
NormalizeOutcome::Normalized(_)
));
}
#[test]
fn error_tokens_and_oversize_bail() {
assert!(matches!(
norm("RETURN 1B2c3e67"),
NormalizeOutcome::NotCacheable
));
let oversized = format!("RETURN {}", "1".repeat(MAX_QUERY_BYTES));
assert!(matches!(norm(&oversized), NormalizeOutcome::NotCacheable));
}
#[test]
fn rewritten_statement_parses() {
for src in [
"MATCH (n:User {id: 42}) RETURN n",
"MATCH (n:User {id: 'alice'}) RETURN n.name",
"MATCH (n) WHERE n.age > 21 AND n.name STARTS WITH 'a' RETURN n ORDER BY n.age LIMIT 5",
"MATCH (n) WHERE n.t IN [1, 2, 3] RETURN n",
"RETURN { 'name': 'x', age: 5 } AS m",
"UNWIND [1, 2] AS x RETURN x",
"MATCH (n {id: $uid}) RETURN n",
"match (n:person) return n limit 10",
] {
crate::parser::parse(src).expect("the original parses");
let n = normalized(src);
crate::parser::parse(&n.rewritten)
.unwrap_or_else(|e| panic!("rewritten of {src:?} parses: {e}\n{}", n.rewritten));
}
}
#[test]
fn cypher_version_integer_fails_closed() {
let n = normalized("CYPHER 5 MATCH (n) RETURN n");
assert!(
n.rewritten.contains("$__AUTO_INT_0__"),
"the version became a placeholder: {}",
n.rewritten
);
assert!(crate::parser::parse(&n.rewritten).is_err());
}
#[test]
fn whitespace_and_comments_do_not_affect_the_template() {
assert_eq!(
template("MATCH (n:User {id: 42}) RETURN n"),
template("MATCH\n ( n : User { id: 42 } )\n// find\n RETURN n")
);
let n = normalized("MATCH (n:User {id: 42}) RETURN n LIMIT 3");
crate::parser::parse(&n.rewritten)
.unwrap_or_else(|e| panic!("the spliced form parses: {e}\n{}", n.rewritten));
let limited = normalized("RETURN 1 LIMIT 2");
assert!(
limited.rewritten.contains("$__AUTO_INT_0__ LIMIT"),
"keyword stays a separate token: {}",
limited.rewritten
);
let added = normalized("RETURN 1+2");
assert!(
added.rewritten.contains("$__AUTO_INT_0__+$__AUTO_INT_1__"),
"operator stays glued: {}",
added.rewritten
);
}
#[test]
fn escaped_strings_share_by_value() {
assert_eq!(template("RETURN 'a\\nb'"), template("RETURN \"a\nb\""));
assert_eq!(
values("RETURN 'a\\nb'"),
vec![("__AUTO_STR_0__".into(), NormalizedValue::Str("a\nb".into()))]
);
}
#[test]
fn deeply_nested_lists_decline_instead_of_recursing() {
let depth = 100_000;
let deep = format!("RETURN {}{}", "[".repeat(depth), "]".repeat(depth));
assert_eq!(norm(&deep), NormalizeOutcome::NotCacheable);
let parsed = relate_test_support::run_with_big_stack(move || crate::parser::parse(&deep));
assert!(parsed.is_err(), "the parser's depth guard rejects it");
let inside = format!("RETURN {}1{}", "[".repeat(8), "]".repeat(8));
let NormalizeOutcome::Normalized(inner) = norm(&inside) else {
panic!("the inside-cap payload normalizes");
};
assert_eq!(inner.template, "RETURN $__AUTO_LIST_0__");
}