Skip to content

Commit

Permalink
feat: Add TestCase utility for configuring insta settings (#277)
Browse files Browse the repository at this point in the history
Add some utilities to set some common `insta` settings. The settings are
configurable using the `TestCaseConfig` builder struct. The builder
yields a `TestCase` struct containing the configured `insta` settings.

The settings available to configure currently are:

1. Redact UUIDs from snapshots; UUIDs are replaced with the string
`[uuid]`
2. Set the suffix of the snapshot (which can be provided manually or
automatically based on the thread name). This is useful when using
`insta` along with `rstest`.
3. Auto-bind to the current scope on creation of the `TestCase` struct
to avoid the need to remember to bind the settings scope.
  • Loading branch information
spencewenski authored Jul 22, 2024
1 parent 23be615 commit bc52376
Show file tree
Hide file tree
Showing 33 changed files with 354 additions and 103 deletions.
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jwt-openid = ["jwt"]
cli = ["dep:clap"]
otel = ["dep:opentelemetry", "dep:opentelemetry_sdk", "dep:opentelemetry-otlp", "dep:tracing-opentelemetry"]
grpc = ["dep:tonic"]
testing = ["dep:insta", "dep:rstest"]

[dependencies]
# Config
Expand Down Expand Up @@ -75,6 +76,10 @@ clap = { workspace = true, features = ["derive", "string"], optional = true }
# gRPC
tonic = { workspace = true, optional = true }

# Testing
insta = { workspace = true, optional = true }
rstest = { workspace = true, optional = true }

# Others
anyhow = { workspace = true }
serde = { workspace = true }
Expand All @@ -87,7 +92,7 @@ serde_json = "1.0.96"
toml = "0.8.0"
url = { version = "2.2.2", features = ["serde"] }
uuid = { version = "1.1.2", features = ["v4", "serde"] }
futures = "0.3.19"
futures = "0.3.21"
futures-core = "0.3.28"
chrono = { version = "0.4.34", features = ["serde"] }
byte-unit = { version = "5.0.0", features = ["serde"] }
Expand All @@ -103,10 +108,10 @@ time = "0.3.36"

[dev-dependencies]
cargo-husky = { version = "1.5.0", default-features = false, features = ["user-hooks"] }
insta = { version = "1.39.0", features = ["toml"] }
insta = { workspace = true }
mockall = "0.12.1"
mockall_double = "0.3.1"
rstest = "0.21.0"
rstest = { workspace = true }

[workspace]
members = [".", "examples/*"]
Expand All @@ -131,6 +136,10 @@ tonic = { version = "0.11.0" }
# Todo: the default `rss-stats` feature has a dependency that currently can't be satisfied (memchr: ~2.3)
rusty-sidekiq = { version = "0.10.5", default-features = false }

# Testing
insta = { version = "1.39.0", features = ["toml", "filters"] }
rstest = { version = "0.21.0" }

# Others
# Todo: minimize tokio features included in `roadster`
tokio = { version = "1.28.0", features = ["full"] }
Expand Down
2 changes: 1 addition & 1 deletion src/api/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mockall::mock! {
mod tests {
use super::*;
use crate::app::MockApp;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use insta::assert_toml_snapshot;
use itertools::Itertools;
use rstest::{fixture, rstest};
Expand Down
2 changes: 1 addition & 1 deletion src/config/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::config::health_check::HealthCheck;
use crate::config::service::Service;
use crate::config::tracing::Tracing;
use crate::error::RoadsterResult;
use crate::util::serde_util::default_true;
use crate::util::serde::default_true;
use config::builder::DefaultState;
use config::{Case, Config, ConfigBuilder, FileFormat};
use dotenvy::dotenv;
Expand Down
4 changes: 2 additions & 2 deletions src/config/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::util::serde_util::UriOrString;
use crate::util::serde::UriOrString;
use serde_derive::{Deserialize, Serialize};
use validator::Validate;

Expand Down Expand Up @@ -35,7 +35,7 @@ pub struct JwtClaims {
#[cfg(test)]
mod tests {
use super::*;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use insta::assert_toml_snapshot;
use rstest::{fixture, rstest};

Expand Down
2 changes: 1 addition & 1 deletion src/config/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl From<&Database> for ConnectOptions {
#[cfg(test)]
mod deserialize_tests {
use super::*;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use insta::{assert_debug_snapshot, assert_toml_snapshot};
use rstest::{fixture, rstest};

Expand Down
2 changes: 1 addition & 1 deletion src/config/health_check/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::app::context::AppContext;
use crate::config::app_config::CustomConfig;
use crate::util::serde_util::default_true;
use crate::util::serde::default_true;
use axum::extract::FromRef;
use config::{FileFormat, FileSourceString};
use serde_derive::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion src/config/service/http/default_routes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::app::context::AppContext;
use crate::util::serde_util::default_true;
use crate::util::serde::default_true;
use axum::extract::FromRef;
use serde_derive::{Deserialize, Serialize};
use validator::Validate;
Expand Down
2 changes: 1 addition & 1 deletion src/config/service/http/initializer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::app::context::AppContext;
use crate::config::app_config::CustomConfig;
use crate::service::http::initializer::normalize_path::NormalizePathConfig;
use crate::util::serde_util::default_true;
use crate::util::serde::default_true;
use axum::extract::FromRef;
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand Down
2 changes: 1 addition & 1 deletion src/config/service/http/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::service::http::middleware::sensitive_headers::{
use crate::service::http::middleware::size_limit::SizeLimitConfig;
use crate::service::http::middleware::timeout::TimeoutConfig;
use crate::service::http::middleware::tracing::TracingConfig;
use crate::util::serde_util::default_true;
use crate::util::serde::default_true;
use axum::extract::FromRef;
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand Down
2 changes: 1 addition & 1 deletion src/config/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::config::service::grpc::GrpcServiceConfig;
use crate::config::service::http::HttpServiceConfig;
#[cfg(feature = "sidekiq")]
use crate::config::service::worker::sidekiq::SidekiqServiceConfig;
use crate::util::serde_util::default_true;
use crate::util::serde::default_true;
use serde_derive::{Deserialize, Serialize};
use validator::Validate;

Expand Down
2 changes: 1 addition & 1 deletion src/config/service/worker/sidekiq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct ConnectionPool {
#[cfg(test)]
mod deserialize_tests {
use super::*;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use insta::assert_toml_snapshot;
use rstest::{fixture, rstest};

Expand Down
4 changes: 2 additions & 2 deletions src/config/tracing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "otel")]
use crate::util::serde_util::default_true;
use crate::util::serde::default_true;
use config::{FileFormat, FileSourceString};
use serde_derive::{Deserialize, Serialize};
use strum_macros::{EnumString, IntoStaticStr};
Expand Down Expand Up @@ -50,7 +50,7 @@ pub enum Format {
#[cfg(all(test, feature = "otel"))]
mod deserialize_tests {
use super::*;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use insta::assert_toml_snapshot;
use rstest::{fixture, rstest};

Expand Down
2 changes: 1 addition & 1 deletion src/health_check/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn default_health_checks(
mod tests {
use crate::app::context::AppContext;
use crate::config::app_config::AppConfig;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use bb8::Pool;
use insta::assert_toml_snapshot;
use itertools::Itertools;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ pub mod error;
pub mod health_check;
pub mod middleware;
pub mod service;
#[cfg(any(test, feature = "testing"))]
pub mod testing;
pub mod tracing;
pub mod util;
4 changes: 2 additions & 2 deletions src/middleware/http/auth/jwt/ietf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::middleware::http::auth::jwt::Subject;
use crate::util::serde_util::UriOrString;
use crate::util::serde::UriOrString;
use chrono::serde::ts_seconds;
use chrono::{DateTime, Utc};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -55,7 +55,7 @@ mod tests {
use super::*;
use crate::error::RoadsterResult;
use crate::middleware::http::auth::jwt::decode_auth_token;
use crate::util::serde_util::{UriOrString, Wrapper};
use crate::util::serde::{UriOrString, Wrapper};
use chrono::{TimeDelta, Utc};
use jsonwebtoken::{encode, EncodingKey, Header, TokenData};
use serde_json::from_str;
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/http/auth/jwt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::error::{Error, RoadsterResult};
use crate::middleware::http::auth::jwt::ietf::Claims;
#[cfg(all(feature = "jwt-openid", not(feature = "jwt-ietf")))]
use crate::middleware::http::auth::jwt::openid::Claims;
use crate::util::serde_util::{deserialize_from_str, serialize_to_str};
use crate::util::serde::{deserialize_from_str, serialize_to_str};
#[cfg(feature = "open-api")]
use aide::OperationInput;
use async_trait::async_trait;
Expand Down Expand Up @@ -130,7 +130,7 @@ pub enum Subject {
#[cfg(test)]
mod tests {
use super::*;
use crate::util::serde_util::Wrapper;
use crate::util::serde::Wrapper;
use serde_json::from_str;
use std::str::FromStr;
use url::Url;
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/http/auth/jwt/openid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_with::serde_as;
use std::collections::BTreeMap;
use url::Url;

use crate::util::serde_util::{deserialize_from_str, serialize_to_str, UriOrString};
use crate::util::serde::{deserialize_from_str, serialize_to_str, UriOrString};

/// JWT Claims. Provides fields for the default/recommended registered claim names. Additional
/// claim names are collected in the `custom` map.
Expand Down Expand Up @@ -71,7 +71,7 @@ pub enum Acr {
#[cfg(test)]
mod tests {
use super::*;
use crate::util::serde_util::Wrapper;
use crate::util::serde::Wrapper;
use serde_json::from_str;
use std::str::FromStr;
use url::Url;
Expand Down
4 changes: 2 additions & 2 deletions src/service/http/middleware/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ where
mod tests {
use super::*;
use crate::config::app_config::AppConfig;
use crate::util::serde_util::Wrapper;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use crate::util::serde::Wrapper;
use insta::assert_toml_snapshot;
use rstest::{fixture, rstest};

Expand Down
2 changes: 1 addition & 1 deletion src/service/http/middleware/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
mod tests {
use crate::app::context::AppContext;
use crate::config::app_config::AppConfig;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use insta::assert_toml_snapshot;
use itertools::Itertools;
use rstest::{fixture, rstest};
Expand Down
4 changes: 2 additions & 2 deletions src/service/worker/sidekiq/app_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::util::serde_util::Wrapper;
use crate::util::serde::Wrapper;
use serde_json::from_str;

#[test]
Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
#[cfg(test)]
mod deserialize_tests {
use super::*;
use crate::util::test_util::TestCase;
use crate::testing::snapshot::TestCase;
use insta::assert_toml_snapshot;
use rstest::{fixture, rstest};

Expand Down
1 change: 1 addition & 0 deletions src/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod snapshot;
Loading

0 comments on commit bc52376

Please sign in to comment.