From 1fd1029e6bc9051dc1530c564f1117ae5f160532 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 8 Sep 2024 20:48:09 +0100 Subject: [PATCH] Port "deprecated" tests to new integration test style --- schemars/tests/deprecated.rs | 39 ----------- schemars/tests/integration/deprecated.rs | 69 +++++++++++++++++++ schemars/tests/integration/main.rs | 3 + .../deprecated.rs~deprecated_enum.de.json} | 0 .../deprecated.rs~deprecated_enum.ser.json | 45 ++++++++++++ .../deprecated.rs~deprecated_struct.de.json} | 4 +- .../deprecated.rs~deprecated_struct.ser.json | 20 ++++++ schemars/tests/integration/test_helper.rs | 5 ++ 8 files changed, 144 insertions(+), 41 deletions(-) delete mode 100644 schemars/tests/deprecated.rs create mode 100644 schemars/tests/integration/deprecated.rs rename schemars/tests/{expected/deprecated-enum.json => integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_enum.de.json} (100%) create mode 100644 schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_enum.ser.json rename schemars/tests/{expected/deprecated-struct.json => integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.de.json} (86%) create mode 100644 schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.ser.json diff --git a/schemars/tests/deprecated.rs b/schemars/tests/deprecated.rs deleted file mode 100644 index 1772a8f5..00000000 --- a/schemars/tests/deprecated.rs +++ /dev/null @@ -1,39 +0,0 @@ -#![allow(deprecated)] - -mod util; -use schemars::JsonSchema; -use util::*; - -#[allow(dead_code)] -#[derive(JsonSchema)] -#[deprecated] -struct DeprecatedStruct { - foo: i32, - #[deprecated] - deprecated_field: bool, -} - -#[test] -fn deprecated_struct() -> TestResult { - test_default_generated_schema::("deprecated-struct") -} - -#[allow(dead_code)] -#[derive(JsonSchema)] -#[deprecated] -enum DeprecatedEnum { - Unit, - #[deprecated] - DeprecatedUnitVariant, - #[deprecated] - DeprecatedStructVariant { - foo: i32, - #[deprecated] - deprecated_field: bool, - }, -} - -#[test] -fn deprecated_enum() -> TestResult { - test_default_generated_schema::("deprecated-enum") -} diff --git a/schemars/tests/integration/deprecated.rs b/schemars/tests/integration/deprecated.rs new file mode 100644 index 00000000..9bdfbef8 --- /dev/null +++ b/schemars/tests/integration/deprecated.rs @@ -0,0 +1,69 @@ +#![allow(deprecated)] + +use crate::prelude::*; + +#[derive(JsonSchema, Default, Serialize, Deserialize)] +#[deprecated] +struct DeprecatedStruct { + foo: i32, + #[deprecated] + bar: bool, +} + +#[allow(deprecated)] +#[test] +fn deprecated_struct() { + test!(DeprecatedStruct) + .assert_snapshot() + .assert_allows_ser_roundtrip_default() + .custom(|schema, _| { + assert_eq!( + schema.as_value().pointer("/deprecated"), + Some(&Value::Bool(true)), + ); + assert_eq!( + schema.as_value().pointer("/properties/bar/deprecated"), + Some(&Value::Bool(true)), + ); + }); +} + +#[derive(JsonSchema, Default, Serialize, Deserialize)] +#[deprecated] +enum DeprecatedEnum { + #[default] + Unit, + #[deprecated] + DeprecatedUnitVariant, + #[deprecated] + DeprecatedStructVariant { + foo: i32, + #[deprecated] + deprecated_field: bool, + }, +} + +#[test] +fn deprecated_enum() { + test!(DeprecatedEnum) + .assert_snapshot() + .assert_allows_ser_roundtrip_default() + .custom(|schema, _| { + assert_eq!( + schema.as_value().pointer("/deprecated"), + Some(&Value::Bool(true)), + ); + assert_eq!( + schema.as_value().pointer("/oneOf/1/deprecated"), + Some(&Value::Bool(true)), + ); + assert_eq!( + schema.as_value().pointer("/oneOf/2/deprecated"), + Some(&Value::Bool(true)), + ); + assert_eq!( + schema.as_value().pointer("/oneOf/2/properties/DeprecatedStructVariant/properties/deprecated_field/deprecated"), + Some(&Value::Bool(true)), + ); + }); +} diff --git a/schemars/tests/integration/main.rs b/schemars/tests/integration/main.rs index 710c0188..3f9efe60 100644 --- a/schemars/tests/integration/main.rs +++ b/schemars/tests/integration/main.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_names)] + #[cfg(feature = "arrayvec07")] mod arrayvec; mod bound; @@ -9,6 +11,7 @@ mod contract; mod crate_alias; #[cfg(any(feature = "rust_decimal1", feature = "bigdecimal04"))] mod decimal; +mod deprecated; mod prelude { pub use crate::test; diff --git a/schemars/tests/expected/deprecated-enum.json b/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_enum.de.json similarity index 100% rename from schemars/tests/expected/deprecated-enum.json rename to schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_enum.de.json diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_enum.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_enum.ser.json new file mode 100644 index 00000000..a61c3223 --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_enum.ser.json @@ -0,0 +1,45 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "DeprecatedEnum", + "oneOf": [ + { + "type": "string", + "enum": [ + "Unit" + ] + }, + { + "type": "string", + "const": "DeprecatedUnitVariant", + "deprecated": true + }, + { + "type": "object", + "properties": { + "DeprecatedStructVariant": { + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "deprecated_field": { + "type": "boolean", + "deprecated": true + } + }, + "required": [ + "foo", + "deprecated_field" + ] + } + }, + "required": [ + "DeprecatedStructVariant" + ], + "additionalProperties": false, + "deprecated": true + } + ], + "deprecated": true +} \ No newline at end of file diff --git a/schemars/tests/expected/deprecated-struct.json b/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.de.json similarity index 86% rename from schemars/tests/expected/deprecated-struct.json rename to schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.de.json index b7396ab0..55967e51 100644 --- a/schemars/tests/expected/deprecated-struct.json +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.de.json @@ -7,14 +7,14 @@ "type": "integer", "format": "int32" }, - "deprecated_field": { + "bar": { "type": "boolean", "deprecated": true } }, "required": [ "foo", - "deprecated_field" + "bar" ], "deprecated": true } \ No newline at end of file diff --git a/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.ser.json b/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.ser.json new file mode 100644 index 00000000..55967e51 --- /dev/null +++ b/schemars/tests/integration/snapshots/schemars/tests/integration/deprecated.rs~deprecated_struct.ser.json @@ -0,0 +1,20 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "DeprecatedStruct", + "type": "object", + "properties": { + "foo": { + "type": "integer", + "format": "int32" + }, + "bar": { + "type": "boolean", + "deprecated": true + } + }, + "required": [ + "foo", + "bar" + ], + "deprecated": true +} \ No newline at end of file diff --git a/schemars/tests/integration/test_helper.rs b/schemars/tests/integration/test_helper.rs index 012ea165..19ccf0b6 100644 --- a/schemars/tests/integration/test_helper.rs +++ b/schemars/tests/integration/test_helper.rs @@ -87,6 +87,11 @@ impl TestHelper { self } + pub fn custom(&self, assertion: impl Fn(&Schema, Contract)) { + assertion(self.de_schema(), Contract::Deserialize); + assertion(self.ser_schema(), Contract::Serialize); + } + fn schema_for(&self, contract: Contract) -> Schema { self.settings .clone()