#[cfg(test)]
mod tests {
use super::*;
use crate::models::{ConfigError, EnvConfig, LoadingParam};
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Deserialize, Serialize, Debug, PartialEq)]
struct TestConfig {
database_url: String,
port: u16,
debug: bool,
}
#[test]
fn test_invalid_loading_param_both_none() {
info!("Starting test: test_invalid_loading_param_both_none");
let param = LoadingParam {
file: None,
env_prefix: None,
};
let result = crate::loading::load_config_with_param::<TestConfig>(¶m);
debug!("Result of load_config_with_param: {:?}", result);
assert!(matches!(result, Err(ConfigError::InvalidLoadingParam)));
info!("Completed test: test_invalid_loading_param_both_none successfully");
}
#[test]
fn test_invalid_env_config_prefix_contains_separator() {
info!("Starting test: test_invalid_env_config_prefix_contains_separator");
let param = LoadingParam {
file: None,
env_prefix: Some(EnvConfig::new(
"TEST_CONFIG".to_string(),
Some("_".to_string()),
)),
};
let result = crate::loading::load_config_with_param::<TestConfig>(¶m);
debug!("Result of load_config_with_param: {:?}", result);
assert!(matches!(result, Err(ConfigError::InvalidEnvConfig { .. })));
info!("Completed test: test_invalid_env_config_prefix_contains_separator successfully");
}
#[test]
fn test_valid_env_config() {
info!("Starting test: test_valid_env_config");
let param = LoadingParam {
file: None,
env_prefix: Some(EnvConfig::new("TEST".to_string(), Some("_".to_string()))),
};
let result = crate::loading::validate_loading_params(¶m);
debug!("Result of validate_loading_params: {:?}", result);
assert!(result.is_ok());
info!("Completed test: test_valid_env_config successfully");
}
#[test]
fn test_env_config_default_separator() {
info!("Starting test: test_env_config_default_separator");
let env_config = EnvConfig::new("TEST".to_string(), None);
debug!("Created EnvConfig with default separator");
assert_eq!(env_config.get_separator(), "__");
info!("Completed test: test_env_config_default_separator successfully");
}
#[test]
fn test_env_config_custom_separator() {
info!("Starting test: test_env_config_custom_separator");
let env_config = EnvConfig::new("TEST".to_string(), Some("-".to_string()));
debug!("Created EnvConfig with custom separator: -");
assert_eq!(env_config.get_separator(), "-");
info!("Completed test: test_env_config_custom_separator successfully");
}
#[test]
fn test_no_env_vars_with_prefix() {
info!("Starting test: test_no_env_vars_with_prefix");
let unique_prefix = "NONEXISTENT_PREFIX_12345";
let env_vars_with_prefix: Vec<String> = std::env::vars()
.filter(|(key, _)| key.starts_with(unique_prefix))
.map(|(key, _)| key)
.collect();
assert!(
env_vars_with_prefix.is_empty(),
"Test environment has unexpected variables with prefix {}",
unique_prefix
);
let param = LoadingParam {
file: None,
env_prefix: Some(EnvConfig::new(unique_prefix.to_string(), None)),
};
let result = crate::loading::load_config_with_param::<TestConfig>(¶m);
debug!("Result of load_config_with_param: {:?}", result);
match result {
Err(ConfigError::Config(_)) => {
info!("Got expected config error due to missing fields");
}
Err(e) => {
panic!("Unexpected error type: {:?}", e);
}
Ok(_) => {
panic!("Expected an error due to missing configuration fields");
}
}
info!("Completed test: test_no_env_vars_with_prefix successfully");
}
}