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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use temp_env::with_vars;
|
use temp_env::{with_vars, with_vars_unset};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn build_database_url_with_defaults() {
|
fn build_database_url_with_defaults() {
|
||||||
temp_env::with_vars([("DB_HOST", None::<&str>)], || {
|
temp_env::with_vars(
|
||||||
// Was sieht die direkte Umgebung? (Sollte Err sein)
|
[
|
||||||
println!(
|
("DB_USER", None::<&str>),
|
||||||
"Inside temp_env (unset): std::env::var(\"DB_HOST\") is {:?}",
|
("DB_NAME", None::<&str>),
|
||||||
std::env::var("DB_HOST")
|
("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."
|
||||||
);
|
);
|
||||||
|
},
|
||||||
// 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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn build_database_url_with_all_vars() {
|
fn build_database_url_with_all_vars() {
|
||||||
dotenvy::dotenv().ok();
|
|
||||||
with_vars(
|
with_vars(
|
||||||
[
|
[
|
||||||
("DB_USER", Some("testuser")),
|
("DB_USER", Some("testuser")),
|
||||||
|
@ -139,7 +139,8 @@ 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() {
|
||||||
dotenvy::dotenv().ok();
|
with_vars_unset(["DB_HOST"], || {
|
||||||
build_database_url();
|
build_database_url();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue