From 1206244e30a49b63d84742d110e3001c4243e6a2 Mon Sep 17 00:00:00 2001 From: Marc Plano-Lesay Date: Sat, 3 May 2025 15:49:08 +1000 Subject: [PATCH] Add tests for Notification::should_notify() --- src/notifications/notification.rs | 177 ++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) diff --git a/src/notifications/notification.rs b/src/notifications/notification.rs index 4f82af9..dfe0e2c 100644 --- a/src/notifications/notification.rs +++ b/src/notifications/notification.rs @@ -37,3 +37,180 @@ pub trait Notification { true } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::models::Magnet; + use chrono::NaiveDateTime; + + struct FakeNotification; + + #[async_trait] + impl Notification for FakeNotification { + fn name() -> &'static str { + "FakeNotificationService" + } + + fn get_name(&self) -> &'static str { + Self::name() + } + + async fn send_notification( + &self, + _action_results: &HashMap, + _total_new_links: usize, + ) -> Result<()> { + Ok(()) + } + } + + fn create_fake_magnet() -> Magnet { + Magnet { + id: None, + title: "foo".to_string(), + submitter: "bar".to_string(), + subreddit: "baz".to_string(), + link: "magnet:abcd".to_string(), + published_at: NaiveDateTime::default(), + imdb_id: None, + } + } + + #[test] + fn should_not_notify_when_no_new_links_and_action_results_empty() { + let service = FakeNotification; + let action_results: HashMap = HashMap::new(); + let total_new_links = 0; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(!should_notify); + } + + #[test] + fn should_not_notify_when_no_new_links_and_all_processed_magnets_empty() { + let service = FakeNotification; + let mut action_results = HashMap::new(); + action_results.insert( + "action1".to_string(), + ProcessedMagnets { + success: vec![], + failed: vec![], + }, + ); + action_results.insert( + "action2".to_string(), + ProcessedMagnets { + success: vec![], + failed: vec![], + }, + ); + let total_new_links = 0; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(!should_notify); + } + + #[test] + fn should_notify_when_there_are_new_links() { + let service = FakeNotification; + let action_results: HashMap = HashMap::new(); + let total_new_links = 1; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(should_notify); + } + + #[test] + fn should_notify_when_there_are_failed_links() { + let service = FakeNotification; + let mut action_results = HashMap::new(); + action_results.insert( + "action1".to_string(), + ProcessedMagnets { + success: vec![], + failed: vec![create_fake_magnet()], + }, + ); + let total_new_links = 0; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(should_notify); + } + + #[test] + fn should_notify_when_there_are_successful_links() { + let service = FakeNotification; + let mut action_results = HashMap::new(); + action_results.insert( + "action1".to_string(), + ProcessedMagnets { + success: vec![create_fake_magnet()], + failed: vec![], + }, + ); + let total_new_links = 0; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(should_notify); + } + + #[test] + fn should_notify_when_there_are_both_successful_and_failed_links_even_with_no_new_links() { + let service = FakeNotification; + let mut action_results = HashMap::new(); + action_results.insert( + "action1".to_string(), + ProcessedMagnets { + success: vec![create_fake_magnet()], + failed: vec![create_fake_magnet()], + }, + ); + let total_new_links = 0; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(should_notify); + } + + #[test] + fn should_notify_when_some_actions_have_results_and_there_are_new_links() { + let service = FakeNotification; + let mut action_results = HashMap::new(); + action_results.insert( + "action1".to_string(), + ProcessedMagnets { + success: vec![create_fake_magnet()], + failed: vec![], + }, + ); + let total_new_links = 2; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(should_notify); + } + + #[test] + fn should_notify_when_some_actions_have_failures_and_there_are_no_new_links() { + let service = FakeNotification; + let mut action_results = HashMap::new(); + action_results.insert( + "action1".to_string(), + ProcessedMagnets { + success: vec![], + failed: vec![create_fake_magnet()], + }, + ); + let total_new_links = 0; + + let should_notify = service.should_notify(&action_results, total_new_links); + + assert!(should_notify); + } +}