init
This commit is contained in:
commit
c9d1c372c9
7 changed files with 816 additions and 0 deletions
390
src/test.rs
Normal file
390
src/test.rs
Normal file
|
@ -0,0 +1,390 @@
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::matches;
|
||||
use serde_json::json;
|
||||
|
||||
#[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 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 nested_modifier() {
|
||||
assert!(matches(
|
||||
&json!({
|
||||
"key": {
|
||||
"nested": {
|
||||
"$gt": 5
|
||||
}
|
||||
}
|
||||
}),
|
||||
&json!({
|
||||
"key": { "nested": 7 }
|
||||
})
|
||||
));
|
||||
|
||||
assert!(!matches(
|
||||
&json!({
|
||||
"key": {
|
||||
"nested": {
|
||||
"$gt": 5
|
||||
}
|
||||
}
|
||||
}),
|
||||
&json!({
|
||||
"key": { "nested": 2 }
|
||||
})
|
||||
));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue