Skip to content

Commit

Permalink
utopia-gen/tests/schema_generics: Use insta for snapshot testing
Browse files Browse the repository at this point in the history
`insta` produces diffs like the following, which makes it much easier to see at first glance what changed. The `cargo-insta` extension also makes it very fast to update snapshots in case of any desired changes.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Snapshot Summary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Snapshot file: utoipa-gen/tests/snapshots/schema_generics__generic_schema_full_api.snap
Snapshot: generic_schema_full_api
Source: utoipa-gen/tests/schema_generics.rs:221
────────────────────────────────────────────────────────────────────────────────
Expression: doc
────────────────────────────────────────────────────────────────────────────────
-old snapshot
+new results
────────────┬───────────────────────────────────────────────────────────────────
   64    64 │                     "items"
   65    65 │                   ],
   66    66 │                   "properties": {
   67    67 │                     "items": {
   68       │-                      "type": "object",
         68 │+                      "type": "array",
   69    69 │                       "items": {
   70    70 │                         "type": "object",
   71    71 │                         "required": [
   72    72 │                           "id",
────────────┴───────────────────────────────────────────────────────────────────
To update snapshots run `cargo insta review`
  • Loading branch information
Turbo87 committed Dec 19, 2024
1 parent 71f6e37 commit 83bf334
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 483 deletions.
1 change: 1 addition & 0 deletions utoipa-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ chrono = { version = "0.4", features = ["serde"] }
assert-json-diff = "2"
time = { version = "0.3", features = ["serde-human-readable"] }
serde_with = "3.0"
insta = { version = "1.41", features = ["json"] }

[features]
# See README.md for list and explanations of features
Expand Down
127 changes: 12 additions & 115 deletions utoipa-gen/tests/schema_generics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::borrow::Cow;
use std::marker::PhantomData;

use assert_json_diff::assert_json_eq;
use insta::assert_json_snapshot;
use serde::Serialize;
use serde_json::json;
use utoipa::openapi::{Info, RefOr, Schema};
use utoipa::{schema, OpenApi, PartialSchema, ToSchema};

Expand Down Expand Up @@ -66,89 +65,7 @@ fn generic_request_body_schema() {
let mut doc = ApiDoc::openapi();
doc.info = Info::new("title", "version");

let actual = serde_json::to_value(&doc).expect("operation is JSON serializable");
let json = serde_json::to_string_pretty(&actual).unwrap();

println!("{json}");

assert_json_eq!(
actual,
json!({
"openapi": "3.1.0",
"info": {
"title": "title",
"version": "version"
},
"paths": {
"/handler": {
"get": {
"tags": [],
"operationId": "handler",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"field",
"t"
],
"properties": {
"field": {
"type": "string"
},
"t": {
"type": "object",
"required": [
"t"
],
"properties": {
"t": {
"type": "integer",
"format": "int32"
}
}
}
}
}
}
},
"required": true
},
"responses": {}
}
}
},
"components": {
"schemas": {
"Person_String_path.MyType_i32": {
"type": "object",
"required": [
"field",
"t"
],
"properties": {
"field": {
"type": "string"
},
"t": {
"type": "object",
"required": [
"t"
],
"properties": {
"t": {
"type": "integer",
"format": "int32"
}
}
}
}
}
}
}
})
);
assert_json_snapshot!(doc);
}

#[test]
Expand Down Expand Up @@ -217,11 +134,7 @@ fn generic_schema_full_api() {
let mut doc = ApiDoc::openapi();
doc.info = Info::new("title", "version");

let actual = doc.to_pretty_json().expect("OpenApi is JSON serializable");
println!("{actual}");
let expected = include_str!("./testdata/schema_generics_openapi");

assert_eq!(expected.trim(), actual.trim());
assert_json_snapshot!(doc);
}

#[test]
Expand Down Expand Up @@ -251,11 +164,7 @@ fn schema_with_non_generic_root() {
let mut api = ApiDoc::openapi();
api.info = Info::new("title", "version");

let actual = api.to_pretty_json().expect("schema is JSON serializable");
println!("{actual}");
let expected = include_str!("./testdata/schema_non_generic_root_generic_references");

assert_eq!(actual.trim(), expected.trim())
assert_json_snapshot!(api);
}

