use std::collections::HashMap;
use std::sync::Arc;
use relate_core::Value;
use crate::graph::Graph;
use crate::shortest::test_support::{counting_expands, min_hops_counterexample, TernaryTree};
use super::test_support::{exec_cypher, Builder};
use crate::test_support::ints;
fn value(g: &Graph, q: &str) -> (Value, u64) {
let (rows, expands) = counting_expands(|| exec_cypher(g, q).rows);
assert_eq!(rows.len(), 1, "{q}");
(rows[0][0].clone(), expands)
}
fn names(paths: &[&[&str]]) -> Value {
let path = |ids: &&[&str]| {
Value::List(
ids.iter()
.map(|&id| Value::String(id.into()))
.collect::<Vec<_>>()
.into(),
)
};
Value::List(paths.iter().map(path).collect::<Vec<_>>().into())
}
fn sorted(routes: Value) -> Value {
let Value::List(mut routes) = routes else {
panic!("expected a list, got {routes:?}");
};
Arc::make_mut(&mut routes).sort_by(Value::order_by_cmp);
Value::List(routes)
}
#[test]
fn all_shortest_closed_trails_on_an_undirected_triangle() {
let b = Builder::new();
let a = b.node("a", &["N"]);
let (x, y) = (b.node("x", &[]), b.node("y", &[]));
b.chain(&[a, x, y, a]);
let g = b.done();
let (lengths, expands) = value(
&g,
"MATCH (s:N {id: 'a'}) \
RETURN [p IN allShortestPaths((s)-[*1..8]-(s)) | length(p)] AS ls",
);
assert_eq!(lengths, ints(&[3, 3]));
assert!(expands > 0, "the closed-trail search did not run");
let (routes, _) = value(
&g,
"MATCH (s:N {id: 'a'}) \
RETURN [p IN allShortestPaths((s)-[*1..8]-(s)) | [n IN nodes(p) | n.id]] AS ps",
);
assert_eq!(
sorted(routes),
names(&[&["a", "x", "y", "a"], &["a", "y", "x", "a"]])
);
}
#[test]
fn min_hops_above_one_runs_on_enumeration() {
let g = min_hops_counterexample();
let bound = "MATCH (s:N {id: 's'}), (t:N {id: 't'}) ";
let (hops, expands) = value(
&g,
&format!("{bound}RETURN length(shortestPath((s)-[*4..8]->(t))) AS hops"),
);
assert_eq!(hops, Value::Int(4));
assert_eq!(expands, 0, "min_hops > 1 ran a BFS");
let (routes, _) = value(
&g,
&format!(
"{bound}RETURN [p IN allShortestPaths((s)-[*4..8]->(t)) | \
[n IN nodes(p) | n.id]] AS ps"
),
);
assert_eq!(routes, names(&[&["s", "p", "x", "y", "t"]]));
}
#[test]
fn unbound_end_stops_after_the_first_matching_layer() {
let b = Builder::new();
let (s, t) = (b.node("s", &["N"]), b.node("t", &["T"]));
b.edge(s, t);
b.fan_out_tree(t, 6);
let g = b.done();
let start = "MATCH (s:N {id: 's'}) ";
for (q, want) in [
("RETURN length(shortestPath((s)-[*]->(:T)))", Value::Int(1)),
(
"RETURN size(allShortestPaths((s)-[*]->(:T)))",
Value::Int(1),
),
] {
let (got, expands) = value(&g, &format!("{start}{q}"));
assert_eq!(got, want, "{q}");
assert!(expands > 0, "{q} did not run the BFS");
assert!(
expands < 10,
"{q} walked the ball behind t: expands={expands}"
);
}
let (got, expands) = value(
&g,
&format!("{start}RETURN shortestPath((s)-[*]->(:Missing))"),
);
assert_eq!(got, Value::Null);
assert!(expands > 1000, "expands={expands}");
}
#[test]
fn start_candidates_take_the_global_shortest() {
let b = Builder::new();
let starts = ["s1", "s2", "s3"].map(|id| b.node(id, &["S"]));
let t = b.node("t", &["N"]);
let ends = ["t1", "t2", "t3"].map(|id| b.node(id, &["T"]));
let mid = |id: &str| b.node(id, &[]);
b.chain(&[starts[0], mid("x"), mid("y"), t]);
b.chain(&[starts[1], t]);
b.chain(&[starts[1], mid("e"), mid("f"), t]);
b.chain(&[starts[2], mid("u"), mid("v"), t]);
b.chain(&[starts[0], mid("a"), mid("b"), ends[0]]);
b.chain(&[starts[1], ends[1]]);
b.chain(&[starts[1], mid("g"), mid("h"), ends[1]]);
b.chain(&[starts[1], mid("c"), mid("d"), ends[2]]);
let g = b.done();
let ids = "[p IN allShortestPaths({pattern}) | [n IN nodes(p) | n.id]]";
for (bind, pattern, all) in [
(
"MATCH (t:N {id: 't'}) ",
"(:S)-[*]->(t)",
names(&[&["s2", "t"]]),
),
("", "(:S)-[*]->(:T)", names(&[&["s2", "t2"]])),
] {
let (hops, _) = value(&g, &format!("{bind}RETURN length(shortestPath({pattern}))"));
assert_eq!(hops, Value::Int(1), "{pattern}");
let q = format!("{bind}RETURN {}", ids.replace("{pattern}", pattern));
assert_eq!(value(&g, &q).0, all, "{q}");
}
}
#[test]
fn bound_end_failing_its_pattern_returns_null() {
let b = Builder::new();
let (a, end) = (b.node("a", &["N"]), b.node("b", &["N"]));
b.chain(&[a, b.node("m", &[]), end]);
let g = b.done();
for (end, want) in [
("(b:N)", [Value::Int(2), Value::Int(1)]),
("(b {id: 'b'})", [Value::Int(2), Value::Int(1)]),
("(b:Missing)", [Value::Null, Value::Null]),
("(b {id: 'z'})", [Value::Null, Value::Null]),
] {
let q = format!(
"MATCH (a:N {{id: 'a'}}), (b:N {{id: 'b'}}) \
RETURN length(shortestPath((a)-[*]->{end})), \
size(allShortestPaths((a)-[*]->{end}))"
);
let (rows, expands) = counting_expands(|| exec_cypher(&g, &q).rows);
assert_eq!(rows, vec![want.to_vec()], "{q}");
if want[0] == Value::Null {
assert_eq!(expands, 0, "{q} searched for an end that fails its pattern");
} else {
assert!(expands > 0, "{q} did not run the BFS");
}
}
}
#[test]
fn null_bound_end_matches_nothing_on_both_engines() {
let b = Builder::new();
let (a, end) = (b.node("a", &["N"]), b.node("b", &["N"]));
b.chain(&[a, b.node("m", &[]), end]);
let g = b.done();
let (hops, expands) = value(
&g,
"MATCH (a:N {id: 'a'}), (b:N {id: 'b'}) \
RETURN length(shortestPath((a)-[*]->(b)))",
);
assert_eq!(hops, Value::Int(2));
assert!(expands > 0, "the fast path did not run the BFS");
for bound in ["null", "1"] {
let rows = exec_cypher(
&g,
&format!(
"MATCH (a:N {{id: 'a'}}) WITH a, {bound} AS b \
RETURN [(a)-[*]->(b) | 1] AS xs"
),
)
.rows;
assert_eq!(
rows,
vec![vec![Value::List(Vec::new().into())]],
"{bound}: the walk matched a null-bound end"
);
assert_shortest_null_bound(&g, bound, "*2..2", "enumeration");
assert_shortest_null_bound(&g, bound, "*", "the fast path");
}
}
fn assert_shortest_null_bound(g: &Graph, bound: &str, hops: &str, engine: &str) {
let (rows, expands) = counting_expands(|| {
exec_cypher(
g,
&format!(
"MATCH (a:N {{id: 'a'}}) WITH a, {bound} AS b \
RETURN length(shortestPath((a)-[{hops}]->(b))), \
size(allShortestPaths((a)-[{hops}]->(b)))"
),
)
.rows
});
assert_eq!(
rows,
vec![vec![Value::Null, Value::Null]],
"{bound}: {engine} matched a null-bound end"
);
assert_eq!(
expands, 0,
"{bound}: {engine} searched for a null-bound end"
);
}
fn named_graph(edges: &[(&str, &str)]) -> Graph {
let b = Builder::new();
let mut ids = HashMap::new();
for &(from, to) in edges {
let [from, to] = [from, to].map(|id| {
*ids.entry(id).or_insert_with(|| {
let labels: &[&str] = if matches!(id, "s" | "t") { &["N"] } else { &[] };
b.node(id, labels)
})
});
b.edge(from, to);
}
b.done()
}
fn assert_unbound_end_routes(edges: &[(&str, &str)], rel: &str, routes: &[&[&str]]) {
let g = named_graph(edges);
let pattern = format!("(s){rel}(:N)");
let start = "MATCH (s:N {id: 's'}) ";
let (got, expands) = value(
&g,
&format!(
"{start}RETURN [p IN allShortestPaths({pattern}) | \
[n IN nodes(p) | n.id]]"
),
);
assert_eq!(sorted(got), sorted(names(routes)), "{edges:?} {pattern}");
assert!(expands > 0, "{edges:?} {pattern} did not run the BFS");
let (hops, _) = value(
&g,
&format!("{start}RETURN length(shortestPath({pattern}))"),
);
let want = routes[0].len() as i64 - 1;
assert_eq!(hops, Value::Int(want), "{edges:?} {pattern}");
}
#[test]
fn start_matching_the_unbound_end_merges_its_closed_trails() {
assert_unbound_end_routes(
&[("s", "x"), ("x", "y"), ("y", "s")],
"-[*]-",
&[&["s", "x", "y", "s"], &["s", "y", "x", "s"]],
);
assert_unbound_end_routes(
&[("s", "x"), ("x", "s"), ("s", "a"), ("a", "b"), ("b", "t")],
"-[*]->",
&[&["s", "x", "s"]],
);
assert_unbound_end_routes(
&[("s", "x"), ("x", "s"), ("s", "a"), ("a", "t")],
"-[*]->",
&[&["s", "a", "t"], &["s", "x", "s"]],
);
assert_unbound_end_routes(
&[("s", "x"), ("x", "s"), ("s", "t")],
"-[*]->",
&[&["s", "t"]],
);
}
#[test]
fn relationship_property_map_runs_on_enumeration() {
let b = Builder::new();
let (a, end, m) = (b.node("a", &["N"]), b.node("b", &["N"]), b.node("m", &[]));
for (from, to, k) in [(a, end, 0), (a, m, 1), (m, end, 1)] {
b.rel(from, to, "E", &[("k", Value::Int(k))]);
}
let g = b.done();
let (hops, expands) = value(
&g,
"MATCH (a:N {id: 'a'}), (b:N {id: 'b'}) \
RETURN length(shortestPath((a)-[*1..3 {k: 1}]->(b)))",
);
assert_eq!(hops, Value::Int(2));
assert_eq!(expands, 0, "the BFS ignores relationship property maps");
}