Skip to content

Commit

Permalink
test: static_source::new
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKreil committed Mar 5, 2024
1 parent c485168 commit d09f828
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
27 changes: 27 additions & 0 deletions versatiles/src/server/sources/static_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>;
fn get_data(&self, path: &[&str], accept: &TargetCompression) -> Option<SourceResponse>;
}
Expand Down Expand Up @@ -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<SourceResponse> {
if self.path.is_empty() {
self.source.get_data(path, accept)
Expand Down Expand Up @@ -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<String> {
Ok("MockSource".into())
}
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion versatiles/src/server/sources/static_source_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Ok(self.name.clone())
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions versatiles/src/server/sources/static_source_tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Ok(self.name.to_owned())
}
Expand Down

0 comments on commit d09f828

Please sign in to comment.