From eba86a25c0d4dc9dddffc018dd074def2f7b56a9 Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Wed, 18 Sep 2024 19:45:40 +0200 Subject: [PATCH] test: More tests for uniqueItems Signed-off-by: Dmitry Dygalo --- .../jsonschema/src/keywords/unique_items.rs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/jsonschema/src/keywords/unique_items.rs b/crates/jsonschema/src/keywords/unique_items.rs index d63a1bd8..329df74c 100644 --- a/crates/jsonschema/src/keywords/unique_items.rs +++ b/crates/jsonschema/src/keywords/unique_items.rs @@ -148,8 +148,10 @@ pub(crate) fn compile<'a>( #[cfg(test)] mod tests { + use super::{is_unique, ITEMS_SIZE_THRESHOLD}; use crate::tests_util; - use serde_json::json; + use serde_json::{json, Value}; + use test_case::test_case; #[test] fn schema_path() { @@ -159,4 +161,32 @@ mod tests { "/uniqueItems", ) } + + #[test_case(&[] => true; "empty array")] + #[test_case(&[json!(1)] => true; "one element array")] + #[test_case(&[json!(1), json!(2)] => true; "two unique elements")] + #[test_case(&[json!(1), json!(1)] => false; "two non-unique elements")] + #[test_case(&[json!(1), json!(2), json!(3)] => true; "three unique elements")] + #[test_case(&[json!(1), json!(2), json!(1)] => false; "three non-unique elements")] + #[test_case(&[json!(1), json!("string"), json!(true), json!(null), json!({"key": "value"}), json!([1, 2, 3])] => true; "mixed types")] + #[test_case(&[json!({"a": 1, "b": 1}), json!({"a": 1, "b": 2}), json!({"a": 1, "b": 3})] => true; "complex objects unique")] + #[test_case(&[json!({"a": 1, "b": 2}), json!({"b": 2, "a": 1}), json!({"a": 1, "b": 2})] => false; "complex objects non-unique")] + fn test_is_unique(items: &[Value]) -> bool { + is_unique(items) + } + + #[test_case(ITEMS_SIZE_THRESHOLD => true; "small array unique")] + #[test_case(ITEMS_SIZE_THRESHOLD + 1 => true; "large array unique")] + fn test_unique_arrays(size: usize) -> bool { + let arr = (1..=size).map(|i| json!(i)).collect::>(); + is_unique(&arr) + } + + #[test_case(ITEMS_SIZE_THRESHOLD => false; "small array non-unique")] + #[test_case(ITEMS_SIZE_THRESHOLD + 1 => false; "large array non-unique")] + fn test_non_unique_arrays(size: usize) -> bool { + let mut arr = (1..=size).map(|i| json!(i)).collect::>(); + arr[size - 1] = json!(1); + is_unique(&arr) + } }