use bson::{Bson, Document, doc};
use super::{Mongo, MongoConjunction, MongoLoweringError, MongoOperator};
use crate::{At, Backend, FieldName, RecordAddress};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);
fn key(at: At<'_>) -> String {
let mut rendered = String::new();
<Mongo as Backend>::write_column(at, &mut rendered);
rendered
}
#[test]
fn one_segment_is_the_bare_field_name() {
let frame = At::new(&ROW).frame(FieldName::new("title"));
assert_eq!(key(frame.at()), "title");
}
#[test]
fn segments_join_with_dots_at_every_depth() {
let outer = At::new(&ROW).frame(FieldName::new("timebase"));
let inner = outer.at().frame(FieldName::new("num"));
assert_eq!(key(inner.at()), "timebase.num");
let deepest = inner.at().frame(FieldName::new("scale"));
assert_eq!(key(deepest.at()), "timebase.num.scale");
}
#[test]
fn the_empty_path_writes_a_key_the_server_refuses() {
assert_eq!(key(At::new(&ROW)), "$");
}
#[cfg(feature = "sqlite")]
#[test]
fn the_two_families_join_the_same_segments_differently() {
fn rendered<B: Backend>(at: At<'_>) -> String
where
B::Buffer: AsRef<str>,
{
let mut out = B::buffer();
B::write_column(at, &mut out);
out.as_ref().to_owned()
}
let outer = At::new(&ROW).frame(FieldName::new("timebase"));
let inner = outer.at().frame(FieldName::new("num"));
assert_eq!(rendered::<sqlx::Sqlite>(inner.at()), "\"timebase_num\"");
assert_eq!(rendered::<Mongo>(inner.at()), "timebase.num");
}
#[test]
fn an_untouched_accumulator_is_the_filter_that_matches_everything() {
assert!(MongoConjunction::new().into_document().is_empty());
}
#[test]
fn two_operators_on_one_field_share_one_key() {
let mut accumulator = MongoConjunction::new();
accumulator.merge("a", MongoOperator::Gt, Bson::Int32(1));
accumulator.merge("a", MongoOperator::Lt, Bson::Int32(5));
assert_eq!(
accumulator.into_document(),
doc! { "a": { "$gt": 1, "$lt": 5 } }
);
}
#[test]
fn two_fields_are_two_keys_and_nothing_joins_them() {
let mut accumulator = MongoConjunction::new();
accumulator.merge("a", MongoOperator::Eq, Bson::Int32(1));
accumulator.merge("b", MongoOperator::Eq, Bson::Int32(2));
assert_eq!(
accumulator.into_document(),
doc! { "a": { "$eq": 1 }, "b": { "$eq": 2 } }
);
}
#[test]
fn a_repeated_operator_overflows_into_and() {
let mut accumulator = MongoConjunction::new();
accumulator.merge("a", MongoOperator::Eq, Bson::Int32(1));
accumulator.merge("a", MongoOperator::Eq, Bson::Int32(2));
accumulator.merge("b", MongoOperator::Gt, Bson::Int32(3));
assert_eq!(
accumulator.into_document(),
doc! {
"a": { "$eq": 1 },
"b": { "$gt": 3 },
"$and": [ { "a": { "$eq": 2 } } ],
}
);
}
#[test]
fn every_operator_carries_its_sigil() {
for op in [
MongoOperator::Eq,
MongoOperator::Ne,
MongoOperator::Gt,
MongoOperator::Gte,
MongoOperator::Lt,
MongoOperator::Lte,
MongoOperator::In,
MongoOperator::Nin,
MongoOperator::Regex,
MongoOperator::Not,
MongoOperator::All,
MongoOperator::ElemMatch,
MongoOperator::Type,
MongoOperator::Exists,
MongoOperator::BitsAllSet,
MongoOperator::BitsAnySet,
MongoOperator::BitsAllClear,
] {
let spelled = match op {
MongoOperator::Eq => "$eq",
MongoOperator::Ne => "$ne",
MongoOperator::Gt => "$gt",
MongoOperator::Gte => "$gte",
MongoOperator::Lt => "$lt",
MongoOperator::Lte => "$lte",
MongoOperator::In => "$in",
MongoOperator::Nin => "$nin",
MongoOperator::Regex => "$regex",
MongoOperator::Not => "$not",
MongoOperator::All => "$all",
MongoOperator::ElemMatch => "$elemMatch",
MongoOperator::Type => "$type",
MongoOperator::Exists => "$exists",
MongoOperator::BitsAllSet => "$bitsAllSet",
MongoOperator::BitsAnySet => "$bitsAnySet",
MongoOperator::BitsAllClear => "$bitsAllClear",
};
assert_eq!(op.as_str(), spelled);
assert!(op.as_str().starts_with('$'));
}
}
#[test]
fn only_all_is_refused_the_element_scope() {
assert!(!MongoOperator::All.in_element_scope());
for op in [
MongoOperator::Eq,
MongoOperator::Ne,
MongoOperator::Gt,
MongoOperator::Gte,
MongoOperator::Lt,
MongoOperator::Lte,
MongoOperator::In,
MongoOperator::Nin,
MongoOperator::Regex,
MongoOperator::Not,
MongoOperator::ElemMatch,
MongoOperator::Type,
MongoOperator::Exists,
] {
assert!(op.in_element_scope(), "{op:?}");
}
}
#[test]
fn operators_at_the_member_become_the_scope_documents_own_keys() {
let mut accumulator = MongoConjunction::element();
accumulator.merge("$", MongoOperator::Gt, Bson::Int32(1));
accumulator.merge("$", MongoOperator::Lt, Bson::Int32(6));
assert_eq!(
accumulator.into_element_document(),
Ok(doc! { "$gt": 1, "$lt": 6 })
);
}
#[test]
fn a_field_below_the_member_keeps_the_and_fold() {
let mut accumulator = MongoConjunction::element();
accumulator.merge("price", MongoOperator::Gt, Bson::Int32(1));
accumulator.merge("price", MongoOperator::Gt, Bson::Int32(2));
assert_eq!(
accumulator.into_element_document(),
Ok(doc! { "price": { "$gt": 1 }, "$and": [ { "price": { "$gt": 2 } } ] })
);
}
#[test]
fn a_repeated_operator_at_the_member_is_refused() {
let mut accumulator = MongoConjunction::element();
accumulator.merge("$", MongoOperator::Gt, Bson::Int32(1));
accumulator.merge("$", MongoOperator::Gt, Bson::Int32(2));
assert_eq!(
accumulator.into_element_document(),
Err(MongoLoweringError::ElementConjunction)
);
}
#[test]
fn a_bare_operator_beside_a_field_name_is_refused() {
let mut accumulator = MongoConjunction::element();
accumulator.merge("$", MongoOperator::Gt, Bson::Int32(1));
accumulator.merge("p", MongoOperator::Eq, Bson::Int32(2));
assert_eq!(
accumulator.into_element_document(),
Err(MongoLoweringError::ElementConjunction)
);
}
#[test]
fn an_operator_the_value_form_ignores_is_refused_by_name() {
let mut accumulator = MongoConjunction::element();
accumulator.merge("$", MongoOperator::All, Bson::Array(vec![Bson::Int32(1)]));
accumulator.merge("$", MongoOperator::All, Bson::Array(vec![Bson::Int32(2)]));
assert_eq!(
accumulator.into_element_document(),
Err(MongoLoweringError::ElementOperator(MongoOperator::All))
);
}
#[test]
fn a_top_level_operator_has_no_element_scope() {
let mut accumulator = MongoConjunction::element();
accumulator.and(doc! { "$expr": { "$gt": [ 1, 0 ] } });
assert_eq!(
accumulator.into_element_document(),
Err(MongoLoweringError::ElementDocument)
);
}
#[test]
fn a_top_level_operator_folds_beside_the_merged_keys() {
let mut accumulator = MongoConjunction::new();
accumulator.merge("a", MongoOperator::Eq, Bson::Int32(1));
accumulator.and(doc! { "$expr": { "$gt": [ 1, 0 ] } });
assert_eq!(
accumulator.into_document(),
doc! { "a": { "$eq": 1 }, "$and": [ { "$expr": { "$gt": [ 1, 0 ] } } ] }
);
}
#[test]
fn an_untouched_element_scope_is_the_empty_document() {
assert_eq!(
MongoConjunction::element().into_element_document(),
Ok(Document::new())
);
}