feat: atomic goal, watchlist, profile writes
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use crate::support::create_goal_command;
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::testing::{FakeStatsRepository, InMemoryStore, NoopEventPublisher};
|
||||
use domain::testing::{FakeStatsRepository, InMemoryStore, RefusingGoalCommand};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::support::TestContextBuilder;
|
||||
@@ -13,12 +14,10 @@ use application::goals::deps::GoalCommandDeps;
|
||||
async fn creates_goal_and_returns_progress() {
|
||||
let goals = InMemoryStore::new();
|
||||
let stats = FakeStatsRepository::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: Arc::clone(&stats) as _,
|
||||
event_publisher: Arc::clone(&events) as _,
|
||||
};
|
||||
|
||||
let result = create::execute(&deps, create_goal_command(2025, 50))
|
||||
@@ -36,12 +35,10 @@ async fn creates_goal_with_review_count() {
|
||||
let goals = InMemoryStore::new();
|
||||
let stats = FakeStatsRepository::new();
|
||||
stats.set_review_count(Uuid::nil(), 2025, 5);
|
||||
let events = NoopEventPublisher::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: Arc::clone(&stats) as _,
|
||||
event_publisher: Arc::clone(&events) as _,
|
||||
};
|
||||
|
||||
let result = create::execute(&deps, create_goal_command(2025, 50))
|
||||
@@ -53,26 +50,45 @@ async fn creates_goal_with_review_count() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn emits_goal_created_event() {
|
||||
let b = TestContextBuilder::new();
|
||||
let events = NoopEventPublisher::new();
|
||||
async fn creating_a_goal_records_the_goal_and_its_event_together() {
|
||||
let goals = InMemoryStore::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
event_publisher: Arc::clone(&events) as _,
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: FakeStatsRepository::new() as _,
|
||||
};
|
||||
|
||||
create::execute(&deps, create_goal_command(2025, 10))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let published = events.published();
|
||||
assert!(
|
||||
published
|
||||
.iter()
|
||||
.any(|e| matches!(e, DomainEvent::GoalCreated { year: 2025, .. }))
|
||||
);
|
||||
assert_eq!(goals.goal_count(), 1);
|
||||
let recorded = goals.recorded_events();
|
||||
assert_eq!(recorded.len(), 1);
|
||||
assert!(matches!(
|
||||
recorded[0],
|
||||
DomainEvent::GoalCreated {
|
||||
year: 2025,
|
||||
target_count: 10,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_create_propagates_and_records_nothing() {
|
||||
let goals = InMemoryStore::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::new(RefusingGoalCommand),
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: FakeStatsRepository::new() as _,
|
||||
};
|
||||
|
||||
let result = create::execute(&deps, create_goal_command(2025, 10)).await;
|
||||
|
||||
assert!(matches!(result, Err(DomainError::InfrastructureError(m)) if m == "refused"));
|
||||
assert_eq!(goals.goal_count(), 0);
|
||||
assert!(goals.recorded_events().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -82,7 +98,6 @@ async fn rejects_duplicate_year() {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
event_publisher: b.event_publisher.clone(),
|
||||
};
|
||||
let cmd = create_goal_command(2025, 10);
|
||||
|
||||
@@ -100,7 +115,6 @@ async fn rejects_year_before_2020() {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
event_publisher: b.event_publisher.clone(),
|
||||
};
|
||||
let result = create::execute(&deps, create_goal_command(2019, 10)).await;
|
||||
|
||||
@@ -114,7 +128,6 @@ async fn rejects_zero_target() {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
event_publisher: b.event_publisher.clone(),
|
||||
};
|
||||
let result = create::execute(&deps, create_goal_command(2025, 0)).await;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user