graph_api_test/index/
vertex_full_text.rs

1use crate::{Edge, PersonMut, Vertex, assert_elements_eq, populate_graph};
2use graph_api_lib::{Graph, SupportsVertexFullTextIndex, VertexReferenceMut};
3
4/// Tests that a vertex can be added to the graph and that the indexed field
5/// search returns the added vertex.
6///
7/// This function creates a vertex, adds it to the graph, and then verifies
8/// that a search for the indexed field of the added vertex returns the
9/// expected result.
10pub fn test_index<T>(graph: &mut T)
11where
12    T: Graph<Vertex = Vertex, Edge = Edge> + SupportsVertexFullTextIndex,
13{
14    let refs = populate_graph(graph);
15    let collected = graph
16        .walk()
17        .vertices(Vertex::person_by_biography("graph"))
18        .collect::<Vec<_>>();
19    assert_elements_eq!(graph, collected, vec![refs.bryn]);
20    graph.remove_vertex(refs.bryn);
21}
22
23/// Tests that a vertex can be removed from the graph, and that the indexed field
24/// search no longer returns the removed vertex.
25///
26/// This function creates a vertex, adds it to the graph, and then removes it.
27/// It then verifies that a search for the indexed field of the removed vertex
28/// returns an empty result.
29pub fn test_index_remove<T>(graph: &mut T)
30where
31    T: Graph<Vertex = Vertex, Edge = Edge> + SupportsVertexFullTextIndex,
32{
33    let refs = populate_graph(graph);
34    graph.remove_vertex(refs.bryn);
35    assert_eq!(
36        graph
37            .walk()
38            .vertices(Vertex::person_by_biography("graph"))
39            .count(),
40        0
41    );
42}
43
44/// Tests that a vertex's indexed field can be updated and that the graph's
45/// search functionality reflects this change.
46///
47/// This function creates a vertex, adds it to the graph, updates the indexed
48/// field of the vertex, and then performs searches to verify that:
49///
50/// - The old value of the indexed field is no longer present in the graph
51/// - The new value of the indexed field is correctly associated
52///   with the updated vertex
53pub fn test_index_update<T>(graph: &mut T)
54where
55    T: Graph<Vertex = Vertex, Edge = Edge> + SupportsVertexFullTextIndex,
56{
57    let refs = populate_graph(graph);
58    graph
59        .vertex_mut(refs.bryn)
60        .expect("bryn must exist")
61        .project_mut::<PersonMut<_, _>>()
62        .expect("must be a person")
63        .set_biography("Developed a graphql proxy in Rust".to_string());
64    assert_eq!(
65        graph
66            .walk()
67            .vertices(Vertex::person_by_biography("graph"))
68            .count(),
69        0
70    );
71    assert_eq!(
72        graph
73            .walk()
74            .vertices(Vertex::person_by_biography("proxy"))
75            .count(),
76        1
77    );
78}