Reviewed-on: #26
This commit was merged in pull request #26.
This commit is contained in:
155
crates/application/tests/wrapup/compute_test.rs
Normal file
155
crates/application/tests/wrapup/compute_test.rs
Normal file
@@ -0,0 +1,155 @@
|
||||
use chrono::NaiveDate;
|
||||
use domain::models::WrapUpMovieRow;
|
||||
use domain::models::wrapup::{DateRange, WrapUpScope};
|
||||
use domain::testing::InMemoryWrapUpStatsQuery;
|
||||
use uuid::Uuid;
|
||||
|
||||
use application::wrapup::queries::ComputeWrapUpQuery;
|
||||
|
||||
fn make_row(title: &str, rating: u8, watched_at: &str) -> WrapUpMovieRow {
|
||||
WrapUpMovieRow {
|
||||
movie_id: Uuid::new_v4(),
|
||||
title: title.to_string(),
|
||||
release_year: 2024,
|
||||
director: Some("Director".to_string()),
|
||||
poster_path: None,
|
||||
rating,
|
||||
watched_at: chrono::NaiveDateTime::parse_from_str(
|
||||
&format!("{watched_at} 20:00:00"),
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
.unwrap(),
|
||||
user_id: Uuid::new_v4(),
|
||||
runtime_minutes: Some(120),
|
||||
budget_usd: Some(50_000_000),
|
||||
original_language: Some("en".to_string()),
|
||||
genres: vec!["Action".to_string()],
|
||||
keywords: vec!["heist".to_string()],
|
||||
cast_names: vec![("Actor A".to_string(), 1, 12345)],
|
||||
cast_profile_paths: vec![None],
|
||||
watch_medium: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn year_2024_range() -> DateRange {
|
||||
DateRange::new(
|
||||
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn empty_report() {
|
||||
let stats = InMemoryWrapUpStatsQuery::new();
|
||||
let user_id = Uuid::new_v4();
|
||||
|
||||
let report = application::wrapup::compute::execute(
|
||||
stats,
|
||||
ComputeWrapUpQuery {
|
||||
scope: WrapUpScope::User(user_id),
|
||||
date_range: year_2024_range(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(report.total_movies, 0);
|
||||
assert!(report.avg_rating.is_none());
|
||||
assert_eq!(report.rating_distribution, [0; 5]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn basic_stats() {
|
||||
let user_id = Uuid::new_v4();
|
||||
let mut r1 = make_row("Film A", 4, "2024-03-10");
|
||||
r1.user_id = user_id;
|
||||
r1.runtime_minutes = Some(120);
|
||||
r1.genres = vec!["Action".to_string()];
|
||||
|
||||
let mut r2 = make_row("Film B", 2, "2024-03-20");
|
||||
r2.user_id = user_id;
|
||||
r2.runtime_minutes = Some(90);
|
||||
r2.genres = vec!["Comedy".to_string()];
|
||||
|
||||
let stats = InMemoryWrapUpStatsQuery::with_rows(vec![r1, r2]);
|
||||
|
||||
let report = application::wrapup::compute::execute(
|
||||
stats,
|
||||
ComputeWrapUpQuery {
|
||||
scope: WrapUpScope::User(user_id),
|
||||
date_range: year_2024_range(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(report.total_movies, 2);
|
||||
assert_eq!(report.total_watch_time_minutes, 210);
|
||||
assert!((report.avg_rating.unwrap() - 3.0).abs() < f64::EPSILON);
|
||||
assert_eq!(report.rating_distribution, [0, 1, 0, 1, 0]);
|
||||
assert_eq!(report.busiest_month.as_deref(), Some("March 2024"));
|
||||
assert_eq!(report.director_diversity, 1);
|
||||
assert_eq!(report.genre_diversity, 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rewatch_detection() {
|
||||
let user_id = Uuid::new_v4();
|
||||
let movie_id = Uuid::new_v4();
|
||||
|
||||
let mut r1 = make_row("Film A", 3, "2024-02-01");
|
||||
r1.user_id = user_id;
|
||||
r1.movie_id = movie_id;
|
||||
|
||||
let mut r2 = make_row("Film A", 5, "2024-06-01");
|
||||
r2.user_id = user_id;
|
||||
r2.movie_id = movie_id;
|
||||
|
||||
let stats = InMemoryWrapUpStatsQuery::with_rows(vec![r1, r2]);
|
||||
|
||||
let report = application::wrapup::compute::execute(
|
||||
stats,
|
||||
ComputeWrapUpQuery {
|
||||
scope: WrapUpScope::User(user_id),
|
||||
date_range: year_2024_range(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(report.total_rewatches, 1);
|
||||
assert_eq!(
|
||||
report.most_rewatched_movie.as_ref().unwrap().title,
|
||||
"Film A"
|
||||
);
|
||||
assert!((report.avg_rating_change_on_rewatch.unwrap() - 2.0).abs() < f64::EPSILON);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn global_scope() {
|
||||
let user_a = Uuid::new_v4();
|
||||
let user_b = Uuid::new_v4();
|
||||
|
||||
let mut r1 = make_row("Film X", 4, "2024-05-01");
|
||||
r1.user_id = user_a;
|
||||
|
||||
let mut r2 = make_row("Film Y", 3, "2024-07-01");
|
||||
r2.user_id = user_b;
|
||||
|
||||
let stats = InMemoryWrapUpStatsQuery::with_rows(vec![r1, r2]);
|
||||
|
||||
let report = application::wrapup::compute::execute(
|
||||
stats,
|
||||
ComputeWrapUpQuery {
|
||||
scope: WrapUpScope::Global,
|
||||
date_range: year_2024_range(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(report.total_movies, 2);
|
||||
assert_eq!(report.total_users_active, Some(2));
|
||||
assert!(report.most_active_user.is_some());
|
||||
}
|
||||
40
crates/application/tests/wrapup/delete_test.rs
Normal file
40
crates/application/tests/wrapup/delete_test.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use chrono::NaiveDate;
|
||||
use domain::models::wrapup::{WrapUpRecord, WrapUpStatus};
|
||||
use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use application::wrapup::delete;
|
||||
use application::wrapup::deps::DeleteWrapUpDeps;
|
||||
|
||||
#[tokio::test]
|
||||
async fn deletes_existing_wrapup() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let id = WrapUpId::generate();
|
||||
repo.store.lock().unwrap().push(WrapUpRecord {
|
||||
id: id.clone(),
|
||||
user_id: None,
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
|
||||
status: WrapUpStatus::Ready,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
});
|
||||
|
||||
let deps = DeleteWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
delete::execute(&deps, id).await.unwrap();
|
||||
assert_eq!(repo.store.lock().unwrap().len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_when_not_found() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let deps = DeleteWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = delete::execute(&deps, WrapUpId::generate()).await;
|
||||
assert!(result.is_err());
|
||||
}
|
||||
110
crates/application/tests/wrapup/generate_test.rs
Normal file
110
crates/application/tests/wrapup/generate_test.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
use crate::support::request_wrap_up_command;
|
||||
use chrono::NaiveDate;
|
||||
use domain::events::DomainEvent;
|
||||
use domain::models::wrapup::{WrapUpRecord, WrapUpStatus};
|
||||
use domain::testing::{InMemoryWrapUpRepository, RefusingWrapUpWrites};
|
||||
use domain::value_objects::WrapUpId;
|
||||
use uuid::Uuid;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use application::wrapup::deps::GenerateWrapUpDeps;
|
||||
use application::wrapup::{commands::RequestWrapUpCommand, generate};
|
||||
|
||||
fn deps(repo: &Arc<InMemoryWrapUpRepository>) -> GenerateWrapUpDeps {
|
||||
GenerateWrapUpDeps {
|
||||
wrapup_repo: Arc::clone(repo) as _,
|
||||
}
|
||||
}
|
||||
|
||||
fn past_cmd() -> RequestWrapUpCommand {
|
||||
request_wrap_up_command(Some(Uuid::nil()), 2024)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn creates_pending_record_and_records_its_event_together() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
|
||||
let id = generate::execute(&deps(&repo), past_cmd()).await.unwrap();
|
||||
|
||||
let stored = repo.store.lock().unwrap();
|
||||
assert_eq!(stored.len(), 1);
|
||||
assert_eq!(stored[0].id, id);
|
||||
assert_eq!(stored[0].status, WrapUpStatus::Pending);
|
||||
|
||||
let recorded = repo.recorded_events();
|
||||
assert_eq!(recorded.len(), 1);
|
||||
assert!(matches!(
|
||||
&recorded[0],
|
||||
DomainEvent::WrapUpRequested { wrapup_id, .. } if *wrapup_id == id
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reuses_existing_ready_wrapup() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let existing_id = WrapUpId::generate();
|
||||
repo.store.lock().unwrap().push(WrapUpRecord {
|
||||
id: existing_id.clone(),
|
||||
user_id: Some(Uuid::nil()),
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
|
||||
status: WrapUpStatus::Ready,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
});
|
||||
|
||||
let id = generate::execute(&deps(&repo), past_cmd()).await.unwrap();
|
||||
assert_eq!(id, existing_id);
|
||||
assert_eq!(repo.store.lock().unwrap().len(), 1);
|
||||
assert!(repo.recorded_events().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn replaces_failed_wrapup() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
repo.store.lock().unwrap().push(WrapUpRecord {
|
||||
id: WrapUpId::generate(),
|
||||
user_id: Some(Uuid::nil()),
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
|
||||
status: WrapUpStatus::Failed,
|
||||
report: None,
|
||||
error_message: Some("boom".into()),
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
});
|
||||
|
||||
let id = generate::execute(&deps(&repo), past_cmd()).await.unwrap();
|
||||
|
||||
let stored = repo.store.lock().unwrap();
|
||||
assert_eq!(stored.len(), 1);
|
||||
assert_eq!(stored[0].id, id);
|
||||
assert_eq!(stored[0].status, WrapUpStatus::Pending);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rejects_future_end_date() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let err = generate::execute(&deps(&repo), request_wrap_up_command(None, 2030))
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(err.to_string().contains("future"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_create_propagates_and_records_nothing() {
|
||||
let inner = InMemoryWrapUpRepository::new();
|
||||
let deps = GenerateWrapUpDeps {
|
||||
wrapup_repo: RefusingWrapUpWrites::new(Arc::clone(&inner)),
|
||||
};
|
||||
|
||||
let result = generate::execute(&deps, past_cmd()).await;
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(inner.store.lock().unwrap().is_empty());
|
||||
assert!(inner.recorded_events().is_empty());
|
||||
}
|
||||
149
crates/application/tests/wrapup/get_ready_report_test.rs
Normal file
149
crates/application/tests/wrapup/get_ready_report_test.rs
Normal file
@@ -0,0 +1,149 @@
|
||||
use chrono::NaiveDate;
|
||||
use uuid::Uuid;
|
||||
|
||||
use domain::errors::DomainError;
|
||||
use domain::models::wrapup::{DateRange, WrapUpRecord, WrapUpScope, WrapUpStatus};
|
||||
use domain::ports::WrapUpRepository;
|
||||
use domain::services::wrapup_analyzer;
|
||||
use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use application::wrapup::deps::GetReadyReportDeps;
|
||||
use application::wrapup::get_ready_report;
|
||||
|
||||
fn year_range(year: i32) -> DateRange {
|
||||
DateRange::new(
|
||||
NaiveDate::from_ymd_opt(year, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn record(
|
||||
user_id: Option<Uuid>,
|
||||
year: i32,
|
||||
status: WrapUpStatus,
|
||||
with_report: bool,
|
||||
) -> WrapUpRecord {
|
||||
let range = year_range(year);
|
||||
let domain_scope = match user_id {
|
||||
Some(u) => WrapUpScope::User(u),
|
||||
None => WrapUpScope::Global,
|
||||
};
|
||||
let report = if with_report {
|
||||
Some(wrapup_analyzer::build_report(
|
||||
domain_scope,
|
||||
range.clone(),
|
||||
&[],
|
||||
))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
WrapUpRecord {
|
||||
id: WrapUpId::generate(),
|
||||
user_id,
|
||||
start_date: range.start(),
|
||||
end_date: range.end(),
|
||||
status,
|
||||
report,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_returns_report_when_ready() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let user_id = Uuid::new_v4();
|
||||
repo.create(&record(Some(user_id), 2024, WrapUpStatus::Ready, true), &[])
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let report = get_ready_report::execute(&deps, WrapUpScope::User(user_id), 2024)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(report.total_movies, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_not_found_when_status_not_ready() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let user_id = Uuid::new_v4();
|
||||
repo.create(
|
||||
&record(Some(user_id), 2024, WrapUpStatus::Generating, false),
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::User(user_id), 2024)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, DomainError::NotFound(_)), "got {err:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_not_found_when_report_absent() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let user_id = Uuid::new_v4();
|
||||
// Status is Ready but `report` is None — an inconsistent-but-representable
|
||||
// state the old handler guarded against explicitly (`match record.report {
|
||||
// Some(r) => ..., None => 404 }`), separate from the status gate above.
|
||||
repo.create(
|
||||
&record(Some(user_id), 2024, WrapUpStatus::Ready, false),
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::User(user_id), 2024)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, DomainError::NotFound(_)), "got {err:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_not_found_when_no_record_exists() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::User(Uuid::new_v4()), 2024)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, DomainError::NotFound(_)), "got {err:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_global_scope_ignores_user_id() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
repo.create(&record(None, 2024, WrapUpStatus::Ready, true), &[])
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
get_ready_report::execute(&deps, WrapUpScope::Global, 2024)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ready_report_is_validation_error_for_unrepresentable_year() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let deps = GetReadyReportDeps { wrapup_repo: repo };
|
||||
|
||||
let err = get_ready_report::execute(&deps, WrapUpScope::Global, 999_999)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(err, DomainError::ValidationError(_)),
|
||||
"got {err:?}"
|
||||
);
|
||||
}
|
||||
43
crates/application/tests/wrapup/get_wrapup_test.rs
Normal file
43
crates/application/tests/wrapup/get_wrapup_test.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use chrono::NaiveDate;
|
||||
use domain::models::wrapup::{WrapUpRecord, WrapUpStatus};
|
||||
use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use application::wrapup::deps::GetWrapUpDeps;
|
||||
use application::wrapup::get_wrapup;
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_record_when_exists() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let id = WrapUpId::generate();
|
||||
repo.store.lock().unwrap().push(WrapUpRecord {
|
||||
id: id.clone(),
|
||||
user_id: None,
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
|
||||
status: WrapUpStatus::Pending,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
});
|
||||
|
||||
let deps = GetWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = get_wrapup::execute(&deps, id).await.unwrap();
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap().status, WrapUpStatus::Pending);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_none_when_missing() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let deps = GetWrapUpDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = get_wrapup::execute(&deps, WrapUpId::generate())
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(result.is_none());
|
||||
}
|
||||
209
crates/application/tests/wrapup/handle_requested_test.rs
Normal file
209
crates/application/tests/wrapup/handle_requested_test.rs
Normal file
@@ -0,0 +1,209 @@
|
||||
use chrono::{NaiveDate, Utc};
|
||||
use domain::models::wrapup::{WrapUpRecord, WrapUpStatus};
|
||||
use domain::ports::WrapUpRepository;
|
||||
use domain::testing::{InMemoryWrapUpRepository, InMemoryWrapUpStatsQuery, RefusingWrapUpWrites};
|
||||
use domain::value_objects::WrapUpId;
|
||||
|
||||
use application::wrapup::{deps::HandleWrapUpRequestedDeps, handle_requested};
|
||||
|
||||
#[tokio::test]
|
||||
async fn skips_if_already_ready() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let wrapup_id = WrapUpId::generate();
|
||||
|
||||
let record = WrapUpRecord {
|
||||
id: wrapup_id.clone(),
|
||||
user_id: None,
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
status: WrapUpStatus::Ready,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
repo.create(&record, &[]).await.unwrap();
|
||||
|
||||
let deps = HandleWrapUpRequestedDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
wrapup_stats: InMemoryWrapUpStatsQuery::new(),
|
||||
};
|
||||
|
||||
let result = handle_requested::execute(
|
||||
&deps,
|
||||
wrapup_id,
|
||||
None,
|
||||
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn generates_wrapup_and_marks_complete() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let stats = InMemoryWrapUpStatsQuery::new();
|
||||
let wrapup_id = WrapUpId::generate();
|
||||
let uid = uuid::Uuid::new_v4();
|
||||
|
||||
let record = WrapUpRecord {
|
||||
id: wrapup_id.clone(),
|
||||
user_id: Some(uid),
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
status: WrapUpStatus::Pending,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
repo.create(&record, &[]).await.unwrap();
|
||||
|
||||
let deps = HandleWrapUpRequestedDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
wrapup_stats: stats.clone(),
|
||||
};
|
||||
|
||||
let result = handle_requested::execute(
|
||||
&deps,
|
||||
wrapup_id.clone(),
|
||||
Some(uid),
|
||||
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Verify it was marked as Ready
|
||||
let final_rec = repo.get_by_id(&wrapup_id).await.unwrap().unwrap();
|
||||
assert_eq!(final_rec.status, WrapUpStatus::Ready);
|
||||
assert!(final_rec.report.is_some());
|
||||
|
||||
let recorded = repo.recorded_events();
|
||||
assert_eq!(recorded.len(), 1);
|
||||
assert!(matches!(
|
||||
&recorded[0],
|
||||
domain::events::DomainEvent::WrapUpCompleted { wrapup_id: id } if *id == wrapup_id
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_wrapup_left_generating_is_completed_on_redelivery() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let wrapup_id = WrapUpId::generate();
|
||||
|
||||
let record = WrapUpRecord {
|
||||
id: wrapup_id.clone(),
|
||||
user_id: None,
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
status: WrapUpStatus::Generating,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
repo.create(&record, &[]).await.unwrap();
|
||||
|
||||
let deps = HandleWrapUpRequestedDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
wrapup_stats: InMemoryWrapUpStatsQuery::new(),
|
||||
};
|
||||
|
||||
handle_requested::execute(
|
||||
&deps,
|
||||
wrapup_id.clone(),
|
||||
None,
|
||||
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let final_rec = repo.get_by_id(&wrapup_id).await.unwrap().unwrap();
|
||||
assert_eq!(final_rec.status, WrapUpStatus::Ready);
|
||||
assert!(final_rec.report.is_some());
|
||||
assert_eq!(repo.recorded_events().len(), 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wrapup_is_idempotent_when_run_twice() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let wrapup_id = WrapUpId::generate();
|
||||
let record = WrapUpRecord {
|
||||
id: wrapup_id.clone(),
|
||||
user_id: None,
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
status: WrapUpStatus::Pending,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
repo.create(&record, &[]).await.unwrap();
|
||||
let deps = HandleWrapUpRequestedDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
wrapup_stats: InMemoryWrapUpStatsQuery::new(),
|
||||
};
|
||||
|
||||
for _ in 0..2 {
|
||||
handle_requested::execute(
|
||||
&deps,
|
||||
wrapup_id.clone(),
|
||||
None,
|
||||
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let rec = repo.get_by_id(&wrapup_id).await.unwrap().unwrap();
|
||||
assert_eq!(rec.status, WrapUpStatus::Ready);
|
||||
assert_eq!(
|
||||
repo.recorded_events().len(),
|
||||
1,
|
||||
"WrapUpCompleted emitted once"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_completion_propagates_and_records_nothing() {
|
||||
let inner = InMemoryWrapUpRepository::new();
|
||||
let wrapup_id = WrapUpId::generate();
|
||||
let record = WrapUpRecord {
|
||||
id: wrapup_id.clone(),
|
||||
user_id: None,
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
status: WrapUpStatus::Pending,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
};
|
||||
inner.create(&record, &[]).await.unwrap();
|
||||
let deps = HandleWrapUpRequestedDeps {
|
||||
wrapup_repo: RefusingWrapUpWrites::new(inner.clone()),
|
||||
wrapup_stats: InMemoryWrapUpStatsQuery::new(),
|
||||
};
|
||||
|
||||
let result = handle_requested::execute(
|
||||
&deps,
|
||||
wrapup_id.clone(),
|
||||
None,
|
||||
NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
NaiveDate::from_ymd_opt(2024, 12, 31).unwrap(),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_err());
|
||||
let rec = inner.get_by_id(&wrapup_id).await.unwrap().unwrap();
|
||||
assert_ne!(rec.status, WrapUpStatus::Ready);
|
||||
assert!(rec.report.is_none());
|
||||
assert!(inner.recorded_events().is_empty());
|
||||
}
|
||||
62
crates/application/tests/wrapup/list_wrapups_test.rs
Normal file
62
crates/application/tests/wrapup/list_wrapups_test.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use chrono::NaiveDate;
|
||||
use domain::models::wrapup::{WrapUpRecord, WrapUpStatus};
|
||||
use domain::testing::InMemoryWrapUpRepository;
|
||||
use domain::value_objects::WrapUpId;
|
||||
use uuid::Uuid;
|
||||
|
||||
use application::wrapup::deps::ListWrapUpsDeps;
|
||||
use application::wrapup::list_wrapups::{self, ListWrapUpsQuery};
|
||||
|
||||
fn make_record(user_id: Option<Uuid>) -> WrapUpRecord {
|
||||
WrapUpRecord {
|
||||
id: WrapUpId::generate(),
|
||||
user_id,
|
||||
start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
|
||||
end_date: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
|
||||
status: WrapUpStatus::Ready,
|
||||
report: None,
|
||||
error_message: None,
|
||||
created_at: chrono::Utc::now().naive_utc(),
|
||||
completed_at: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn filters_by_user() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
let uid = Uuid::new_v4();
|
||||
{
|
||||
let mut store = repo.store.lock().unwrap();
|
||||
store.push(make_record(Some(uid)));
|
||||
store.push(make_record(Some(Uuid::new_v4())));
|
||||
store.push(make_record(None));
|
||||
}
|
||||
|
||||
let deps = ListWrapUpsDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = list_wrapups::execute(&deps, ListWrapUpsQuery { user_id: Some(uid) })
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(result[0].user_id, Some(uid));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_global_when_no_user() {
|
||||
let repo = InMemoryWrapUpRepository::new();
|
||||
{
|
||||
let mut store = repo.store.lock().unwrap();
|
||||
store.push(make_record(None));
|
||||
store.push(make_record(None));
|
||||
store.push(make_record(Some(Uuid::new_v4())));
|
||||
}
|
||||
|
||||
let deps = ListWrapUpsDeps {
|
||||
wrapup_repo: repo.clone(),
|
||||
};
|
||||
let result = list_wrapups::execute(&deps, ListWrapUpsQuery { user_id: None })
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(result.len(), 2);
|
||||
}
|
||||
Reference in New Issue
Block a user