use crate::exec::test_support::write_scratch;
use crate::graph::Graph;
#[test]
fn resolved_neighbor_walk_covers_committed_and_scratch_types() {
use std::collections::BTreeMap;
use relate_ir::Direction;
use super::ExecGraph;
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 committed = g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
g.commit_write();
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
let scratch_r = view.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
let scratch_new = view
.create_rel(a, b, "NEWTYPE".into(), BTreeMap::new())
.unwrap();
let walk = |types: &RelTypes| {
let mut out = Vec::new();
view.try_for_each_neighbor_resolved(a, Direction::Outgoing, types, |rid| {
out.push(rid);
Ok::<(), ()>(())
})
.unwrap();
out
};
let mut got = walk(&view.rel_types(&["R"]));
got.sort();
let mut want = vec![committed, scratch_r];
want.sort();
assert_eq!(got, want);
assert_eq!(walk(&view.rel_types(&["NEWTYPE"])), vec![scratch_new]);
let mut got = walk(&view.rel_types(&["R", "NEWTYPE"]));
got.sort();
let mut want = vec![committed, scratch_r, scratch_new];
want.sort();
assert_eq!(got, want);
assert!(walk(&view.rel_types(&["GHOST"])).is_empty());
assert_eq!(walk(&RelTypes::Any).len(), 3);
}
#[test]
fn resolved_hop_walk_covers_read_only_and_scratch_arms() {
use std::collections::BTreeMap;
use relate_ir::Direction;
use super::ExecGraph;
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 committed = g.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
let loop_r = g.create_rel(a, a, "LOOP".into(), BTreeMap::new()).unwrap();
g.commit_write();
let hop_pairs = |view: &ExecGraph, dir: Direction, types: &RelTypes| {
let mut out = Vec::new();
view.try_for_each_hop_resolved(a, dir, types, |rid, peer| {
out.push((rid, peer));
Ok::<(), ()>(())
})
.unwrap();
out
};
let read = ExecGraph::read(&g);
assert_eq!(
hop_pairs(&read, Direction::Outgoing, &RelTypes::Any),
vec![(committed, b), (loop_r, a)]
);
assert_eq!(
hop_pairs(&read, Direction::Incoming, &RelTypes::Any),
vec![(loop_r, a)],
"the committed a->b rel is in b's in-list, not a's; the loop is"
);
assert_eq!(
hop_pairs(&read, Direction::Both, &RelTypes::Any).len(),
2,
"the self-loop is one hop under Both"
);
let scratch = write_scratch(&g);
let view = ExecGraph::write(&g, &scratch);
view.delete_rel(committed);
let scratch_r = view.create_rel(a, b, "R".into(), BTreeMap::new()).unwrap();
let scratch_loop = view
.create_rel(a, a, "LOOP".into(), BTreeMap::new())
.unwrap();
let c = view.create_node(vec!["C".into()], BTreeMap::new()).unwrap();
let scratch_in = view.create_rel(c, a, "R".into(), BTreeMap::new()).unwrap();
let sorted = |dir: Direction| {
let mut pairs = hop_pairs(&view, dir, &RelTypes::Any);
pairs.sort();
pairs
};
assert_eq!(
sorted(Direction::Outgoing),
vec![(loop_r, a), (scratch_r, b), (scratch_loop, a)],
"the deleted committed rel is hidden; the surviving committed rel \
and the scratch rels answer with peers"
);
assert_eq!(
sorted(Direction::Both),
{
let mut want = vec![
(loop_r, a),
(scratch_r, b),
(scratch_loop, a),
(scratch_in, c),
];
want.sort();
want
},
"one hop per self-loop under Both, and the in-list entry's peer \
is its start"
);
assert_eq!(
sorted(Direction::Incoming),
{
let mut want = vec![(loop_r, a), (scratch_loop, a), (scratch_in, c)];
want.sort();
want
},
"an Incoming walk's in pass is the only listing of the node's \
scratch rels: the scratch self-loop answers, not skipped, and \
the non-self-loop's peer is its start"
);
}