use std::env;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_config_file_creation() {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("config.toml");
let config_content = r#"
database_url = "postgres://localhost/test"
default_queue = "test_queue"
default_limit = 100
log_level = "info"
connection_pool_size = 5
"#;
fs::write(&config_path, config_content).unwrap();
let read_content = fs::read_to_string(&config_path).unwrap();
assert!(read_content.contains("database_url"));
assert!(read_content.contains("postgres://localhost/test"));
}
#[test]
fn test_env_var_precedence() {
unsafe {
env::set_var("DATABASE_URL", "postgres://env_override/db");
}
let env_url = env::var("DATABASE_URL").unwrap();
assert_eq!(env_url, "postgres://env_override/db");
unsafe {
env::remove_var("DATABASE_URL");
}
}
#[test]
fn test_validation_functions() {
assert!(is_valid_database_url("postgres://localhost/db"));
assert!(is_valid_database_url("mysql://localhost/db"));
assert!(!is_valid_database_url("invalid_url"));
assert!(!is_valid_database_url(""));
assert!(is_valid_queue_name("valid_queue"));
assert!(is_valid_queue_name("queue-with-dashes"));
assert!(!is_valid_queue_name("queue with spaces"));
assert!(!is_valid_queue_name(""));
assert!(!is_valid_queue_name("queue/with/slashes"));
}
#[test]
fn test_log_level_validation() {
assert!(is_valid_log_level("error"));
assert!(is_valid_log_level("warn"));
assert!(is_valid_log_level("info"));
assert!(is_valid_log_level("debug"));
assert!(is_valid_log_level("trace"));
assert!(!is_valid_log_level("invalid"));
assert!(!is_valid_log_level(""));
}
fn is_valid_database_url(url: &str) -> bool {
!url.is_empty() && (url.starts_with("postgres://") || url.starts_with("mysql://"))
}
fn is_valid_queue_name(name: &str) -> bool {
!name.is_empty()
&& !name.contains(' ')
&& !name.contains('/')
&& name
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
}
fn is_valid_log_level(level: &str) -> bool {
matches!(level, "error" | "warn" | "info" | "debug" | "trace")
}
#[cfg(test)]
mod cli_tests {
#[test]
fn test_cli_structure() {
let expected_commands = vec!["migration", "config"];
for command in expected_commands {
assert!(!command.is_empty());
assert!(command.chars().all(|c| c.is_ascii_alphanumeric()));
}
}
#[test]
fn test_migration_subcommands() {
let migration_commands = vec!["run", "status"];
for command in migration_commands {
assert!(!command.is_empty());
assert!(command.chars().all(|c| c.is_ascii_alphanumeric()));
}
}
#[test]
fn test_config_subcommands() {
let config_commands = vec!["show", "set", "get", "reset", "path"];
for command in config_commands {
assert!(!command.is_empty());
assert!(command.chars().all(|c| c.is_ascii_alphanumeric()));
}
}
}
#[cfg(test)]
mod database_tests {
use super::*;
#[test]
fn test_database_url_parsing() {
let pg_url = "postgres://user:pass@localhost:5432/dbname";
assert!(is_valid_database_url(pg_url));
let mysql_url = "mysql://user:pass@localhost:3306/dbname";
assert!(is_valid_database_url(mysql_url));
assert!(!is_valid_database_url("http://localhost/db"));
assert!(!is_valid_database_url("invalid"));
}
#[test]
fn test_connection_pool_size_validation() {
assert!(is_valid_pool_size(1));
assert!(is_valid_pool_size(5));
assert!(is_valid_pool_size(20));
assert!(!is_valid_pool_size(0));
assert!(!is_valid_pool_size(101)); }
}
fn is_valid_pool_size(size: u32) -> bool {
size > 0 && size <= 100
}