graph_api_test/graph/
mod.rs

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