use super::*;
use relate_names::SmallName;
#[test]
fn test_parse_simple_match_return() {
let stmt = parse("MATCH (n) RETURN n").unwrap();
assert_eq!(stmt.clauses.len(), 2);
match &stmt.clauses[0] {
Clause::Match(m) => {
assert!(!m.optional);
assert_eq!(m.pattern.len(), 1);
assert_eq!(m.pattern[0].elements.len(), 1);
match &m.pattern[0].elements[0] {
PatternElement::Node(n) => {
assert_eq!(n.variable, Some("n".into()));
}
_ => panic!("expected node pattern"),
}
}
_ => panic!("expected match clause"),
}
match &stmt.clauses[1] {
Clause::Return(r) => {
assert_eq!(r.items.len(), 1);
match &r.items[0].expr {
Expr::Variable(name) => assert_eq!(name, "n"),
_ => panic!("expected variable"),
}
}
_ => panic!("expected return clause"),
}
}
#[test]
fn test_parse_match_with_labels() {
let stmt = parse("MATCH (n:Person) RETURN n").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[0] {
PatternElement::Node(n) => {
assert_eq!(
n.label_expr.as_ref().and_then(|e| e.as_label_list()),
Some(vec!["Person".into()])
);
}
_ => panic!("expected node"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_match_with_properties() {
let stmt = parse("MATCH (n:Person {name: 'Alice'}) RETURN n").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[0] {
PatternElement::Node(n) => {
assert!(n.has_properties);
assert_eq!(n.properties.len(), 1);
assert_eq!(n.properties[0].0, "name");
assert_eq!(
n.properties[0].1,
Expr::Literal(LiteralValue::String("Alice".into()))
);
}
_ => panic!("expected node"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_relationship() {
let stmt = parse("MATCH (a)-[r:KNOWS]->(b) RETURN a, b").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => {
assert_eq!(m.pattern[0].elements.len(), 3);
match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.variable, Some("r".into()));
assert_eq!(
r.type_expr.as_ref().and_then(|e| e.as_label_list()),
Some(vec!["KNOWS".into()])
);
assert_eq!(r.direction, Direction::Right);
}
_ => panic!("expected relationship"),
}
}
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_left_arrow() {
let stmt = parse("MATCH (a)<-[r]-(b) RETURN a").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.direction, Direction::Left);
}
_ => panic!("expected relationship"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_undirected() {
let stmt = parse("MATCH (a)-[r]-(b) RETURN a").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.direction, Direction::Both);
}
_ => panic!("expected relationship"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_spaced_arrowhead_is_the_right_arrow() {
for q in [
"MATCH (a)-[:R*2]- >(b) RETURN b",
"MATCH (a) - [:R*2] - > (b) RETURN b",
"MATCH (a)-- >(b) RETURN b",
"MATCH (a)-[r WHERE r.p > 1]- >(b) RETURN b",
"MATCH (a)-[:R]- >(b) WHERE a.x > 1 RETURN b",
] {
let stmt = parse_ok(q);
match &match_clause(&stmt).pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.direction, Direction::Right, "{q}");
}
_ => panic!("{q}: expected relationship"),
}
}
}
#[test]
fn test_spaced_arrowhead_keeps_the_direction_matrix() {
for (q, want) in [
("MATCH (a)-[:R]- >(b) RETURN b", Direction::Right),
("MATCH (a)-- >(b) RETURN b", Direction::Right),
("MATCH (a)<-[:R]- >(b) RETURN b", Direction::Both),
("MATCH (a)<- - >(b) RETURN b", Direction::Both),
("MATCH (a)< -[:R]-> (b) RETURN b", Direction::Both),
("MATCH (a)< -[:R]- > (b) RETURN b", Direction::Both),
("MATCH (a)< -- (b) RETURN b", Direction::Left),
("MATCH (a)< -[:R]- (b) RETURN b", Direction::Left),
("MATCH (a)-[:R]-(b) RETURN b", Direction::Both),
("MATCH (a)--(b) RETURN b", Direction::Both),
("MATCH (a)<-[:R]-(b) RETURN b", Direction::Left),
("MATCH (a)<--(b) RETURN b", Direction::Left),
("MATCH (a)-[:R]->(b) RETURN b", Direction::Right),
("MATCH (a)<-[:R]->(b) RETURN b", Direction::Both),
] {
let stmt = parse_ok(q);
match &match_clause(&stmt).pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.direction, want, "{q}");
}
_ => panic!("{q}: expected relationship"),
}
}
}
#[test]
fn test_single_dash_arrowhead_spaced_or_glued_stays_rejected() {
let tail =
parse("MATCH (a) - > (b) RETURN b").expect_err("the spaced single-dash form must reject");
assert!(
tail.message.contains("expected -> or -, got"),
"wrong error: {}",
tail.message
);
let head =
parse("MATCH (a)->(b) RETURN b").expect_err("the glued single-dash form must reject");
assert!(
head.message.contains("expected - or <-, got"),
"wrong error: {}",
head.message
);
let left =
parse("MATCH (a) < (b) RETURN b").expect_err("a `<` without its head dash must reject");
assert!(
left.message.contains("expected - after <, got"),
"wrong error: {}",
left.message
);
}
#[test]
fn test_expression_comparisons_survive_the_spaced_head_acceptance() {
for (q, twin) in [
("RETURN (a) < (b)", "RETURN a < b"),
("RETURN (a) < -1", "RETURN a < -1"),
("RETURN (1) < (2)", "RETURN 1 < 2"),
] {
let (stmt, twin_stmt) = (parse_ok(q), parse_ok(twin));
let (Clause::Return(r), Clause::Return(tr)) = (&stmt.clauses[0], &twin_stmt.clauses[0])
else {
panic!("{q}: expected a RETURN");
};
assert!(
matches!(r.items[0].expr, Expr::Comparison(_)),
"{q}: expected a comparison, got {:?}",
r.items[0].expr
);
assert!(
matches!(tr.items[0].expr, Expr::Comparison(_)),
"{twin}: expected a comparison, got {:?}",
tr.items[0].expr
);
}
}
#[test]
fn test_parse_where() {
let stmt = parse("MATCH (n) WHERE n.age > 30 RETURN n").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => {
assert!(m.where_expr.is_some());
}
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_named_path() {
let stmt = parse("MATCH p = (a)-[r]->(b) RETURN p").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => {
assert_eq!(m.pattern[0].variable, Some("p".into()));
}
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_variable_length_rel() {
let stmt = parse("MATCH (a)-[*1..3]->(b) RETURN a").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.min_hops, Some(1));
assert_eq!(r.max_hops, Some(3));
}
_ => panic!("expected relationship"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_pattern_predicate() {
let stmt = parse("MATCH (n) WHERE (n)-[:KNOWS]->() RETURN n").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match m.where_expr.as_ref().unwrap() {
Expr::PatternPredicate(_) => {}
_ => panic!("expected pattern predicate"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_multiple_rel_types() {
let stmt = parse("MATCH (a)-[:KNOWS|LIKES]->(b) RETURN a").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => match r.type_expr.as_ref().unwrap() {
LabelExpr::Or(parts) => {
let names: Vec<SmallName> = parts
.iter()
.map(|p| match p {
LabelExpr::Label(n) => n.clone(),
_ => panic!("expected label"),
})
.collect();
assert_eq!(names, vec!["KNOWS", "LIKES"]);
}
_ => panic!("expected disjunction"),
},
_ => panic!("expected relationship"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_multiple_patterns() {
let stmt = parse("MATCH (a), (b) RETURN a, b").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => {
assert_eq!(m.pattern.len(), 2);
}
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_empty_node() {
let stmt = parse("MATCH () RETURN 1").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[0] {
PatternElement::Node(n) => {
assert!(n.variable.is_none());
assert!(n.label_expr.is_none());
}
_ => panic!("expected node"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_bare_star_rel() {
let stmt = parse("MATCH (a)-[*]->(b) RETURN a").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.min_hops, Some(1));
assert!(r.max_hops.is_none());
}
_ => panic!("expected relationship"),
},
_ => panic!("expected match"),
}
}
#[test]
fn test_parse_legacy_hop_bounds_all_spellings() {
for (q, want_min, want_max) in [
("MATCH (a)-[*]->(b) RETURN a", 1, None),
("MATCH (a)-[*3]->(b) RETURN a", 3, Some(3)),
("MATCH (a)-[*2..]->(b) RETURN a", 2, None),
("MATCH (a)-[*1..3]->(b) RETURN a", 1, Some(3)),
("MATCH (a)-[*..]->(b) RETURN a", 1, None),
("MATCH (a)-[*..4]->(b) RETURN a", 1, Some(4)),
] {
let stmt = parse(q).unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.min_hops, Some(want_min), "{q}: min_hops");
assert_eq!(r.max_hops, want_max, "{q}: max_hops");
}
_ => panic!("{q}: expected relationship"),
},
_ => panic!("{q}: expected match"),
}
}
}
#[test]
fn test_relationship_type_negation_or_wildcard_with_quantifier_accepted() {
assert!(parse("MATCH ()-[:!S*]->() RETURN 1").is_ok());
assert!(parse("MATCH ()-[:%*]->() RETURN 1").is_ok());
assert!(parse("MATCH ()-[r:!A&!B*1..5]->() RETURN r").is_ok());
assert!(parse("MATCH ()-[:S*]->() RETURN 1").is_ok());
assert!(parse("MATCH ()-[:S|T*1..2]->() RETURN 1").is_ok());
assert!(parse("MATCH ()-[:(S)*]->() RETURN 1").is_ok());
}
#[test]
fn test_node_and_relationship_element_pattern_where() {
assert!(parse("MATCH (n WHERE n:A&B)-[r]->(m) RETURN n").is_ok());
assert!(parse("MATCH (n)-[r WHERE r:!S|!T]->(m) RETURN n").is_ok());
}
#[test]
fn test_parenthesized_path_pattern_where() {
let stmt = parse("MATCH ((n)-[r]->(m) WHERE n.p > m.q AND r:S) RETURN n").unwrap();
match &stmt.clauses[0] {
Clause::Match(mc) => {
assert!(mc.pattern[0].where_expr.is_some());
assert_eq!(mc.pattern[0].elements.len(), 3);
}
other => panic!("expected match, got {other:?}"),
}
}
#[test]
fn test_path_variable_on_top_level_parenthesized_pattern() {
let stmt = parse("MATCH p = ((a)-[r:T]->(b)) RETURN p").unwrap();
match &stmt.clauses[0] {
Clause::Match(mc) => {
assert_eq!(
mc.pattern[0].variable.as_ref(),
Some(&SmallName::from_name("p"))
);
assert_eq!(mc.pattern[0].elements.len(), 3);
}
other => panic!("expected match, got {other:?}"),
}
}
#[test]
fn test_path_variable_on_top_level_parenthesized_pattern_with_where() {
let stmt = parse("MATCH p = ((a)-[r:T]->(b) WHERE a.p > b.q) RETURN p").unwrap();
match &stmt.clauses[0] {
Clause::Match(mc) => {
assert_eq!(
mc.pattern[0].variable.as_ref(),
Some(&SmallName::from_name("p"))
);
assert!(mc.pattern[0].where_expr.is_some());
}
other => panic!("expected match, got {other:?}"),
}
}
#[test]
fn test_inline_where_on_legacy_var_length_accepted() {
let stmt = parse("MATCH ()-[r*1..5 WHERE r.p > 30]->() RETURN r").unwrap();
match &stmt.clauses[0] {
Clause::Match(m) => match &m.pattern[0].elements[1] {
PatternElement::Relationship(r) => {
assert_eq!(r.min_hops, Some(1));
assert_eq!(r.max_hops, Some(5));
assert!(r.where_expr.is_some());
}
other => panic!("expected relationship, got {other:?}"),
},
other => panic!("expected match, got {other:?}"),
}
assert!(parse("MATCH ()-[r WHERE r.p > 30]->{1,5}() RETURN r").is_ok());
}
#[test]
fn test_pattern_comprehension_quantifiers_isolated_from_clause_forms() {
assert!(parse(
"MATCH ( (a)-[]-(b)-[]-(c) WHERE size([(a)-[:Z*1..2]->(:X) | a]) > 0 )+ RETURN *"
)
.is_ok());
assert!(parse("RETURN [(n)--+(m) | m], [(n)-[*3]-(m) | m]").is_err());
assert!(parse("MATCH ()-[r:A*1..2]->+() RETURN r").is_err());
}
#[test]
fn test_tree_cache_replay_keeps_a_comprehensions_quantifier_scope() {
let inner = "(m WHERE [(x WHERE (x)-[:B*1..2]->(z))-[:C]->+(w) | x] <> [])";
for outer in ["[:A]", "[:A*1..2]"] {
let err = parse(&format!("MATCH (n)-{outer}->{inner} RETURN n"))
.expect_err("the comprehension-local mixed forms reject");
assert!(
err.to_string().contains("MixedQuantifiers"),
"the typed rejection, not another error: {err}"
);
}
}
#[test]
fn test_call_subquery_body_is_one_label_scope() {
assert!(parse("CALL () { MATCH (n:A:B) MATCH (n)--(m:A&!B) RETURN * } RETURN n").is_err());
assert!(parse("CALL () { MATCH (n:A:B) MATCH (n)--(IS A) RETURN * } RETURN n").is_err());
assert!(parse("CALL () { CREATE (n:A:B) CREATE (m:C&D) RETURN * } RETURN *").is_err());
assert!(parse("CALL () { MERGE (n:A:B) CREATE (m IS C) RETURN * } RETURN *").is_err());
assert!(
parse("CALL () { MATCH (n:A:B) CREATE (n)-[IS R]->(m IS B) RETURN * } RETURN n").is_ok()
);
assert!(parse("CALL () { MATCH (n:A:B) MATCH (n)--+(:A) RETURN * } RETURN n").is_ok());
assert!(
parse("CALL () { MATCH (:A)-[*1..5]->(m:B) WITH m:!A AS n RETURN n } RETURN n").is_ok()
);
assert!(parse("MATCH (n:A:B)-[IS R]->(m) RETURN n").is_err());
assert!(parse("MATCH (n:A:B) WHERE EXISTS { MATCH (m:C|D) } RETURN n").is_err());
assert!(parse("RETURN COUNT { MATCH (n:A:B) MATCH (m:C|D) RETURN m } AS c").is_err());
}
#[test]
fn test_pattern_comprehension_with_inline_node_predicate() {
let stmt = parse("RETURN [(a:A WHERE a.prop > 1)-[r]-(b:B) | a.prop] AS result").unwrap();
let Clause::Return(ret) = &stmt.clauses[0] else {
panic!("expected return");
};
let Expr::PatternComprehension(pc) = &ret.items[0].expr else {
panic!(
"expected pattern comprehension, got {:?}",
ret.items[0].expr
);
};
let PatternElement::Node(node) = &pc.pattern.elements[0] else {
panic!("expected node pattern");
};
assert!(node.where_expr.is_some());
}
#[test]
fn test_pattern_comprehension_predicate_references_later_element() {
let stmt =
parse("RETURN [(a:A WHERE b.prop > 1)-[r]-(b:B) | [a.prop, b.prop]] AS result").unwrap();
let Clause::Return(ret) = &stmt.clauses[0] else {
panic!("expected return");
};
assert!(matches!(&ret.items[0].expr, Expr::PatternComprehension(_)));
}
#[test]
fn test_pattern_comprehension_where_label_expression() {
let stmt =
parse("MATCH (n) RETURN [p = (n)-->() WHERE last(nodes(p)):End | p] AS list").unwrap();
let Clause::Return(ret) = &stmt.clauses[1] else {
panic!("expected return");
};
let Expr::PatternComprehension(pc) = &ret.items[0].expr else {
panic!("expected pattern comprehension");
};
assert!(matches!(pc.where_expr, Some(Expr::HasLabel(_))));
}
#[test]
fn test_path_and_subpath_variables_bind_same_pattern() {
let stmt = parse("MATCH p = ANY SHORTEST (q = (:A)-->+(:B)) RETURN p = q AS result").unwrap();
let Clause::Match(mc) = &stmt.clauses[0] else {
panic!("expected match");
};
assert_eq!(
mc.pattern[0].variable.as_ref(),
Some(&SmallName::from_name("p"))
);
assert_eq!(
mc.pattern[0].subpath_variable.as_ref(),
Some(&SmallName::from_name("q"))
);
assert_eq!(mc.pattern[0].path_names().collect::<Vec<_>>(), ["p", "q"]);
}
#[test]
fn test_path_names_of_unnamed_path_is_empty() {
let stmt = parse("MATCH (n) RETURN n").unwrap();
let Clause::Match(mc) = &stmt.clauses[0] else {
panic!("expected match");
};
assert!(mc.pattern[0].path_names().next().is_none());
}
#[test]
fn nested_named_groups_keep_their_sub_path_variable() {
let stmt = parse_ok("MATCH p = ((q = (:A)-->(:B))) RETURN p");
assert_eq!(
match_clause(&stmt).pattern[0].subpath_variable.as_ref(),
Some(&SmallName::from_name("q"))
);
}
#[test]
fn named_unquantified_groups_reject_outside_a_named_or_selective_path() {
for q in [
"MATCH ((q = (:A)-->(:B))) RETURN q",
"MATCH (( (x = (:A)) )) RETURN 1",
] {
let err = parse(q).expect_err("expected the sub-path-binding rejection");
assert!(
err.message.starts_with("UnsupportedSubPathBinding"),
"{q}: wrong error: {}",
err.message
);
}
assert!(parse("MATCH p = ((q = (:A)-->(:B))) RETURN p").is_ok());
}