graph_api_test/steps/
reduce.rs

1use crate::{
2    Edge, EdgeExt, Knows, Person, Vertex, VertexExt, assert_elements_eq, assert_elements_one_of,
3    populate_graph,
4};
5use graph_api_lib::{EdgeReference, EdgeSearch, Graph, VertexReference, VertexSearch};
6
7/// Test vertex reduce operations with the non-terminal reduce
8pub fn test_vertices_reduce<G>(graph: &mut G)
9where
10    G: Graph<Vertex = Vertex, Edge = Edge>,
11{
12    // Populate the graph with test data
13    let refs = populate_graph(graph);
14
15    let oldest = graph
16        .walk()
17        .vertices(VertexSearch::scan())
18        .filter_person()
19        .reduce(|acc, vertex, _ctx| {
20            let acc_age = acc.project::<Person<_>>().unwrap().age();
21            let vertex_age = vertex.project::<Person<_>>().unwrap().age();
22            if vertex_age > acc_age { vertex } else { acc }
23        })
24        .map(|vertex, _ctx| vertex.id())
25        .next()
26        .expect("should have got an element");
27
28    // Verify the result exists
29    assert_elements_eq!(graph, vec![oldest], vec![refs.julia]);
30}
31
32/// Test edge reduce operations with the non-terminal reduce
33pub fn test_edges_reduce<G>(graph: &mut G)
34where
35    G: Graph<Vertex = Vertex, Edge = Edge>,
36{
37    let refs = populate_graph(graph);
38    let edge_id = graph
39        .walk()
40        .vertices(VertexSearch::scan())
41        .filter_person()
42        .edges(EdgeSearch::scan())
43        .filter_knows()
44        .reduce(|acc, edge, _ctx| {
45            let acc_since = acc.project::<Knows<_>>().unwrap().since();
46            let edge_since = edge.project::<Knows<_>>().unwrap().since();
47            if edge_since > acc_since { edge } else { acc }
48        })
49        .map(|edge, _ctx| edge.id())
50        .next()
51        .expect("should have got an element");
52
53    // Verify the result - in this case, both edges have the same since value (1999)
54    // So the accumulated edge should be one of the two input edges
55    assert_elements_one_of!(
56        graph,
57        vec![edge_id],
58        vec![refs.bryn_knows_julia, refs.julia_knows_bryn]
59    );
60}