From 6bd431a9966101f5184349d1b0422375ce46f57c Mon Sep 17 00:00:00 2001 From: Pushkar Mishra Date: Fri, 23 Feb 2024 21:32:00 +0530 Subject: [PATCH 1/2] error in test Signed-off-by: Pushkar Mishra --- Cargo.toml | 1 + src/errors.rs | 7 +++++++ tests/integration_test.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/integration_test.rs diff --git a/Cargo.toml b/Cargo.toml index 2a8a83f8..ee6b80fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ serde_json = "1.0.82" serde = { version = "1.0.138", features = ["derive"] } url = { version = "2.2.2", features= ["serde"] } thiserror = "1.0.57" +tempfile = "3" diff --git a/src/errors.rs b/src/errors.rs index 7b0333fd..646195fe 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,3 +1,4 @@ +use std::convert::Infallible; use std::{str::Utf8Error, time::SystemTimeError}; use thiserror::Error; @@ -43,3 +44,9 @@ impl From> for ArklibError { Self::Other(anyhow::anyhow!(e.to_string())) } } + +impl From for ArklibError { + fn from(_: Infallible) -> Self { + Self::Parse + } +} diff --git a/tests/integration_test.rs b/tests/integration_test.rs new file mode 100644 index 00000000..a3c82069 --- /dev/null +++ b/tests/integration_test.rs @@ -0,0 +1,37 @@ +#[cfg(test)] +mod tests { + use ark_rust::storage::file_storage::FileStorage; + use std::collections::HashMap; + use std::fs::remove_file; + use tempfile::TempDir; + + #[test] + fn test_file_storage_integration() { + let temp_dir = + TempDir::new().expect("Failed to create temporary directory"); + let storage_path = temp_dir.path().join("test_storage.txt"); + + let mut file_storage = + FileStorage::new("TestStorage".to_string(), &storage_path); + + let mut data_to_write = HashMap::new(); + data_to_write.insert("key1".to_string(), "value1".to_string()); + data_to_write.insert("key2".to_string(), "value2".to_string()); + + file_storage + .write_to_disk(&data_to_write) + .expect("Failed to write data to disk"); + + let mut data_read: HashMap = HashMap::new(); + + file_storage + .read_from_disk(|data: HashMap| { + data_read = data; + }) + .expect("Failed to write data to disk"); + + assert_eq!(data_to_write, data_read); + + remove_file(&storage_path).expect("Failed to remove temporary file"); + } +} From c35441dab4829feeb21f3e6af6bbfae8c8a5174e Mon Sep 17 00:00:00 2001 From: Ishan Bhanuka Date: Sun, 25 Feb 2024 18:11:44 -0500 Subject: [PATCH 2/2] Add tests for read, write and delete logic --- src/storage/file_storage.rs | 96 ++++++++++++++++++++++++++++++++----- tests/integration_test.rs | 37 -------------- 2 files changed, 84 insertions(+), 49 deletions(-) delete mode 100644 tests/integration_test.rs diff --git a/src/storage/file_storage.rs b/src/storage/file_storage.rs index 9f358767..c5211ce5 100644 --- a/src/storage/file_storage.rs +++ b/src/storage/file_storage.rs @@ -1,3 +1,4 @@ +use std::fmt::Debug; use std::fs::{self, File}; use std::io::{BufRead, BufReader, BufWriter, Write}; use std::str::FromStr; @@ -38,13 +39,13 @@ impl FileStorage { /// Data is read as a key value pairs separated by a symbol and stored /// in a [HashMap] with a generic key K and V value. A handler /// is called on the data after reading it. - pub fn read_from_disk( + pub fn read_file( &self, - handle: impl FnOnce(HashMap), + mut handle: impl FnMut(HashMap), ) -> Result<()> where - K: FromStr + std::hash::Hash + std::cmp::Eq, - V: FromStr, + K: FromStr + std::hash::Hash + std::cmp::Eq + Debug, + V: FromStr + Debug, ArklibError: From<::Err>, ArklibError: From<::Err>, { @@ -61,6 +62,21 @@ impl FileStorage { log::info!("the file was modified externally, merging"); + let value_by_id = self.read_file_from_disk()?; + if !value_by_id.is_empty() { + handle(value_by_id); + } + + Ok(()) + } + + fn read_file_from_disk(&self) -> Result> + where + K: FromStr + std::hash::Hash + std::cmp::Eq + Debug, + V: FromStr + Debug, + ArklibError: From<::Err>, + ArklibError: From<::Err>, + { let file = fs::File::open(&self.path)?; let reader = BufReader::new(file); let mut lines = reader.lines(); @@ -83,21 +99,20 @@ impl FileStorage { value_by_id.insert(id, value); } - if !value_by_id.is_empty() { - handle(value_by_id); - } + Ok(value_by_id) } - Some(Err(e)) => return Err(e.into()), - None => (), + Some(Err(e)) => Err(e.into()), + None => Err(ArklibError::Storage( + self.label.clone(), + "Storage file is missing header".to_owned(), + )), } - - Ok(()) } /// Write data to file /// /// Data is a key-value mapping between [ResourceId] and a generic Value - pub fn write_to_disk( + pub fn write_file( &mut self, value_by_id: &HashMap, ) -> Result<()> @@ -172,3 +187,60 @@ impl Drop for FileStorage { } } } + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + use tempfile::TempDir; + + use crate::storage::file_storage::FileStorage; + + #[test] + fn test_file_storage_write_read() { + let temp_dir = + TempDir::new().expect("Failed to create temporary directory"); + let storage_path = temp_dir.path().join("test_storage.txt"); + + let mut file_storage = + FileStorage::new("TestStorage".to_string(), &storage_path); + + let mut data_to_write = HashMap::new(); + data_to_write.insert("key1".to_string(), "value1".to_string()); + data_to_write.insert("key2".to_string(), "value2".to_string()); + + file_storage + .write_file(&data_to_write) + .expect("Failed to write data to disk"); + + let data_read: HashMap<_, _> = file_storage + .read_file_from_disk() + .expect("Failed to read data from disk"); + + assert_eq!(data_read, data_to_write); + } + + #[test] + fn test_file_storage_auto_delete() { + let temp_dir = + TempDir::new().expect("Failed to create temporary directory"); + let storage_path = temp_dir.path().join("test_storage.txt"); + + // File storage should be dropped and the file deleted after this scope + { + let mut file_storage = + FileStorage::new("TestStorage".to_string(), &storage_path); + + let mut data_to_write = HashMap::new(); + data_to_write.insert("key1".to_string(), "value1".to_string()); + data_to_write.insert("key2".to_string(), "value2".to_string()); + + file_storage + .write_file(&data_to_write) + .expect("Failed to write data to disk"); + + assert_eq!(storage_path.exists(), true); + } + + assert_eq!(storage_path.exists(), false); + } +} diff --git a/tests/integration_test.rs b/tests/integration_test.rs deleted file mode 100644 index a3c82069..00000000 --- a/tests/integration_test.rs +++ /dev/null @@ -1,37 +0,0 @@ -#[cfg(test)] -mod tests { - use ark_rust::storage::file_storage::FileStorage; - use std::collections::HashMap; - use std::fs::remove_file; - use tempfile::TempDir; - - #[test] - fn test_file_storage_integration() { - let temp_dir = - TempDir::new().expect("Failed to create temporary directory"); - let storage_path = temp_dir.path().join("test_storage.txt"); - - let mut file_storage = - FileStorage::new("TestStorage".to_string(), &storage_path); - - let mut data_to_write = HashMap::new(); - data_to_write.insert("key1".to_string(), "value1".to_string()); - data_to_write.insert("key2".to_string(), "value2".to_string()); - - file_storage - .write_to_disk(&data_to_write) - .expect("Failed to write data to disk"); - - let mut data_read: HashMap = HashMap::new(); - - file_storage - .read_from_disk(|data: HashMap| { - data_read = data; - }) - .expect("Failed to write data to disk"); - - assert_eq!(data_to_write, data_read); - - remove_file(&storage_path).expect("Failed to remove temporary file"); - } -}