Files
rustytorch/crates/training/rtx-automeasure/tests/error_tests.rs
T
2026-03-04 00:08:42 +00:00

110 lines
4.2 KiB
Rust

use rtx_automeasure::error::{AutoMLError, AutoMLResult};
#[tokio::test]
async fn test_automl_error_creation() {
let config_error = AutoMLError::ConfigurationError("Invalid configuration".to_string());
assert!(matches!(config_error, AutoMLError::ConfigurationError(_)));
let opt_error = AutoMLError::OptimizationError("Optimization failed".to_string());
assert!(matches!(opt_error, AutoMLError::OptimizationError(_)));
}
#[tokio::test]
async fn test_automl_error_display() {
let error = AutoMLError::ResourceError("GPU memory insufficient".to_string());
let display_str = format!("{}", error);
assert!(display_str.contains("GPU memory insufficient"));
}
#[tokio::test]
async fn test_automl_error_from_validation_error() {
// Test conversion from rtx-validation error
let validation_error = rtx_validation::ValidationError::InvalidCV {
message: "Bad input".to_string(),
};
let automl_error: AutoMLError = validation_error.into();
assert!(matches!(automl_error, AutoMLError::ValidationError(_)));
}
#[tokio::test]
async fn test_automl_result_ok() {
let result: AutoMLResult<i32> = Ok(42);
assert!(result.is_ok());
assert_eq!(result.unwrap(), 42);
}
#[tokio::test]
async fn test_automl_result_err() {
let result: AutoMLResult<i32> = Err(AutoMLError::TimeoutError("Timeout occurred".to_string()));
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), AutoMLError::TimeoutError(_)));
}
#[tokio::test]
async fn test_automl_error_chain() {
let inner_error = AutoMLError::ModelError("Inner model error".to_string());
let outer_error = AutoMLError::PipelineError(format!("Pipeline failed: {}", inner_error));
let error_str = format!("{}", outer_error);
assert!(error_str.contains("Pipeline failed"));
assert!(error_str.contains("Inner model error"));
}
#[tokio::test]
async fn test_automl_error_serialization() {
let error = AutoMLError::ConfigurationError("Serialization test".to_string());
let serialized = serde_json::to_string(&error).expect("Failed to serialize");
let deserialized: AutoMLError =
serde_json::from_str(&serialized).expect("Failed to deserialize");
assert!(matches!(deserialized, AutoMLError::ConfigurationError(_)));
}
#[tokio::test]
async fn test_automl_error_from_tensor_error() {
// Test conversion from rtx-tensor error
let tensor_error = rtx_tensor::TensorError::Shape {
message: "expected shape mismatch".to_string(),
};
let automl_error: AutoMLError = tensor_error.into();
assert!(matches!(automl_error, AutoMLError::ModelError(_)));
}
#[tokio::test]
async fn test_automl_error_from_io_error() {
// Test conversion from std::io::Error
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
let automl_error: AutoMLError = io_error.into();
assert!(matches!(automl_error, AutoMLError::IoError(_)));
}
#[tokio::test]
async fn test_all_error_variants() {
let errors = vec![
AutoMLError::ConfigurationError("config".to_string()),
AutoMLError::OptimizationError("opt".to_string()),
AutoMLError::ResourceError("resource".to_string()),
AutoMLError::ModelError("model".to_string()),
AutoMLError::ValidationError("validation".to_string()),
AutoMLError::PreprocessingError("preprocess".to_string()),
AutoMLError::PipelineError("pipeline".to_string()),
AutoMLError::TimeoutError("timeout".to_string()),
AutoMLError::SerializationError("serialization".to_string()),
AutoMLError::IoError("io".to_string()),
AutoMLError::MetaLearningError("meta".to_string()),
AutoMLError::TransferLearningError("transfer".to_string()),
AutoMLError::EnsembleError("ensemble".to_string()),
AutoMLError::MultiFidelityError("multifidelity".to_string()),
AutoMLError::EarlyStoppingError("earlystop".to_string()),
AutoMLError::MonitoringError("monitoring".to_string()),
AutoMLError::GpuError("gpu".to_string()),
AutoMLError::ExternalError("external".to_string()),
];
// All errors should display their messages
for error in errors {
let display = format!("{}", error);
assert!(!display.is_empty());
}
}