Get tests building and sort of passing
This commit is contained in:
61
src/files.rs
61
src/files.rs
@@ -518,6 +518,7 @@ mod tests {
|
|||||||
use crate::tags::SqliteTagDao;
|
use crate::tags::SqliteTagDao;
|
||||||
use actix_web::web::Data;
|
use actix_web::web::Data;
|
||||||
use std::{fs, sync::Arc};
|
use std::{fs, sync::Arc};
|
||||||
|
use actix_web::test::TestRequest;
|
||||||
|
|
||||||
fn setup() {
|
fn setup() {
|
||||||
let _ = env_logger::builder().is_test(true).try_init();
|
let _ = env_logger::builder().is_test(true).try_init();
|
||||||
@@ -546,13 +547,9 @@ mod tests {
|
|||||||
|
|
||||||
let response: HttpResponse = list_photos(
|
let response: HttpResponse = list_photos(
|
||||||
claims,
|
claims,
|
||||||
|
TestRequest::default().to_http_request(),
|
||||||
request,
|
request,
|
||||||
Data::new(AppState::new(
|
Data::new(AppState::test_state()),
|
||||||
Arc::new(StreamActor {}.start()),
|
|
||||||
String::from("/tmp"),
|
|
||||||
String::from("/tmp/thumbs"),
|
|
||||||
String::from("/tmp/video"),
|
|
||||||
)),
|
|
||||||
Data::new(RealFileSystem::new(String::from("/tmp"))),
|
Data::new(RealFileSystem::new(String::from("/tmp"))),
|
||||||
Data::new(Mutex::new(SqliteTagDao::default())),
|
Data::new(Mutex::new(SqliteTagDao::default())),
|
||||||
)
|
)
|
||||||
@@ -588,14 +585,9 @@ mod tests {
|
|||||||
|
|
||||||
let response = list_photos(
|
let response = list_photos(
|
||||||
claims,
|
claims,
|
||||||
HttpRequest::default(),
|
TestRequest::default().to_http_request(),
|
||||||
request,
|
request,
|
||||||
Data::new(AppState::new(
|
Data::new(AppState::test_state()),
|
||||||
Arc::new(StreamActor {}.start()),
|
|
||||||
String::from("/tmp"),
|
|
||||||
String::from("/tmp/thumbs"),
|
|
||||||
String::from("/tmp/video"),
|
|
||||||
)),
|
|
||||||
Data::new(RealFileSystem::new(String::from("./"))),
|
Data::new(RealFileSystem::new(String::from("./"))),
|
||||||
Data::new(Mutex::new(SqliteTagDao::default())),
|
Data::new(Mutex::new(SqliteTagDao::default())),
|
||||||
)
|
)
|
||||||
@@ -617,12 +609,12 @@ mod tests {
|
|||||||
|
|
||||||
let mut tag_dao = SqliteTagDao::new(in_memory_db_connection());
|
let mut tag_dao = SqliteTagDao::new(in_memory_db_connection());
|
||||||
|
|
||||||
let tag1 = tag_dao.create_tag("tag1").unwrap();
|
let tag1 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag1").unwrap();
|
||||||
let _tag2 = tag_dao.create_tag("tag2").unwrap();
|
let _tag2 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag2").unwrap();
|
||||||
let tag3 = tag_dao.create_tag("tag3").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(&opentelemetry::Context::current(), "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", tag3.id).unwrap();
|
||||||
|
|
||||||
let mut files = HashMap::new();
|
let mut files = HashMap::new();
|
||||||
files.insert(
|
files.insert(
|
||||||
@@ -636,14 +628,9 @@ mod tests {
|
|||||||
|
|
||||||
let response: HttpResponse = list_photos(
|
let response: HttpResponse = list_photos(
|
||||||
claims,
|
claims,
|
||||||
HttpRequest::default(),
|
TestRequest::default().to_http_request(),
|
||||||
request,
|
request,
|
||||||
Data::new(AppState::new(
|
Data::new(AppState::test_state()),
|
||||||
Arc::new(StreamActor {}.start()),
|
|
||||||
String::from(""),
|
|
||||||
String::from("/tmp/thumbs"),
|
|
||||||
String::from("/tmp/video"),
|
|
||||||
)),
|
|
||||||
Data::new(FakeFileSystem::new(files)),
|
Data::new(FakeFileSystem::new(files)),
|
||||||
Data::new(Mutex::new(tag_dao)),
|
Data::new(Mutex::new(tag_dao)),
|
||||||
)
|
)
|
||||||
@@ -667,15 +654,15 @@ mod tests {
|
|||||||
|
|
||||||
let mut tag_dao = SqliteTagDao::new(in_memory_db_connection());
|
let mut tag_dao = SqliteTagDao::new(in_memory_db_connection());
|
||||||
|
|
||||||
let tag1 = tag_dao.create_tag("tag1").unwrap();
|
let tag1 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag1").unwrap();
|
||||||
let _tag2 = tag_dao.create_tag("tag2").unwrap();
|
let _tag2 = tag_dao.create_tag(&opentelemetry::Context::current(), "tag2").unwrap();
|
||||||
let tag3 = tag_dao.create_tag("tag3").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(&opentelemetry::Context::current(), "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", tag3.id).unwrap();
|
||||||
|
|
||||||
// Should get filtered since it doesn't have tag3
|
// 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();
|
let mut files = HashMap::new();
|
||||||
files.insert(
|
files.insert(
|
||||||
@@ -695,13 +682,9 @@ mod tests {
|
|||||||
|
|
||||||
let response: HttpResponse = list_photos(
|
let response: HttpResponse = list_photos(
|
||||||
claims,
|
claims,
|
||||||
|
TestRequest::default().to_http_request(),
|
||||||
request,
|
request,
|
||||||
Data::new(AppState::new(
|
Data::new(AppState::test_state()),
|
||||||
Arc::new(StreamActor {}.start()),
|
|
||||||
String::from(""),
|
|
||||||
String::from("/tmp/thumbs"),
|
|
||||||
String::from("/tmp/video"),
|
|
||||||
)),
|
|
||||||
Data::new(FakeFileSystem::new(files)),
|
Data::new(FakeFileSystem::new(files)),
|
||||||
Data::new(Mutex::new(tag_dao)),
|
Data::new(Mutex::new(tag_dao)),
|
||||||
)
|
)
|
||||||
@@ -772,8 +755,8 @@ mod tests {
|
|||||||
assert!(is_valid_full_path(&base, &test_file, false).is_some());
|
assert!(is_valid_full_path(&base, &test_file, false).is_some());
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Some(PathBuf::from("/tmp/test.png")),
|
Some(PathBuf::from(test_file.clone())),
|
||||||
is_valid_full_path(&base, &PathBuf::from("/tmp/test.png"), false)
|
is_valid_full_path(&base, &PathBuf::from(test_file), false)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
35
src/state.rs
35
src/state.rs
@@ -32,6 +32,7 @@ impl AppState {
|
|||||||
gif_path,
|
gif_path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for AppState {
|
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
|
||||||
|
}
|
||||||
|
|||||||
40
src/tags.rs
40
src/tags.rs
@@ -553,6 +553,7 @@ impl TagDao for SqliteTagDao {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use actix_web::web::{Data, Json};
|
use actix_web::web::{Data, Json};
|
||||||
|
use actix_web::test::TestRequest;
|
||||||
use std::{cell::RefCell, collections::HashMap};
|
use std::{cell::RefCell, collections::HashMap};
|
||||||
|
|
||||||
use diesel::result::Error::NotFound;
|
use diesel::result::Error::NotFound;
|
||||||
@@ -579,7 +580,7 @@ mod tests {
|
|||||||
impl TagDao for TestTagDao {
|
impl TagDao for TestTagDao {
|
||||||
fn get_all_tags(
|
fn get_all_tags(
|
||||||
&mut self,
|
&mut self,
|
||||||
context: &opentelemetry::Context,
|
_context: &opentelemetry::Context,
|
||||||
_option: Option<String>,
|
_option: Option<String>,
|
||||||
) -> anyhow::Result<Vec<(i64, Tag)>> {
|
) -> anyhow::Result<Vec<(i64, Tag)>> {
|
||||||
Ok(self
|
Ok(self
|
||||||
@@ -593,7 +594,7 @@ mod tests {
|
|||||||
|
|
||||||
fn get_tags_for_path(
|
fn get_tags_for_path(
|
||||||
&mut self,
|
&mut self,
|
||||||
context: &opentelemetry::Context,
|
_context: &opentelemetry::Context,
|
||||||
path: &str,
|
path: &str,
|
||||||
) -> anyhow::Result<Vec<Tag>> {
|
) -> anyhow::Result<Vec<Tag>> {
|
||||||
info!("Getting test tags for: {:?}", path);
|
info!("Getting test tags for: {:?}", path);
|
||||||
@@ -609,7 +610,7 @@ mod tests {
|
|||||||
|
|
||||||
fn create_tag(
|
fn create_tag(
|
||||||
&mut self,
|
&mut self,
|
||||||
context: &opentelemetry::Context,
|
_context: &opentelemetry::Context,
|
||||||
name: &str,
|
name: &str,
|
||||||
) -> anyhow::Result<Tag> {
|
) -> anyhow::Result<Tag> {
|
||||||
self.tag_count += 1;
|
self.tag_count += 1;
|
||||||
@@ -629,7 +630,7 @@ mod tests {
|
|||||||
|
|
||||||
fn remove_tag(
|
fn remove_tag(
|
||||||
&mut self,
|
&mut self,
|
||||||
context: &opentelemetry::Context,
|
_context: &opentelemetry::Context,
|
||||||
tag_name: &str,
|
tag_name: &str,
|
||||||
path: &str,
|
path: &str,
|
||||||
) -> anyhow::Result<Option<()>> {
|
) -> anyhow::Result<Option<()>> {
|
||||||
@@ -654,7 +655,7 @@ mod tests {
|
|||||||
|
|
||||||
fn tag_file(
|
fn tag_file(
|
||||||
&mut self,
|
&mut self,
|
||||||
context: &opentelemetry::Context,
|
_context: &opentelemetry::Context,
|
||||||
path: &str,
|
path: &str,
|
||||||
tag_id: i32,
|
tag_id: i32,
|
||||||
) -> anyhow::Result<TaggedPhoto> {
|
) -> anyhow::Result<TaggedPhoto> {
|
||||||
@@ -694,7 +695,7 @@ mod tests {
|
|||||||
&mut self,
|
&mut self,
|
||||||
tag_ids: Vec<i32>,
|
tag_ids: Vec<i32>,
|
||||||
exclude_tag_ids: Vec<i32>,
|
exclude_tag_ids: Vec<i32>,
|
||||||
context: &opentelemetry::Context,
|
_context: &opentelemetry::Context,
|
||||||
) -> anyhow::Result<Vec<FileWithTagCount>> {
|
) -> anyhow::Result<Vec<FileWithTagCount>> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
@@ -703,7 +704,7 @@ mod tests {
|
|||||||
&mut self,
|
&mut self,
|
||||||
tag_ids: Vec<i32>,
|
tag_ids: Vec<i32>,
|
||||||
exclude_tag_ids: Vec<i32>,
|
exclude_tag_ids: Vec<i32>,
|
||||||
context: &opentelemetry::Context,
|
_context: &opentelemetry::Context,
|
||||||
) -> anyhow::Result<Vec<FileWithTagCount>> {
|
) -> anyhow::Result<Vec<FileWithTagCount>> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
@@ -719,10 +720,11 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let tag_data = Data::new(Mutex::new(tag_dao));
|
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 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.len(), 1);
|
||||||
assert_eq!(tags.first().unwrap().1.name, "test-tag");
|
assert_eq!(tags.first().unwrap().1.name, "test-tag");
|
||||||
let tagged_photos = tag_dao.tagged_photos.borrow();
|
let tagged_photos = tag_dao.tagged_photos.borrow();
|
||||||
@@ -744,11 +746,12 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let tag_data = Data::new(Mutex::new(tag_dao));
|
let tag_data = Data::new(Mutex::new(tag_dao));
|
||||||
add_tag(claims.clone(), web::Json(add_request), tag_data.clone()).await;
|
let request = TestRequest::default().to_http_request();
|
||||||
remove_tagged_photo(claims, web::Json(remove_request), tag_data.clone()).await;
|
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 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());
|
assert!(tags.is_empty());
|
||||||
let tagged_photos = tag_dao.tagged_photos.borrow();
|
let tagged_photos = tag_dao.tagged_photos.borrow();
|
||||||
let previously_added_tagged_photo = tagged_photos.get("test.png").unwrap();
|
let previously_added_tagged_photo = tagged_photos.get("test.png").unwrap();
|
||||||
@@ -758,12 +761,12 @@ mod tests {
|
|||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn replace_tags_keeps_existing_tags_removes_extras_adds_missing_test() {
|
async fn replace_tags_keeps_existing_tags_removes_extras_adds_missing_test() {
|
||||||
let mut tag_dao = TestTagDao::new();
|
let mut tag_dao = TestTagDao::new();
|
||||||
let new_tag = tag_dao.create_tag("Test").unwrap();
|
let new_tag = tag_dao.create_tag(&opentelemetry::Context::current(), "Test").unwrap();
|
||||||
let new_tag2 = tag_dao.create_tag("Test2").unwrap();
|
let new_tag2 = tag_dao.create_tag(&opentelemetry::Context::current(), "Test2").unwrap();
|
||||||
let _ = tag_dao.create_tag("Test3").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(&opentelemetry::Context::current(), "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_tag2.id).unwrap();
|
||||||
|
|
||||||
let claims = Claims::valid_user(String::from("1"));
|
let claims = Claims::valid_user(String::from("1"));
|
||||||
let tag_data = Data::new(Mutex::new(tag_dao));
|
let tag_data = Data::new(Mutex::new(tag_dao));
|
||||||
@@ -773,7 +776,8 @@ mod tests {
|
|||||||
file_name: String::from("test.jpg"),
|
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 tag_dao = tag_data.lock().unwrap();
|
||||||
let tags_for_test_photo = &tag_dao.tagged_photos.borrow()["test.jpg"];
|
let tags_for_test_photo = &tag_dao.tagged_photos.borrow()["test.jpg"];
|
||||||
|
|||||||
Reference in New Issue
Block a user