From d09f828451da6ba6271c3cac7c30fcc36773a8fe Mon Sep 17 00:00:00 2001 From: Michael Kreil Date: Tue, 5 Mar 2024 10:46:58 +0100 Subject: [PATCH] test: static_source::new --- .../src/server/sources/static_source.rs | 27 +++++++++++++++++++ .../server/sources/static_source_folder.rs | 5 +++- .../src/server/sources/static_source_tar.rs | 4 +++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/versatiles/src/server/sources/static_source.rs b/versatiles/src/server/sources/static_source.rs index cecb7088..9f297d54 100644 --- a/versatiles/src/server/sources/static_source.rs +++ b/versatiles/src/server/sources/static_source.rs @@ -6,6 +6,7 @@ use versatiles_lib::shared::TargetCompression; #[async_trait] pub trait StaticSourceTrait: Send + Sync + Debug { + fn get_type(&self) -> String; fn get_name(&self) -> Result; fn get_data(&self, path: &[&str], accept: &TargetCompression) -> Option; } @@ -35,6 +36,10 @@ impl StaticSource { path, }) } + #[cfg(test)] + pub fn get_type(&self) -> String { + self.source.get_type() + } pub fn get_data(&self, path: &[&str], accept: &TargetCompression) -> Option { if self.path.is_empty() { self.source.get_data(path, accept) @@ -63,6 +68,10 @@ mod tests { #[async_trait] impl StaticSourceTrait for MockStaticSource { + fn get_type(&self) -> String { + String::from("mock") + } + fn get_name(&self) -> Result { Ok("MockSource".into()) } @@ -80,6 +89,24 @@ mod tests { } } + #[tokio::test] + async fn test_static_source_new_integration() { + // Create temporary file and directory for testing + let temp_dir = tempfile::tempdir().unwrap(); + let temp_file_path = temp_dir.path().join("temp.tar"); + let temp_folder_path = temp_dir.path().join("folder"); + std::fs::create_dir(&temp_folder_path).unwrap(); + std::fs::File::create(&temp_file_path).unwrap(); + + // Test initialization with a .tar file + let tar_source = StaticSource::new(temp_file_path.to_str().unwrap(), "").unwrap(); + assert_eq!(tar_source.get_type(), "tar"); + + // Test initialization with a folder + let folder_source = StaticSource::new(temp_folder_path.to_str().unwrap(), "").unwrap(); + assert_eq!(folder_source.get_type(), "folder"); + } + #[tokio::test] async fn test_get_data_valid_path() { let static_source = StaticSource { diff --git a/versatiles/src/server/sources/static_source_folder.rs b/versatiles/src/server/sources/static_source_folder.rs index 918852c1..1041b5df 100644 --- a/versatiles/src/server/sources/static_source_folder.rs +++ b/versatiles/src/server/sources/static_source_folder.rs @@ -41,6 +41,10 @@ impl Folder { #[async_trait] impl StaticSourceTrait for Folder { + fn get_type(&self) -> String { + String::from("folder") + } + // Returns the name of the folder fn get_name(&self) -> Result { Ok(self.name.clone()) @@ -86,7 +90,6 @@ impl Debug for Folder { mod tests { use super::Folder; use crate::server::sources::static_source::StaticSourceTrait; - use std::env; use versatiles_lib::shared::TargetCompression; #[tokio::test] diff --git a/versatiles/src/server/sources/static_source_tar.rs b/versatiles/src/server/sources/static_source_tar.rs index 9840b2e0..4e723606 100644 --- a/versatiles/src/server/sources/static_source_tar.rs +++ b/versatiles/src/server/sources/static_source_tar.rs @@ -129,6 +129,10 @@ impl TarFile { #[async_trait] impl StaticSourceTrait for TarFile { + fn get_type(&self) -> String { + String::from("tar") + } + fn get_name(&self) -> Result { Ok(self.name.to_owned()) }