Skip to content

Commit

Permalink
simplify the testing (#1632)
Browse files Browse the repository at this point in the history
* simplify the testing

* fix parse_u64_strict and add better tests

* fix compile error
  • Loading branch information
ljeub-pometry authored Jun 4, 2024
1 parent 8a59de9 commit 66b125e
Show file tree
Hide file tree
Showing 38 changed files with 437 additions and 1,201 deletions.
14 changes: 3 additions & 11 deletions raphtory/src/algorithms/centrality/betweenness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ pub fn betweenness_centrality<'graph, G: GraphViewOps<'graph>>(
#[cfg(test)]
mod betweenness_centrality_test {
use super::*;
use crate::{db::api::view::StaticGraphViewOps, prelude::*};
use tempfile::TempDir;
use crate::{prelude::*, test_storage};

#[test]
fn test_betweenness_centrality() {
Expand All @@ -139,11 +138,7 @@ mod betweenness_centrality_test {
graph.add_edge(0, *src, *dst, NO_PROPS, None).unwrap();
}

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let mut expected: HashMap<String, f64> = HashMap::new();
expected.insert("1".to_string(), 0.0);
expected.insert("2".to_string(), 1.0);
Expand All @@ -164,9 +159,6 @@ mod betweenness_centrality_test {
expected.insert("6".to_string(), 0.0);
let res = betweenness_centrality(graph, None, Some(true));
assert_eq!(res.get_all_with_names(), expected);
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}
}
18 changes: 4 additions & 14 deletions raphtory/src/algorithms/centrality/degree_centrality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ pub fn degree_centrality<G: StaticGraphViewOps>(
mod degree_centrality_test {
use crate::{
algorithms::centrality::degree_centrality::degree_centrality,
db::{
api::{mutation::AdditionOps, view::StaticGraphViewOps},
graph::graph::Graph,
},
db::{api::mutation::AdditionOps, graph::graph::Graph},
prelude::NO_PROPS,
test_storage,
};
use std::collections::HashMap;
use tempfile::TempDir;

#[test]
fn test_degree_centrality() {
Expand All @@ -76,11 +73,7 @@ mod degree_centrality_test {
for (src, dst) in &vs {
graph.add_edge(0, *src, *dst, NO_PROPS, None).unwrap();
}
let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let mut hash_map_result: HashMap<String, f64> = HashMap::new();
hash_map_result.insert("1".to_string(), 1.0);
hash_map_result.insert("2".to_string(), 1.0);
Expand All @@ -90,9 +83,6 @@ mod degree_centrality_test {
let binding = degree_centrality(graph, None);
let res = binding.get_all_with_names();
assert_eq!(res, hash_map_result);
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}
}
13 changes: 3 additions & 10 deletions raphtory/src/algorithms/centrality/hits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ mod hits_tests {
use crate::{
db::{api::mutation::AdditionOps, graph::graph::Graph},
prelude::NO_PROPS,
test_storage,
};
use tempfile::TempDir;

use super::*;

Expand Down Expand Up @@ -214,11 +214,7 @@ mod hits_tests {
(7, 3),
(8, 1),
]);
let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let results = hits(graph, 20, None).get_all_with_names();

assert_eq!(
Expand All @@ -234,9 +230,6 @@ mod hits_tests {
("8".to_string(), (0.030866561, 0.05943252))
])
);
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}
}
70 changes: 15 additions & 55 deletions raphtory/src/algorithms/centrality/pagerank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,15 @@ pub fn unweighted_page_rank<G: StaticGraphViewOps>(

#[cfg(test)]
mod page_rank_tests {
use std::borrow::Borrow;

use itertools::Itertools;
use pretty_assertions::assert_eq;
use tempfile::TempDir;

use super::*;
use crate::{
db::{api::mutation::AdditionOps, graph::graph::Graph},
prelude::NO_PROPS,
test_storage,
};

use super::*;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use std::borrow::Borrow;

fn load_graph() -> Graph {
let graph = Graph::new();
Expand All @@ -212,22 +209,14 @@ mod page_rank_tests {
fn test_page_rank() {
let graph = load_graph();

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let results = unweighted_page_rank(graph, Some(1000), Some(1), None, true, None);

assert_eq_f64(results.get("1"), Some(&0.38694), 5);
assert_eq_f64(results.get("2"), Some(&0.20195), 5);
assert_eq_f64(results.get("4"), Some(&0.20195), 5);
assert_eq_f64(results.get("3"), Some(&0.20916), 5);
}

test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}

#[test]
Expand Down Expand Up @@ -264,11 +253,7 @@ mod page_rank_tests {
graph.add_edge(t, src, dst, NO_PROPS, None).unwrap();
}

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let results = unweighted_page_rank(graph, Some(1000), Some(4), None, true, None);

assert_eq_f64(results.get("10"), Some(&0.072082), 5);
Expand All @@ -282,10 +267,7 @@ mod page_rank_tests {
assert_eq_f64(results.get("7"), Some(&0.01638), 5);
assert_eq_f64(results.get("9"), Some(&0.06186), 5);
assert_eq_f64(results.get("5"), Some(&0.19658), 5);
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}

#[test]
Expand All @@ -298,19 +280,12 @@ mod page_rank_tests {
graph.add_edge(t as i64, src, dst, NO_PROPS, None).unwrap();
}

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let results = unweighted_page_rank(graph, Some(1000), Some(4), None, false, None);

assert_eq_f64(results.get("1"), Some(&0.5), 3);
assert_eq_f64(results.get("2"), Some(&0.5), 3);
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}

#[test]
Expand All @@ -323,20 +298,13 @@ mod page_rank_tests {
graph.add_edge(t as i64, src, dst, NO_PROPS, None).unwrap();
}

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let results = unweighted_page_rank(graph, Some(10), Some(4), None, false, None);

assert_eq_f64(results.get("1"), Some(&0.303), 3);
assert_eq_f64(results.get("2"), Some(&0.393), 3);
assert_eq_f64(results.get("3"), Some(&0.303), 3);
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}

#[test]
Expand Down Expand Up @@ -367,12 +335,7 @@ mod page_rank_tests {
for (src, dst, t) in edges {
graph.add_edge(t, src, dst, NO_PROPS, None).unwrap();
}

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let results = unweighted_page_rank(graph, Some(1000), Some(4), None, true, None);

assert_eq_f64(results.get("1"), Some(&0.055), 3);
Expand All @@ -386,10 +349,7 @@ mod page_rank_tests {
assert_eq_f64(results.get("9"), Some(&0.110), 3);
assert_eq_f64(results.get("10"), Some(&0.117), 3);
assert_eq_f64(results.get("11"), Some(&0.122), 3);
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}

fn assert_eq_f64<T: Borrow<f64> + PartialEq + std::fmt::Debug>(
Expand Down
12 changes: 3 additions & 9 deletions raphtory/src/algorithms/community_detection/label_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn find_max_label(label_count: &BTreeMap<u64, f64>) -> Option<u64> {
#[cfg(test)]
mod lpa_tests {
use super::*;
use tempfile::TempDir;
use crate::test_storage;

#[test]
fn lpa_test() {
Expand All @@ -98,11 +98,8 @@ mod lpa_tests {
for (ts, src, dst) in edges {
graph.add_edge(ts, src, dst, NO_PROPS, None).unwrap();
}
let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let seed = Some([5; 32]);
let result = label_propagation(graph, seed).unwrap();
let expected = vec![
Expand All @@ -123,9 +120,6 @@ mod lpa_tests {
for hashset in expected {
assert!(result.contains(&hashset));
}
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}
}
35 changes: 7 additions & 28 deletions raphtory/src/algorithms/community_detection/louvain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ pub fn louvain<'graph, M: ModularityFunction, G: GraphViewOps<'graph>>(
mod test {
use crate::{
algorithms::community_detection::{louvain::louvain, modularity::ModularityUnDir},
db::api::view::StaticGraphViewOps,
prelude::*,
test_storage,
};
use proptest::prelude::*;
use tempfile::TempDir;

#[test]
fn test_louvain() {
Expand Down Expand Up @@ -99,17 +98,11 @@ mod test {
.add_edge(1, dst, src, [("weight", weight)], None)
.unwrap();
}
let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let result = louvain::<ModularityUnDir, _>(graph, 1.0, Some("weight"), None);
assert!(graph.nodes().iter().all(|n| result.get(n).is_some()));
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}

fn test_all_nodes_assigned_inner_unweighted(edges: Vec<(u64, u64)>) {
Expand All @@ -119,17 +112,10 @@ mod test {
graph.add_edge(1, dst, src, NO_PROPS, None).unwrap();
}

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let result = louvain::<ModularityUnDir, _>(graph, 1.0, None, None);
assert!(graph.nodes().iter().all(|n| result.get(n).is_some()));
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}

proptest! {
Expand Down Expand Up @@ -168,16 +154,9 @@ mod test {
})
.unwrap();

let test_dir = TempDir::new().unwrap();
#[cfg(feature = "storage")]
let disk_graph = graph.persist_as_disk_graph(test_dir.path()).unwrap();

fn test<G: StaticGraphViewOps>(graph: &G) {
test_storage!(&graph, |graph| {
let result = louvain::<ModularityUnDir, _>(graph, 1.0, None, None);
println!("{result:?}")
}
test(&graph);
#[cfg(feature = "storage")]
test(&disk_graph);
});
}
}
Loading

0 comments on commit 66b125e

Please sign in to comment.