use bson::doc;
use super::{MongoCountComparison, push_count_slots};
use crate::{
ScalarFilter,
comparison::{TryFromFilter, mongo::MongoConjunction},
};
fn rendered(filter: ScalarFilter<usize>, field: &str) -> bson::Document {
let comparison = MongoCountComparison::<i64>::try_from_filter(filter).expect("a small operand");
let mut accumulator = MongoConjunction::new();
push_count_slots(&comparison, field, &mut accumulator);
accumulator.into_document()
}
#[test]
fn an_ordering_operator_renders_as_a_guarded_expression() {
assert_eq!(
rendered(ScalarFilter::new().with_gt(3), "tags"),
doc! {
"$and": [ {
"$expr": {
"$and": [
{ "$isArray": "$tags" },
{ "$gt": [ { "$size": "$tags" }, 3i64 ] },
],
},
} ],
}
);
}
#[test]
fn equality_takes_the_same_form_as_the_other_seven() {
assert_eq!(
rendered(ScalarFilter::new().with_eq(0), "tags"),
doc! {
"$and": [ {
"$expr": {
"$and": [
{ "$isArray": "$tags" },
{ "$eq": [ { "$size": "$tags" }, 0i64 ] },
],
},
} ],
}
);
}
#[test]
fn negated_membership_wraps_the_positive_form() {
assert_eq!(
rendered(ScalarFilter::new().with_not_in([1, 2]), "tags"),
doc! {
"$and": [ {
"$expr": {
"$and": [
{ "$isArray": "$tags" },
{ "$not": { "$in": [ { "$size": "$tags" }, [ 1i64, 2i64 ] ] } },
],
},
} ],
}
);
}
#[test]
fn an_empty_membership_list_is_the_operator_present() {
let asks_none = rendered(ScalarFilter::new().with_in_([]), "tags");
let asks_all = rendered(ScalarFilter::new().with_not_in([]), "tags");
assert_ne!(asks_none, asks_all);
assert!(rendered(ScalarFilter::new(), "tags").is_empty());
}
#[test]
fn two_operators_are_two_guarded_branches() {
let filter = ScalarFilter::<i32>::new().with_gt(1).with_lt(5);
let comparison = MongoCountComparison::<i64>::try_from_filter(filter).expect("a small operand");
assert_eq!(comparison.conjuncts(), 2);
let mut accumulator = MongoConjunction::new();
push_count_slots(&comparison, "tags", &mut accumulator);
assert_eq!(
accumulator.into_document(),
doc! {
"$and": [
{
"$expr": {
"$and": [
{ "$isArray": "$tags" },
{ "$gt": [ { "$size": "$tags" }, 1i64 ] },
],
},
},
{
"$expr": {
"$and": [
{ "$isArray": "$tags" },
{ "$lt": [ { "$size": "$tags" }, 5i64 ] },
],
},
},
],
}
);
}
#[test]
fn a_nested_path_is_one_field_reference() {
assert_eq!(
rendered(ScalarFilter::new().with_gte(1), "audio.tags"),
doc! {
"$and": [ {
"$expr": {
"$and": [
{ "$isArray": "$audio.tags" },
{ "$gte": [ { "$size": "$audio.tags" }, 1i64 ] },
],
},
} ],
}
);
}
#[test]
#[cfg(target_pointer_width = "64")]
fn an_operand_past_the_width_is_refused() {
let filter = ScalarFilter::<usize>::new().with_eq(usize::MAX);
assert!(MongoCountComparison::<i64>::try_from_filter(filter).is_err());
}
#[test]
fn a_cardinality_reaches_a_filter_only_through_the_walk() {
let comparison =
MongoCountComparison::<i64>::try_from_filter(ScalarFilter::<usize>::new().with_eq(3))
.expect("a small operand");
let mut accumulator = MongoConjunction::new();
push_count_slots(&comparison, "tags", &mut accumulator);
assert!(!accumulator.into_document().is_empty());
}