use bson::{Bson, Document, doc};
use super::MongoNullable;
use crate::{
At, ComplexComparison, FieldName, ListFilter, Mongo, Nullable, RecordAddress, ScalarFilter,
SetFilter, ToDatabaseComparison, TryFromFilter,
comparison::mongo::{
MongoConjunction, MongoListComparison, MongoScalarComparison, MongoSetComparison,
},
};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);
const MISSING: i32 = 3;
const EXPLICIT_NULL: i32 = 2;
const ALL: [i32; 9] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
fn measured_null_tests() -> Vec<(Document, Vec<i32>)> {
vec![
(doc! { "$eq": Bson::Null }, vec![2, 3, 6, 7]),
(doc! { "$type": "null" }, vec![2, 6, 7]),
(doc! { "$exists": false }, vec![3]),
]
}
fn measured_value_tests() -> Vec<(Document, Vec<i32>)> {
vec![
(doc! { "$ne": Bson::Null }, vec![1, 4, 5, 8, 9]),
(
doc! { "$exists": true, "$ne": Bson::Null },
vec![1, 4, 5, 8, 9],
),
(doc! { "$exists": true }, vec![1, 2, 4, 5, 6, 7, 8, 9]),
]
}
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 lowering cannot refuse")
.into_document()
}
fn scalar(null: Option<bool>) -> MongoNullable<MongoScalarComparison<i64>> {
let filter = Nullable::new(ScalarFilter::<i64>::new()).maybe_null(null);
MongoNullable::try_from_filter(filter).expect("the identity crossing cannot fail")
}
fn null_test_key(null: bool) -> Document {
rendered(&scalar(Some(null)), "v")
.get_document("v")
.expect("the null test writes one field key")
.clone()
}
fn measured(table: Vec<(Document, Vec<i32>)>, key: &Document) -> Vec<i32> {
table
.into_iter()
.find(|(candidate, _)| candidate == key)
.unwrap_or_else(|| panic!("no measurement for the rendered key {key:?}"))
.1
}
#[test]
fn the_two_states_render_the_two_native_keys() {
assert_eq!(
rendered(&scalar(Some(true)), "v"),
doc! { "v": { "$eq": Bson::Null } }
);
assert_eq!(
rendered(&scalar(Some(false)), "v"),
doc! { "v": { "$ne": Bson::Null } }
);
}
#[test]
fn the_null_test_answers_for_the_missing_field() {
let matched = measured(measured_null_tests(), &null_test_key(true));
assert!(
matched.contains(&MISSING),
"a missing field reads as null, so document {MISSING} answers the null test"
);
assert!(
matched.contains(&EXPLICIT_NULL),
"an explicit null answers it too"
);
}
#[test]
fn the_value_test_excludes_the_null_and_the_missing_field() {
let matched = measured(measured_value_tests(), &null_test_key(false));
assert!(
!matched.contains(&EXPLICIT_NULL),
"document {EXPLICIT_NULL} holds an explicit null and is not a value"
);
assert!(
!matched.contains(&MISSING),
"document {MISSING} has no field and is not a value"
);
}
#[test]
fn the_presence_test_beside_the_value_test_adds_nothing() {
let table = measured_value_tests();
let bare = measured(table.clone(), &doc! { "$ne": Bson::Null });
let guarded = measured(table, &doc! { "$exists": true, "$ne": Bson::Null });
assert_eq!(bare, guarded);
}
#[test]
fn the_two_states_partition_the_corpus() {
let null = measured(measured_null_tests(), &null_test_key(true));
let value = measured(measured_value_tests(), &null_test_key(false));
let mut union = [null.clone(), value.clone()].concat();
union.sort_unstable();
assert_eq!(union, ALL);
assert!(
!null.iter().any(|id| value.contains(id)),
"no document is in both cells"
);
}
#[test]
fn an_unset_null_test_renders_nothing() {
assert_eq!(rendered(&scalar(None), "v"), Document::new());
assert_eq!(scalar(None).conjuncts(), 0);
assert!(ComplexComparison::<Mongo>::is_empty(&scalar(None)));
assert_eq!(scalar(Some(true)).conjuncts(), 1);
assert!(!ComplexComparison::<Mongo>::is_empty(&scalar(Some(true))));
}
#[test]
fn the_null_test_merges_into_the_fields_own_operator_document() {
let filter =
Nullable::new(ScalarFilter::<i64>::new().with_gt(1i64).with_lt(5i64)).with_null(false);
let comparison = MongoNullable::<MongoScalarComparison<i64>>::try_from_filter(filter)
.expect("the identity crossing cannot fail");
assert_eq!(comparison.conjuncts(), 3);
assert_eq!(
rendered(&comparison, "v"),
doc! { "v": { "$gt": 1_i64, "$lt": 5_i64, "$ne": Bson::Null } }
);
}
#[test]
fn a_shared_key_folds_rather_than_overwriting() {
let filter = Nullable::new(ScalarFilter::<i64>::new().with_ne(5i64)).with_null(false);
let comparison = MongoNullable::<MongoScalarComparison<i64>>::try_from_filter(filter)
.expect("the identity crossing cannot fail");
assert_eq!(
rendered(&comparison, "v"),
doc! {
"v": { "$ne": 5_i64 },
"$and": [ { "v": { "$ne": Bson::Null } } ],
}
);
}
#[test]
fn a_collections_own_key_folds_with_the_null_test() {
let filter = Nullable::new(ListFilter::<i64>::new().with_has_element(1i64)).with_null(true);
let comparison =
MongoNullable::<MongoListComparison<i64, MongoScalarComparison<i64>>>::try_from_filter(filter)
.expect("the identity crossing cannot fail");
assert_eq!(
rendered(&comparison, "v"),
doc! {
"v": { "$eq": 1_i64 },
"$and": [ { "v": { "$eq": Bson::Null } } ],
}
);
}
#[test]
fn the_null_test_composes_with_the_collections_member_tautology() {
let filter =
Nullable::new(ListFilter::<i64>::new().with_any(ScalarFilter::new())).with_null(false);
let comparison =
MongoNullable::<MongoListComparison<i64, MongoScalarComparison<i64>>>::try_from_filter(filter)
.expect("the identity crossing cannot fail");
assert_eq!(
rendered(&comparison, "v"),
doc! {
"v": {
"$elemMatch": { "$exists": true },
"$ne": Bson::Null,
},
}
);
}
#[test]
fn the_set_guard_and_the_null_test_are_separate_keys() {
let filter = Nullable::new(SetFilter::<i64>::new().with_enclosed_by([1i64])).with_null(false);
let comparison =
MongoNullable::<MongoSetComparison<i64, MongoScalarComparison<i64>>>::try_from_filter(filter)
.expect("the identity crossing cannot fail");
assert_eq!(
rendered(&comparison, "v"),
doc! {
"v": {
"$type": "array",
"$not": { "$elemMatch": { "$nin": [1_i64] } },
"$ne": Bson::Null,
},
}
);
}
#[test]
fn the_column_option_lands_on_the_wrapper_and_not_on_the_operands() {
use core::marker::PhantomData;
fn same<A>(_: PhantomData<A>, _: PhantomData<A>) {}
same(
PhantomData::<
<Nullable<ScalarFilter<i64>> as ToDatabaseComparison<Option<i64>, Mongo>>::Comparison,
>,
PhantomData::<MongoNullable<MongoScalarComparison<i64>>>,
);
}
#[test]
fn the_default_is_the_empty_inner_with_no_null_test() {
let empty = MongoNullable::<MongoScalarComparison<i64>>::default();
assert_eq!(empty.null(), None);
assert!(empty.inner().is_empty());
assert_eq!(empty, MongoNullable::new(MongoScalarComparison::new()));
}
#[test]
fn nesting_composes_and_each_layer_writes_its_own_state() {
let filter =
Nullable::new(Nullable::new(ScalarFilter::<i64>::new()).with_null(true)).with_null(false);
let comparison =
MongoNullable::<MongoNullable<MongoScalarComparison<i64>>>::try_from_filter(filter)
.expect("the identity crossing cannot fail");
assert_eq!(comparison.conjuncts(), 2);
assert_eq!(
rendered(&comparison, "v"),
doc! { "v": { "$eq": Bson::Null, "$ne": Bson::Null } }
);
}