use bson::{Document, doc};
use super::{MongoTimeRangeComparison, MongoTimeRangedComparison};
use crate::{
At, ComplexComparison, CompositeComparison, FieldName, Mongo, MongoCarrier, RecordAddress,
ScalarFilter, TimeRange, TimeRangeFilter, TimeRanged, TimeRangedFilter, Timestamp, TryFromFilter,
comparison::mongo::{MongoConjunction, MongoScalarComparison},
};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);
fn at(second: i64) -> Timestamp {
Timestamp::from_parts(second, 0).expect("an instant")
}
fn window() -> TimeRange {
TimeRange::new(at(10), at(20)).expect("a stretch")
}
fn instant(second: i64) -> bson::Bson {
at(second).to_bson()
}
fn rendered<C: ComplexComparison<Mongo>>(comparison: &C, field: &str) -> Document {
let frame = At::new(&ROW).frame(FieldName::new(field));
comparison
.apply(frame.at(), MongoConjunction::new())
.expect("a MongoDB lowering cannot refuse")
.into_document()
}
fn crossed(filter: TimeRangeFilter) -> MongoTimeRangeComparison {
MongoTimeRangeComparison::try_from_filter(filter).unwrap_or_else(|infallible| match infallible {})
}
fn document(filter: TimeRangeFilter) -> Document {
rendered(&crossed(filter), "span")
}
#[test]
fn an_empty_filter_asks_nothing() {
let comparison = crossed(TimeRangeFilter::new());
assert_eq!(ComplexComparison::<Mongo>::conjuncts(&comparison), 0);
assert_eq!(document(TimeRangeFilter::new()), Document::new());
}
#[test]
fn overlaps_compares_each_seat_against_the_other_end() {
assert_eq!(
document(TimeRangeFilter::new().with_overlaps(window())),
doc! {
"span.start": { "$lt": instant(20) },
"span.end": { "$gt": instant(10) },
}
);
assert_eq!(
ComplexComparison::<Mongo>::conjuncts(&crossed(TimeRangeFilter::new().with_overlaps(window()))),
2
);
}
#[test]
fn contains_a_span_puts_the_operand_inside_the_field() {
assert_eq!(
document(TimeRangeFilter::new().with_contains(window())),
doc! {
"span.start": { "$lte": instant(10) },
"span.end": { "$gte": instant(20) },
}
);
}
#[test]
fn contains_an_instant_is_half_open_membership() {
assert_eq!(
document(TimeRangeFilter::new().with_contains(at(15))),
doc! {
"span.start": { "$lte": instant(15) },
"span.end": { "$gt": instant(15) },
}
);
}
#[test]
fn within_puts_the_field_inside_the_operand() {
assert_eq!(
document(TimeRangeFilter::new().with_within(window())),
doc! {
"span.start": { "$gte": instant(10) },
"span.end": { "$lte": instant(20) },
}
);
}
#[test]
fn a_degenerate_span_operand_is_not_the_instant_arm() {
assert_eq!(
document(TimeRangeFilter::new().with_contains(TimeRange::instant(at(15)))),
doc! {
"span.start": { "$lte": instant(15) },
"span.end": { "$gte": instant(15) },
}
);
assert_ne!(
document(TimeRangeFilter::new().with_contains(TimeRange::instant(at(15)))),
document(TimeRangeFilter::new().with_contains(at(15)))
);
}
#[test]
fn the_component_filters_merge_under_the_same_keys() {
assert_eq!(
document(
TimeRangeFilter::new()
.with_start(ScalarFilter::new().with_gte(at(5)))
.with_end(ScalarFilter::new().with_lt(at(30))),
),
doc! {
"span.start": { "$gte": instant(5) },
"span.end": { "$lt": instant(30) },
}
);
}
#[test]
fn a_word_and_a_component_share_the_seats_key() {
assert_eq!(
document(
TimeRangeFilter::new()
.with_overlaps(window())
.with_start(ScalarFilter::new().with_gte(at(5))),
),
doc! {
"span.start": { "$lt": instant(20), "$gte": instant(5) },
"span.end": { "$gt": instant(10) },
}
);
}
#[test]
fn two_words_wanting_one_operator_do_not_overwrite_each_other() {
let both = document(
TimeRangeFilter::new()
.with_overlaps(window())
.with_contains(at(15)),
);
assert_eq!(
both,
doc! {
"span.start": { "$lt": instant(20), "$lte": instant(15) },
"span.end": { "$gt": instant(10) },
"$and": [ { "span.end": { "$gt": instant(15) } } ],
}
);
}
#[test]
fn the_comparison_states_the_fields_the_type_spans() {
const SEGMENTS: &[crate::ColumnShape] =
<MongoTimeRangeComparison as CompositeComparison<Mongo>>::SEGMENTS;
assert_eq!(SEGMENTS, <TimeRange as crate::ColumnKind>::SEGMENTS);
}
#[test]
fn a_timed_payload_splices_the_stretch_and_names_the_payload() {
type Timed = MongoTimeRangedComparison<i64, MongoScalarComparison<i64>>;
let comparison = Timed::try_from_filter(
TimeRangedFilter::new()
.with_range(TimeRangeFilter::new().with_overlaps(window()))
.with_data(ScalarFilter::<i64>::new().with_eq(7i64)),
)
.expect("the crossing");
assert_eq!(
rendered(&comparison, "heard"),
doc! {
"heard.start": { "$lt": instant(20) },
"heard.end": { "$gt": instant(10) },
"heard.data": { "$eq": 7_i64 },
}
);
assert_eq!(ComplexComparison::<Mongo>::conjuncts(&comparison), 3);
assert_eq!(
<Timed as CompositeComparison<Mongo>>::SEGMENTS,
<TimeRanged<i64> as crate::ColumnKind>::SEGMENTS
);
}