From b89454564b1f27b95aea006137062da9f863ae5b Mon Sep 17 00:00:00 2001 From: carvilsi Date: Wed, 4 Sep 2024 20:01:23 +0200 Subject: [PATCH] implemented new method to add a collection of edges into the graph vault --- src/graphs/mod.rs | 18 +++++++++++++++++- tests/graphs_test.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/graphs/mod.rs b/src/graphs/mod.rs index 2c2a7c9..5c2b8c3 100644 --- a/src/graphs/mod.rs +++ b/src/graphs/mod.rs @@ -120,7 +120,23 @@ impl Graphs { graphs_memory_watcher(self); } - // TODO: create method to add a collection of Edges + /// Adds a collection of Edges to the Graphs' vault + /// for the provided graphs vault name + /// if does not exists it creates a new entry + /// at vault. + /// If None name is provided, the current one + /// is use for the addition. + pub fn add_edges(&mut self, edges: &mut Vec, vault_name: Option<&str>) { + let current_vault = self.select_vault_label(vault_name); + if let Some(e) = self.vault.get_mut(¤t_vault) { + e.append(edges); + } else { + self.insert(¤t_vault); + let v = self.vault.get_mut(¤t_vault).unwrap(); + v.append(edges); + } + graphs_memory_watcher(self); + } /// Retrieves the collection of edges /// the default one or by name diff --git a/tests/graphs_test.rs b/tests/graphs_test.rs index c8299f6..7bdc72d 100644 --- a/tests/graphs_test.rs +++ b/tests/graphs_test.rs @@ -534,3 +534,29 @@ fn should_not_find_vertex_by_id_vault_does_not_exists() { let e = graphs.find_vertex_by_id("foobar", Some("!Exists")); assert_eq!(e, Err("provided vault does not exists")); } + +#[test] +fn should_add_a_collection_of_edges() { + let mut graphs = Graphs::init("collection-edges"); + + let v1 = Vertex::new("v1"); + let v2 = Vertex::new("v2"); + let v3 = Vertex::new("v3"); + + let mut edges: Vec = Vec::new(); + + let e1 = Edge::create(&v1, "v1->v2", &v2); + edges.push(e1); + let e2 = Edge::create(&v1, "v1->v3", &v3); + edges.push(e2); + let e3 = Edge::create(&v2, "v2->v1", &v1); + edges.push(e3); + let e4 = Edge::create(&v2, "v2->v3", &v3); + edges.push(e4); + + graphs.add_edges(&mut edges, None); + + let stats = graphs.get_stats(); + assert_eq!(stats.get_total_edges(), 4); + assert_eq!(stats.get_total_vertices(), 8); +} \ No newline at end of file