test(app): add unit tests for app
This commit is contained in:
@@ -1,19 +1,26 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[cfg(test)]
|
||||
use mockall::automock;
|
||||
|
||||
#[cfg_attr(test, automock)]
|
||||
pub trait ForSendingNotification {
|
||||
fn send_notification(&self, message: &str) -> Result<(), Box<dyn std::error::Error>>;
|
||||
}
|
||||
|
||||
#[cfg_attr(test, automock)]
|
||||
pub trait ForMonitoringSystem {
|
||||
fn check_thresholds(&self) -> Vec<Box<dyn ForGettingViolationData>>;
|
||||
fn get_metrics(&self) -> HashMap<String, f32>;
|
||||
}
|
||||
|
||||
#[cfg_attr(test, automock)]
|
||||
pub trait ForFormattingMessage {
|
||||
fn format_violation(&self, violation: Box<dyn ForGettingViolationData>) -> String;
|
||||
fn format_summary(&self, metrics: &HashMap<String, f32>) -> String;
|
||||
}
|
||||
|
||||
#[cfg_attr(test, automock)]
|
||||
pub trait ForGettingViolationData {
|
||||
fn is_critical(&self) -> bool;
|
||||
fn get_metric_name(&self) -> String;
|
||||
|
||||
@@ -40,3 +40,104 @@ impl Service {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::app::ports::driven::{MockForFormattingMessage, MockForMonitoringSystem, MockForSendingNotification};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_alert_on_threshold_violation() {
|
||||
let mut notifier = MockForSendingNotification::new();
|
||||
let mut monitor = MockForMonitoringSystem::new();
|
||||
let mut formatter = MockForFormattingMessage::new();
|
||||
|
||||
// Set up expectations for the mocks
|
||||
notifier.expect_send_notification().returning(|_| Ok(()));
|
||||
monitor.expect_check_thresholds().returning(|| vec![]);
|
||||
monitor.expect_get_metrics().returning(|| HashMap::new());
|
||||
formatter.expect_format_violation().returning(|_| "".to_string());
|
||||
formatter.expect_format_summary().returning(|_| "".to_string());
|
||||
|
||||
let service = new(
|
||||
Box::new(notifier),
|
||||
Box::new(monitor),
|
||||
Box::new(formatter),
|
||||
);
|
||||
|
||||
assert!(service.alert_on_threshold_violation().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_all_metrics_notification() {
|
||||
let mut notifier = MockForSendingNotification::new();
|
||||
let mut monitor = MockForMonitoringSystem::new();
|
||||
let mut formatter = MockForFormattingMessage::new();
|
||||
|
||||
// Set up expectations for the mocks
|
||||
notifier.expect_send_notification().returning(|_| Ok(()));
|
||||
monitor.expect_check_thresholds().returning(|| vec![]);
|
||||
monitor.expect_get_metrics().returning(|| HashMap::new());
|
||||
formatter.expect_format_violation().returning(|_| "".to_string());
|
||||
formatter.expect_format_summary().returning(|_| "".to_string());
|
||||
|
||||
let service = new(
|
||||
Box::new(notifier),
|
||||
Box::new(monitor),
|
||||
Box::new(formatter),
|
||||
);
|
||||
|
||||
assert!(service.send_all_metrics_notification().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_alert_on_threshold_violation_handles_send_error() {
|
||||
let mut notifier = MockForSendingNotification::new();
|
||||
let mut monitor = MockForMonitoringSystem::new();
|
||||
let mut formatter = MockForFormattingMessage::new();
|
||||
|
||||
// Mock a single violation to trigger notification
|
||||
monitor.expect_check_thresholds().returning(|| {
|
||||
vec![Box::new(crate::app::ports::driven::MockForGettingViolationData::new())]
|
||||
});
|
||||
formatter.expect_format_violation().returning(|_| "Test violation".to_string());
|
||||
|
||||
// Mock notification failure
|
||||
notifier.expect_send_notification()
|
||||
.returning(|_| Err("Network error".into()));
|
||||
|
||||
let service = new(
|
||||
Box::new(notifier),
|
||||
Box::new(monitor),
|
||||
Box::new(formatter),
|
||||
);
|
||||
|
||||
assert!(service.alert_on_threshold_violation().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_all_metrics_notification_handles_send_error() {
|
||||
let mut notifier = MockForSendingNotification::new();
|
||||
let mut monitor = MockForMonitoringSystem::new();
|
||||
let mut formatter = MockForFormattingMessage::new();
|
||||
|
||||
monitor.expect_get_metrics().returning(|| HashMap::new());
|
||||
formatter.expect_format_summary().returning(|_| "Test summary".to_string());
|
||||
|
||||
// Mock notification failure
|
||||
notifier.expect_send_notification()
|
||||
.returning(|_| Err("Network error".into()));
|
||||
|
||||
let service = new(
|
||||
Box::new(notifier),
|
||||
Box::new(monitor),
|
||||
Box::new(formatter),
|
||||
);
|
||||
|
||||
assert!(service.send_all_metrics_notification().is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user