use bson::doc;
use super::MongoFlagsComparison;
use crate::{
At, ComplexComparison, FieldName, FlagsFilter, Mongo, RecordAddress, ToDatabaseComparison,
comparison::mongo::MongoConjunction,
};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);
fn lowered(comparison: &MongoFlagsComparison<i64>) -> bson::Document {
let frame = At::new(&ROW).frame(FieldName::new("disposition"));
comparison
.apply(frame.at(), MongoConjunction::new())
.expect("a MongoDB lowering cannot refuse")
.into_document()
}
#[test]
fn the_three_mask_words_lower_to_the_servers_own_bitwise_predicates() {
let comparison = MongoFlagsComparison::<i64>::try_from_filter_with(
FlagsFilter::new()
.with_has_all(0b011i64)
.with_has_any(0b100i64)
.with_has_none(0b1000i64),
Ok::<_, core::convert::Infallible>,
)
.expect("the identity conversion cannot fail");
assert_eq!(comparison.conjuncts(), 3);
assert_eq!(
lowered(&comparison),
doc! {
"disposition": {
"$bitsAllSet": 3i64,
"$bitsAnySet": 4i64,
"$bitsAllClear": 8i64,
}
}
);
}
#[test]
fn the_seven_words_merge_under_one_field_key() {
let comparison = MongoFlagsComparison::<i64>::try_from_filter_with(
FlagsFilter::new()
.with_eq(1i64)
.with_ne(2i64)
.with_in_([1i64, 2])
.with_not_in([4i64])
.with_has_all(1i64)
.with_has_any(2i64)
.with_has_none(4i64),
Ok::<_, core::convert::Infallible>,
)
.expect("the identity conversion cannot fail");
assert_eq!(comparison.conjuncts(), 7);
assert_eq!(
lowered(&comparison),
doc! {
"disposition": {
"$eq": 1i64,
"$ne": 2i64,
"$bitsAllSet": 1i64,
"$bitsAnySet": 2i64,
"$bitsAllClear": 4i64,
"$in": [1i64, 2i64],
"$nin": [4i64],
}
}
);
}
#[test]
fn the_empty_mask_is_written_out_rather_than_dropped() {
let comparison = MongoFlagsComparison::<i64>::try_from_filter_with(
FlagsFilter::new().with_has_any(0i64),
Ok::<_, core::convert::Infallible>,
)
.expect("the identity conversion cannot fail");
assert_eq!(
lowered(&comparison),
doc! { "disposition": { "$bitsAnySet": 0i64 } },
"a set word over no bits is a question with an answer, not an absent conjunct"
);
}
#[test]
fn the_codegen_projection_resolves_for_a_flags_field() {
type Crossed = <FlagsFilter<i64> as ToDatabaseComparison<i64, Mongo>>::Comparison;
let _: Crossed = MongoFlagsComparison::<i64>::new();
}