Added unit tests for pure logic. Refs #2

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-10 17:01:51 +02:00
co-authored by Claude Opus 5.5
parent 5dbe7c36b1
commit 2babe19299
7 changed files with 689 additions and 5 deletions
+57
View File
@@ -154,3 +154,60 @@ pub fn load_config(path: &str) -> Configuration {
toml::from_str(read_to_string(path).expect("Can not open config file").as_str()).expect("Can not parse config file")
}
#[cfg(all(test, feature = "ssr"))]
mod tests {
use super::*;
fn sample() -> Configuration {
load_config(concat!(env!("CARGO_MANIFEST_DIR"), "/config_sample.toml"))
}
#[test]
fn sample_config_is_valid() {
let config = sample();
assert_eq!(config.network().bind_address(), SocketAddr::from_str("127.0.0.1:3000").unwrap());
assert_eq!(config.database().con_string(), "postgres://user:password@localhost:5432/rezervator");
assert_eq!(config.logging().severity(), "debug");
assert!(matches!(config.mailing().transport(), MailTransport::File));
assert_eq!(config.mailing().from(), "[email protected]");
assert_eq!(config.mailing().path(), &Some("Maildir".to_string()));
assert_eq!(config.mailing().server(), &None);
}
#[test]
fn sample_session_key_is_long_enough() {
// actix_web::cookie::Key::from panics on keys shorter than 64 bytes
assert!(sample().session().key().len() >= 64);
}
#[test]
fn database_port_defaults_to_5432() {
let database: Database = toml::from_str(
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'db.example.com'").unwrap();
assert_eq!(database.con_string(), "postgres://u:[email protected]:5432/db");
}
#[test]
fn database_custom_port() {
let database: Database = toml::from_str(
"user = 'u'\npassword = 'p'\ndb_name = 'db'\nhost = 'db.example.com'\nport = 6543").unwrap();
assert_eq!(database.con_string(), "postgres://u:[email protected]:6543/db");
}
#[test]
fn smtp_mailing_config() {
let mailing: Mailing = toml::from_str(
"transport = 'Smtp'\nfrom = '[email protected]'\nserver = 'smtp.b.cz'\nport = 587\ntls = true").unwrap();
assert!(matches!(mailing.transport(), MailTransport::Smtp));
assert_eq!(mailing.server(), &Some("smtp.b.cz".to_string()));
assert_eq!(mailing.port(), Some(587));
assert_eq!(mailing.tls(), Some(true));
assert_eq!(mailing.accept_all_certs(), None);
}
}