use std::collections::BTreeMap;
use relate_core::Value;
use super::super::Graph;
#[test]
fn for_each_neighbor_matches_neighbors_at() {
use relate_ir::Direction;
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
let c = g.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
g.create_rel(c, a, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
for dir in [Direction::Outgoing, Direction::Incoming, Direction::Both] {
let listed = g.neighbors(a, dir);
let mut walked = Vec::new();
g.for_each_neighbor_at(a, dir, g.commit_ts(), &[] as &[String], |rid| {
walked.push(rid)
});
assert_eq!(walked, listed, "dir={dir:?}");
}
}
#[test]
fn typed_adj_walks_newest_ts_first() {
use relate_ir::Direction;
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
let c = g.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
let d = g.create_node(vec!["D".into()], BTreeMap::new()).unwrap();
let old = g
.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(1))]),
)
.unwrap();
let newest = g
.create_rel(
a,
c,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(30))]),
)
.unwrap();
let mid = g
.create_rel(
a,
d,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(10))]),
)
.unwrap();
g.commit_write();
let mut walked = Vec::new();
g.for_each_neighbor_at(
a,
Direction::Outgoing,
g.commit_ts(),
&["T".to_string()],
|rid| walked.push(rid),
);
assert_eq!(walked, vec![newest, mid, old]);
g.begin_write().unwrap();
g.set_property_rel(old, "ts", Value::Int(40));
g.commit_write();
walked.clear();
g.for_each_neighbor_at(
a,
Direction::Outgoing,
g.commit_ts(),
&["T".to_string()],
|rid| walked.push(rid),
);
assert_eq!(walked, vec![old, newest, mid]);
}
#[test]
fn typed_adj_skips_other_types() {
use relate_ir::Direction;
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
let c = g.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
let t = g.create_rel(a, b, "T".into(), BTreeMap::new()).unwrap();
g.create_rel(a, c, "OTHER".into(), BTreeMap::new()).unwrap();
g.commit_write();
let mut only_t = Vec::new();
g.for_each_neighbor_at(
a,
Direction::Outgoing,
g.commit_ts(),
&["T".to_string()],
|rid| only_t.push(rid),
);
assert_eq!(only_t, vec![t]);
let mut dup = Vec::new();
g.for_each_neighbor_at(
a,
Direction::Outgoing,
g.commit_ts(),
&["T".to_string(), "T".to_string()],
|rid| dup.push(rid),
);
assert_eq!(dup, vec![t]);
}
#[test]
fn resolved_typed_walk_filters_by_interned_id() {
use relate_ir::Direction;
use crate::store::RelTypes;
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
let c = g.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
let d = g.create_node(vec!["D".into()], BTreeMap::new()).unwrap();
let r = g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
let s = g.create_rel(a, c, "S".into(), BTreeMap::new()).unwrap();
g.create_rel(a, d, "T".into(), BTreeMap::new()).unwrap();
g.commit_write();
let walk = |types: &RelTypes| {
let mut out = Vec::new();
g.try_for_each_neighbor_resolved_at(a, Direction::Outgoing, g.commit_ts(), types, |rid| {
out.push(rid);
Ok::<(), ()>(())
})
.unwrap();
out
};
assert_eq!(walk(&g.rel_types(&["R"])), vec![r]);
assert_eq!(walk(&g.rel_types(&["S", "R"])), vec![s, r]);
assert!(walk(&g.rel_types(&["GHOST"])).is_empty());
assert_eq!(walk(&g.rel_types(&["GHOST", "R"])), vec![r]);
assert_eq!(walk(&RelTypes::Any).len(), 3);
}
#[test]
fn neighbor_walk_reenters_the_same_store() {
use relate_ir::Direction;
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
let c = g.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
let bc = g.create_rel(b, c, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
let mut second_hop = Vec::new();
g.for_each_neighbor_at(
a,
Direction::Outgoing,
g.commit_ts(),
&["R".to_string()],
|rid| {
let (start, end) = g.rel_ends_at(rid, g.commit_ts()).unwrap();
let next = if start == a { end } else { start };
g.for_each_neighbor_at(
next,
Direction::Outgoing,
g.commit_ts(),
&["R".to_string()],
|next_rid| second_hop.push(next_rid),
);
},
);
assert_eq!(second_hop, vec![bc]);
}
fn hop_pairs(
g: &Graph,
at: relate_core::NodeId,
dir: relate_ir::Direction,
ts: u64,
types: &crate::store::RelTypes,
) -> Vec<(relate_core::RelId, relate_core::NodeId)> {
let mut out = Vec::new();
g.try_for_each_hop_resolved_at(at, dir, ts, types, |rid, peer| {
out.push((rid, peer));
Ok::<(), ()>(())
})
.unwrap();
out
}
#[test]
fn hop_walk_matches_literal_expectations() {
use crate::store::RelTypes;
use relate_ir::Direction;
let g = Graph::default();
g.begin_write().unwrap();
let a = g.create_node(vec!["A".into()], BTreeMap::new()).unwrap();
let b = g.create_node(vec!["B".into()], BTreeMap::new()).unwrap();
let r_ab = g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
let r_ba = g.create_rel(b, a, "R".into(), BTreeMap::new()).unwrap();
let r_loop = g.create_rel(a, a, "LOOP".into(), BTreeMap::new()).unwrap();
let r_ghost = g.create_rel(a, b, "GHOST".into(), BTreeMap::new()).unwrap();
g.commit_write();
let ts = g.commit_ts();
let walk = |dir: Direction, types: &RelTypes| hop_pairs(&g, a, dir, ts, types);
assert_eq!(
walk(Direction::Outgoing, &g.rel_types(&["R"])),
vec![(r_ab, b)]
);
assert_eq!(
walk(Direction::Outgoing, &g.rel_types(&["LOOP"])),
vec![(r_loop, a)]
);
assert_eq!(
walk(Direction::Outgoing, &RelTypes::Any),
vec![(r_ab, b), (r_loop, a), (r_ghost, b)]
);
assert_eq!(
walk(Direction::Incoming, &g.rel_types(&["R"])),
vec![(r_ba, b)]
);
assert_eq!(
walk(Direction::Incoming, &RelTypes::Any),
vec![(r_ba, b), (r_loop, a)]
);
assert_eq!(
walk(Direction::Both, &g.rel_types(&["R"])),
vec![(r_ab, b), (r_ba, b)]
);
assert_eq!(
walk(Direction::Both, &g.rel_types(&["LOOP"])),
vec![(r_loop, a)],
"the self-loop is one hop under Both, not two"
);
assert_eq!(
walk(Direction::Both, &RelTypes::Any),
vec![(r_ab, b), (r_loop, a), (r_ghost, b), (r_ba, b)]
);
g.begin_write().unwrap();
g.delete_rel(r_loop);
g.commit_write();
let ts = g.commit_ts();
let walk = |dir: Direction, types: &RelTypes| hop_pairs(&g, a, dir, ts, types);
assert_eq!(
walk(Direction::Both, &RelTypes::Any),
vec![(r_ab, b), (r_ghost, b), (r_ba, b)],
"the deleted rel is not a hop"
);
}
fn reused_rel_id_graph() -> Graph {
use crate::graph::RedoOp;
let g = Graph::default();
g.apply_redo(
vec![
RedoOp::create_node(1, vec!["A".to_string()], BTreeMap::new()),
RedoOp::create_node(2, vec!["B".to_string()], BTreeMap::new()),
RedoOp::create_node(3, vec!["C".to_string()], BTreeMap::new()),
],
1,
)
.unwrap();
g.apply_redo(
vec![RedoOp::create_rel(
6,
1,
2,
"R".to_string(),
BTreeMap::from([("ts".to_string(), Value::Int(1))]),
)],
2,
)
.unwrap();
g.apply_redo(vec![RedoOp::delete_rel(6)], 3).unwrap();
g.apply_redo(
vec![RedoOp::create_rel(
6,
1,
3,
"R".to_string(),
BTreeMap::from([("ts".to_string(), Value::Int(2))]),
)],
4,
)
.unwrap();
g
}
#[test]
fn hop_walk_dedups_a_reused_rel_ids_stale_hops() {
use relate_ir::Direction;
use crate::store::RelTypes;
let g = reused_rel_id_graph();
let walk = |at, dir| hop_pairs(&g, at, dir, g.commit_ts(), &RelTypes::Any);
assert_eq!(
walk(1, Direction::Both),
vec![(6, 3)],
"Both collapses the two surviving out-hops named 6"
);
assert_eq!(
walk(1, Direction::Outgoing),
vec![(6, 3), (6, 3)],
"the directed arms keep both repeat entries (pre-existing policy, LABS-928)"
);
assert_eq!(walk(1, Direction::Incoming), vec![]);
assert_eq!(walk(2, Direction::Both), vec![]);
assert_eq!(walk(3, Direction::Both), vec![(6, 1)]);
}
#[test]
fn neighbor_walk_drops_a_reused_rel_ids_stale_hops() {
use relate_ir::Direction;
use crate::store::RelTypes;
let g = reused_rel_id_graph();
let rids = |at, dir| {
let mut out = Vec::new();
g.try_for_each_neighbor_resolved_at(at, dir, g.commit_ts(), &RelTypes::Any, |rid| {
out.push(rid);
Ok::<(), ()>(())
})
.unwrap();
out
};
assert_eq!(
rids(1, Direction::Both),
vec![6],
"Both dedups the reused id"
);
assert_eq!(
rids(1, Direction::Outgoing),
vec![6, 6],
"the directed arm keeps both repeat entries"
);
assert_eq!(
rids(2, Direction::Both),
Vec::<relate_core::RelId>::new(),
"the stale hop drops: node 2 is not an endpoint"
);
assert_eq!(rids(3, Direction::Both), vec![6]);
let named = |at, dir| {
let mut out = Vec::new();
g.for_each_neighbor_at(at, dir, g.commit_ts(), &["R".to_string()], |rid| {
out.push(rid)
});
out
};
assert_eq!(named(1, Direction::Both), vec![6]);
assert_eq!(named(1, Direction::Outgoing), vec![6, 6]);
assert_eq!(named(2, Direction::Both), Vec::<relate_core::RelId>::new());
assert_eq!(named(3, Direction::Both), vec![6]);
}