Add temp-env as a dev dependency and enhance database URL tests
This commit is contained in:
parent
72bb162b52
commit
d0045e80e3
2 changed files with 67 additions and 5 deletions
|
@ -29,5 +29,9 @@ uuid = "1"
|
|||
|
||||
dotenvy = "0.15"
|
||||
|
||||
[dev-dependencies]
|
||||
temp-env = "*"
|
||||
|
||||
|
||||
[features]
|
||||
serve = []
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
use actix_files::NamedFile;
|
||||
use actix_session::{storage::RedisSessionStore, SessionMiddleware};
|
||||
use actix_web::{cookie::Key, middleware::Logger, web, App, HttpResponse, HttpServer};
|
||||
use actix_session::{SessionMiddleware, storage::RedisSessionStore};
|
||||
use actix_web::{App, HttpResponse, HttpServer, cookie::Key, middleware::Logger, web};
|
||||
use log::debug;
|
||||
|
||||
mod controller;
|
||||
mod db;
|
||||
mod error;
|
||||
|
||||
pub use db::entity;
|
||||
pub use db::Database;
|
||||
pub use db::entity;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppConfig {
|
||||
|
@ -80,8 +80,66 @@ fn build_database_url() -> String {
|
|||
.map(|x| x.parse::<u16>().expect("DB_PORT is not a valid port"))
|
||||
.unwrap_or(5432);
|
||||
|
||||
format!(
|
||||
let result = format!(
|
||||
"postgresql://{}:{}@{}:{}/{}",
|
||||
db_user, db_password, db_host, db_port, db_name
|
||||
)
|
||||
);
|
||||
|
||||
println!("Database URL: {}", result);
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use temp_env::with_vars;
|
||||
|
||||
#[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();
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_database_url_with_all_vars() {
|
||||
dotenvy::dotenv().ok();
|
||||
with_vars(
|
||||
[
|
||||
("DB_USER", Some("testuser")),
|
||||
("DB_NAME", Some("testdb")),
|
||||
("DB_PASSWORD", Some("testpass")),
|
||||
("DB_HOST", Some("otherhost.internal")),
|
||||
("DB_PORT", Some("5433")),
|
||||
],
|
||||
|| {
|
||||
let expected_url = "postgresql://testuser:testpass@otherhost.internal:5433/testdb";
|
||||
let actual_url = build_database_url();
|
||||
assert_eq!(
|
||||
actual_url, expected_url,
|
||||
"Database URL should use all provided env vars."
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "DB_HOST must be set in .env")]
|
||||
fn build_database_url_missing_host_panics() {
|
||||
dotenvy::dotenv().ok();
|
||||
build_database_url();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue