Refactor database URL tests to use temp_env with multiple variables and improve clarity
This commit is contained in:
parent
d0045e80e3
commit
cee89e31aa
1 changed files with 21 additions and 20 deletions
|
@ -92,31 +92,31 @@ fn build_database_url() -> String {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use temp_env::with_vars;
|
||||
use temp_env::{with_vars, with_vars_unset};
|
||||
|
||||
#[test]
|
||||
fn build_database_url_with_defaults() {
|
||||
temp_env::with_vars([("DB_HOST", None::<&str>)], || {
|
||||
// Was sieht die direkte Umgebung? (Sollte Err sein)
|
||||
println!(
|
||||
"Inside temp_env (unset): std::env::var(\"DB_HOST\") is {:?}",
|
||||
std::env::var("DB_HOST")
|
||||
);
|
||||
|
||||
// Was sieht dotenvy? (Ist wahrscheinlich Ok(...) wegen .env)
|
||||
println!(
|
||||
"Inside temp_env (unset): dotenvy::var(\"DB_HOST\") is {:?}",
|
||||
dotenvy::var("DB_HOST")
|
||||
);
|
||||
|
||||
// Jetzt der Aufruf, der panicen sollte
|
||||
build_database_url();
|
||||
});
|
||||
temp_env::with_vars(
|
||||
[
|
||||
("DB_USER", None::<&str>),
|
||||
("DB_NAME", None::<&str>),
|
||||
("DB_PASSWORD", None::<&str>),
|
||||
("DB_HOST", Some("localhost")),
|
||||
("DB_PORT", None::<&str>),
|
||||
],
|
||||
|| {
|
||||
let expected_url = "postgresql://pgg:pgg@localhost:5432/pgg";
|
||||
let actual_url = build_database_url();
|
||||
assert_eq!(
|
||||
actual_url, expected_url,
|
||||
"Database URL should use default values for unset env vars."
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_database_url_with_all_vars() {
|
||||
dotenvy::dotenv().ok();
|
||||
with_vars(
|
||||
[
|
||||
("DB_USER", Some("testuser")),
|
||||
|
@ -139,7 +139,8 @@ mod tests {
|
|||
#[test]
|
||||
#[should_panic(expected = "DB_HOST must be set in .env")]
|
||||
fn build_database_url_missing_host_panics() {
|
||||
dotenvy::dotenv().ok();
|
||||
build_database_url();
|
||||
with_vars_unset(["DB_HOST"], || {
|
||||
build_database_url();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue