fix: honest review update rollback test, goal delete NotFound fake, read before write

This commit is contained in:
2026-10-26 00:52:45 +01:00
parent 62ad9f4017
commit 2f133afb96
4 changed files with 20 additions and 7 deletions

View File

@@ -118,11 +118,20 @@ async fn a_failing_enqueue_after_the_review_update_rolls_the_update_back() {
let repo = SqliteReviewRepository::new(pool.clone());
repo.save_review(&review, &[]).await.unwrap();
fail_event_inserts(&pool).await;
let event = updated(&review);
let edited = review.apply_edit(domain::models::ReviewEdit {
rating: Some(domain::value_objects::Rating::new(1).unwrap()),
..Default::default()
});
let event = updated(&edited);
let result = repo.update_review(&review, &[event]).await;
let result = repo.update_review(&edited, &[event]).await;
assert!(result.is_err());
let persisted: i64 = sqlx::query_scalar("SELECT rating FROM reviews")
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(persisted, 4, "rating update rolled back");
assert_eq!(count(&pool, "event_queue").await, 0);
}

View File

@@ -29,6 +29,8 @@ pub async fn execute(
cmd.target_count,
GoalType::Movies,
)?;
// Reads that can fail happen before the write, so an error never follows a commit.
let current_count = deps.stats.count_reviews_in_year(&user_id, cmd.year).await?;
let event = DomainEvent::GoalCreated {
goal_id: g.id().clone(),
user_id: user_id.clone(),
@@ -37,8 +39,6 @@ pub async fn execute(
};
deps.goal_command.save(&g, &[event]).await?;
let current_count = deps.stats.count_reviews_in_year(&user_id, cmd.year).await?;
Ok(GoalWithProgress {
goal: g,
current_count,

View File

@@ -17,6 +17,8 @@ pub async fn execute(
.ok_or_else(|| DomainError::NotFound(format!("Goal for year {}", cmd.year)))?;
g.update_target(cmd.target_count)?;
// Reads that can fail happen before the write, so an error never follows a commit.
let current_count = deps.stats.count_reviews_in_year(&user_id, cmd.year).await?;
let event = DomainEvent::GoalUpdated {
goal_id: g.id().clone(),
user_id: user_id.clone(),
@@ -25,8 +27,6 @@ pub async fn execute(
};
deps.goal_command.update(&g, &[event]).await?;
let current_count = deps.stats.count_reviews_in_year(&user_id, cmd.year).await?;
Ok(GoalWithProgress {
goal: g,
current_count,

View File

@@ -50,7 +50,11 @@ impl GoalCommand for InMemoryStore {
_user_id: &UserId,
events: &[DomainEvent],
) -> Result<(), DomainError> {
self.goals.write().unwrap().remove(&id.value());
self.goals
.write()
.unwrap()
.remove(&id.value())
.ok_or_else(|| DomainError::NotFound("Goal not found".into()))?;
self.record_events(events);
Ok(())
}