Merge branch 'notify-tests' into 'main'
Add tests for Notification::should_notify() See merge request kernald/reddit-magnet!23
This commit is contained in:
commit
4f960b12b9
1 changed files with 177 additions and 0 deletions
|
|
@ -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<String, ProcessedMagnets>,
|
||||
_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<String, ProcessedMagnets> = 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<String, ProcessedMagnets> = 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue