graph_api_test/index/
vertex_hash.rs

1#[cfg(feature = "vertex-hash-index")]
2use crate::{PersonMut, assert_elements_eq, populate_graph};
3#[cfg(feature = "vertex-hash-index")]
4use graph_api_lib::VertexReferenceMut;
5
6use crate::{Edge, Vertex};
7use graph_api_lib::Graph;
8
9/// Tests that a vertex can be added to the graph and that the indexed field
10/// search returns the added vertex.
11///
12/// This function creates a vertex, adds it to the graph, and then verifies
13/// that a search for the indexed field of the added vertex returns the
14/// expected result.
15#[cfg(feature = "vertex-hash-index")]
16pub fn test_index<T>(graph: &mut T)
17where
18    T: Graph<Vertex = Vertex, Edge = Edge> + graph_api_lib::SupportsVertexHashIndex,
19{
20    let refs = populate_graph(graph);
21    let collected = graph
22        .walk()
23        .vertices(Vertex::person_by_name("Bryn"))
24        .collect::<Vec<_>>();
25    assert_elements_eq!(graph, collected, vec![refs.bryn]);
26}
27
28#[cfg(not(feature = "vertex-hash-index"))]
29pub fn test_index<T>(_graph: &mut T)
30where
31    T: Graph<Vertex = Vertex, Edge = Edge>,
32{
33}
34/// Tests that a vertex can be removed from the graph, and that the indexed field
35/// search no longer returns the removed vertex.
36///
37/// This function creates a vertex, adds it to the graph, and then removes it.
38/// It then verifies that a search for the indexed field of the removed vertex
39/// returns an empty result.
40#[cfg(all(feature = "vertex-hash-index", feature = "element-removal"))]
41pub fn test_index_remove<T>(graph: &mut T)
42where
43    T: Graph<Vertex = Vertex, Edge = Edge>
44        + graph_api_lib::SupportsVertexHashIndex
45        + graph_api_lib::SupportsElementRemoval,
46{
47    let refs = populate_graph(graph);
48    graph.remove_vertex(refs.bryn);
49    assert_eq!(
50        graph
51            .walk()
52            .vertices(Vertex::person_by_name("Bryn"))
53            .count(),
54        0
55    );
56}
57
58#[cfg(not(all(feature = "vertex-hash-index", feature = "element-removal")))]
59pub fn test_index_remove<T>(_graph: &mut T)
60where
61    T: Graph<Vertex = Vertex, Edge = Edge>,
62{
63}
64
65#[cfg(feature = "vertex-hash-index")]
66pub fn test_index_update<T>(graph: &mut T)
67where
68    T: Graph<Vertex = Vertex, Edge = Edge> + graph_api_lib::SupportsVertexHashIndex,
69{
70    let refs = populate_graph(graph);
71    graph
72        .vertex_mut(refs.bryn)
73        .expect("bryn must exist")
74        .project_mut::<PersonMut<_, _>>()
75        .expect("must be a person")
76        .set_name("Dyllan".to_string());
77    assert_eq!(
78        graph
79            .walk()
80            .vertices(Vertex::person_by_name("Bryn"))
81            .count(),
82        0
83    );
84    assert_eq!(
85        graph
86            .walk()
87            .vertices(Vertex::person_by_name("Dyllan"))
88            .count(),
89        1
90    );
91}
92
93#[cfg(not(feature = "vertex-hash-index"))]
94pub fn test_index_update<T>(_graph: &mut T)
95where
96    T: Graph<Vertex = Vertex, Edge = Edge>,
97{
98}