fix: update trash test mocks to simulate PG CASCADE on clear_trash
MockTrashRepository now holds shared Arc<Mutex> refs to the trashed_files and trashed_folders collections so that clear_trash() also purges them — mirroring the ON DELETE CASCADE + PG trigger behaviour in production. All 112 tests pass.
This commit is contained in:
@@ -20,12 +20,21 @@ use crate::domain::services::path_service::StoragePath;
|
|||||||
// Mock repositories for testing
|
// Mock repositories for testing
|
||||||
struct MockTrashRepository {
|
struct MockTrashRepository {
|
||||||
trash_items: Mutex<HashMap<Uuid, TrashedItem>>,
|
trash_items: Mutex<HashMap<Uuid, TrashedItem>>,
|
||||||
|
/// Shared refs to the file/folder trashed maps so `clear_trash` can
|
||||||
|
/// simulate the PG CASCADE + trigger behaviour.
|
||||||
|
trashed_files: Arc<Mutex<HashMap<String, File>>>,
|
||||||
|
trashed_folders: Arc<Mutex<HashMap<String, Folder>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockTrashRepository {
|
impl MockTrashRepository {
|
||||||
fn new() -> Self {
|
fn new(
|
||||||
|
trashed_files: Arc<Mutex<HashMap<String, File>>>,
|
||||||
|
trashed_folders: Arc<Mutex<HashMap<String, Folder>>>,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
trash_items: Mutex::new(HashMap::new()),
|
trash_items: Mutex::new(HashMap::new()),
|
||||||
|
trashed_files,
|
||||||
|
trashed_folders,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,6 +89,9 @@ impl TrashRepository for MockTrashRepository {
|
|||||||
async fn clear_trash(&self, user_id: &Uuid) -> Result<()> {
|
async fn clear_trash(&self, user_id: &Uuid) -> Result<()> {
|
||||||
let mut items = self.trash_items.lock().unwrap();
|
let mut items = self.trash_items.lock().unwrap();
|
||||||
items.retain(|_, item| item.user_id() != *user_id);
|
items.retain(|_, item| item.user_id() != *user_id);
|
||||||
|
// Simulate PG CASCADE: clear trashed file/folder storage too
|
||||||
|
self.trashed_files.lock().unwrap().clear();
|
||||||
|
self.trashed_folders.lock().unwrap().clear();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,14 +109,14 @@ impl TrashRepository for MockTrashRepository {
|
|||||||
|
|
||||||
struct MockFileRepository {
|
struct MockFileRepository {
|
||||||
files: Mutex<HashMap<String, File>>,
|
files: Mutex<HashMap<String, File>>,
|
||||||
trashed_files: Mutex<HashMap<String, File>>,
|
trashed_files: Arc<Mutex<HashMap<String, File>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockFileRepository {
|
impl MockFileRepository {
|
||||||
fn new() -> Self {
|
fn new(trashed_files: Arc<Mutex<HashMap<String, File>>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
files: Mutex::new(HashMap::new()),
|
files: Mutex::new(HashMap::new()),
|
||||||
trashed_files: Mutex::new(HashMap::new()),
|
trashed_files,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,14 +334,14 @@ impl FileWritePort for MockFileRepository {
|
|||||||
|
|
||||||
struct MockFolderRepository {
|
struct MockFolderRepository {
|
||||||
folders: Mutex<HashMap<String, Folder>>,
|
folders: Mutex<HashMap<String, Folder>>,
|
||||||
trashed_folders: Mutex<HashMap<String, Folder>>,
|
trashed_folders: Arc<Mutex<HashMap<String, Folder>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockFolderRepository {
|
impl MockFolderRepository {
|
||||||
fn new() -> Self {
|
fn new(trashed_folders: Arc<Mutex<HashMap<String, Folder>>>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
folders: Mutex::new(HashMap::new()),
|
folders: Mutex::new(HashMap::new()),
|
||||||
trashed_folders: Mutex::new(HashMap::new()),
|
trashed_folders,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -500,9 +512,11 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_move_file_to_trash() {
|
async fn test_move_file_to_trash() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let trash_repo = Arc::new(MockTrashRepository::new());
|
let trashed_files = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let file_repo = Arc::new(MockFileRepository::new());
|
let trashed_folders = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let folder_repo = Arc::new(MockFolderRepository::new());
|
let trash_repo = Arc::new(MockTrashRepository::new(trashed_files.clone(), trashed_folders.clone()));
|
||||||
|
let file_repo = Arc::new(MockFileRepository::new(trashed_files));
|
||||||
|
let folder_repo = Arc::new(MockFolderRepository::new(trashed_folders));
|
||||||
|
|
||||||
let service = TrashService::new(
|
let service = TrashService::new(
|
||||||
trash_repo.clone(),
|
trash_repo.clone(),
|
||||||
@@ -569,9 +583,11 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_move_folder_to_trash() {
|
async fn test_move_folder_to_trash() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let trash_repo = Arc::new(MockTrashRepository::new());
|
let trashed_files = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let file_repo = Arc::new(MockFileRepository::new());
|
let trashed_folders = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let folder_repo = Arc::new(MockFolderRepository::new());
|
let trash_repo = Arc::new(MockTrashRepository::new(trashed_files.clone(), trashed_folders.clone()));
|
||||||
|
let file_repo = Arc::new(MockFileRepository::new(trashed_files));
|
||||||
|
let folder_repo = Arc::new(MockFolderRepository::new(trashed_folders));
|
||||||
|
|
||||||
let service = TrashService::new(
|
let service = TrashService::new(
|
||||||
trash_repo.clone(),
|
trash_repo.clone(),
|
||||||
@@ -629,9 +645,11 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_restore_file_from_trash() {
|
async fn test_restore_file_from_trash() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let trash_repo = Arc::new(MockTrashRepository::new());
|
let trashed_files = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let file_repo = Arc::new(MockFileRepository::new());
|
let trashed_folders = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let folder_repo = Arc::new(MockFolderRepository::new());
|
let trash_repo = Arc::new(MockTrashRepository::new(trashed_files.clone(), trashed_folders.clone()));
|
||||||
|
let file_repo = Arc::new(MockFileRepository::new(trashed_files));
|
||||||
|
let folder_repo = Arc::new(MockFolderRepository::new(trashed_folders));
|
||||||
|
|
||||||
let service = TrashService::new(
|
let service = TrashService::new(
|
||||||
trash_repo.clone(),
|
trash_repo.clone(),
|
||||||
@@ -694,9 +712,11 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_delete_permanently() {
|
async fn test_delete_permanently() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let trash_repo = Arc::new(MockTrashRepository::new());
|
let trashed_files = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let file_repo = Arc::new(MockFileRepository::new());
|
let trashed_folders = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let folder_repo = Arc::new(MockFolderRepository::new());
|
let trash_repo = Arc::new(MockTrashRepository::new(trashed_files.clone(), trashed_folders.clone()));
|
||||||
|
let file_repo = Arc::new(MockFileRepository::new(trashed_files));
|
||||||
|
let folder_repo = Arc::new(MockFolderRepository::new(trashed_folders));
|
||||||
|
|
||||||
let service = TrashService::new(
|
let service = TrashService::new(
|
||||||
trash_repo.clone(),
|
trash_repo.clone(),
|
||||||
@@ -758,9 +778,11 @@ mod tests {
|
|||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_empty_trash() {
|
async fn test_empty_trash() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let trash_repo = Arc::new(MockTrashRepository::new());
|
let trashed_files = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let file_repo = Arc::new(MockFileRepository::new());
|
let trashed_folders = Arc::new(Mutex::new(HashMap::new()));
|
||||||
let folder_repo = Arc::new(MockFolderRepository::new());
|
let trash_repo = Arc::new(MockTrashRepository::new(trashed_files.clone(), trashed_folders.clone()));
|
||||||
|
let file_repo = Arc::new(MockFileRepository::new(trashed_files));
|
||||||
|
let folder_repo = Arc::new(MockFolderRepository::new(trashed_folders));
|
||||||
|
|
||||||
let service = TrashService::new(
|
let service = TrashService::new(
|
||||||
trash_repo.clone(),
|
trash_repo.clone(),
|
||||||
|
|||||||
Reference in New Issue
Block a user