Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nkz-soft committed Nov 7, 2024
1 parent d28b7e8 commit b2e64e5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ You can also configure via environment variables.
```bash
export MICROSERVICE__SERVICE__HTTP_URL="127.0.0.1:8181"
export MICROSERVICE__SERVICE__SERVICE_NAME="rust_template_service"
export MICROSERVICE__DATABASE__PG__USER="postgres"
export MICROSERVICE__DATABASE__PG__PASSWORD="postgres"
export MICROSERVICE__DATABASE__PG__HOST="127.0.0.1"
export MICROSERVICE__DATABASE__PG__PORT=5432
export MICROSERVICE__DATABASE__PG__DBNAME="rust_template_db"
export MICROSERVICE__DATABASE__PG__POOL__MAX_SIZE=16
export MICROSERVICE__DATABASE__PG.USER="postgres"
export MICROSERVICE__DATABASE__PG.PASSWORD="postgres"
export MICROSERVICE__DATABASE__PG.HOST="127.0.0.1"
export MICROSERVICE__DATABASE__PG.PORT=5432
export MICROSERVICE__DATABASE__PG.DBNAME="rust_template_db"
export MICROSERVICE__DATABASE__PG.POOL.MAX_SIZE=16
```

## Architecture
Expand Down
10 changes: 4 additions & 6 deletions src/starter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ use anyhow::Result;

mod settings;

pub async fn run() -> Result<()> {
pub async fn run() -> Result<Server> {
let settings = Settings::default().load()?;
run_internal(&settings).await?.await?;
Ok(())
Ok(run_internal(&settings).await?)
}

pub async fn run_with_config(path: &str) -> Result<()> {
pub async fn run_with_config(path: &str) -> Result<Server> {
let settings = Settings::with_path(path).load()?;
run_internal(&settings).await?.await?;
Ok(())
Ok(run_internal(&settings).await?)
}
async fn run_internal(settings: &Settings) -> Result<Server> {
info!("Starting HTTP server at {}", &settings.service.http_url);
Expand Down
3 changes: 2 additions & 1 deletion src/starter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use starter::run;
async fn main() -> Result<()> {
dotenv::dotenv().ok();
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "info"));
run().await?.await?;

run().await
Ok(())
}
9 changes: 7 additions & 2 deletions src/starter/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod tests {
use std::env;
use serial_test::serial;

#[serial]
#[test]
fn default_settings_test() {
let settings = Settings::default();
Expand All @@ -93,10 +94,14 @@ mod tests {
#[test]
fn with_path_settings_override_env_test() {
env::set_var("MICROSERVICE__SERVICE__HTTP_URL", "localhost:8080");
env::set_var("MICROSERVICE__DATABASE__PG__USER", "pg_user");
env::set_var("MICROSERVICE__DATABASE__PG.USER", "pg_user");
env::set_var("MICROSERVICE__DATABASE__PG.POOL.MAX_SIZE", "10");
let settings = Settings::with_path("./../../").load().unwrap();
assert_eq!(settings.service.http_url, "localhost:8080");
assert_eq!(settings.database.pg.user.unwrap(), "pg_user");
env::remove_var("MICROSERVICE__SERVICE__HTTP_URL")
assert_eq!(settings.database.pg.pool.unwrap().max_size, 10);
env::remove_var("MICROSERVICE__SERVICE__HTTP_URL");
env::remove_var("MICROSERVICE__DATABASE__PG.USER");
env::remove_var("MICROSERVICE__DATABASE__PG.POOL.MAX_SIZE");
}
}

0 comments on commit b2e64e5

Please sign in to comment.