use super::*;
use relate_names::SmallName;
#[test]
fn test_qpp_quantifier_star_is_zero_or_more() {
let q = qpp_quantifier("MATCH (x)((a)-->(b))*(y) RETURN 1");
assert_eq!(q, PathQuantifier { min: 0, max: None });
}
#[test]
fn test_qpp_quantifier_plus_is_one_or_more() {
let q = qpp_quantifier("MATCH (x)((a)-->(b))+(y) RETURN 1");
assert_eq!(q, PathQuantifier { min: 1, max: None });
}
#[test]
fn test_qpp_quantifier_normalizes_empty_braces_to_zero_unbounded() {
let q = qpp_quantifier("MATCH (x)((a)-->(b)){,}(y) RETURN 1");
assert_eq!(q, PathQuantifier { min: 0, max: None });
}
#[test]
fn test_qpp_quantifier_fixed_braces_equivalent_to_matching_bounds() {
let fixed = qpp_quantifier("MATCH (x)((a)-->(b)){2}(y) RETURN 1");
let general = qpp_quantifier("MATCH (x)((a)-->(b)){2,2}(y) RETURN 1");
assert_eq!(fixed, general);
assert_eq!(
fixed,
PathQuantifier {
min: 2,
max: Some(2)
}
);
}
#[test]
fn test_qpp_quantifier_zero_upper_bound_rejected() {
assert!(parse("MATCH (x)-[r]->{0}(y) RETURN 1").is_err());
assert!(parse("MATCH (x)-[r]->{,0}(y) RETURN 1").is_err());
}
#[test]
fn test_qpp_quantifier_lower_exceeds_upper_rejected() {
assert!(parse("MATCH (x)-[r]->{3,2}(y) RETURN 1").is_err());
}
#[test]
fn test_qpp_top_level_star_alone_is_minimum_node_count_zero() {
assert!(parse("MATCH ((a)-[]->(b))* RETURN 1").is_err());
assert!(parse("MATCH (x)((a)-[]->(b))* RETURN 1").is_ok());
assert!(parse("MATCH ((a)-[]->(b))*(y) RETURN 1").is_ok());
}
#[test]
fn test_qpp_single_node_operand_is_minimum_path_length_zero() {
assert!(parse("MATCH ((a)){1,5} RETURN 1").is_err());
}
#[test]
fn test_qpp_nested_quantification_rejected() {
assert!(parse("MATCH (((a)-[b]->(c))*)+ RETURN 1").is_err());
assert!(parse("MATCH ((a)-->(b)-[r]->*(c))+ RETURN 1").is_err());
}
#[test]
fn test_qpp_unquantified_parenthesized_pattern_cannot_be_juxtaposed() {
assert!(parse("MATCH (a)-->(b)((c)-->(d)) RETURN 1").is_err());
}
#[test]
fn test_qpp_quantified_relationship_form5_lowers_to_var_length_rel_pattern() {
let stmt = parse("MATCH (x)-[r:R]->*(y) RETURN 1").unwrap();
match &stmt.clauses[0] {
Clause::Match(mc) => match &mc.pattern[0].elements[1] {
PatternElement::Relationship(rp) => {
assert!(rp.quantified);
assert_eq!(rp.min_hops, Some(0));
assert_eq!(rp.max_hops, None);
}
other => panic!("expected a Relationship element, got {other:?}"),
},
other => panic!("expected match, got {other:?}"),
}
}
#[test]
fn test_qpp_subpath_variable_inside_quantification_parses() {
let stmt = parse("MATCH (x)(p2 = (a)-->(b))*(y) RETURN p2").unwrap();
match &stmt.clauses[0] {
Clause::Match(mc) => match &mc.pattern[0].elements[1] {
PatternElement::Quantified(qpp) => {
assert_eq!(
qpp.subpath_variable.as_ref(),
Some(&SmallName::from_name("p2"))
);
}
other => panic!("expected a Quantified element, got {other:?}"),
},
other => panic!("expected match, got {other:?}"),
}
}
#[test]
fn test_qpp_zero_lower_bound_braces_keep_their_bounds() {
let bounded = qpp_quantifier("MATCH (x)((a)-->(b)){0,3}(y) RETURN 1");
assert_eq!(
bounded,
PathQuantifier {
min: 0,
max: Some(3)
}
);
let unbounded = qpp_quantifier("MATCH (x)((a)-->(b)){0,}(y) RETURN 1");
assert_eq!(unbounded, PathQuantifier { min: 0, max: None });
}