app: add organization + sharing commands/queries
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
use std::sync::Arc;
|
||||
use application::testing::InMemoryAlbumRepository;
|
||||
use application::organization::{
|
||||
AlbumAction, CreateAlbumCommand, CreateAlbumHandler,
|
||||
ManageAlbumEntriesCommand, ManageAlbumEntriesHandler,
|
||||
};
|
||||
use domain::errors::DomainError;
|
||||
use domain::value_objects::SystemId;
|
||||
|
||||
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(_))));
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
mod create_album;
|
||||
mod manage_album_entries;
|
||||
mod tag_asset;
|
||||
|
||||
81
crates/application/tests/organization/commands/tag_asset.rs
Normal file
81
crates/application/tests/organization/commands/tag_asset.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use std::sync::Arc;
|
||||
use application::testing::{InMemoryAssetRepository, InMemoryTagRepository};
|
||||
use application::organization::{TagAssetCommand, TagAssetHandler};
|
||||
use domain::entities::{Asset, AssetType, SourceReference};
|
||||
use domain::errors::DomainError;
|
||||
use domain::ports::{AssetRepository, TagRepository};
|
||||
use domain::value_objects::{Checksum, SystemId};
|
||||
|
||||
async fn seed_asset(repo: &Arc<InMemoryAssetRepository>) -> SystemId {
|
||||
let owner = SystemId::new();
|
||||
let asset = Asset::new(
|
||||
SourceReference {
|
||||
volume_id: SystemId::new(),
|
||||
relative_path: "photos/test.jpg".into(),
|
||||
checksum: Checksum::new("a".repeat(64)).unwrap(),
|
||||
},
|
||||
AssetType::Image,
|
||||
"image/jpeg",
|
||||
1024,
|
||||
owner,
|
||||
);
|
||||
let id = asset.asset_id;
|
||||
repo.save(&asset).await.unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn tags_asset_creates_new_tag() {
|
||||
let asset_repo = Arc::new(InMemoryAssetRepository::new());
|
||||
let tag_repo = Arc::new(InMemoryTagRepository::new());
|
||||
let asset_id = seed_asset(&asset_repo).await;
|
||||
let user = SystemId::new();
|
||||
|
||||
let handler = TagAssetHandler::new(asset_repo, tag_repo);
|
||||
let (tag, asset_tag) = handler.execute(TagAssetCommand {
|
||||
asset_id,
|
||||
tag_name: "sunset".into(),
|
||||
user_id: user,
|
||||
}).await.unwrap();
|
||||
|
||||
assert_eq!(tag.name, "sunset");
|
||||
assert_eq!(asset_tag.asset_id, asset_id);
|
||||
assert_eq!(asset_tag.tag_id, tag.tag_id);
|
||||
assert_eq!(asset_tag.tagged_by_user_id, Some(user));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reuses_existing_tag() {
|
||||
let asset_repo = Arc::new(InMemoryAssetRepository::new());
|
||||
let tag_repo = Arc::new(InMemoryTagRepository::new());
|
||||
let asset_id = seed_asset(&asset_repo).await;
|
||||
let user = SystemId::new();
|
||||
|
||||
// Pre-create tag
|
||||
let existing = domain::entities::Tag::new_manual("sunset");
|
||||
let existing_id = existing.tag_id;
|
||||
tag_repo.save_tag(&existing).await.unwrap();
|
||||
|
||||
let handler = TagAssetHandler::new(asset_repo, tag_repo);
|
||||
let (tag, _) = handler.execute(TagAssetCommand {
|
||||
asset_id,
|
||||
tag_name: "sunset".into(),
|
||||
user_id: user,
|
||||
}).await.unwrap();
|
||||
|
||||
assert_eq!(tag.tag_id, existing_id);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_nonexistent_asset() {
|
||||
let asset_repo = Arc::new(InMemoryAssetRepository::new());
|
||||
let tag_repo = Arc::new(InMemoryTagRepository::new());
|
||||
|
||||
let handler = TagAssetHandler::new(asset_repo, tag_repo);
|
||||
let result = handler.execute(TagAssetCommand {
|
||||
asset_id: SystemId::new(),
|
||||
tag_name: "sunset".into(),
|
||||
user_id: SystemId::new(),
|
||||
}).await;
|
||||
assert!(matches!(result, Err(DomainError::NotFound(_))));
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
mod commands;
|
||||
mod queries;
|
||||
|
||||
38
crates/application/tests/organization/queries/get_album.rs
Normal file
38
crates/application/tests/organization/queries/get_album.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use std::sync::Arc;
|
||||
use application::testing::InMemoryAlbumRepository;
|
||||
use application::organization::{
|
||||
CreateAlbumCommand, CreateAlbumHandler,
|
||||
GetAlbumQuery, GetAlbumHandler,
|
||||
};
|
||||
use domain::errors::DomainError;
|
||||
use domain::value_objects::SystemId;
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_album() {
|
||||
let repo = Arc::new(InMemoryAlbumRepository::new());
|
||||
let creator = SystemId::new();
|
||||
|
||||
let create_handler = CreateAlbumHandler::new(repo.clone());
|
||||
let album = create_handler.execute(CreateAlbumCommand {
|
||||
title: "My Album".into(),
|
||||
creator_id: creator,
|
||||
}).await.unwrap();
|
||||
|
||||
let query_handler = GetAlbumHandler::new(repo);
|
||||
let found = query_handler.execute(GetAlbumQuery {
|
||||
album_id: album.album_id,
|
||||
}).await.unwrap();
|
||||
|
||||
assert_eq!(found.album_id, album.album_id);
|
||||
assert_eq!(found.title, "My Album");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_nonexistent() {
|
||||
let repo = Arc::new(InMemoryAlbumRepository::new());
|
||||
let handler = GetAlbumHandler::new(repo);
|
||||
let result = handler.execute(GetAlbumQuery {
|
||||
album_id: SystemId::new(),
|
||||
}).await;
|
||||
assert!(matches!(result, Err(DomainError::NotFound(_))));
|
||||
}
|
||||
1
crates/application/tests/organization/queries/mod.rs
Normal file
1
crates/application/tests/organization/queries/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
mod get_album;
|
||||
Reference in New Issue
Block a user