Skip to content

Commit

Permalink
Port garde tests to new integration test style
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Sep 11, 2024
1 parent 8652bce commit 7310b48
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 147 deletions.
59 changes: 59 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ trybuild = "1.0"
serde = { version = "1.0", features = ["derive"] }
jsonschema = { version = "0.18.1", default-features = false, features = ["draft201909", "draft202012"] }
snapbox = { version = "0.6.17", features = ["json"] }
serde_repr = "0.1.19"
# Use github source until published garde version supports `length(equal = ...)` attr
garde = { git = "https://github.com/jprochazk/garde.git", rev = "be00ddddf8de14530ee890ccfdbaf0b13fb32852", features = ["derive", "email", "regex", "url"] }

arrayvec07 = { version = "0.7", default-features = false, features = ["serde"], package = "arrayvec"}
bytes1 = { version = "1.0", default-features = false, features = ["serde"], package = "bytes" }
chrono04 = { version = "0.4", default-features = false, features = ["serde"], package = "chrono" }
bigdecimal04 = { version = "0.4", default-features = false, features = ["serde"], package = "bigdecimal" }
rust_decimal1 = { version = "1", default-features = false, features = ["serde"], package = "rust_decimal" }
either1 = { version = "1.3", default-features = false, features = ["serde"], package = "either" }
serde_repr = "0.1.19"

[features]
default = ["derive", "std"]
Expand Down
99 changes: 0 additions & 99 deletions schemars/tests/garde.rs

This file was deleted.

159 changes: 159 additions & 0 deletions schemars/tests/integration/garde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
use crate::prelude::*;
use garde::Validate;

const ONE: usize = 1;
const HUNDRED: usize = 10;

#[derive(JsonSchema, Deserialize, Serialize, Validate)]
pub struct GardeAttrStruct {
#[garde(range(min = 1.0, max = 100.0))]
min_max: f32,
#[garde(range(min = ONE as f32, max = HUNDRED as f32))]
min_max2: f32,
#[garde(pattern(r"^[Hh]ello"))]
regex_str: String,
#[garde(contains(concat!("sub","string...")))]
contains_str: String,
#[garde(email)]
email_address: String,
#[garde(url)]
homepage: String,
#[garde(length(min = ONE, max = HUNDRED))]
non_empty_str: String,
#[garde(length(equal = 2), inner(length(min = 1)))]
pair: Vec<String>,
#[garde(required)]
required_option: Option<bool>,
#[garde(required, dive)]
#[serde(flatten)]
required_flattened: Option<GardeAttrInner>,
}

#[derive(JsonSchema, Deserialize, Serialize, Validate)]
pub struct GardeAttrInner {
#[garde(range(min = -100, max = 100))]
x: i32,
}

impl Default for GardeAttrStruct {
fn default() -> Self {
Self {
min_max: 1.0,
min_max2: 1.0,
regex_str: "Hello world".to_owned(),
contains_str: "Contains substring...".to_owned(),
email_address: "[email protected]".to_owned(),
homepage: "http://test.test".to_owned(),
non_empty_str: "test".to_owned(),
pair: vec!["a".to_owned(), "b".to_owned()],
required_option: Some(true),
required_flattened: Some(GardeAttrInner { x: 0 }),
}
}
}

impl GardeAttrStruct {
pub fn invalid_values() -> impl IntoIterator<Item = Self> {
static MUTATORS: &[fn(&mut GardeAttrStruct)] = &[
|v| v.min_max = 0.9,
|v| v.min_max = 100.1,
|v| v.min_max2 = 0.9,
|v| v.min_max2 = 100.1,
|v| v.regex_str = "fail".to_owned(),
|v| v.contains_str = "fail".to_owned(),
|v| v.email_address = "fail".to_owned(),
|v| v.homepage = "fail".to_owned(),
|v| v.non_empty_str = String::new(),
|v| v.pair = Vec::new(),
|v| v.pair = vec!["a".to_owned(), "b".to_owned(), "c".to_owned()],
|v| v.pair = vec!["".to_owned(), "b".to_owned()],
|v| v.required_option = None,
|v| v.required_flattened = None,
|v| v.required_flattened = Some(GardeAttrInner { x: -101 }),
|v| v.required_flattened = Some(GardeAttrInner { x: 101 }),
];
MUTATORS.iter().map(|f| {
let mut result = GardeAttrStruct::default();
f(&mut result);
result
})
}
}

#[test]
fn garde_attrs() {
test!(GardeAttrStruct)
.with_validator(|v| v.validate().is_ok())
.assert_snapshot()
.assert_allows_ser_roundtrip_default()
.assert_rejects_invalid(GardeAttrStruct::invalid_values())
.assert_matches_de_roundtrip(arbitrary_values());
}

#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(rename = "GardeAttrStruct")]
pub struct SchemarsAttrStruct {
#[schemars(range(min = 1.0, max = 100.0))]
min_max: f32,
#[schemars(range(min = ONE as f32, max = HUNDRED as f32))]
min_max2: f32,
#[schemars(pattern(r"^[Hh]ello"))]
regex_str: String,
#[schemars(contains(concat!("sub","string...")))]
contains_str: String,
#[schemars(email)]
email_address: String,
#[schemars(url)]
homepage: String,
#[schemars(length(min = ONE, max = HUNDRED))]
non_empty_str: String,
#[schemars(length(equal = 2), inner(length(min = 1)))]
pair: Vec<String>,
#[schemars(required)]
required_option: Option<bool>,
#[schemars(required)]
#[serde(flatten)]
required_flattened: Option<SchemarsAttrInner>,
}

#[allow(dead_code)]
#[derive(JsonSchema)]
pub struct SchemarsAttrInner {
#[schemars(range(min = -100, max = 100))]
x: i32,
}

#[test]
fn schemars_attrs() {
test!(SchemarsAttrStruct).assert_identical::<GardeAttrStruct>();
}

#[derive(JsonSchema, Deserialize, Serialize, Validate)]
pub struct GardeAttrTuple(
#[garde(range(max = 10))] u8,
#[garde(required)] Option<bool>,
);

#[test]
fn garde_attrs_tuple() {
test!(GardeAttrTuple)
.with_validator(|v| v.validate().is_ok())
.assert_snapshot()
.assert_allows_ser_roundtrip([GardeAttrTuple(10, Some(false))])
.assert_rejects_invalid([GardeAttrTuple(11, Some(false)), GardeAttrTuple(10, None)])
.assert_matches_de_roundtrip(arbitrary_values());
}

#[derive(JsonSchema, Deserialize, Serialize, Validate)]
pub struct GardeAttrNewType(#[garde(range(max = 10))] u8);

#[test]
fn garde_attrs_newtype() {
test!(GardeAttrNewType)
.with_validator(|v| v.validate().is_ok())
.assert_snapshot()
.assert_allows_ser_roundtrip([GardeAttrNewType(10)])
.assert_rejects_invalid([GardeAttrNewType(11)])
.assert_matches_de_roundtrip(arbitrary_values());
}
1 change: 1 addition & 0 deletions schemars/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod enums_flattened;
mod extend;
mod flatten;
mod from_value;
mod garde;
mod std_types;

mod prelude {
Expand Down
Loading

0 comments on commit 7310b48

Please sign in to comment.