use proptest::prelude::*;
use relate_core::Value;
use crate::exec::test_support::{build, exec_cypher, reset_frontier_facts, Built};
use crate::graph::Graph;
use crate::test_support::config_with_floor;
use crate::QueryResult;
use super::super::take_frontier_counters;
use super::rearival_tests::{multigraph, multigraph_case, pattern_body};
use super::{take_kernel_engagements, with_frontier_disabled};
fn layered(layers: usize, width: usize) -> Built {
let labels: Vec<&[&str]> = (0..(layers + 1) * width).map(|_| &["N"][..]).collect();
let mut rels = Vec::new();
for layer in 0..layers {
for a in 0..width {
for b in 0..width {
rels.push((layer * width + a, (layer + 1) * width + b, "R"));
}
}
}
build(&labels, &rels)
}
fn scalar(g: &Graph, cypher: &str) -> Value {
let result = exec_cypher(g, cypher);
assert_eq!(result.rows.len(), 1, "{cypher}: one row");
result.rows[0][0].clone()
}
fn engaged(g: &Graph, cypher: &str) -> Value {
reset_frontier_facts();
let answer = scalar(g, cypher);
assert_eq!(take_kernel_engagements(), 1, "the walk count took {cypher}");
assert!(
take_frontier_counters().levels > 0,
"the walk count ran for {cypher}"
);
answer
}
fn assert_matches_dfs(g: &Graph, cypher: &str, rows: &QueryResult) {
let dfs = with_frontier_disabled(|| exec_cypher(g, cypher));
assert_eq!(rows.columns, dfs.columns, "{cypher}");
assert_eq!(rows.rows, dfs.rows, "{cypher}");
}
fn differential(g: &Graph, cypher: &str) -> u32 {
let _ = take_kernel_engagements();
let engaged = exec_cypher(g, cypher);
let engagements = take_kernel_engagements();
let _ = take_frontier_counters();
assert_matches_dfs(g, cypher, &engaged);
assert_eq!(
take_kernel_engagements(),
0,
"the stand-down holds: {cypher}"
);
engagements
}
#[derive(Clone, Copy)]
enum Expect {
Declines,
TiesWithDfs,
}
fn assert_stood_down(g: &Graph, cypher: &str, rows: &QueryResult) {
assert_eq!(
take_frontier_counters().levels,
0,
"no frontier run: {cypher}"
);
assert_matches_dfs(g, cypher, rows);
}
fn run(g: &Graph, cypher: &str) -> QueryResult {
reset_frontier_facts();
exec_cypher(g, cypher)
}
fn stood_down(g: &Graph, cypher: &str) {
let rows = run(g, cypher);
assert_stood_down(g, cypher, &rows);
}
fn declines(g: &Graph, cypher: &str) {
let rows = run(g, cypher);
assert_eq!(take_kernel_engagements(), 0, "nothing takes {cypher}");
assert_stood_down(g, cypher, &rows);
}
#[test]
fn counts_walks_not_endpoints_on_a_multi_path_graph() {
let built = layered(3, 2);
let g = &built.graph;
for counted in ["count(*)", "count(d)", "count(s)"] {
let cypher = format!(
"MATCH REPEATABLE ELEMENTS (s:N {{id: 0}})-[:R*1..3]->(d) RETURN {counted} AS n"
);
assert_eq!(engaged(g, &cypher), Value::Int(14), "{cypher}");
differential(g, &cypher);
}
for (cypher, walks) in [
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*0..2]->(d) RETURN count(*) AS n",
7,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*2..3]->(d) RETURN count(*) AS n",
12,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 6})<-[:R*1..3]-(d) RETURN count(*) AS n",
14,
),
(
"MATCH REPEATABLE ELEMENTS (s:N)-[:R*3..3]->(d) RETURN count(*) AS n",
16,
),
] {
assert_eq!(engaged(g, cypher), Value::Int(walks), "{cypher}");
differential(g, cypher);
}
}
#[test]
fn branching_twelve_level_count_answers_without_enumeration() {
let built = layered(12, 4);
let started = std::time::Instant::now();
let answer = engaged(
&built.graph,
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..12]->(d) RETURN count(d) AS walks",
);
assert_eq!(answer, Value::Int(22_369_620));
assert!(
started.elapsed() < std::time::Duration::from_secs(5),
"the contraction never enumerates: {:?}",
started.elapsed()
);
}
#[test]
fn every_guard_shape_pins_its_verdict() {
let built = layered(3, 2);
let g = &built.graph;
for (cypher, expect) in [
(
"MATCH (s:N {id: 0})-[:R*1..3]->(d) RETURN count(*) AS n",
Expect::Declines,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) WHERE d.id > s.id \
RETURN count(*) AS n",
Expect::Declines,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) \
RETURN count(DISTINCT d) AS n",
Expect::TiesWithDfs,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) RETURN sum(d.id) AS n",
Expect::Declines,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) RETURN count(d.id) AS n",
Expect::Declines,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) \
RETURN s.id AS s, count(*) AS n",
Expect::Declines,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d WHERE d.id > 3) \
RETURN count(*) AS n",
Expect::Declines,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) RETURN count(*) AS n SKIP 1",
Expect::Declines,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) WITH d RETURN count(*) AS n",
Expect::Declines,
),
] {
match expect {
Expect::Declines => declines(g, cypher),
Expect::TiesWithDfs => {
differential(g, cypher);
}
}
}
}
#[test]
fn a_target_filter_counts_the_admitted_arrivals() {
let built = layered(3, 2);
let g = &built.graph;
for (query, walks) in [
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d:N) RETURN count(*) AS n",
14,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d:Missing) RETURN count(*) AS n",
0,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d {id: 6}) RETURN count(*) AS n",
4,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) WHERE d.id > 3 \
RETURN count(*) AS n",
12,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*1..3]->(d) WHERE d.id IS NULL \
RETURN count(*) AS n",
0,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*0..0]->(d {id: 0}) RETURN count(*) AS n",
1,
),
(
"MATCH REPEATABLE ELEMENTS (s:N {id: 0})-[:R*0..0]->(d {id: 1}) RETURN count(*) AS n",
0,
),
] {
assert_eq!(engaged(g, query), Value::Int(walks), "{query}");
differential(g, query);
}
}
proptest! {
#![proptest_config(config_with_floor(256))]
#[test]
fn walk_count_matches_the_dfs_on_generated_multigraphs(
(n, edges, seed, pattern) in multigraph_case(),
) {
let body = pattern_body(pattern, seed).replace("(y:N)", "(y)");
let built = multigraph(n, &edges);
let g = &built.graph;
let walk = format!("MATCH REPEATABLE ELEMENTS {body} RETURN count(*) AS n");
prop_assert_eq!(differential(g, &walk), 1, "the walk count takes {}", walk);
let default = format!("MATCH {body} RETURN count(*) AS n");
stood_down(g, &default);
}
#[test]
fn filtered_walk_count_matches_the_dfs_on_generated_multigraphs(
(n, edges, seed, pattern) in multigraph_case(),
) {
let target = (seed + 1) % n;
let filtered = format!("(y:N {{id: {target}}})");
let body = pattern_body(pattern, seed).replace("(y:N)", &filtered);
prop_assert!(
body.contains(&filtered),
"the target filter lands in the pattern: {body}"
);
let built = multigraph(n, &edges);
let walk = format!("MATCH REPEATABLE ELEMENTS {body} RETURN count(*) AS n");
prop_assert_eq!(
differential(&built.graph, &walk),
1,
"the walk count takes {}",
walk
);
}
}