crates/kleene-gar/tests/id_space.rs
//! One id space over two vertex types whose rows share their numbers: person rows 0..3 and
//! pet rows 0..4, so pet row `r` is id `3 + r`.
use std::path::{Path, PathBuf};
use arrow_array::cast::AsArray;
use arrow_array::types::UInt64Type;
use graphar::mapped::{IntEdges, IntGraphWriter};
use kleene_gar::{GarGraph, IdSpace};
struct TempDir(PathBuf);
impl TempDir {
fn new(tag: &str) -> Self {
let p = std::env::temp_dir().join(format!(
"kleene-gar-ids-{tag}-{}-{:?}",
std::process::id(),
std::thread::current().id()
));
let _ = std::fs::remove_dir_all(&p);
std::fs::create_dir_all(&p).unwrap();
TempDir(p)
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
const PERSONS: u32 = 3;
const PETS: u32 = 4;
/// (edge, source type, destination type, pairs of rows)
type Edge = (
&'static str,
&'static str,
&'static str,
&'static [(u32, u32)],
);
const EDGES: [Edge; 3] = [
(
"owns",
"person",
"pet",
&[(0, 0), (0, 1), (1, 0), (1, 3), (2, 2)],
),
("knows", "person", "person", &[(0, 1), (1, 2), (2, 0)]),
("likes", "pet", "person", &[(0, 1), (3, 0), (2, 2), (3, 2)]),
];
fn write(dir: &Path) {
let mut w = IntGraphWriter::new(dir, "X").unwrap();
w.vertices("person", PERSONS, vec![]).unwrap();
w.vertices("pet", PETS, vec![]).unwrap();
for (e, s, d, pairs) in EDGES {
let n = |t: &str| if t == "person" { PERSONS } else { PETS };
let src: Vec<u32> = pairs.iter().map(|p| p.0).collect();
let dst: Vec<u32> = pairs.iter().map(|p| p.1).collect();
w.edges(IntEdges::from_pairs((s, e, d), (n(s), n(d)), &src, &dst, vec![]).unwrap())
.unwrap();
}
w.finish().unwrap();
}
fn base(t: &str) -> u64 {
if t == "person" { 0 } else { PERSONS as u64 }
}
#[test]
fn the_types_are_numbered_in_the_order_the_graph_info_lists_them() {
// Written (and so listed) `zoo` before `ant`: not the name order.
let d = TempDir::new("order");
let mut w = IntGraphWriter::new(&d.0, "X").unwrap();
w.vertices("zoo", 3, vec![]).unwrap();
w.vertices("ant", 2, vec![]).unwrap();
w.edges(IntEdges::from_pairs(("zoo", "has", "ant"), (3, 2), &[0, 2], &[1, 0], vec![]).unwrap())
.unwrap();
w.finish().unwrap();
for _ in 0..2 {
let g = GarGraph::open(&d.0).unwrap();
assert_eq!(g.vertex_types(), ["zoo", "ant"]);
let s = g.id_space().unwrap();
assert_eq!(
s.types().collect::<Vec<_>>(),
vec![("zoo", 0, 3), ("ant", 3, 2)]
);
}
}
#[test]
fn the_types_are_numbered_one_after_the_other() {
let d = TempDir::new("space");
write(&d.0);
let g = GarGraph::open(&d.0).unwrap();
let s = g.id_space().unwrap();
assert_eq!(
s.types().collect::<Vec<_>>(),
vec![("person", 0, 3), ("pet", 3, 4)]
);
assert_eq!(s.total(), 7);
for id in 0..7 {
let (t, r) = s.locate(id).unwrap();
assert_eq!(s.id(t, r), Some(id));
assert_eq!(base(t) + r, id);
}
assert_eq!(s.locate(7), None);
assert_eq!(s.id("person", 3), None);
let ids = g.vertex_ids("pet").unwrap();
assert_eq!(
ids.as_primitive::<UInt64Type>().values().to_vec(),
vec![3, 4, 5, 6]
);
assert_eq!(
IdSpace::new([("a", 2), ("b", 0), ("c", 1)]).locate(2),
Some(("c", 0))
);
}