use std::collections::BTreeMap;
use super::test_support::run_write;
use super::ExecGraph;
use crate::exec::test_support::{
commit_scratch, exec_write, exec_write_uncommitted, write_scratch,
};
use crate::graph::Graph;
use relate_core::Value;
#[test]
fn overlay_expand_sees_scratch_create() {
let r = run_write(
"CREATE (a:A {id: 1})-[:R]->(b:B {id: 2}) \
WITH a MATCH (a)-[:R]->(x) RETURN x.id AS id",
);
assert_eq!(r.rows.len(), 1);
assert_eq!(r.rows[0][0].as_int(), Some(2));
}
#[test]
fn overlay_expand_hides_deleted_rel() {
let g = Graph::default();
let setup = write_scratch(&g);
exec_write(&g, &setup, "CREATE (:A {id: 1})-[:R]->(:B {id: 2})").unwrap();
commit_scratch(&g, setup);
let result = exec_write_uncommitted(
&g,
"MATCH (a:A {id: 1})-[r:R]->() DELETE r \
WITH a MATCH (a)-[:R]->(x) RETURN x",
);
assert!(result.rows.is_empty(), "deleted overlay rel still visible");
}
#[test]
fn prop_ix_seek_sees_set_and_added_label_after_the_pair_is_built() {
let g = Graph::default();
let a = g
.create_node(
vec!["A".into()],
BTreeMap::from([("x".into(), Value::Int(1))]),
)
.unwrap();
let b = g
.create_node(
vec!["B".into()],
BTreeMap::from([("x".into(), Value::Int(2))]),
)
.unwrap();
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
let labels = ["A".to_string()];
let props = BTreeMap::from([("x".to_string(), Value::Int(2))]);
assert_eq!(view.lookup_nodes(&labels, &props), Some(vec![]));
view.set_property_node(a, "x", Value::Int(2));
view.add_labels(b, &labels);
assert_eq!(view.lookup_nodes(&labels, &props), Some(vec![a, b]));
}
#[test]
fn multi_label_property_seek_admits_only_whole_label_scratch_nodes() {
let g = Graph::default();
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
let both = view
.create_node(
vec!["A".into(), "B".into()],
BTreeMap::from([("x".into(), Value::Int(1))]),
)
.unwrap();
let _one_label_only = view
.create_node(
vec!["A".into()],
BTreeMap::from([("x".into(), Value::Int(1))]),
)
.unwrap();
let labels = ["A".to_string(), "B".to_string()];
let props = BTreeMap::from([("x".to_string(), Value::Int(1))]);
assert_eq!(view.lookup_nodes(&labels, &props), Some(vec![both]));
}
#[test]
fn equality_bucket_len_sums_committed_and_scratch() {
let g = Graph::default();
for _ in 0..2 {
g.create_node(
vec!["P".into()],
BTreeMap::from([("a".into(), Value::Int(1))]),
)
.unwrap();
}
g.create_node(
vec!["P".into()],
BTreeMap::from([("a".into(), Value::Int(2))]),
)
.unwrap();
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
assert_eq!(view.equality_bucket_len("P", "a", &Value::Int(1)), 2);
assert_eq!(view.equality_bucket_len("P", "a", &Value::Int(2)), 1);
view.create_node(
vec!["P".into()],
BTreeMap::from([("a".into(), Value::Int(1))]),
)
.unwrap();
assert_eq!(view.equality_bucket_len("P", "a", &Value::Int(1)), 3);
assert_eq!(
view.equality_bucket_len("P", "a", &Value::Float(f64::NAN)),
0
);
}
#[test]
fn a_keyed_lookup_finds_statement_creates_through_the_scratch_index() {
let g = Graph::default();
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
let a = view
.create_node(
vec!["A".into(), "B".into()],
BTreeMap::from([("k".into(), Value::Int(1))]),
)
.unwrap();
let only_a = view
.create_node(
vec!["A".into()],
BTreeMap::from([("k".into(), Value::Int(1))]),
)
.unwrap();
let a_label = ["A".to_string()];
let k1 = BTreeMap::from([("k".to_string(), Value::Int(1))]);
assert_eq!(view.lookup_nodes(&a_label, &k1), Some(vec![a, only_a]));
let k1_float = BTreeMap::from([("k".to_string(), Value::Float(1.0))]);
assert_eq!(
view.lookup_nodes(&a_label, &k1_float),
Some(vec![a, only_a])
);
let ab = ["A".to_string(), "B".to_string()];
assert_eq!(view.lookup_nodes(&ab, &k1), Some(vec![a]));
view.set_property_node(only_a, "k", Value::Int(2));
assert_eq!(view.lookup_nodes(&a_label, &k1), Some(vec![a]));
let k2 = BTreeMap::from([("k".to_string(), Value::Int(2))]);
assert_eq!(view.lookup_nodes(&a_label, &k2), Some(vec![only_a]));
}
#[test]
fn the_scratch_half_matches_the_base_half_s_equality_mode() {
use relate_test_support::temporal::dst_overlap_half;
let g = Graph::default();
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
let early = dst_overlap_half(false);
let late = dst_overlap_half(true);
let first = view
.create_node(
vec!["E".into()],
BTreeMap::from([("t".into(), early.clone())]),
)
.unwrap();
let second = view
.create_node(
vec!["E".into()],
BTreeMap::from([("t".into(), late.clone())]),
)
.unwrap();
assert_ne!(first, second);
assert!(
early.eq_non_null(&late),
"the halves are `=`-equal by wall clock + zone"
);
let needle = BTreeMap::from([("t".to_string(), early.clone())]);
let label = ["E".to_string()];
assert_eq!(
view.lookup_nodes(&label, &needle),
Some(vec![first, second])
);
crate::exec::test_support::exec_autocommit(&g, "CREATE INDEX FOR (e:E) ON (e.t)").unwrap();
assert_eq!(view.lookup_nodes(&label, &needle), Some(vec![first]));
}
#[test]
fn postings_served_lookup_merges_base_and_scratch_halves() {
use relate_test_support::temporal::dst_overlap_half;
let g = Graph::default();
let base_hit = g
.create_node(
vec!["A".into()],
BTreeMap::from([("x".into(), Value::Int(1))]),
)
.unwrap();
let base_twin = g
.create_node(
vec!["E".into()],
BTreeMap::from([("t".into(), dst_overlap_half(false))]),
)
.unwrap();
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
let added = view
.create_node(
vec!["A".into()],
BTreeMap::from([("x".into(), Value::Int(1))]),
)
.unwrap();
let _other_value = view
.create_node(
vec!["A".into()],
BTreeMap::from([("x".into(), Value::Int(2))]),
)
.unwrap();
let scratch_twin = view
.create_node(
vec!["E".into()],
BTreeMap::from([("t".into(), dst_overlap_half(true))]),
)
.unwrap();
let a = ["A".to_string()];
let x1 = BTreeMap::from([("x".to_string(), Value::Int(1))]);
let mut served = vec![base_hit, added];
served.sort_unstable();
assert_eq!(view.lookup_nodes(&a, &x1), Some(served));
let e = ["E".to_string()];
let needle = BTreeMap::from([("t".to_string(), dst_overlap_half(false))]);
let mut twins = vec![base_twin, scratch_twin];
twins.sort_unstable();
assert_eq!(view.lookup_nodes(&e, &needle), Some(twins));
}