test: move application tests to tests/

test_support/test_helpers -> tests/support; watched_at unified w/ domain test_watched_at (2024-03-15).
Kept inline: users::diary_filter tests (feed_sort_to_direction_all_variants, paged_user_filter_builds_correctly) - pub(super) helpers in private module.
Dropped stale never-compiled users/tests/get_users.rs (old execute signature).
This commit is contained in:
2026-10-25 22:36:52 +01:00
parent a2c713c1fa
commit 8526b77f18
175 changed files with 541 additions and 695 deletions

View 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());
}

View 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());
}

View 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, NoopEventPublisher};
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>,
events: &Arc<NoopEventPublisher>,
) -> GenerateWrapUpDeps {
GenerateWrapUpDeps {
wrapup_repo: Arc::clone(repo) as _,
event_publisher: Arc::clone(events) as _,
}
}
fn past_cmd() -> RequestWrapUpCommand {
request_wrap_up_command(Some(Uuid::nil()), 2024)
}
#[tokio::test]
async fn creates_pending_record_and_emits_event() {
let repo = InMemoryWrapUpRepository::new();
let events = NoopEventPublisher::new();
let id = generate::execute(&deps(&repo, &events), 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 published = events.published();
assert!(
published
.iter()
.any(|e| matches!(e, DomainEvent::WrapUpRequested { .. }))
);
}
#[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 events = NoopEventPublisher::new();
let id = generate::execute(&deps(&repo, &events), past_cmd())
.await
.unwrap();
assert_eq!(id, existing_id);
assert_eq!(repo.store.lock().unwrap().len(), 1);
}
#[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 events = NoopEventPublisher::new();
let id = generate::execute(&deps(&repo, &events), 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 events = NoopEventPublisher::new();
let err = generate::execute(&deps(&repo, &events), request_wrap_up_command(None, 2030))
.await
.unwrap_err();
assert!(err.to_string().contains("future"));
}

View File

@@ -0,0 +1,148 @@
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:?}"
);
}

View 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());
}

View File

@@ -0,0 +1,135 @@
use chrono::{NaiveDate, Utc};
use domain::models::wrapup::{WrapUpRecord, WrapUpStatus};
use domain::ports::WrapUpRepository;
use domain::testing::{InMemoryWrapUpRepository, InMemoryWrapUpStatsQuery, NoopEventPublisher};
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(),
event_publisher: NoopEventPublisher::new(),
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 events = NoopEventPublisher::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(),
event_publisher: events.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());
// Verify event was published
let published = events.published();
assert!(
published
.iter()
.any(|e| matches!(e, domain::events::DomainEvent::WrapUpCompleted { .. }))
);
}
#[tokio::test]
async fn skips_if_already_generating() {
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(),
event_publisher: NoopEventPublisher::new(),
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_ok());
// Status should still be Generating (not changed to Ready)
let final_rec = repo.get_by_id(&wrapup_id).await.unwrap().unwrap();
assert_eq!(final_rec.status, WrapUpStatus::Generating);
}

View 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);
}