114 lines
3.2 KiB
Rust
114 lines
3.2 KiB
Rust
use application::organization::{
|
|
AlbumAction, CreateAlbumCommand, CreateAlbumHandler, ManageAlbumEntriesCommand,
|
|
ManageAlbumEntriesHandler,
|
|
};
|
|
use application::testing::InMemoryAlbumRepository;
|
|
use domain::errors::DomainError;
|
|
use domain::value_objects::SystemId;
|
|
use std::sync::Arc;
|
|
|
|
async fn setup_album(repo: &Arc<InMemoryAlbumRepository>, creator: SystemId) -> SystemId {
|
|
let handler = CreateAlbumHandler::new(repo.clone());
|
|
let album = handler
|
|
.execute(CreateAlbumCommand {
|
|
title: "Test Album".into(),
|
|
creator_id: creator,
|
|
})
|
|
.await
|
|
.unwrap();
|
|
album.album_id
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn adds_asset_to_album() {
|
|
let repo = Arc::new(InMemoryAlbumRepository::new());
|
|
let user = SystemId::new();
|
|
let album_id = setup_album(&repo, user).await;
|
|
let asset_id = SystemId::new();
|
|
|
|
let handler = ManageAlbumEntriesHandler::new(repo.clone());
|
|
let album = handler
|
|
.execute(ManageAlbumEntriesCommand {
|
|
album_id,
|
|
action: AlbumAction::Add { asset_id },
|
|
user_id: user,
|
|
})
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(album.asset_count(), 1);
|
|
assert_eq!(album.entries[0].asset_id, asset_id);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn removes_asset_from_album() {
|
|
let repo = Arc::new(InMemoryAlbumRepository::new());
|
|
let user = SystemId::new();
|
|
let album_id = setup_album(&repo, user).await;
|
|
let asset_id = SystemId::new();
|
|
|
|
let handler = ManageAlbumEntriesHandler::new(repo.clone());
|
|
handler
|
|
.execute(ManageAlbumEntriesCommand {
|
|
album_id,
|
|
action: AlbumAction::Add { asset_id },
|
|
user_id: user,
|
|
})
|
|
.await
|
|
.unwrap();
|
|
|
|
let album = handler
|
|
.execute(ManageAlbumEntriesCommand {
|
|
album_id,
|
|
action: AlbumAction::Remove { asset_id },
|
|
user_id: user,
|
|
})
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(album.asset_count(), 0);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rejects_nonexistent_album() {
|
|
let repo = Arc::new(InMemoryAlbumRepository::new());
|
|
let handler = ManageAlbumEntriesHandler::new(repo);
|
|
let result = handler
|
|
.execute(ManageAlbumEntriesCommand {
|
|
album_id: SystemId::new(),
|
|
action: AlbumAction::Add {
|
|
asset_id: SystemId::new(),
|
|
},
|
|
user_id: SystemId::new(),
|
|
})
|
|
.await;
|
|
assert!(matches!(result, Err(DomainError::NotFound(_))));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rejects_duplicate_add() {
|
|
let repo = Arc::new(InMemoryAlbumRepository::new());
|
|
let user = SystemId::new();
|
|
let album_id = setup_album(&repo, user).await;
|
|
let asset_id = SystemId::new();
|
|
|
|
let handler = ManageAlbumEntriesHandler::new(repo.clone());
|
|
handler
|
|
.execute(ManageAlbumEntriesCommand {
|
|
album_id,
|
|
action: AlbumAction::Add { asset_id },
|
|
user_id: user,
|
|
})
|
|
.await
|
|
.unwrap();
|
|
|
|
let result = handler
|
|
.execute(ManageAlbumEntriesCommand {
|
|
album_id,
|
|
action: AlbumAction::Add { asset_id },
|
|
user_id: user,
|
|
})
|
|
.await;
|
|
assert!(matches!(result, Err(DomainError::Conflict(_))));
|
|
}
|