Skip to content

Commit

Permalink
utoipa: Replace assert-json-diff with snapshot testing via insta
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Dec 23, 2024
1 parent 2782358 commit 5277e6c
Show file tree
Hide file tree
Showing 19 changed files with 369 additions and 444 deletions.
6 changes: 6 additions & 0 deletions utoipa/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
**`utoipa`** is in direct correlation with **`utoipa-gen`** ([CHANGELOG.md](../utoipa-gen/CHANGELOG.md)). You might want
to look into changes introduced to **`utoipa-gen`**.

## Unreleased

### Changed

* Replace `assert-json-diff` with snapshot testing via `insta` (https://github.com/juhaku/utoipa/pull/1254)

## 5.3.0 - Dec 19 2024

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion utoipa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ utoipa-gen = { version = "5.3.0", path = "../utoipa-gen", optional = true }
indexmap = { version = "2", features = ["serde"] }

[dev-dependencies]
assert-json-diff = "2"
insta = { version = "1.41", features = ["json", "redactions"] }
utoipa = { path = ".", features = ["debug"] }

[package.metadata.docs.rs]
Expand Down
132 changes: 23 additions & 109 deletions utoipa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ mod utoipa {
/// .build()
/// )
/// );
/// # assert_json_diff::assert_json_eq!(serde_json::to_value(&number).unwrap(), serde_json::to_value(&number2).unwrap());
/// # assert_eq!(serde_json::to_value(&number).unwrap(), serde_json::to_value(&number2).unwrap());
/// ```
///
/// _**Construct a Pet object schema manually.**_
Expand Down Expand Up @@ -1564,7 +1564,7 @@ pub mod __dev {

#[cfg(test)]
mod tests {
use assert_json_diff::assert_json_eq;
use insta::assert_compact_json_snapshot;
use serde_json::json;

use super::*;
Expand Down Expand Up @@ -1600,117 +1600,31 @@ mod tests {
#[cfg(not(feature = "non_strict_integers"))]
#[test]
fn test_partial_schema_strict_integers() {
use assert_json_diff::{assert_json_matches, CompareMode, Config, NumericMode};

for (name, schema, value) in [
(
"i8",
i8::schema(),
json!({"type": "integer", "format": "int32"}),
),
(
"i16",
i16::schema(),
json!({"type": "integer", "format": "int32"}),
),
(
"i32",
i32::schema(),
json!({"type": "integer", "format": "int32"}),
),
(
"i64",
i64::schema(),
json!({"type": "integer", "format": "int64"}),
),
("i128", i128::schema(), json!({"type": "integer"})),
("isize", isize::schema(), json!({"type": "integer"})),
(
"u8",
u8::schema(),
json!({"type": "integer", "format": "int32", "minimum": 0.0}),
),
(
"u16",
u16::schema(),
json!({"type": "integer", "format": "int32", "minimum": 0.0}),
),
(
"u32",
u32::schema(),
json!({"type": "integer", "format": "int32", "minimum": 0.0}),
),
(
"u64",
u64::schema(),
json!({"type": "integer", "format": "int64", "minimum": 0.0}),
),
] {
println!(
"{name}: {json}",
json = serde_json::to_string(&schema).unwrap()
);
let schema = serde_json::to_value(schema).unwrap();

let config = Config::new(CompareMode::Strict).numeric_mode(NumericMode::AssumeFloat);
assert_json_matches!(schema, value, config);
}
assert_compact_json_snapshot!(i8::schema(), @r#"{"type": "integer", "format": "int32"}"#);
assert_compact_json_snapshot!(i16::schema(), @r#"{"type": "integer", "format": "int32"}"#);
assert_compact_json_snapshot!(i32::schema(), @r#"{"type": "integer", "format": "int32"}"#);
assert_compact_json_snapshot!(i64::schema(), @r#"{"type": "integer", "format": "int64"}"#);
assert_compact_json_snapshot!(i128::schema(), @r#"{"type": "integer"}"#);
assert_compact_json_snapshot!(isize::schema(), @r#"{"type": "integer"}"#);
assert_compact_json_snapshot!(u8::schema(), @r#"{"type": "integer", "format": "int32", "minimum": 0}"#);
assert_compact_json_snapshot!(u16::schema(), @r#"{"type": "integer", "format": "int32", "minimum": 0}"#);
assert_compact_json_snapshot!(u32::schema(), @r#"{"type": "integer", "format": "int32", "minimum": 0}"#);
assert_compact_json_snapshot!(u64::schema(), @r#"{"type": "integer", "format": "int64", "minimum": 0}"#);
}

#[cfg(feature = "non_strict_integers")]
#[test]
fn test_partial_schema_non_strict_integers() {
for (name, schema, value) in [
(
"i8",
i8::schema(),
json!({"type": "integer", "format": "int8"}),
),
(
"i16",
i16::schema(),
json!({"type": "integer", "format": "int16"}),
),
(
"i32",
i32::schema(),
json!({"type": "integer", "format": "int32"}),
),
(
"i64",
i64::schema(),
json!({"type": "integer", "format": "int64"}),
),
("i128", i128::schema(), json!({"type": "integer"})),
("isize", isize::schema(), json!({"type": "integer"})),
(
"u8",
u8::schema(),
json!({"type": "integer", "format": "uint8", "minimum": 0}),
),
(
"u16",
u16::schema(),
json!({"type": "integer", "format": "uint16", "minimum": 0}),
),
(
"u32",
u32::schema(),
json!({"type": "integer", "format": "uint32", "minimum": 0}),
),
(
"u64",
u64::schema(),
json!({"type": "integer", "format": "uint64", "minimum": 0}),
),
] {
println!(
"{name}: {json}",
json = serde_json::to_string(&schema).unwrap()
);
let schema = serde_json::to_value(schema).unwrap();
assert_json_eq!(schema, value);
}
assert_compact_json_snapshot!(i8::schema(), @r#"{"type": "integer", "format": "int8"}"#);
assert_compact_json_snapshot!(i16::schema(), @r#"{"type": "integer", "format": "int16"}"#);
assert_compact_json_snapshot!(i32::schema(), @r#"{"type": "integer", "format": "int32"}"#);
assert_compact_json_snapshot!(i64::schema(), @r#"{"type": "integer", "format": "int64"}"#);
assert_compact_json_snapshot!(i128::schema(), @r#"{"type": "integer"}"#);
assert_compact_json_snapshot!(isize::schema(), @r#"{"type": "integer"}"#);
assert_compact_json_snapshot!(u8::schema(), @r#"{"type": "integer", "format": "uint8", "minimum": 0}"#);
assert_compact_json_snapshot!(u16::schema(), @r#"{"type": "integer", "format": "uint16", "minimum": 0}"#);
assert_compact_json_snapshot!(u32::schema(), @r#"{"type": "integer", "format": "int32", "minimum": 0}"#);
assert_compact_json_snapshot!(u64::schema(), @r#"{"type": "integer", "format": "int64", "minimum": 0}"#);
}

#[test]
Expand All @@ -1736,7 +1650,7 @@ mod tests {
json = serde_json::to_string(&schema).unwrap()
);
let schema = serde_json::to_value(schema).unwrap();
assert_json_eq!(schema, value);
assert_eq!(schema, value);
}
}
}
Loading

0 comments on commit 5277e6c

Please sign in to comment.