Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: More tests for uniqueItems #519

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}