From 83bf334c0808621493524f07b10f12602b0f9f1d Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 19 Dec 2024 10:45:22 +0100 Subject: [PATCH] utopia-gen/tests/schema_generics: Use `insta` for snapshot testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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` --- utoipa-gen/Cargo.toml | 1 + utoipa-gen/tests/schema_generics.rs | 127 +------- ...collect_recursive_schema_not_inlined.snap} | 5 + ..._derive_generic_schema_enum_variants.snap} | 5 + ...generics__generic_request_body_schema.snap | 80 +++++ ...ma_generics__generic_schema_full_api.snap} | 5 + .../schema_generics__high_order_types.snap} | 5 + ...nerics__schema_with_non_generic_root.snap} | 5 + ...pi_schemas_resolve_inner_schema_references | 293 ------------------ .../tests/testdata/rc_schema_high_order_types | 42 --- .../tests/testdata/uuid_type_generic_argument | 33 -- 11 files changed, 118 insertions(+), 483 deletions(-) rename utoipa-gen/tests/{testdata/schema_generic_collect_non_inlined_schema => snapshots/schema_generics__derive_generic_schema_collect_recursive_schema_not_inlined.snap} (98%) rename utoipa-gen/tests/{testdata/schema_generic_enum_variant_with_generic_type => snapshots/schema_generics__derive_generic_schema_enum_variants.snap} (98%) create mode 100644 utoipa-gen/tests/snapshots/schema_generics__generic_request_body_schema.snap rename utoipa-gen/tests/{testdata/schema_generics_openapi => snapshots/schema_generics__generic_schema_full_api.snap} (98%) rename utoipa-gen/tests/{testdata/schema_high_order_types => snapshots/schema_generics__high_order_types.snap} (93%) rename utoipa-gen/tests/{testdata/schema_non_generic_root_generic_references => snapshots/schema_generics__schema_with_non_generic_root.snap} (93%) delete mode 100644 utoipa-gen/tests/testdata/openapi_schemas_resolve_inner_schema_references delete mode 100644 utoipa-gen/tests/testdata/rc_schema_high_order_types delete mode 100644 utoipa-gen/tests/testdata/uuid_type_generic_argument diff --git a/utoipa-gen/Cargo.toml b/utoipa-gen/Cargo.toml index 2f50282b..9374e2f0 100644 --- a/utoipa-gen/Cargo.toml +++ b/utoipa-gen/Cargo.toml @@ -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 diff --git a/utoipa-gen/tests/schema_generics.rs b/utoipa-gen/tests/schema_generics.rs index 96b1af64..df06e25e 100644 --- a/utoipa-gen/tests/schema_generics.rs +++ b/utoipa-gen/tests/schema_generics.rs @@ -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}; @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] diff --git a/utoipa-gen/tests/testdata/schema_generic_collect_non_inlined_schema b/utoipa-gen/tests/snapshots/schema_generics__derive_generic_schema_collect_recursive_schema_not_inlined.snap similarity index 98% rename from utoipa-gen/tests/testdata/schema_generic_collect_non_inlined_schema rename to utoipa-gen/tests/snapshots/schema_generics__derive_generic_schema_collect_recursive_schema_not_inlined.snap index d91775cc..8043cb14 100644 --- a/utoipa-gen/tests/testdata/schema_generic_collect_non_inlined_schema +++ b/utoipa-gen/tests/snapshots/schema_generics__derive_generic_schema_collect_recursive_schema_not_inlined.snap @@ -1,3 +1,8 @@ +--- +source: utoipa-gen/tests/schema_generics.rs +expression: api +snapshot_kind: text +--- { "openapi": "3.1.0", "info": { diff --git a/utoipa-gen/tests/testdata/schema_generic_enum_variant_with_generic_type b/utoipa-gen/tests/snapshots/schema_generics__derive_generic_schema_enum_variants.snap similarity index 98% rename from utoipa-gen/tests/testdata/schema_generic_enum_variant_with_generic_type rename to utoipa-gen/tests/snapshots/schema_generics__derive_generic_schema_enum_variants.snap index 83fb8bee..683a1fd2 100644 --- a/utoipa-gen/tests/testdata/schema_generic_enum_variant_with_generic_type +++ b/utoipa-gen/tests/snapshots/schema_generics__derive_generic_schema_enum_variants.snap @@ -1,3 +1,8 @@ +--- +source: utoipa-gen/tests/schema_generics.rs +expression: api +snapshot_kind: text +--- { "openapi": "3.1.0", "info": { diff --git a/utoipa-gen/tests/snapshots/schema_generics__generic_request_body_schema.snap b/utoipa-gen/tests/snapshots/schema_generics__generic_request_body_schema.snap new file mode 100644 index 00000000..2111c5f1 --- /dev/null +++ b/utoipa-gen/tests/snapshots/schema_generics__generic_request_body_schema.snap @@ -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" + } + } + } + } + } + } + } +} diff --git a/utoipa-gen/tests/testdata/schema_generics_openapi b/utoipa-gen/tests/snapshots/schema_generics__generic_schema_full_api.snap similarity index 98% rename from utoipa-gen/tests/testdata/schema_generics_openapi rename to utoipa-gen/tests/snapshots/schema_generics__generic_schema_full_api.snap index f69eda2f..c0bf8c91 100644 --- a/utoipa-gen/tests/testdata/schema_generics_openapi +++ b/utoipa-gen/tests/snapshots/schema_generics__generic_schema_full_api.snap @@ -1,3 +1,8 @@ +--- +source: utoipa-gen/tests/schema_generics.rs +expression: doc +snapshot_kind: text +--- { "openapi": "3.1.0", "info": { diff --git a/utoipa-gen/tests/testdata/schema_high_order_types b/utoipa-gen/tests/snapshots/schema_generics__high_order_types.snap similarity index 93% rename from utoipa-gen/tests/testdata/schema_high_order_types rename to utoipa-gen/tests/snapshots/schema_generics__high_order_types.snap index 35c0f837..749770db 100644 --- a/utoipa-gen/tests/testdata/schema_high_order_types +++ b/utoipa-gen/tests/snapshots/schema_generics__high_order_types.snap @@ -1,3 +1,8 @@ +--- +source: utoipa-gen/tests/schema_generics.rs +expression: api +snapshot_kind: text +--- { "openapi": "3.1.0", "info": { diff --git a/utoipa-gen/tests/testdata/schema_non_generic_root_generic_references b/utoipa-gen/tests/snapshots/schema_generics__schema_with_non_generic_root.snap similarity index 93% rename from utoipa-gen/tests/testdata/schema_non_generic_root_generic_references rename to utoipa-gen/tests/snapshots/schema_generics__schema_with_non_generic_root.snap index f41b53ab..3b31c83e 100644 --- a/utoipa-gen/tests/testdata/schema_non_generic_root_generic_references +++ b/utoipa-gen/tests/snapshots/schema_generics__schema_with_non_generic_root.snap @@ -1,3 +1,8 @@ +--- +source: utoipa-gen/tests/schema_generics.rs +expression: api +snapshot_kind: text +--- { "openapi": "3.1.0", "info": { diff --git a/utoipa-gen/tests/testdata/openapi_schemas_resolve_inner_schema_references b/utoipa-gen/tests/testdata/openapi_schemas_resolve_inner_schema_references deleted file mode 100644 index 9e0cd742..00000000 --- a/utoipa-gen/tests/testdata/openapi_schemas_resolve_inner_schema_references +++ /dev/null @@ -1,293 +0,0 @@ -{ - "schemas": { - "Account": { - "properties": { - "id": { - "format": "int32", - "type": "integer" - } - }, - "required": [ - "id" - ], - "type": "object" - }, - "Boo": { - "properties": { - "boo": { - "type": "boolean" - } - }, - "required": [ - "boo" - ], - "type": "object" - }, - "Element_String": { - "oneOf": [ - { - "properties": { - "One": { - "type": "string" - } - }, - "required": [ - "One" - ], - "type": "object" - }, - { - "properties": { - "Many": { - "items": { - "type": "string" - }, - "type": "array" - } - }, - "required": [ - "Many" - ], - "type": "object" - } - ] - }, - "Element_Yeah": { - "oneOf": [ - { - "properties": { - "One": { - "properties": { - "accounts": { - "items": { - "oneOf": [ - { - "type": "null" - }, - { - "$ref": "#/components/schemas/Account" - } - ] - }, - "type": "array" - }, - "foo_bar": { - "$ref": "#/components/schemas/Foobar" - }, - "name": { - "type": "string" - } - }, - "required": [ - "name", - "foo_bar", - "accounts" - ], - "type": "object" - } - }, - "required": [ - "One" - ], - "type": "object" - }, - { - "properties": { - "Many": { - "items": { - "properties": { - "accounts": { - "items": { - "oneOf": [ - { - "type": "null" - }, - { - "$ref": "#/components/schemas/Account" - } - ] - }, - "type": "array" - }, - "foo_bar": { - "$ref": "#/components/schemas/Foobar" - }, - "name": { - "type": "string" - } - }, - "required": [ - "name", - "foo_bar", - "accounts" - ], - "type": "object" - }, - "type": "array" - } - }, - "required": [ - "Many" - ], - "type": "object" - } - ] - }, - "EnumMixedContent": { - "oneOf": [ - { - "enum": [ - "ContentZero" - ], - "type": "string" - }, - { - "properties": { - "One": { - "$ref": "#/components/schemas/Foobar" - } - }, - "required": [ - "One" - ], - "type": "object" - }, - { - "properties": { - "NamedSchema": { - "properties": { - "f": { - "type": "boolean" - }, - "foo": { - "$ref": "#/components/schemas/ThisIsNone" - }, - "int": { - "format": "int32", - "type": "integer" - }, - "value": { - "$ref": "#/components/schemas/Account" - }, - "value2": { - "$ref": "#/components/schemas/Boo" - } - }, - "required": [ - "value", - "value2", - "foo", - "int", - "f" - ], - "type": "object" - } - }, - "required": [ - "NamedSchema" - ], - "type": "object" - }, - { - "properties": { - "Many": { - "items": { - "$ref": "#/components/schemas/Person" - }, - "type": "array" - } - }, - "required": [ - "Many" - ], - "type": "object" - } - ] - }, - "Foob": { - "properties": { - "item": { - "$ref": "#/components/schemas/Element_String" - }, - "item2": { - "$ref": "#/components/schemas/Element_Yeah" - } - }, - "required": [ - "item", - "item2" - ], - "type": "object" - }, - "Foobar": { - "default": null - }, - "OneOfOne": { - "$ref": "#/components/schemas/Person" - }, - "OneOfYeah": { - "$ref": "#/components/schemas/Yeah" - }, - "Person": { - "properties": { - "accounts": { - "items": { - "oneOf": [ - { - "type": "null" - }, - { - "$ref": "#/components/schemas/Account" - } - ] - }, - "type": "array" - }, - "foo_bar": { - "$ref": "#/components/schemas/Foobar" - }, - "name": { - "type": "string" - } - }, - "required": [ - "name", - "foo_bar", - "accounts" - ], - "type": "object" - }, - "ThisIsNone": { - "default": null - }, - "Yeah": { - "properties": { - "accounts": { - "items": { - "oneOf": [ - { - "type": "null" - }, - { - "$ref": "#/components/schemas/Account" - } - ] - }, - "type": "array" - }, - "foo_bar": { - "$ref": "#/components/schemas/Foobar" - }, - "name": { - "type": "string" - } - }, - "required": [ - "name", - "foo_bar", - "accounts" - ], - "type": "object" - } - } -} diff --git a/utoipa-gen/tests/testdata/rc_schema_high_order_types b/utoipa-gen/tests/testdata/rc_schema_high_order_types deleted file mode 100644 index fec4ce54..00000000 --- a/utoipa-gen/tests/testdata/rc_schema_high_order_types +++ /dev/null @@ -1,42 +0,0 @@ -{ - "openapi": "3.1.0", - "info": { - "title": "title", - "version": "version" - }, - "paths": {}, - "components": { - "schemas": { - "HighArc": { - "$ref": "#/components/schemas/High_Arc_i32" - }, - "HighRc": { - "$ref": "#/components/schemas/High_Rc_i32" - }, - "High_Arc_i32": { - "type": "object", - "required": [ - "high" - ], - "properties": { - "high": { - "type": "integer", - "format": "int32" - } - } - }, - "High_Rc_i32": { - "type": "object", - "required": [ - "high" - ], - "properties": { - "high": { - "type": "integer", - "format": "int32" - } - } - } - } - } -} diff --git a/utoipa-gen/tests/testdata/uuid_type_generic_argument b/utoipa-gen/tests/testdata/uuid_type_generic_argument deleted file mode 100644 index cc03373d..00000000 --- a/utoipa-gen/tests/testdata/uuid_type_generic_argument +++ /dev/null @@ -1,33 +0,0 @@ -{ - "openapi": "3.1.0", - "info": { - "title": "title", - "version": "version" - }, - "paths": {}, - "components": { - "schemas": { - "HighUuid": { - "$ref": "#/components/schemas/High_Option_String" - }, - "High_Option_String": { - "type": "object", - "required": [ - "high" - ], - "properties": { - "high": { - "oneOf": [ - { - "type": "null" - }, - { - "type": "string" - } - ] - } - } - } - } - } -}