mod composite;
use bson::{Document, doc};
use super::{MongoCollectionCrossingError, MongoListComparison, MongoSetComparison};
use crate::{
At, ComplexComparison, FieldName, ListFilter, Mongo, QuantifiedFilter, RecordAddress,
ScalarFilter, SetFilter, TextFilter, TryFromFilter,
comparison::mongo::{
MongoConjunction, MongoLoweringError, MongoOperator, MongoScalarComparison, MongoTextComparison,
},
};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);
type Sequence = MongoListComparison<i64, MongoScalarComparison<i64>>;
type Set = MongoSetComparison<i64, MongoScalarComparison<i64>>;
fn rendered<C: ComplexComparison<Mongo>>(comparison: &C, column: &str) -> Document {
let frame = At::new(&ROW).frame(FieldName::new(column));
comparison
.apply(frame.at(), MongoConjunction::new())
.expect("this comparison writes nothing an element scope has to hold")
.into_document()
}
fn refusal<C: ComplexComparison<Mongo>>(comparison: &C) -> Option<MongoLoweringError> {
let frame = At::new(&ROW).frame(FieldName::new("tags"));
comparison.apply(frame.at(), MongoConjunction::new()).err()
}
#[test]
fn whole_collection_equality_binds_the_array_itself() {
let comparison =
Sequence::try_from_filter(ListFilter::<i64>::new().with_eq([1, 2])).expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! { "tags": { "$eq": [ 1i64, 2i64 ] } }
);
}
#[test]
fn the_ordering_operators_are_wired_rather_than_refused() {
let comparison = Sequence::try_from_filter(
ListFilter::<i64>::new()
.with_gt([1])
.with_gte([2])
.with_lt([3])
.with_lte([4]),
)
.expect("the identity");
assert_eq!(comparison.conjuncts(), 4);
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": {
"$gt": [ 1i64 ],
"$gte": [ 2i64 ],
"$lt": [ 3i64 ],
"$lte": [ 4i64 ],
},
}
);
}
#[test]
fn membership_and_whole_collection_equality_share_a_key_and_overflow() {
let comparison =
Sequence::try_from_filter(ListFilter::<i64>::new().with_eq([1, 2]).with_has_element(5))
.expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": { "$eq": [ 1i64, 2i64 ] },
"$and": [ { "tags": { "$eq": 5i64 } } ],
}
);
}
#[test]
fn the_element_scope_writes_the_members_operators_bare() {
let comparison = Sequence::try_from_filter(
ListFilter::<i64>::new().with_any(ScalarFilter::new().with_gt(1).with_lt(6)),
)
.expect("the identity");
assert_eq!(comparison.conjuncts(), 1);
assert_eq!(
rendered(&comparison, "tags"),
doc! { "tags": { "$elemMatch": { "$gt": 1i64, "$lt": 6i64 } } }
);
}
#[test]
fn an_empty_element_comparison_asks_for_a_non_empty_collection() {
let comparison =
Sequence::try_from_filter(ListFilter::<i64>::new().with_any(ScalarFilter::new()))
.expect("the identity");
assert!(!comparison.is_empty());
assert_eq!(
rendered(&comparison, "tags"),
doc! { "tags": { "$elemMatch": { "$exists": true } } }
);
}
#[test]
fn a_cardinality_lands_beside_the_fields_keys() {
let comparison = Sequence::try_from_filter(
ListFilter::<i64>::new()
.with_has_element(5)
.with_count(ScalarFilter::new().with_gt(3)),
)
.expect("the identity");
assert_eq!(comparison.conjuncts(), 2);
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": { "$eq": 5i64 },
"$and": [ {
"$expr": {
"$and": [
{ "$isArray": "$tags" },
{ "$gt": [ { "$size": "$tags" }, 3i64 ] },
],
},
} ],
}
);
}
#[test]
fn an_empty_cardinality_folds_in() {
let comparison =
Sequence::try_from_filter(ListFilter::<i64>::new().with_count(ScalarFilter::new()))
.expect("the identity");
assert!(comparison.is_empty());
assert!(rendered(&comparison, "tags").is_empty());
}
#[test]
fn the_quantified_crossing_renders_what_the_wide_one_does() {
let narrow = Sequence::try_from_filter(
QuantifiedFilter::<i64>::new()
.with_any(ScalarFilter::new().with_gt(1).with_lt(6))
.with_count(ScalarFilter::new().with_gte(2)),
)
.expect("a quantified crossing refuses no operand");
let wide = Sequence::try_from_filter(
ListFilter::<i64>::new()
.with_any(ScalarFilter::new().with_gt(1).with_lt(6))
.with_count(ScalarFilter::new().with_gte(2)),
)
.expect("the identity");
assert_eq!(narrow, wide);
assert_eq!(narrow.conjuncts(), wide.conjuncts());
assert_eq!(rendered(&narrow, "tags"), rendered(&wide, "tags"));
}
#[test]
fn the_quantifier_law_crosses_with_the_two_slots() {
let quantified =
Sequence::try_from_filter(QuantifiedFilter::<i64>::new().with_any(ScalarFilter::new()))
.expect("a quantified crossing refuses no operand");
assert!(!quantified.is_empty());
assert_eq!(
rendered(&quantified, "tags"),
doc! { "tags": { "$elemMatch": { "$exists": true } } }
);
let counted =
Sequence::try_from_filter(QuantifiedFilter::<i64>::new().with_count(ScalarFilter::new()))
.expect("a quantified crossing refuses no operand");
assert!(counted.is_empty());
assert!(rendered(&counted, "tags").is_empty());
}
#[test]
fn the_quantified_crossing_leaves_every_element_slot_absent() {
let comparison = Sequence::try_from_filter(
QuantifiedFilter::<i64>::new().with_any(ScalarFilter::new().with_eq(1)),
)
.expect("a quantified crossing refuses no operand");
assert!(comparison.has_element().is_none());
assert!(comparison.eq().is_none());
assert!(comparison.ne().is_none());
assert!(comparison.gt().is_none());
assert!(comparison.gte().is_none());
assert!(comparison.lt().is_none());
assert!(comparison.lte().is_none());
assert_eq!(comparison.conjuncts(), 1);
}
#[test]
fn a_cardinality_past_the_family_integer_is_refused() {
let refused = Sequence::try_from_filter(
QuantifiedFilter::<i64>::new().with_count(ScalarFilter::new().with_eq(usize::MAX)),
);
assert!(matches!(
refused,
Err(MongoCollectionCrossingError::Cardinality(_))
));
}
#[test]
fn the_direct_set_operators_are_one_key_each() {
let comparison = Set::try_from_filter(
SetFilter::<i64>::new()
.with_encloses([1, 2])
.with_overlaps([3])
.with_has_element(4),
)
.expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": {
"$all": [ 1i64, 2i64 ],
"$in": [ 3i64 ],
"$eq": 4i64,
},
}
);
}
#[test]
fn a_subset_test_is_guarded_against_the_missing_field() {
let comparison =
Set::try_from_filter(SetFilter::<i64>::new().with_enclosed_by([1, 2])).expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": {
"$type": "array",
"$not": { "$elemMatch": { "$nin": [ 1i64, 2i64 ] } },
},
}
);
}
#[test]
fn set_equality_is_mutual_containment() {
let comparison =
Set::try_from_filter(SetFilter::<i64>::new().with_eq([1, 2])).expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": {
"$type": "array",
"$all": [ 1i64, 2i64 ],
"$not": { "$elemMatch": { "$nin": [ 1i64, 2i64 ] } },
},
}
);
}
#[test]
fn set_inequality_negates_the_whole_equality_document() {
let comparison =
Set::try_from_filter(SetFilter::<i64>::new().with_ne([1, 2])).expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"$and": [ {
"$nor": [ {
"tags": {
"$type": "array",
"$all": [ 1i64, 2i64 ],
"$not": { "$elemMatch": { "$nin": [ 1i64, 2i64 ] } },
},
} ],
} ],
}
);
}
#[test]
fn enclosing_nothing_is_the_guard_alone() {
let comparison =
Set::try_from_filter(SetFilter::<i64>::new().with_encloses([])).expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! { "tags": { "$type": "array" } }
);
}
#[test]
fn equality_with_an_empty_operand_drops_only_the_all() {
let comparison = Set::try_from_filter(SetFilter::<i64>::new().with_eq([])).expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": {
"$type": "array",
"$not": { "$elemMatch": { "$nin": [] } },
},
}
);
}
#[test]
fn overlapping_nothing_matches_nothing_by_the_operators_own_reading() {
let comparison =
Set::try_from_filter(SetFilter::<i64>::new().with_overlaps([])).expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! { "tags": { "$in": [] } }
);
}
#[test]
fn two_set_operators_sharing_a_key_overflow() {
let comparison = Set::try_from_filter(
SetFilter::<i64>::new()
.with_eq([1])
.with_enclosed_by([1, 2]),
)
.expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": {
"$type": "array",
"$all": [ 1i64 ],
"$not": { "$elemMatch": { "$nin": [ 1i64 ] } },
},
"$and": [
{ "tags": { "$type": "array" } },
{ "tags": { "$not": { "$elemMatch": { "$nin": [ 1i64, 2i64 ] } } } },
],
}
);
}
#[test]
fn an_element_comparison_that_overflows_is_refused() {
let comparison = MongoListComparison::<String, MongoTextComparison<String>>::try_from_filter(
ListFilter::<String, TextFilter<String>>::new().with_any(
TextFilter::<String>::new()
.with_contains("cam".to_owned())
.with_starts_with("ca".to_owned()),
),
)
.expect("the identity");
assert_eq!(
refusal(&comparison),
Some(MongoLoweringError::ElementConjunction)
);
}
#[test]
fn one_affix_operator_on_a_member_renders() {
let comparison = MongoListComparison::<String, MongoTextComparison<String>>::try_from_filter(
ListFilter::<String, TextFilter<String>>::new()
.with_any(TextFilter::<String>::new().with_contains("cam".to_owned())),
)
.expect("the identity");
assert_eq!(
rendered(&comparison, "tags"),
doc! {
"tags": {
"$elemMatch": {
"$regex": bson::Regex {
pattern: "cam".try_into().unwrap(),
options: "".try_into().unwrap(),
},
},
},
}
);
}
#[test]
fn an_operator_the_element_scope_ignores_is_refused_by_name() {
let comparison = MongoListComparison::<i64, Set>::try_from_filter(
ListFilter::<i64, SetFilter<i64>>::new().with_any(SetFilter::new().with_encloses([1])),
)
.expect("the identity");
assert_eq!(
refusal(&comparison),
Some(MongoLoweringError::ElementOperator(MongoOperator::All))
);
}
#[test]
fn a_nested_cardinality_has_no_element_scope() {
let comparison = MongoListComparison::<i64, Set>::try_from_filter(
ListFilter::<i64, SetFilter<i64>>::new()
.with_any(SetFilter::new().with_count(ScalarFilter::new().with_gt(1))),
)
.expect("the identity");
assert_eq!(
refusal(&comparison),
Some(MongoLoweringError::ElementDocument)
);
}
#[test]
#[cfg(target_pointer_width = "64")]
fn a_cardinality_past_the_width_is_refused_at_the_crossing() {
let crossed = Sequence::try_from_filter(
ListFilter::<i64>::new().with_count(ScalarFilter::new().with_eq(usize::MAX)),
);
assert!(matches!(
crossed,
Err(MongoCollectionCrossingError::Cardinality(_))
));
}
#[test]
fn the_element_crossings_refusal_travels_as_its_own() {
use crate::{BytesFilter, comparison::mongo::MongoBytesComparison};
let crossed = MongoListComparison::<Vec<u8>, MongoBytesComparison<Vec<u8>>>::try_from_filter(
ListFilter::<Vec<u8>, BytesFilter<Vec<u8>>>::new()
.with_any(BytesFilter::new().with_contains(b"ab".to_vec())),
);
assert!(matches!(
crossed,
Err(MongoCollectionCrossingError::Element(_))
));
}
#[test]
fn a_member_addressed_by_a_field_is_the_query_form() {
struct Member(MongoScalarComparison<i64>);
impl ComplexComparison<Mongo> for Member {
fn conjuncts(&self) -> usize {
ComplexComparison::<Mongo>::conjuncts(&self.0)
}
fn apply(
&self,
at: At<'_>,
builder: MongoConjunction,
) -> Result<MongoConjunction, MongoLoweringError> {
let frame = at.frame(FieldName::new("price"));
self.0.apply(frame.at(), builder)
}
}
let mut comparison = MongoListComparison::<i64, Member>::new();
comparison.any = Some(Member(
MongoScalarComparison::try_from_filter(ScalarFilter::<i64>::new().with_gt(5))
.expect("the identity"),
));
assert_eq!(
rendered(&comparison, "items"),
doc! { "items": { "$elemMatch": { "price": { "$gt": 5i64 } } } }
);
}
#[test]
fn one_declared_converter_reaches_both_levels() {
fn length(tag: String) -> Result<i64, core::convert::Infallible> {
Ok(tag.len() as i64)
}
let filter = SetFilter::<String, TextFilter<String>>::new()
.with_has_element("stereo".to_owned())
.with_any(TextFilter::<String>::new().with_eq("hdr".to_owned()));
let crossed =
MongoSetComparison::<i64, MongoTextComparison<i64>>::try_from_filter_with(filter, length)
.expect("an infallible crossing");
assert_eq!(crossed.has_element(), Some(&6i64));
assert_eq!(
crossed.any().expect("the element question").eq(),
Some(&3i64),
"the nested operand crossed through the same converter, one level down"
);
}