graph_api_test/steps/
mutation.rs

1use crate::{Edge, EdgeExt, Vertex, assert_elements_eq, populate_graph};
2use graph_api_lib::{EdgeReference, EdgeSearch, Graph};
3
4pub fn test_mutation<T>(graph: &mut T)
5where
6    T: Graph<Vertex = Vertex, Edge = Edge>,
7{
8    let refs = populate_graph(graph);
9    assert_eq!(
10        graph
11            .walk_mut()
12            .vertices_by_id(vec![refs.julia])
13            .mutate(|graph, vertex_id, _context| {
14                graph.add_edge(vertex_id, refs.graph_api, Edge::Created);
15            }),
16        1
17    );
18
19    let collected = graph
20        .walk()
21        .vertices_by_id(vec![refs.julia])
22        .edges(EdgeSearch::scan().outgoing())
23        .head()
24        .collect::<Vec<_>>();
25    assert_elements_eq!(graph, collected, vec![refs.graph_api, refs.bryn]);
26}
27
28pub fn test_edge_mutation<T>(graph: &mut T)
29where
30    T: Graph<Vertex = Vertex, Edge = Edge>,
31{
32    let refs = populate_graph(graph);
33
34    // Use a simple approach - start with one vertex, find all outgoing edges
35    // We know bryn has two edges: knows julia and created graph_api
36    let initial_edge_count = graph
37        .walk()
38        .vertices_by_id(vec![refs.bryn])
39        .edges(EdgeSearch::scan().outgoing())
40        .count();
41
42    // Should be 2 edges (bryn knows julia, bryn created graph_api)
43    assert_eq!(initial_edge_count, 2, "Expected 2 initial edges from bryn");
44
45    // Now use the mutate fn to add a new edge for each outgoing edge from bryn
46    let mutations = graph
47        .walk_mut()
48        .vertices_by_id(vec![refs.bryn])
49        .edges(EdgeSearch::scan().outgoing())
50        .mutate(|graph, edge_id, _context| {
51            let tail = graph.edge(edge_id).unwrap().tail();
52
53            // Add a Language edge from this edge's source to the rust vertex
54            graph.add_edge(
55                tail,
56                refs.rust,
57                Edge::Language(crate::Language {
58                    name: "EdgeMutationTest".to_string(),
59                }),
60            );
61        });
62
63    // Should have mutated 2 edges
64    assert_eq!(mutations, 2, "Expected to process 2 edges");
65
66    // Count bryn's edges again
67    let final_edge_count = graph
68        .walk()
69        .vertices_by_id(vec![refs.bryn])
70        .edges(EdgeSearch::scan().outgoing())
71        .count();
72
73    // Should have 4 edges now - the original 2 plus the 2 new ones
74    assert_eq!(final_edge_count, 4, "Expected 4 final edges from bryn");
75
76    // Verify new edges connect bryn to rust
77    let bryn_language_edges = graph
78        .walk()
79        .vertices_by_id(vec![refs.bryn])
80        .edges(EdgeSearch::scan().outgoing())
81        .filter_by_language(|_, _| true)
82        .count();
83
84    assert_eq!(bryn_language_edges, 2, "Expected to find 2 language edges");
85}