graph_api_test/steps/
fold.rs

1use crate::{Edge, Vertex, populate_graph};
2use graph_api_lib::{EdgeReference, EdgeSearch, Graph, VertexReference, VertexSearch};
3
4/// Test vertex fold operations
5pub fn test_vertices_fold<G>(graph: &mut G)
6where
7    G: Graph<Vertex = Vertex, Edge = Edge>,
8{
9    // Populate the graph with test data
10    let _refs = populate_graph(graph);
11
12    // Calculate the sum of ages using fold
13    let total_age = graph
14        .walk()
15        .vertices(VertexSearch::scan())
16        .fold(0, |total, vertex, _| {
17            if let Vertex::Person { age, .. } = *vertex.weight() {
18                total + age
19            } else {
20                total
21            }
22        });
23
24    // Check that the total age is positive - should include Bryn and Julia
25    assert_eq!(total_age, 93);
26}
27
28/// Test edge fold operations
29pub fn test_edges_fold<G>(graph: &mut G)
30where
31    G: Graph<Vertex = Vertex, Edge = Edge>,
32{
33    // Populate the graph with test data
34    let _refs = populate_graph(graph);
35
36    // Find the relationship year in the graph
37    let knows_year = graph
38        .walk()
39        .vertices(VertexSearch::scan())
40        .edges(EdgeSearch::scan())
41        .fold(None, |result, edge, _| {
42            if let Edge::Knows { since } = edge.weight() {
43                Some(*since)
44            } else {
45                result
46            }
47        });
48
49    // There should be at least one relationship with a year
50    assert!(knows_year.is_some());
51    // The standard test data uses 1999
52    assert_eq!(knows_year.unwrap(), 1999);
53}