graph_api_test/graph/
mod.rs

1use crate::{Edge, Knows, KnowsMut, Person, PersonMut, Vertex, populate_graph};
2#[cfg(feature = "element-removal")]
3use graph_api_lib::SupportsElementRemoval;
4use graph_api_lib::{EdgeReference, EdgeReferenceMut, VertexReference, VertexReferenceMut};
5use uuid::Uuid;
6
7pub fn test_add_vertex<Graph>(graph: &mut Graph)
8where
9    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge>,
10{
11    let vertex = graph.add_vertex(Vertex::Person {
12        name: "Julia".to_string(),
13        age: Default::default(),
14        unique_id: Uuid::new_v4(),
15        username: "".to_string(),
16        biography: "".to_string(),
17    });
18    assert!(graph.vertex(vertex).is_some());
19}
20
21#[cfg(feature = "element-removal")]
22pub fn test_remove_vertex<Graph>(graph: &mut Graph)
23where
24    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge> + SupportsElementRemoval,
25{
26    let vertex = graph.add_vertex(Vertex::Person {
27        name: "Bryn".to_string(),
28        age: Default::default(),
29        unique_id: Uuid::new_v4(),
30        username: "".to_string(),
31        biography: "".to_string(),
32    });
33    graph.remove_vertex(vertex);
34    assert!(graph.vertex(vertex).is_none());
35}
36
37#[cfg(not(feature = "element-removal"))]
38pub fn test_remove_vertex<Graph>(_graph: &mut Graph)
39where
40    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge>,
41{
42}
43
44pub fn test_add_edge<Graph>(graph: &mut Graph)
45where
46    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge>,
47{
48    let v1 = graph.add_vertex(Vertex::Person {
49        name: "Julia".to_string(),
50        age: Default::default(),
51        unique_id: Uuid::new_v4(),
52        username: "".to_string(),
53        biography: "".to_string(),
54    });
55    let v2 = graph.add_vertex(Vertex::Person {
56        name: "Bryn".to_string(),
57        age: Default::default(),
58        unique_id: Uuid::new_v4(),
59        username: "".to_string(),
60        biography: "".to_string(),
61    });
62    let edge = graph.add_edge(v1, v2, Edge::Knows { since: 2020 });
63    assert!(graph.edge(edge).is_some());
64}
65
66#[cfg(feature = "element-removal")]
67pub fn test_remove_edge<Graph>(graph: &mut Graph)
68where
69    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge> + SupportsElementRemoval,
70{
71    let v1 = graph.add_vertex(Vertex::Person {
72        name: "Julia".to_string(),
73        age: Default::default(),
74        unique_id: Uuid::new_v4(),
75        username: "".to_string(),
76        biography: "".to_string(),
77    });
78    let v2 = graph.add_vertex(Vertex::Person {
79        name: "Bryn".to_string(),
80        age: Default::default(),
81        unique_id: Uuid::new_v4(),
82        username: "".to_string(),
83        biography: "".to_string(),
84    });
85    let edge = graph.add_edge(v1, v2, Edge::Knows { since: 2020 });
86    graph.remove_edge(edge);
87    assert!(graph.edge(edge).is_none());
88}
89
90#[cfg(not(feature = "element-removal"))]
91pub fn test_remove_edge<Graph>(_graph: &mut Graph)
92where
93    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge>,
94{
95}
96
97#[cfg(feature = "element-removal")]
98pub fn test_remove_vertex_with_edges<Graph>(graph: &mut Graph)
99where
100    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge> + SupportsElementRemoval,
101{
102    // Create vertices
103    let v1 = graph.add_vertex(Vertex::Person {
104        name: "Julia".to_string(),
105        age: Default::default(),
106        unique_id: Uuid::new_v4(),
107        username: "".to_string(),
108        biography: "".to_string(),
109    });
110    let v2 = graph.add_vertex(Vertex::Person {
111        name: "Bryn".to_string(),
112        age: Default::default(),
113        unique_id: Uuid::new_v4(),
114        username: "".to_string(),
115        biography: "".to_string(),
116    });
117    let v3 = graph.add_vertex(Vertex::Person {
118        name: "Pixel".to_string(),
119        age: Default::default(),
120        unique_id: Uuid::new_v4(),
121        username: "".to_string(),
122        biography: "".to_string(),
123    });
124
125    // Create edges in different configurations
126    let e1 = graph.add_edge(v1, v2, Edge::Knows { since: 2020 });
127    let e2 = graph.add_edge(v2, v3, Edge::Knows { since: 2021 });
128    let e3 = graph.add_edge(v3, v1, Edge::Knows { since: 2022 });
129
130    // Remove middle vertex
131    graph.remove_vertex(v2);
132
133    // Verify v2 and its connected edges are gone
134    assert!(graph.vertex(v2).is_none());
135    assert!(graph.edge(e1).is_none());
136    assert!(graph.edge(e2).is_none());
137
138    // Verify remaining vertices and edge still exist
139    assert!(graph.vertex(v1).is_some());
140    assert!(graph.vertex(v3).is_some());
141    assert!(graph.edge(e3).is_some());
142}
143
144#[cfg(not(feature = "element-removal"))]
145pub fn test_remove_vertex_with_edges<Graph>(_graph: &mut Graph)
146where
147    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge>,
148{
149}
150
151pub fn test_mutate_vertex<Graph>(graph: &mut Graph)
152where
153    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge>,
154{
155    let refs = populate_graph(graph);
156    {
157        let mut person = graph.vertex_mut(refs.bryn).expect("expected vertex");
158        let mut bryn = person
159            .project_mut::<PersonMut<_, _>>()
160            .expect("expected person");
161        let age = bryn.age();
162        bryn.set_age(age + 1);
163    }
164    let person = graph.vertex(refs.bryn).expect("expected vertex");
165    let bryn = person.project::<Person<_>>().expect("expected person");
166    assert_eq!(bryn.age(), 46);
167}
168
169pub fn test_mutate_edge<Graph>(graph: &mut Graph)
170where
171    Graph: graph_api_lib::Graph<Vertex = Vertex, Edge = Edge>,
172{
173    let refs = populate_graph(graph);
174    {
175        let mut edge = graph
176            .edge_mut(refs.bryn_knows_julia)
177            .expect("expected edge");
178        let mut knows = edge
179            .project_mut::<KnowsMut<_, _>>()
180            .expect("expected knows");
181        let since = knows.since();
182        knows.set_since(since + 1);
183    }
184    let edge = graph.edge(refs.bryn_knows_julia).expect("expected edge");
185    let knows = edge.project::<Knows<_>>().expect("expected person");
186    assert_eq!(knows.since(), 2000);
187}