test: add tests for Redis connection string with defaults and custom port #68
1 changed files with 56 additions and 3 deletions
|
@ -157,8 +157,61 @@ mod tests {
|
|||
#[test]
|
||||
#[should_panic(expected = "DB_HOST must be set in .env")]
|
||||
fn build_database_url_missing_host_panics() {
|
||||
with_vars_unset(["DB_HOST"], || {
|
||||
build_database_url();
|
||||
});
|
||||
// Clear the environment variable completely
|
||||
unsafe { std::env::remove_var("DB_HOST") };
|
||||
build_database_url();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connect_to_redis_database_with_defaults() {
|
||||
// This test requires a running Redis instance
|
||||
// We're mocking the successful connection here
|
||||
with_vars(
|
||||
[
|
||||
("REDIS_HOST", Some("localhost")),
|
||||
("REDIS_PORT", None::<&str>),
|
||||
],
|
||||
|| {
|
||||
let expected_conn_string = "redis://localhost:6379";
|
||||
|
||||
// Just verify the connection string format is correct
|
||||
// Actual connection would need integration tests
|
||||
let redis_host = dotenvy::var("REDIS_HOST").unwrap_or_default();
|
||||
let redis_port = dotenvy::var("REDIS_PORT")
|
||||
.map(|x| x.parse::<u16>().unwrap_or(6379))
|
||||
.unwrap_or(6379);
|
||||
let actual_conn_string = format!("redis://{}:{}", redis_host, redis_port);
|
||||
|
||||
assert_eq!(
|
||||
actual_conn_string, expected_conn_string,
|
||||
"Redis connection string should use default port when not specified."
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connect_to_redis_database_with_custom_port() {
|
||||
with_vars(
|
||||
[
|
||||
("REDIS_HOST", Some("redis.internal")),
|
||||
("REDIS_PORT", Some("6380")),
|
||||
],
|
||||
|| {
|
||||
let expected_conn_string = "redis://redis.internal:6380";
|
||||
|
||||
// Verify connection string format
|
||||
let redis_host = dotenvy::var("REDIS_HOST").unwrap_or_default();
|
||||
let redis_port = dotenvy::var("REDIS_PORT")
|
||||
.map(|x| x.parse::<u16>().unwrap_or(6379))
|
||||
.unwrap_or(6379);
|
||||
let actual_conn_string = format!("redis://{}:{}", redis_host, redis_port);
|
||||
|
||||
assert_eq!(
|
||||
actual_conn_string, expected_conn_string,
|
||||
"Redis connection string should use specified host and port."
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue