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_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_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 test_different_relationship_singular_match_mode() {
let stmt = parse("MATCH DIFFERENT RELATIONSHIP (a) RETURN a").unwrap();
let Clause::Match(mc) = &stmt.clauses[0] else {
panic!("expected match");
};
assert_eq!(mc.match_mode, MatchMode::DifferentRelationships);
}
#[test]
fn test_unbounded_qpp_allowed_inside_exists_but_not_at_top_level() {
parse(
"MATCH REPEATABLE ELEMENTS q = (x)-->{3,6}(x) \
WHERE EXISTS { MATCH REPEATABLE ELEMENTS p = (x)-->+({i:2})-->+({i:1}) } \
RETURN q",
)
.expect("unbounded QPP inside EXISTS must parse");
assert!(parse("MATCH REPEATABLE ELEMENTS ()--+() RETURN 1").is_err());
}
#[test]
fn test_match_mode_spellings_resolve_to_their_mode() {
for (q, want_mode) in [
(
"MATCH REPEATABLE ELEMENTS (a)-[r]->(b) RETURN a",
MatchMode::RepeatableElements,
),
(
"MATCH REPEATABLE ELEMENTS p = (a)-[r]->(b) RETURN p",
MatchMode::RepeatableElements,
),
(
"MATCH p = REPEATABLE ELEMENTS (a)-[r]->(b) RETURN p",
MatchMode::RepeatableElements,
),
(
"MATCH DIFFERENT RELATIONSHIPS (a)-[r]->(b) RETURN a",
MatchMode::DifferentRelationships,
),
(
"MATCH DIFFERENT RELATIONSHIPS p = (a)-[r]->(b) RETURN p",
MatchMode::DifferentRelationships,
),
(
"MATCH p = DIFFERENT RELATIONSHIPS (a)-[r]->(b) RETURN p",
MatchMode::DifferentRelationships,
),
(
"MATCH (a)-[r]->(b) RETURN a",
MatchMode::DifferentRelationships,
),
] {
let stmt = parse_ok(q);
assert_eq!(match_clause(&stmt).match_mode, want_mode, "{q}");
}
}
#[test]
fn test_match_mode_path_variable_first_binds_the_path_variable() {
for q in [
"MATCH p = REPEATABLE ELEMENTS (a)-[r]->(b) RETURN p",
"MATCH p = DIFFERENT RELATIONSHIPS (a)-[r]->(b) RETURN p",
"MATCH REPEATABLE ELEMENTS p = (a)-[r]->(b) RETURN p",
"MATCH DIFFERENT RELATIONSHIPS p = (a)-[r]->(b) RETURN p",
] {
let stmt = parse_ok(q);
assert_eq!(
match_clause(&stmt).pattern[0].variable.as_ref(),
Some(&SmallName::from_name("p")),
"{q}: path variable"
);
}
}
#[test]
fn test_explicit_match_mode_still_rejects_a_shortest_path_function() {
for q in [
"MATCH REPEATABLE ELEMENTS shortestPath((a)-[r]->(b)) RETURN p",
"MATCH p = REPEATABLE ELEMENTS shortestPath((a)-[r]->(b)) RETURN p",
] {
let err = parse(q).expect_err("expected a mixing error");
assert!(
err.message.starts_with("MixingShortestPathAndMatchMode"),
"{q}: wrong error: {}",
err.message
);
}
assert!(parse("MATCH p = shortestPath((a)-[r]->(b)) RETURN p").is_ok());
}
#[test]
fn test_match_mode_phrase_before_path_variable_is_not_a_path_mode() {
let err = parse("MATCH TRAIL p = (x)--{2}(x) RETURN count(*) AS count")
.expect_err("expected InvalidPathModePosition");
assert!(
err.message.starts_with("InvalidPathModePosition"),
"wrong error: {}",
err.message
);
}
#[test]
fn test_match_mode_path_variable_first_honors_the_combination_rules() {
let err = parse("MATCH p = REPEATABLE ELEMENTS TRAIL (a)-[r]->(b) RETURN p")
.expect_err("expected UnsupportedCombinationOfMatchModeAndPathMode");
assert!(
err.message
.starts_with("UnsupportedCombinationOfMatchModeAndPathMode"),
"wrong error: {}",
err.message
);
let err = parse("MATCH p = REPEATABLE ELEMENTS ((a)-[r]->(b))+ RETURN p")
.expect_err("expected UnsafeRepeatableElements");
assert!(
err.message.starts_with("UnsafeRepeatableElements"),
"wrong error: {}",
err.message
);
}
#[test]
fn test_path_mode_keywords_set_the_path_mode() {
for (q, want) in [
("MATCH p = WALK (a)-[r]->(b) RETURN p", PathMode::Walk),
("MATCH p = TRAIL (a)-[r]->(b) RETURN p", PathMode::Trail),
("MATCH p = ACYCLIC (a)-[r]->(b) RETURN p", PathMode::Acyclic),
("MATCH p = SIMPLE (a)-[r]->(b) RETURN p", PathMode::Simple),
("MATCH TRAIL (a)-[r]->(b) RETURN a", PathMode::Trail),
] {
let stmt = parse_ok(q);
assert_eq!(match_clause(&stmt).pattern[0].mode, Some(want), "{q}");
}
let stmt = parse_ok("MATCH (a)-[r]->(b) RETURN a");
assert_eq!(match_clause(&stmt).pattern[0].mode, 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"))
);
}