From 3ec0b294402e4e3e052350dda1c3d5425a7e4340 Mon Sep 17 00:00:00 2001 From: Natanael Mojica Date: Tue, 13 Feb 2024 15:53:31 -0600 Subject: [PATCH] Add settings test (#128) This adds: - Adds a default value for the database.port field in DatabaseConfig - Implements a test to validate Settings.example.toml against the Settings struct, ensuring that changes in the configuration code are consistently reflected in the example file used in documentation. --- src/config.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/config.rs b/src/config.rs index fc82a10..0f4acd4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -37,6 +37,7 @@ pub struct DatabaseConfig { pub user: String, pub password: String, pub dbname: String, + #[serde(default = "default_db_port")] pub port: u16, // The limit in seconds to wait for a ready database connection pub connection_timeout: Option, @@ -44,6 +45,10 @@ pub struct DatabaseConfig { pub create_index: bool, } +const fn default_db_port() -> u16 { + 5432 +} + #[derive(Debug, Deserialize)] pub struct JaegerConfig { pub enable: bool, @@ -265,3 +270,31 @@ impl Settings { &self.prometheus } } + +#[cfg(test)] +mod tests { + use super::*; + use config::{Config, File}; + use std::path::PathBuf; + + #[test] + fn test_settings_deserialization() { + let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + d.push("config/Settings.example.toml"); + + assert!( + d.exists(), + "Settings.example.toml file does not exist at {:?}", + d + ); + + let config = Config::builder() + .add_source(File::from(d)) + .build() + .expect("Failed to build config"); + + let _: Settings = config + .try_deserialize() + .expect("Failed to deserialize Settings.example.toml into the Settings struct"); + } +}