From 182568adbd5176b40910c42bb2590125b87f5aa1 Mon Sep 17 00:00:00 2001 From: Spencer Ferris <3319370+spencewenski@users.noreply.github.com> Date: Tue, 14 May 2024 18:55:12 -0700 Subject: [PATCH] Add tests for the default_middleware and default_initializers methods Also, add a method to construct an empty AppConfig for testing --- src/config/app_config.rs | 43 +++++++++++++++++++++++++ src/service/http/builder.rs | 2 +- src/service/http/initializer/default.rs | 25 ++++++++++++++ src/service/http/middleware/default.rs | 25 ++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/config/app_config.rs b/src/config/app_config.rs index d10b45a3..a77be218 100644 --- a/src/config/app_config.rs +++ b/src/config/app_config.rs @@ -84,6 +84,39 @@ impl AppConfig { Ok(config) } + + #[cfg(test)] + pub(crate) fn empty(config_str: Option<&str>) -> anyhow::Result { + let config = config_str.unwrap_or( + r#" + environment = "test" + + [app] + name = "Test" + + [tracing] + level = "debug" + + [database] + uri = "postgres://example:example@localhost:5432/example_test" + auto-migrate = true + max-connections = 10 + + [auth.jwt] + secret = "secret-test" + + [service.http] + host = "127.0.0.1" + port = 3000 + + [service.sidekiq.redis] + uri = "redis://localhost:6379" + "#, + ); + + let config = toml::from_str(config)?; + Ok(config) + } } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -182,3 +215,13 @@ pub struct CustomConfig { #[serde(flatten)] pub config: BTreeMap, } + +#[cfg(test)] +mod tests { + use crate::config::app_config::AppConfig; + + #[test] + fn empty() { + AppConfig::empty(None).unwrap(); + } +} diff --git a/src/service/http/builder.rs b/src/service/http/builder.rs index a0b1a22e..a41f994c 100644 --- a/src/service/http/builder.rs +++ b/src/service/http/builder.rs @@ -54,7 +54,7 @@ impl HttpServiceBuilder { } } - #[cfg_attr(not(test), allow(dead_code))] + #[cfg(test)] fn empty(context: &AppContext) -> Self { #[cfg(not(feature = "open-api"))] let router = Router::>::new(); diff --git a/src/service/http/initializer/default.rs b/src/service/http/initializer/default.rs index ee985758..a32a9c21 100644 --- a/src/service/http/initializer/default.rs +++ b/src/service/http/initializer/default.rs @@ -14,3 +14,28 @@ pub fn default_initializers( .map(|initializer| (initializer.name(), initializer)) .collect() } + +#[cfg(test)] +mod tests { + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(true, 1)] + #[case(false, 0)] + fn default_initializers(#[case] default_enable: bool, #[case] expected_size: usize) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.initializer.default_enable = default_enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + // Act + let middleware = super::default_initializers(&context); + + // Assert + assert_eq!(middleware.len(), expected_size); + } +} diff --git a/src/service/http/middleware/default.rs b/src/service/http/middleware/default.rs index 613f8529..d1a918ab 100644 --- a/src/service/http/middleware/default.rs +++ b/src/service/http/middleware/default.rs @@ -34,3 +34,28 @@ pub fn default_middleware( .map(|middleware| (middleware.name(), middleware)) .collect() } + +#[cfg(test)] +mod tests { + use crate::app_context::MockAppContext; + use crate::config::app_config::AppConfig; + use rstest::rstest; + + #[rstest] + #[case(true, 9)] + #[case(false, 0)] + fn default_middleware(#[case] default_enable: bool, #[case] expected_size: usize) { + // Arrange + let mut config = AppConfig::empty(None).unwrap(); + config.service.http.custom.middleware.default_enable = default_enable; + + let mut context = MockAppContext::<()>::default(); + context.expect_config().return_const(config); + + // Act + let middleware = super::default_middleware(&context); + + // Assert + assert_eq!(middleware.len(), expected_size); + } +}