Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the default_middleware and default_initializers methods #118

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/config/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ impl AppConfig {

Ok(config)
}

#[cfg(test)]
pub(crate) fn empty(config_str: Option<&str>) -> anyhow::Result<Self> {
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)]
Expand Down Expand Up @@ -182,3 +215,13 @@ pub struct CustomConfig {
#[serde(flatten)]
pub config: BTreeMap<String, Value>,
}

#[cfg(test)]
mod tests {
use crate::config::app_config::AppConfig;

#[test]
fn empty() {
AppConfig::empty(None).unwrap();
}
}
2 changes: 1 addition & 1 deletion src/service/http/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<A: App> HttpServiceBuilder<A> {
}
}

#[cfg_attr(not(test), allow(dead_code))]
#[cfg(test)]
fn empty(context: &AppContext<A::State>) -> Self {
#[cfg(not(feature = "open-api"))]
let router = Router::<AppContext<A::State>>::new();
Expand Down
25 changes: 25 additions & 0 deletions src/service/http/initializer/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ pub fn default_initializers<S: Send + Sync + 'static>(
.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);
}
}
25 changes: 25 additions & 0 deletions src/service/http/middleware/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,28 @@ pub fn default_middleware<S: Send + Sync + 'static>(
.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);
}
}