Video Gifs #34

Merged
cameron merged 9 commits from feature/video-gifs into master 2025-08-08 18:55:15 +00:00
3 changed files with 79 additions and 57 deletions
Showing only changes of commit 2ea36a4c9b - Show all commits

View File

@@ -518,6 +518,7 @@ mod tests {
use crate::tags::SqliteTagDao;
use actix_web::web::Data;
use std::{fs, sync::Arc};
use actix_web::test::TestRequest;
fn setup() {
let _ = env_logger::builder().is_test(true).try_init();
@@ -546,13 +547,9 @@ mod tests {
let response: HttpResponse = list_photos(
claims,
TestRequest::default().to_http_request(),
request,
Data::new(AppState::new(
Arc::new(StreamActor {}.start()),
String::from("/tmp"),
String::from("/tmp/thumbs"),
String::from("/tmp/video"),
)),
Data::new(AppState::test_state()),
Data::new(RealFileSystem::new(String::from("/tmp"))),
Data::new(Mutex::new(SqliteTagDao::default())),
)
@@ -588,14 +585,9 @@ mod tests {
let response = list_photos(
claims,
HttpRequest::default(),
TestRequest::default().to_http_request(),
request,
Data::new(AppState::new(
Arc::new(StreamActor {}.start()),
String::from("/tmp"),
String::from("/tmp/thumbs"),
String::from("/tmp/video"),
)),
Data::new(AppState::test_state()),
Data::new(RealFileSystem::new(String::from("./"))),
Data::new(Mutex::new(SqliteTagDao::default())),
)
@@ -617,12 +609,12 @@ mod tests {
let mut tag_dao = SqliteTagDao::new(in_memory_db_connection());
let tag1 = tag_dao.create_tag("tag1").unwrap();
let _tag2 = tag_dao.create_tag("tag2").unwrap();
let tag3 = tag_dao.create_tag("tag3").unwrap();
let tag1 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag1").unwrap();
let _tag2 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag2").unwrap();
let tag3 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag3").unwrap();
let _ = &tag_dao.tag_file("test.jpg", tag1.id).unwrap();
let _ = &tag_dao.tag_file("test.jpg", tag3.id).unwrap();
let _ = &tag_dao.tag_file(&opentelemetry::Context::current(), "test.jpg", tag1.id).unwrap();
let _ = &tag_dao.tag_file(&opentelemetry::Context::current(), "test.jpg", tag3.id).unwrap();
let mut files = HashMap::new();
files.insert(
@@ -636,14 +628,9 @@ mod tests {
let response: HttpResponse = list_photos(
claims,
HttpRequest::default(),
TestRequest::default().to_http_request(),
request,
Data::new(AppState::new(
Arc::new(StreamActor {}.start()),
String::from(""),
String::from("/tmp/thumbs"),
String::from("/tmp/video"),
)),
Data::new(AppState::test_state()),
Data::new(FakeFileSystem::new(files)),
Data::new(Mutex::new(tag_dao)),
)
@@ -667,15 +654,15 @@ mod tests {
let mut tag_dao = SqliteTagDao::new(in_memory_db_connection());
let tag1 = tag_dao.create_tag("tag1").unwrap();
let _tag2 = tag_dao.create_tag("tag2").unwrap();
let tag3 = tag_dao.create_tag("tag3").unwrap();
let tag1 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag1").unwrap();
let _tag2 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag2").unwrap();
let tag3 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag3").unwrap();
let _ = &tag_dao.tag_file("test.jpg", tag1.id).unwrap();
let _ = &tag_dao.tag_file("test.jpg", tag3.id).unwrap();
let _ = &tag_dao.tag_file(&opentelemetry::Context::current(), "test.jpg", tag1.id).unwrap();
let _ = &tag_dao.tag_file(&opentelemetry::Context::current(), "test.jpg", tag3.id).unwrap();
// Should get filtered since it doesn't have tag3
tag_dao.tag_file("some-other.jpg", tag1.id).unwrap();
tag_dao.tag_file(&opentelemetry::Context::current(), "some-other.jpg", tag1.id).unwrap();
let mut files = HashMap::new();
files.insert(
@@ -695,13 +682,9 @@ mod tests {
let response: HttpResponse = list_photos(
claims,
TestRequest::default().to_http_request(),
request,
Data::new(AppState::new(
Arc::new(StreamActor {}.start()),
String::from(""),
String::from("/tmp/thumbs"),
String::from("/tmp/video"),
)),
Data::new(AppState::test_state()),
Data::new(FakeFileSystem::new(files)),
Data::new(Mutex::new(tag_dao)),
)
@@ -772,8 +755,8 @@ mod tests {
assert!(is_valid_full_path(&base, &test_file, false).is_some());
assert_eq!(
Some(PathBuf::from("/tmp/test.png")),
is_valid_full_path(&base, &PathBuf::from("/tmp/test.png"), false)
Some(PathBuf::from(test_file.clone())),
is_valid_full_path(&base, &PathBuf::from(test_file), false)
);
}

View File

@@ -32,6 +32,7 @@ impl AppState {
gif_path,
}
}
}
impl Default for AppState {
@@ -45,3 +46,37 @@ impl Default for AppState {
)
}
}
#[cfg(test)]
impl AppState {
/// Creates an AppState instance for testing with temporary directories
pub fn test_state() -> Self {
use actix::Actor;
// Create a base temporary directory
let temp_dir = tempfile::tempdir().expect("Failed to create temp directory");
let base_path = temp_dir.path().to_path_buf();
// Create subdirectories for thumbnails, videos, and gifs
let thumbnail_path = create_test_subdir(&base_path, "thumbnails");
let video_path = create_test_subdir(&base_path, "videos");
let gif_path = create_test_subdir(&base_path, "gifs");
// Create the AppState with the temporary paths
AppState::new(
std::sync::Arc::new(crate::video::actors::StreamActor {}.start()),
base_path.to_string_lossy().to_string(),
thumbnail_path.to_string_lossy().to_string(),
video_path.to_string_lossy().to_string(),
gif_path.to_string_lossy().to_string(),
)
}
}
/// Helper function to create a subdirectory inside the base directory for testing
#[cfg(test)]
fn create_test_subdir(base_path: &std::path::Path, name: &str) -> std::path::PathBuf {
let dir_path = base_path.join(name);
std::fs::create_dir_all(&dir_path)
.expect(&format!("Failed to create {} directory", name));
dir_path
}

View File

@@ -553,6 +553,7 @@ impl TagDao for SqliteTagDao {
#[cfg(test)]
mod tests {
use actix_web::web::{Data, Json};
use actix_web::test::TestRequest;
use std::{cell::RefCell, collections::HashMap};
use diesel::result::Error::NotFound;
@@ -579,7 +580,7 @@ mod tests {
impl TagDao for TestTagDao {
fn get_all_tags(
&mut self,
context: &opentelemetry::Context,
_context: &opentelemetry::Context,
_option: Option<String>,
) -> anyhow::Result<Vec<(i64, Tag)>> {
Ok(self
@@ -593,7 +594,7 @@ mod tests {
fn get_tags_for_path(
&mut self,
context: &opentelemetry::Context,
_context: &opentelemetry::Context,
path: &str,
) -> anyhow::Result<Vec<Tag>> {
info!("Getting test tags for: {:?}", path);
@@ -609,7 +610,7 @@ mod tests {
fn create_tag(
&mut self,
context: &opentelemetry::Context,
_context: &opentelemetry::Context,
name: &str,
) -> anyhow::Result<Tag> {
self.tag_count += 1;
@@ -629,7 +630,7 @@ mod tests {
fn remove_tag(
&mut self,
context: &opentelemetry::Context,
_context: &opentelemetry::Context,
tag_name: &str,
path: &str,
) -> anyhow::Result<Option<()>> {
@@ -654,7 +655,7 @@ mod tests {
fn tag_file(
&mut self,
context: &opentelemetry::Context,
_context: &opentelemetry::Context,
path: &str,
tag_id: i32,
) -> anyhow::Result<TaggedPhoto> {
@@ -694,7 +695,7 @@ mod tests {
&mut self,
tag_ids: Vec<i32>,
exclude_tag_ids: Vec<i32>,
context: &opentelemetry::Context,
_context: &opentelemetry::Context,
) -> anyhow::Result<Vec<FileWithTagCount>> {
todo!()
}
@@ -703,7 +704,7 @@ mod tests {
&mut self,
tag_ids: Vec<i32>,
exclude_tag_ids: Vec<i32>,
context: &opentelemetry::Context,
_context: &opentelemetry::Context,
) -> anyhow::Result<Vec<FileWithTagCount>> {
todo!()
}
@@ -719,10 +720,11 @@ mod tests {
};
let tag_data = Data::new(Mutex::new(tag_dao));
add_tag(claims, web::Json(body), tag_data.clone()).await;
let request = TestRequest::default().to_http_request();
add_tag(claims, request, web::Json(body), tag_data.clone()).await;
let mut tag_dao = tag_data.lock().unwrap();
let tags = tag_dao.get_all_tags(None).unwrap();
let tags = tag_dao.get_all_tags(&opentelemetry::Context::current(), None).unwrap();
assert_eq!(tags.len(), 1);
assert_eq!(tags.first().unwrap().1.name, "test-tag");
let tagged_photos = tag_dao.tagged_photos.borrow();
@@ -744,11 +746,12 @@ mod tests {
};
let tag_data = Data::new(Mutex::new(tag_dao));
add_tag(claims.clone(), web::Json(add_request), tag_data.clone()).await;
remove_tagged_photo(claims, web::Json(remove_request), tag_data.clone()).await;
let request = TestRequest::default().to_http_request();
add_tag(claims.clone(), request.clone(), web::Json(add_request), tag_data.clone()).await;
remove_tagged_photo(claims, request, web::Json(remove_request), tag_data.clone()).await;
let mut tag_dao = tag_data.lock().unwrap();
let tags = tag_dao.get_all_tags(None).unwrap();
let tags = tag_dao.get_all_tags(&opentelemetry::Context::current(), None).unwrap();
assert!(tags.is_empty());
let tagged_photos = tag_dao.tagged_photos.borrow();
let previously_added_tagged_photo = tagged_photos.get("test.png").unwrap();
@@ -758,12 +761,12 @@ mod tests {
#[actix_rt::test]
async fn replace_tags_keeps_existing_tags_removes_extras_adds_missing_test() {
let mut tag_dao = TestTagDao::new();
let new_tag = tag_dao.create_tag("Test").unwrap();
let new_tag2 = tag_dao.create_tag("Test2").unwrap();
let _ = tag_dao.create_tag("Test3").unwrap();
let new_tag = tag_dao.create_tag(&opentelemetry::Context::current(), "Test").unwrap();
let new_tag2 = tag_dao.create_tag(&opentelemetry::Context::current(), "Test2").unwrap();
let _ = tag_dao.create_tag(&opentelemetry::Context::current(), "Test3").unwrap();
tag_dao.tag_file("test.jpg", new_tag.id).unwrap();
tag_dao.tag_file("test.jpg", new_tag2.id).unwrap();
tag_dao.tag_file(&opentelemetry::Context::current(), "test.jpg", new_tag.id).unwrap();
tag_dao.tag_file(&opentelemetry::Context::current(), "test.jpg", new_tag2.id).unwrap();
let claims = Claims::valid_user(String::from("1"));
let tag_data = Data::new(Mutex::new(tag_dao));
@@ -773,7 +776,8 @@ mod tests {
file_name: String::from("test.jpg"),
};
update_tags(claims, tag_data.clone(), Json(add_tags_request)).await;
let request = TestRequest::default().to_http_request();
update_tags(claims, tag_data.clone(), request, web::Json(add_tags_request)).await;
let tag_dao = tag_data.lock().unwrap();
let tags_for_test_photo = &tag_dao.tagged_photos.borrow()["test.jpg"];