sal/src/redisclient/tests.rs
2025-04-02 08:38:40 +02:00

126 lines
3.7 KiB
Rust

use super::*;
use std::env;
use redis::RedisResult;
#[cfg(test)]
mod redis_client_tests {
use super::*;
#[test]
fn test_env_vars() {
// Save original REDISDB value to restore later
let original_redisdb = env::var("REDISDB").ok();
// Set test environment variables
env::set_var("REDISDB", "5");
// Test with invalid value
env::set_var("REDISDB", "invalid");
// Test with unset value
env::remove_var("REDISDB");
// Restore original REDISDB value
if let Some(redisdb) = original_redisdb {
env::set_var("REDISDB", redisdb);
} else {
env::remove_var("REDISDB");
}
}
#[test]
fn test_redis_client_creation_mock() {
// This is a simplified test that doesn't require an actual Redis server
// It just verifies that the function handles environment variables correctly
// Save original HOME value to restore later
let original_home = env::var("HOME").ok();
// Set HOME to a test value
env::set_var("HOME", "/tmp");
// The actual client creation would be tested in integration tests
// with a real Redis server or a mock
// Restore original HOME value
if let Some(home) = original_home {
env::set_var("HOME", home);
} else {
env::remove_var("HOME");
}
}
#[test]
fn test_reset_mock() {
// This is a simplified test that doesn't require an actual Redis server
// In a real test, we would need to mock the Redis client
// Just verify that the reset function doesn't panic
// This is a minimal test - in a real scenario, we would use mocking
// to verify that the client is properly reset
if let Err(_) = reset() {
// If Redis is not available, this is expected to fail
// So we don't assert anything here
}
}
}
// Integration tests that require a real Redis server
// These tests will be skipped if Redis is not available
#[cfg(test)]
mod redis_integration_tests {
use super::*;
// Helper function to check if Redis is available
fn is_redis_available() -> bool {
match get_redis_client() {
Ok(_) => true,
Err(_) => false,
}
}
#[test]
fn test_redis_client_integration() {
if !is_redis_available() {
println!("Skipping Redis integration tests - Redis server not available");
return;
}
println!("Running Redis integration tests...");
// Test basic operations
test_basic_redis_operations();
}
fn test_basic_redis_operations() {
if !is_redis_available() {
return;
}
// Test setting and getting values
let client_result = get_redis_client();
if client_result.is_err() {
// Skip the test if we can't connect to Redis
return;
}
// Create SET command
let mut set_cmd = redis::cmd("SET");
set_cmd.arg("test_key").arg("test_value");
// Execute SET command
let set_result: RedisResult<()> = execute(&mut set_cmd);
assert!(set_result.is_ok());
// Create GET command
let mut get_cmd = redis::cmd("GET");
get_cmd.arg("test_key");
// Execute GET command and check the result
if let Ok(value) = execute::<String>(&mut get_cmd) {
assert_eq!(value, "test_value");
}
let _: RedisResult<()> = execute(&mut redis::cmd("DEL").arg("test_key"));
}
}