#[test]
Expand Down Expand Up @@ -289,10 +198,8 @@ fn derive_generic_schema_enum_variants() {

let mut api = Api::openapi();
api.info = Info::new("title", "version");
let api_json = api.to_pretty_json().expect("OpenAPI is JSON serializable");
println!("{api_json}");
let expected = include_str!("./testdata/schema_generic_enum_variant_with_generic_type");
assert_eq!(expected.trim(), api_json.trim());

assert_json_snapshot!(api);
}

#[test]
Expand Down Expand Up @@ -348,10 +255,8 @@ fn derive_generic_schema_collect_recursive_schema_not_inlined() {

let mut api = Api::openapi();
api.info = Info::new("title", "version");
let api_json = api.to_pretty_json().expect("OpenAPI is JSON serializable");
println!("{api_json}");
let expected = include_str!("./testdata/schema_generic_collect_non_inlined_schema");
assert_eq!(expected.trim(), api_json.trim());

assert_json_snapshot!(api);
}

#[test]
Expand Down Expand Up @@ -381,10 +286,8 @@ fn high_order_types() {

let mut api = Api::openapi();
api.info = Info::new("title", "version");
let api_json = api.to_pretty_json().expect("OpenAPI is JSON serializable");
println!("{api_json}");
let expected = include_str!("./testdata/schema_high_order_types");
assert_eq!(expected.trim(), api_json.trim());

assert_json_snapshot!(api);
}

#[test]
Expand All @@ -409,11 +312,8 @@ fn rc_schema_high_order_types() {

let mut api = Api::openapi();
api.info = Info::new("title", "version");
let api_json = api.to_pretty_json().expect("OpenAPI is JSON serializable");
println!("{api_json}");

let expected = include_str!("./testdata/rc_schema_high_order_types");
assert_eq!(expected.trim(), api_json.trim());
assert_json_snapshot!(api);
}

#[test]
Expand All @@ -435,11 +335,8 @@ fn uuid_type_generic_argument() {

let mut api = Api::openapi();
api.info = Info::new("title", "version");
let api_json = api.to_pretty_json().expect("OpenAPI is JSON serializable");
println!("{api_json}");

let expected = include_str!("./testdata/uuid_type_generic_argument");
assert_eq!(expected.trim(), api_json.trim());
assert_json_snapshot!(api);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
source: utoipa-gen/tests/schema_generics.rs
expression: api
snapshot_kind: text
---
{
"openapi": "3.1.0",
"info": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
source: utoipa-gen/tests/schema_generics.rs
expression: api
snapshot_kind: text
---
{
"openapi": "3.1.0",
"info": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
source: utoipa-gen/tests/schema_generics.rs
expression: doc
snapshot_kind: text
---
{
"openapi": "3.1.0",
"info": {
"title": "title",
"version": "version"
},
"paths": {
"/handler": {
"get": {
"tags": [],
"operationId": "handler",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"field",
"t"
],
"properties": {
"field": {
"type": "string"
},
"t": {
"type": "object",
"required": [
"t"
],
"properties": {
"t": {
"type": "integer",
"format": "int32"
}
}
}
}
}
}
},
"required": true
},
"responses": {}
}
}
},
"components": {
"schemas": {
"Person_String_path.MyType_i32": {
"type": "object",
"required": [
"field",
"t"
],
"properties": {
"field": {
"type": "string"
},
"t": {
"type": "object",
"required": [
"t"
],
"properties": {
"t": {
"type": "integer",
"format": "int32"
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
source: utoipa-gen/tests/schema_generics.rs
expression: doc
snapshot_kind: text
---
{
"openapi": "3.1.0",
"info": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
source: utoipa-gen/tests/schema_generics.rs
expression: api
snapshot_kind: text
---
{
"openapi": "3.1.0",
"info": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
source: utoipa-gen/tests/schema_generics.rs
expression: api
snapshot_kind: text
---
{
"openapi": "3.1.0",
"info": {
Expand Down
Loading

0 comments on commit 83bf334

Please sign in to comment.