Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Add settings test (#128)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
neithanmo authored Feb 13, 2024
1 parent 78216b6 commit 3ec0b29
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ 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<u64>,
// Give the option to skip the index creation
pub create_index: bool,
}

const fn default_db_port() -> u16 {
5432
}

#[derive(Debug, Deserialize)]
pub struct JaegerConfig {
pub enable: bool,
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 3ec0b29

Please sign in to comment.