#[cfg(test)] mod tests { use crate::matches; use serde_json::json; #[test] fn array_element_match() { assert!(matches( &json!( { "a": { "$contains": 3 }} ), &json!({ "a": [1,2,3,4]}) )); } #[test] fn array_any_str() { assert!(matches( &json!( { "a": { "$any": { "$contains": "world"} }} ), &json!({ "a": ["hello", "world"]}) )); } #[test] fn array_any_sub() { assert!(matches( &json!( { "a": { "$any": { "key": { "$contains": "world"} }}} ), &json!({ "a": [{ "key": "hello" }, {"key": "world"}]}) )); } #[test] fn array_all_nested() { assert!(!matches( &json!( { "key": { "$all": { "$gt": 10} }} ), &json!({ "key": [1,2,3,4,5]}) )); assert!(matches( &json!( { "key": { "$all": { "$gt": 5} }} ), &json!({ "key": [6,7,8,9]}) )); } #[test] fn array_all_direct() { assert!(!matches( &json!( { "$all": { "$gt": 10} } ), &json!([1, 2, 3, 4, 5]) )); assert!(matches( &json!( { "$all": { "$gt": 5} } ), &json!([6, 7, 8, 9]) )); } #[test] fn array_any_direct() { assert!(matches( &json!( { "$any": { "$contains": "world"} } ), &json!(["hello", "world"]) )); } #[test] fn simple_mask() { assert!(matches( &json!( { "key": "value" } ), &json!({ "key": "value", "num": 3 }) )); assert!(!matches( &json!( { "key": "value" } ), &json!({ "key": "not_value", "num": 3 }) )); } #[test] fn nested_mask() { assert!(matches( &json!({ "key": { "nested": "value" } }), &json!({ "key": { "nested": "value" } }) )); } #[test] fn not_equal() { assert!(matches( &json!({ "key": { "$ne": "value" } }), &json!({ "key": "not_value"}) )); } #[test] fn greater_than() { assert!(!matches( &json!({ "key": { "$gt": 5 } }), &json!({ "key": 4}) )); assert!(!matches( &json!({ "key": { "$gt": 5 } }), &json!({ "key": 5}) )); assert!(matches( &json!({ "key": { "$gte": 5 } }), &json!({ "key": 5}) )); } #[test] fn less_than() { assert!(!matches( &json!({ "key": { "$lt": 5 } }), &json!({ "key": 6}) )); assert!(!matches( &json!({ "key": { "$lt": 5 } }), &json!({ "key": 5}) )); assert!(matches( &json!({ "key": { "$lte": 5 } }), &json!({ "key": 5}) )); } #[test] fn text_contains() { assert!(matches( &json!({ "key": { "$contains": "world" } }), &json!({ "key": "hello world" }) )); } #[test] fn range_op() { assert!(matches( &json!({"key": { "$range": [18, 30] }}), &json!({ "key": 20 }) )); assert!(!matches( &json!({"key": { "$range": [18, 30] }}), &json!({ "key": 15 }) )); assert!(!matches( &json!({"key": { "$range": [18, 30] }}), &json!({ "key": 40 }) )); } #[test] fn in_array_contains() { assert!(matches( &json!({ "key": { "$contains": 3 } }), &json!({ "key": [1,2,3,4,5] }) )); assert!(matches( &json!({ "$not": { "key": { "$contains": 3 }} }), &json!({ "key": [1,2,4,5] }) )); } #[test] fn in_array() { assert!(matches( &json!({ "key": { "$in": 3 } }), &json!({ "key": [1,2,3,4,5] }) )); assert!(matches( &json!({ "key": { "$nin": 3 } }), &json!({ "key": [1,2,4,5] }) )); } #[test] fn and_op() { assert!(matches( &json!({ "$and": [ { "key": "value" }, { "num": { "$gt": 5 }} ] }), &json!({ "key": "value", "num": 7 }) )); assert!(!matches( &json!({ "$and": [ { "key": "value" }, { "num": { "$gt": 5 }} ] }), &json!({ "key": "value", "num": 3 }) )); } #[test] fn or_op() { let filter = json!({ "$or": [ { "key": "value" }, { "num": { "$gt": 5} } ] }); assert!(matches( &filter, &json!({ "key": "value", "num": 6 }) )); assert!(matches( &filter, &json!({ "key": "value", "num": 2 }) )); assert!(matches( &filter, &json!({ "key": "not_value", "num": 6 }) )); assert!(!matches( &filter, &json!({ "key": "not_value", "num": 2 }) )); } #[test] fn negation() { assert!(matches( &json!({ "num": { "$not": { "$gt": 5 }} }), &json!({ "num": 3 }) )); } #[test] fn exists() { assert!(matches( &json!({ "key": { "$exists": true } }), &json!({ "key": "value" }) )); assert!(!matches( &json!({ "key": { "$exists": true } }), &json!({}) )); assert!(!matches( &json!({ "key": { "$exists": false } }), &json!({ "key": "value" }) )); } #[test] fn size() { assert!(matches( &json!({ "list": { "$size": 3} }), &json!({ "list": [1,2,3] }) )); assert!(!matches( &json!({ "list": { "$size": 5} }), &json!({ "list": [1,2,3] }) )); } #[test] fn type_match() { assert!(matches( &json!({ "key": { "$type": "number"} }), &json!({ "key": 3 }) )); assert!(matches( &json!({ "key": { "$type": "string"} }), &json!({ "key": "value" }) )); assert!(matches( &json!({ "key": { "$type": "array"} }), &json!({ "key": [1,2,3] }) )); assert!(matches( &json!({ "key": { "$type": "object"} }), &json!({ "key": { "nested": "value" } }) )); } #[test] fn regex_match() { assert!(matches( &json!({ "key": { "$regex": "hello (world|json)"} }), &json!({ "key": "hello world" }) )); assert!(matches( &json!({ "key": { "$regex": "hello (world|json)"} }), &json!({ "key": "hello json" }) )); assert!(!matches( &json!({ "key": { "$regex": "hello (world|json)"} }), &json!({ "key": "hello rust" }) )); assert!(matches( &json!({ "key": { "$regex": "hello (world|json)"} }), &json!({ "key": "hello world!" }) )); } #[test] fn multiple_mask() { assert!(matches( &json!({ "key": "value", "num": 3 }), &json!({ "key": "value", "num": 3, "extra": "value" }) )); assert!(!matches( &json!({ "key": "value", "num": 3 }), &json!({ "key": "value", "num": 5, "extra": "value" }) )); } #[test] fn array_size_cmp() { let empty: Vec = vec![]; let one = vec![1]; let two = vec![1, 2]; let many = vec![1, 2, 3, 4, 5, 6]; let filter = json!({ "list": {"$size": {"$gt": 0}} }); assert!(!matches(&filter, &json!({"list": empty}))); assert!(matches(&filter, &json!({"list": one}))); assert!(matches(&filter, &json!({"list": two}))); assert!(matches(&filter, &json!({"list": many}))); } #[test] fn nested_modifier() { assert!(matches( &json!({ "key": { "nested": { "$gt": 5 } } }), &json!({ "key": { "nested": 7 } }) )); assert!(!matches( &json!({ "key": { "nested": { "$gt": 5 } } }), &json!({ "key": { "nested": 2 } }) )); } }