Skip to content

Commit

Permalink
test: More tests for uniqueItems
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed Sep 18, 2024
1 parent d133bdc commit eba86a2
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion crates/jsonschema/src/keywords/unique_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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::<Vec<_>>();
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::<Vec<_>>();
arr[size - 1] = json!(1);
is_unique(&arr)
}
}

0 comments on commit eba86a2

Please sign in to comment.