Skip to content

Commit

Permalink
use assert_graph_equals
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmurariu committed Jul 9, 2024
1 parent 7a35003 commit bcdb881
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
32 changes: 18 additions & 14 deletions raphtory/src/db/api/view/serialise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,10 @@ mod proto_test {

use crate::{
core::DocumentInput,
db::api::{mutation::DeletionOps, properties::internal::ConstPropertiesOps},
db::{
api::{mutation::DeletionOps, properties::internal::ConstPropertiesOps},
graph::graph::assert_graph_equal,
},
prelude::*,
};

Expand All @@ -749,7 +752,7 @@ mod proto_test {
g1.add_node(1, "Alice", NO_PROPS, None).unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);
}

#[test]
Expand All @@ -761,7 +764,7 @@ mod proto_test {
.unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);
}

#[test]
Expand All @@ -778,7 +781,7 @@ mod proto_test {

g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);
}

#[test]
Expand All @@ -790,7 +793,7 @@ mod proto_test {
g1.add_edge(3, "Alice", "Bob", NO_PROPS, None).unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);
}

#[test]
Expand All @@ -801,10 +804,9 @@ mod proto_test {
g1.delete_edge(19, "Alice", "Bob", None).unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = PersistentGraph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);

let edge = g2.edge("Alice", "Bob").expect("Failed to get edge");

let deletions = edge.deletions().iter().copied().collect::<Vec<_>>();
assert_eq!(deletions, vec![19]);
}
Expand All @@ -819,7 +821,7 @@ mod proto_test {
.unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);
}

#[test]
Expand All @@ -831,7 +833,7 @@ mod proto_test {
.expect("Failed to update constant properties");
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);
}

#[test]
Expand All @@ -844,7 +846,7 @@ mod proto_test {
.unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);
}

#[test]
Expand All @@ -857,7 +859,7 @@ mod proto_test {
g1.add_node(1, "Alice", props.clone(), None).unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);

let node = g2.node("Alice").expect("Failed to get node");

Expand All @@ -883,7 +885,7 @@ mod proto_test {
g1.add_edge(1, "Alice", "Bob", props.clone(), None).unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);

let edge = g2.edge("Alice", "Bob").expect("Failed to get edge");

Expand Down Expand Up @@ -911,7 +913,7 @@ mod proto_test {
.expect("Failed to update constant properties");
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);

let edge = g2
.edge("Alice", "Bob")
Expand Down Expand Up @@ -940,7 +942,7 @@ mod proto_test {
.expect("Failed to update constant properties");
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_eq!(&g1, &g2);
assert_graph_equal(&g1, &g2);

let node = g2.node("Alice").expect("Failed to get node");

Expand All @@ -965,6 +967,7 @@ mod proto_test {
let temp_file = tempfile::NamedTempFile::new().unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_graph_equal(&g1, &g2);

props.into_iter().for_each(|(name, prop)| {
let id = g2.get_const_prop_id(name).expect("Failed to get prop id");
Expand All @@ -986,6 +989,7 @@ mod proto_test {
let temp_file = tempfile::NamedTempFile::new().unwrap();
g1.stable_serialise(&temp_file).unwrap();
let g2 = Graph::decode(&temp_file).unwrap();
assert_graph_equal(&g1, &g2);

props
.into_iter()
Expand Down
49 changes: 30 additions & 19 deletions raphtory/src/db/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,11 @@ pub fn graph_equal<'graph1, 'graph2, G1: GraphViewOps<'graph1>, G2: GraphViewOps
) -> bool {
if g1.count_nodes() == g2.count_nodes() && g1.count_edges() == g2.count_edges() {
g1.nodes().id().par_values().all(|v| g2.has_node(v)) && // all nodes exist in other
g1.nodes().par_iter().all(|v1| {
let c1 = v1.properties().constant().into_iter().count();
let t1 = v1.properties().temporal().into_iter().count();
g2.node(v1.id()).filter(|node|{
c1 == node.properties().constant().into_iter().count() &&
t1 == node.properties().temporal().into_iter().count()
}).is_some()
}) &&
g1.count_temporal_edges() == g2.count_temporal_edges() && // same number of exploded edges
g1.edges().explode().iter().all(|e| { // all exploded edges exist in other

let c1 = e.properties().constant().into_iter().count();
let t1 = e.properties().temporal().into_iter().count();
g2
.edge(e.src().id(), e.dst().id())
.filter(|ee| {
ee.active(e.time().expect("exploded")) &&
c1 == e.properties().constant().into_iter().count() &&
t1 == e.properties().temporal().into_iter().count()
})
.filter(|ee| ee.active(e.time().expect("exploded")))
.is_some()
})
} else {
Expand Down Expand Up @@ -103,9 +88,22 @@ pub fn assert_graph_equal<
g1.count_temporal_edges(),
g2.count_temporal_edges()
);
for n_id in g1.nodes().id().values() {
assert!(g2.has_node(n_id), "missing node {n_id}");
for n1 in g1.nodes() {
assert!(g2.has_node(n1.id()), "missing node {}", n1.id());

let c1 = n1.properties().constant().into_iter().count();
let t1 = n1.properties().temporal().into_iter().count();
let check = g2
.node(n1.id())
.filter(|node| {
c1 == node.properties().constant().into_iter().count()
&& t1 == node.properties().temporal().into_iter().count()
})
.is_some();

assert!(check, "node {:?} properties mismatch", n1.id());
}

for e in g1.edges().explode() {
// all exploded edges exist in other
let e2 = g2
Expand All @@ -116,7 +114,20 @@ pub fn assert_graph_equal<
"exploded edge {:?} not active as expected at time {}",
e2.id(),
e.time().unwrap()
)
);

let c1 = e.properties().constant().into_iter().count();
let t1 = e.properties().temporal().into_iter().count();
let check = g2
.edge(e.src().id(), e.dst().id())
.filter(|ee| {
ee.active(e.time().expect("exploded"))
&& c1 == e.properties().constant().into_iter().count()
&& t1 == e.properties().temporal().into_iter().count()
})
.is_some();

assert!(check, "edge {:?} properties mismatch", e.id());
}
}

Expand Down

0 comments on commit bcdb881

Please sign in to comment.