Reviewed-on: #26
This commit was merged in pull request #26.
This commit is contained in:
135
crates/application/tests/goals/create_test.rs
Normal file
135
crates/application/tests/goals/create_test.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
use crate::support::create_goal_command;
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::testing::{FakeStatsRepository, InMemoryStore, RefusingGoalCommand};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::support::TestContextBuilder;
|
||||
use application::goals::create;
|
||||
use application::goals::deps::GoalCommandDeps;
|
||||
|
||||
#[tokio::test]
|
||||
async fn creates_goal_and_returns_progress() {
|
||||
let goals = InMemoryStore::new();
|
||||
let stats = FakeStatsRepository::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: Arc::clone(&stats) as _,
|
||||
};
|
||||
|
||||
let result = create::execute(&deps, create_goal_command(2025, 50))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.goal.year(), 2025);
|
||||
assert_eq!(result.goal.target_count(), 50);
|
||||
assert_eq!(result.current_count, 0);
|
||||
assert_eq!(goals.goal_count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn creates_goal_with_review_count() {
|
||||
let goals = InMemoryStore::new();
|
||||
let stats = FakeStatsRepository::new();
|
||||
stats.set_review_count(Uuid::nil(), 2025, 5);
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: Arc::clone(&stats) as _,
|
||||
};
|
||||
|
||||
let result = create::execute(&deps, create_goal_command(2025, 50))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.current_count, 5);
|
||||
assert_eq!(goals.goal_count(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn creating_a_goal_records_the_goal_and_its_event_together() {
|
||||
let goals = InMemoryStore::new();
|
||||
let deps = GoalCommandDeps {
|
||||
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();
|
||||
|
||||
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]
|
||||
async fn rejects_duplicate_year() {
|
||||
let b = TestContextBuilder::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let cmd = create_goal_command(2025, 10);
|
||||
|
||||
create::execute(&deps, cmd).await.unwrap();
|
||||
|
||||
let result = create::execute(&deps, create_goal_command(2025, 20)).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_year_before_2020() {
|
||||
let b = TestContextBuilder::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let result = create::execute(&deps, create_goal_command(2019, 10)).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_zero_target() {
|
||||
let b = TestContextBuilder::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let result = create::execute(&deps, create_goal_command(2025, 0)).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
121
crates/application/tests/goals/delete_test.rs
Normal file
121
crates/application/tests/goals/delete_test.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use crate::support::create_goal_command;
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::testing::{FakeStatsRepository, InMemoryStore, RefusingGoalCommand};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::support::TestContextBuilder;
|
||||
use application::goals::deps::GoalCommandDeps;
|
||||
use application::goals::{commands::DeleteGoalCommand, create, delete};
|
||||
|
||||
#[tokio::test]
|
||||
async fn deletes_existing_goal() {
|
||||
let goals = InMemoryStore::new();
|
||||
let stats = FakeStatsRepository::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: Arc::clone(&stats) as _,
|
||||
};
|
||||
|
||||
create::execute(&deps, create_goal_command(2025, 10))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(goals.goal_count(), 1);
|
||||
|
||||
delete::execute(
|
||||
&deps,
|
||||
DeleteGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(goals.goal_count(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_not_found() {
|
||||
let b = TestContextBuilder::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let result = delete::execute(
|
||||
&deps,
|
||||
DeleteGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn deleting_a_goal_removes_it_and_records_its_event_together() {
|
||||
let goals = InMemoryStore::new();
|
||||
let deps = GoalCommandDeps {
|
||||
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();
|
||||
|
||||
delete::execute(
|
||||
&deps,
|
||||
DeleteGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(goals.goal_count(), 0);
|
||||
let recorded = goals.recorded_events();
|
||||
assert_eq!(recorded.len(), 2, "created + deleted");
|
||||
assert!(matches!(
|
||||
recorded[1],
|
||||
DomainEvent::GoalDeleted { year: 2025, .. }
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_delete_propagates_and_records_nothing() {
|
||||
let goals = InMemoryStore::new();
|
||||
let seed = GoalCommandDeps {
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: FakeStatsRepository::new() as _,
|
||||
};
|
||||
create::execute(&seed, create_goal_command(2025, 10))
|
||||
.await
|
||||
.unwrap();
|
||||
let before = goals.recorded_events().len();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::new(RefusingGoalCommand),
|
||||
..seed
|
||||
};
|
||||
|
||||
let result = delete::execute(
|
||||
&deps,
|
||||
DeleteGoalCommand {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(matches!(result, Err(DomainError::InfrastructureError(m)) if m == "refused"));
|
||||
assert_eq!(goals.goal_count(), 1);
|
||||
assert_eq!(goals.recorded_events().len(), before);
|
||||
}
|
||||
57
crates/application/tests/goals/get_test.rs
Normal file
57
crates/application/tests/goals/get_test.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use crate::support::create_goal_command;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::support::TestContextBuilder;
|
||||
use application::goals::deps::{GoalCommandDeps, GoalQueryDeps};
|
||||
use application::goals::{create, get, queries::GetGoalQuery};
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_goal_when_exists() {
|
||||
let b = TestContextBuilder::new();
|
||||
let cmd_deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let query_deps = GoalQueryDeps {
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
|
||||
create::execute(&cmd_deps, create_goal_command(2025, 50))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = get::execute(
|
||||
&query_deps,
|
||||
GetGoalQuery {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap().goal.target_count(), 50);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_none_when_missing() {
|
||||
let b = TestContextBuilder::new();
|
||||
let query_deps = GoalQueryDeps {
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let result = get::execute(
|
||||
&query_deps,
|
||||
GetGoalQuery {
|
||||
user_id: Uuid::nil(),
|
||||
year: 2025,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(result.is_none());
|
||||
}
|
||||
56
crates/application/tests/goals/list_test.rs
Normal file
56
crates/application/tests/goals/list_test.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use crate::support::create_goal_command;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::support::TestContextBuilder;
|
||||
use application::goals::deps::{GoalCommandDeps, GoalQueryDeps};
|
||||
use application::goals::{create, list, queries::ListGoalsQuery};
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_empty_when_no_goals() {
|
||||
let b = TestContextBuilder::new();
|
||||
let query_deps = GoalQueryDeps {
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let result = list::execute(
|
||||
&query_deps,
|
||||
ListGoalsQuery {
|
||||
user_id: Uuid::nil(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(result.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_all_goals_for_user() {
|
||||
let b = TestContextBuilder::new();
|
||||
let cmd_deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let query_deps = GoalQueryDeps {
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
|
||||
for year in [2023, 2024, 2025] {
|
||||
create::execute(&cmd_deps, create_goal_command(year, 10))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let result = list::execute(
|
||||
&query_deps,
|
||||
ListGoalsQuery {
|
||||
user_id: Uuid::nil(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.len(), 3);
|
||||
}
|
||||
113
crates/application/tests/goals/update_test.rs
Normal file
113
crates/application/tests/goals/update_test.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use crate::support::{create_goal_command, update_goal_command};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::testing::{FakeStatsRepository, InMemoryStore, RefusingGoalCommand};
|
||||
|
||||
use crate::support::TestContextBuilder;
|
||||
use application::goals::deps::GoalCommandDeps;
|
||||
use application::goals::{create, update};
|
||||
|
||||
#[tokio::test]
|
||||
async fn updates_target_count() {
|
||||
let b = TestContextBuilder::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
|
||||
create::execute(&deps, create_goal_command(2025, 10))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = update::execute(&deps, update_goal_command(2025, 100))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(result.goal.target_count(), 100);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_goal_not_found() {
|
||||
let b = TestContextBuilder::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
let result = update::execute(&deps, update_goal_command(2025, 10)).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_zero_target() {
|
||||
let b = TestContextBuilder::new();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: b.goal_command.clone(),
|
||||
goal_query: b.goal_query.clone(),
|
||||
stats: b.stats_repo.clone(),
|
||||
};
|
||||
|
||||
create::execute(&deps, create_goal_command(2025, 10))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = update::execute(&deps, update_goal_command(2025, 0)).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn updating_a_goal_records_the_change_and_its_event_together() {
|
||||
let goals = InMemoryStore::new();
|
||||
let deps = GoalCommandDeps {
|
||||
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();
|
||||
|
||||
update::execute(&deps, update_goal_command(2025, 100))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let recorded = goals.recorded_events();
|
||||
assert_eq!(recorded.len(), 2, "created + updated");
|
||||
assert!(matches!(
|
||||
recorded[1],
|
||||
DomainEvent::GoalUpdated {
|
||||
year: 2025,
|
||||
target_count: 100,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_update_propagates_and_records_nothing() {
|
||||
let goals = InMemoryStore::new();
|
||||
let seed = GoalCommandDeps {
|
||||
goal_command: Arc::clone(&goals) as _,
|
||||
goal_query: Arc::clone(&goals) as _,
|
||||
stats: FakeStatsRepository::new() as _,
|
||||
};
|
||||
create::execute(&seed, create_goal_command(2025, 10))
|
||||
.await
|
||||
.unwrap();
|
||||
let before = goals.recorded_events().len();
|
||||
let deps = GoalCommandDeps {
|
||||
goal_command: Arc::new(RefusingGoalCommand),
|
||||
..seed
|
||||
};
|
||||
|
||||
let result = update::execute(&deps, update_goal_command(2025, 100)).await;
|
||||
|
||||
assert!(matches!(result, Err(DomainError::InfrastructureError(m)) if m == "refused"));
|
||||
assert_eq!(goals.recorded_events().len(), before);
|
||||
}
|
||||
Reference in New Issue
Block a user