use relate_core::{CapacityKind, Error};
use crate::exec::test_support::try_exec_cypher;
use crate::graph::Graph;
use super::anchor::{with_list_limit, AnchorArm};
use super::fixtures::{counting, parity, ring, ring_unindexed, sorted, Selector, BODY, SELECTORS};
fn list_with_noise(ids: std::ops::Range<usize>) -> String {
let mut items: Vec<String> = ids.map(|i| format!("'{i}'")).collect();
assert!(!items.is_empty(), "the caller passes a non-empty id range");
items.push("null".into());
let first = items[0].clone();
items.push(first);
format!("[{}]", items.join(", "))
}
#[test]
fn a_where_equality_end_seeks_the_pair() {
let g = ring();
let by_where = format!(
"MATCH p = ANY SHORTEST (a:N {{id: '0'}}){BODY}(b:N) WHERE b.id = '150' \
RETURN a.id, b.id, length(p)"
);
let by_map = format!(
"MATCH p = ANY SHORTEST (a:N {{id: '0'}}){BODY}(b:N {{id: '150'}}) \
RETURN a.id, b.id, length(p)"
);
let by_where_counts = parity(&g, &by_where);
assert_eq!(by_where_counts.arms, vec![AnchorArm::Pair]);
let by_map_counts = parity(&g, &by_map);
assert_eq!(by_map_counts.arms, by_where_counts.arms);
assert_eq!(
by_where_counts.expands, by_map_counts.expands,
"the WHERE form must cost the map form"
);
}
#[test]
fn a_function_spelling_where_equality_seeks_the_end() {
let g = ring();
let by_where_counts = parity(
&g,
&format!(
"MATCH p = shortestPath((a:N {{id: '0'}}){BODY}(b:N)) WHERE b.id = '150' \
RETURN length(p)"
),
);
let by_map_counts = parity(
&g,
&format!(
"MATCH p = shortestPath((a:N {{id: '0'}}){BODY}(b:N {{id: '150'}})) \
RETURN length(p)"
),
);
assert_eq!(by_where_counts.arms, vec![AnchorArm::Pair]);
assert_eq!(by_map_counts.arms, by_where_counts.arms);
assert_eq!(by_where_counts.expands, by_map_counts.expands);
}
#[test]
fn an_unwound_end_equality_costs_the_bound_pair_form() {
let g = ring();
let by_where = format!(
"UNWIND ['150', '17', '99'] AS t MATCH p = ANY SHORTEST (a:N {{id: '0'}}){BODY}(b:N) \
WHERE b.id = t RETURN t, length(p)"
);
let bound = format!(
"UNWIND ['150', '17', '99'] AS t MATCH (a:N {{id: '0'}}), (b:N {{id: t}}) \
MATCH p = ANY SHORTEST (a){BODY}(b) RETURN t, length(p)"
);
let by_where_counts = parity(&g, &by_where);
assert_eq!(by_where_counts.arms, vec![AnchorArm::Pair; 3]);
let bound_counts = parity(&g, &bound);
assert_eq!(by_where_counts.expands, bound_counts.expands);
}
#[test]
fn in_lists_on_both_ends_fan_from_the_listed_start() {
let g = ring();
let q = format!(
"MATCH p = ANY SHORTEST (a:N){BODY}(b:N) \
WHERE a.id IN {} AND b.id IN {} RETURN a.id, b.id, length(p)",
list_with_noise(0..6),
list_with_noise(100..140)
);
let counts = parity(&g, &q);
assert_eq!(counts.arms, vec![AnchorArm::FanForward]);
let unindexed = parity(&ring_unindexed(), &q).expands;
assert_eq!(counts.expands, unindexed);
let every_start = parity(
&ring_unindexed(),
&q.replace("a.id IN", "a.id IS NOT NULL OR a.id IN"),
);
assert!(
counts.expands * 5 < every_start.expands,
"{} vs unfiltered {}",
counts.expands,
every_start.expands
);
}
#[test]
fn an_end_only_in_list_fans_backward_and_all_selectors_agree() {
let g = ring();
for Selector { open, close, .. } in SELECTORS {
let q = format!(
"MATCH p = {open}(a:N){BODY}(b:N) WHERE b.id IN {} \
RETURN a.id, b.id, length(p){close}",
list_with_noise(7..9)
);
let arms = parity(&g, &q).arms;
assert_eq!(arms, vec![AnchorArm::FanBackward], "{open}");
}
}
#[test]
fn mixed_conjuncts_seek_and_the_residual_still_filters() {
let g = ring();
let q = format!(
"MATCH p = ANY SHORTEST (a:N {{id: '0'}}){BODY}(b:N) \
WHERE b.id IN ['150', '17', '99', '4'] AND b.g = 1 AND length(p) >= 1 \
RETURN b.id, length(p)"
);
let arms = parity(&g, &q).arms;
assert_eq!(arms, vec![AnchorArm::Pair]);
}
#[test]
fn an_unserved_or_does_not_seek() {
let g = ring();
let q = format!(
"MATCH p = ANY SHORTEST (a:N {{id: '0'}}){BODY}(b:N) \
WHERE b.id = '150' OR b.g = 1 RETURN b.id, length(p)"
);
let arms = parity(&g, &q).arms;
assert_eq!(arms, vec![AnchorArm::FanForward]);
}
#[test]
fn without_an_index_the_where_equality_lists_the_filtered_end() {
let g = ring_unindexed();
let q = format!(
"MATCH p = ANY SHORTEST (a:N {{id: '0'}}){BODY}(b:N) WHERE b.id = '150' \
RETURN b.id, length(p)"
);
let arms = parity(&g, &q).arms;
assert_eq!(arms, vec![AnchorArm::Pair]);
let both = format!(
"MATCH p = ANY SHORTEST (a:N){BODY}(b:N) \
WHERE a.id IN {} AND b.id IN {} RETURN a.id, b.id, length(p)",
list_with_noise(0..30),
list_with_noise(100..130)
);
let arms = parity(&g, &both).arms;
assert_eq!(arms, vec![AnchorArm::FanForward]);
}
#[test]
fn same_node_pairs_keep_the_unseeked_answer() {
let (seeked, plain) = (ring(), ring_unindexed());
let outcome = |g: &Graph, q: &str| {
let (res, counts) = counting(|| try_exec_cypher(g, q));
match res {
Ok(r) => {
assert!(counts.route_runs > 0, "the route must take {q}");
Ok(sorted(r.rows))
}
Err(e) => Err(e),
}
};
for q in [
"MATCH p = shortestPath((a:N)-[*..6]->(b:N)) \
WHERE a.id IN ['1', '2'] AND b.id IN ['1', '2'] RETURN a.id, b.id",
"MATCH p = shortestPath((a:N)-[*..6]->(b:N)) \
WHERE a.id IN ['1', '2'] AND b.id IN ['1', '2'] AND a <> b RETURN a.id, b.id",
"MATCH p = ANY SHORTEST (a:N)-[:R*..6]->(b:N) \
WHERE a.id IN ['1', '2'] AND b.id IN ['1', '2'] RETURN a.id, b.id, length(p)",
] {
assert_eq!(outcome(&seeked, q), outcome(&plain, q), "{q}");
}
parity(
&seeked,
"MATCH p = ANY SHORTEST (a:N)-[:R*..6]->(b:N) \
WHERE a.id IN ['1', '2'] AND b.id IN ['1', '2'] RETURN a.id, b.id, length(p)",
);
}
#[test]
fn an_over_limit_where_seek_is_a_typed_refusal() {
let g = ring();
let q = format!(
"MATCH p = ANY SHORTEST (a:N){BODY}(b:N) \
WHERE a.id IN {} AND b.id IN {} RETURN count(p)",
list_with_noise(0..20),
list_with_noise(100..120)
);
let err = with_list_limit(4, || try_exec_cypher(&g, &q))
.err()
.expect("past the listing limit must refuse");
assert!(
matches!(
err,
Error::Capacity {
kind: CapacityKind::SeekCandidates,
..
}
),
"{err:?}"
);
parity(&g, &q);
}