From 636f04ab74d435b28dcebdea03bd21511b252c3f Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 9 Apr 2025 23:47:55 +0200 Subject: [PATCH] test: add tests for Redis connection string with defaults and custom port --- crates/backend/src/main.rs | 59 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index fda900c..811277e 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -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::().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::().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." + ); + }, + ); } } -- 2.45.3