test: add tests for Redis connection string with defaults and custom port
Some checks failed
ci/woodpecker/push/deployment Pipeline failed
ci/woodpecker/pr/deployment Pipeline failed
ci/woodpecker/pull_request_closed/deployment Pipeline failed
ci/woodpecker/pr/check_fmt Pipeline failed
ci/woodpecker/pr/cargo_check Pipeline was successful
ci/woodpecker/pr/cargo_clippy Pipeline was successful
ci/woodpecker/pr/cargo_test Pipeline failed

This commit is contained in:
Mika 2025-04-09 23:47:55 +02:00
parent 81cd4798da
commit 636f04ab74

View file

@ -157,8 +157,61 @@ mod tests {
#[test] #[test]
#[should_panic(expected = "DB_HOST must be set in .env")] #[should_panic(expected = "DB_HOST must be set in .env")]
fn build_database_url_missing_host_panics() { fn build_database_url_missing_host_panics() {
with_vars_unset(["DB_HOST"], || { // Clear the environment variable completely
unsafe { std::env::remove_var("DB_HOST") };
build_database_url(); 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."
);
},
);
} }
} }