use bson::{Document, doc};
use super::{MongoDerivedComparison, MongoRefComparison, MongoRelatedDocument};
use crate::{
At, FieldName, RecordAddress, ScalarFilter,
comparison::{
Backend, CollapsibleComparison, ComplexComparison, TryFromFilter,
mongo::{
Mongo, MongoConjunction, MongoCountComparison, MongoLoweringError, MongoPredicate,
MongoScalarComparison, RelationDepthError, RelationWidthError,
},
},
};
const SHOT: RecordAddress = RecordAddress::new(FieldName::new("shot"), &[FieldName::new("id")]);
const KEYFRAME: MongoRelatedDocument = MongoRelatedDocument::new(
RecordAddress::new(FieldName::new("keyframe"), &[FieldName::new("id")]),
&[
"__ig_keyframe_0__",
"__ig_keyframe_1__",
"__ig_keyframe_2__",
],
);
const SPLICE: MongoRelatedDocument = MongoRelatedDocument::new(
RecordAddress::new(
FieldName::new("splice"),
&[FieldName::new("media_id"), FieldName::new("splice_id")],
),
&["__ig_splice_0__", "__ig_splice_1__"],
);
const SCENE: MongoRelatedDocument = MongoRelatedDocument::new(
RecordAddress::new(FieldName::new("scene"), &[FieldName::new("id")]),
&["__ig_scene_0__", "__ig_scene_1__", "__ig_scene_2__"],
);
const SHALLOW: MongoRelatedDocument = MongoRelatedDocument::new(
RecordAddress::new(FieldName::new("scene"), &[FieldName::new("id")]),
&["__ig_scene_0__"],
);
const PARENT_ID: &FieldName = FieldName::new("parent_id");
const SHOT_ID: &FieldName = FieldName::new("shot_id");
type Scalar = MongoScalarComparison<i64>;
type Text = MongoScalarComparison<String>;
type Ref = MongoRefComparison<Keyframe>;
type Derived = MongoDerivedComparison<Keyframe>;
#[derive(Debug, Clone, PartialEq)]
struct Keyframe {
id: Scalar,
title: Text,
referential: bool,
}
impl Keyframe {
fn new() -> Self {
Self {
id: Scalar::new(),
title: Text::new(),
referential: true,
}
}
fn with_id(mut self, id: i64) -> Self {
self.id = Scalar::try_from_filter(ScalarFilter::<i64>::new().with_eq(id)).unwrap();
self
}
fn with_title(mut self, title: &str) -> Self {
self.title =
Text::try_from_filter(ScalarFilter::<std::string::String>::new().with_eq(title.to_owned()))
.unwrap();
self
}
const fn unreferential(mut self) -> Self {
self.referential = false;
self
}
}
impl ComplexComparison<Mongo> for Keyframe {
fn conjuncts(&self) -> usize {
self.id.conjuncts() + self.title.conjuncts()
}
fn apply<'q>(
&'q self,
at: At<'_>,
builder: <Mongo as Backend>::Builder<'q>,
) -> Result<<Mongo as Backend>::Builder<'q>, <Mongo as Backend>::Error> {
let id = at.frame(FieldName::new("id"));
let builder = self.id.apply(id.at(), builder)?;
let title = at.frame(FieldName::new("title"));
self.title.apply(title.at(), builder)
}
}
impl CollapsibleComparison<Mongo> for Keyframe {
type Id = Scalar;
fn only_id(&self) -> Option<&Scalar> {
(self.referential && self.title.is_empty()).then_some(&self.id)
}
}
#[derive(Debug, Clone, PartialEq)]
struct Nesting(Derived);
impl ComplexComparison<Mongo> for Nesting {
fn conjuncts(&self) -> usize {
ComplexComparison::conjuncts(&self.0)
}
fn apply<'q>(
&'q self,
at: At<'_>,
builder: <Mongo as Backend>::Builder<'q>,
) -> Result<<Mongo as Backend>::Builder<'q>, <Mongo as Backend>::Error> {
self.0.apply(at, builder)
}
}
impl CollapsibleComparison<Mongo> for Nesting {
type Id = Scalar;
fn only_id(&self) -> Option<&Scalar> {
None
}
}
fn lowered(
at: At<'_>,
comparison: &impl ComplexComparison<Mongo>,
) -> Result<MongoPredicate, MongoLoweringError> {
Ok(
comparison
.apply(at, MongoConjunction::new())?
.into_predicate(),
)
}
fn at_field(field: &str, comparison: &impl ComplexComparison<Mongo>) -> MongoPredicate {
let frame = At::new(&SHOT).frame(FieldName::new(field));
lowered(frame.at(), comparison).expect("a rendered predicate")
}
fn forward_lookup(pipeline: bson::Bson) -> Document {
doc! { "$lookup": {
"from": "keyframe",
"localField": "cover_id",
"foreignField": "_id",
"as": "__ig_keyframe_1__",
"pipeline": pipeline,
} }
}
fn reverse_lookup() -> Document {
doc! { "$lookup": {
"from": "keyframe",
"localField": "_id",
"foreignField": "shot_id",
"as": "__ig_keyframe_1__",
} }
}
fn reverse_lookup_with(pipeline: bson::Bson) -> Document {
doc! { "$lookup": {
"from": "keyframe",
"localField": "_id",
"foreignField": "shot_id",
"as": "__ig_keyframe_1__",
"pipeline": pipeline,
} }
}
fn dropped() -> Document {
doc! { "$project": { "__ig_keyframe_1__": 0 } }
}
#[test]
fn a_forward_reference_renders_a_semi_join_through_the_reference() {
let cover = Ref::new(KEYFRAME, Keyframe::new().with_title("Autumn"));
let predicate = at_field("cover_id", &cover);
assert_eq!(predicate.filter(), &Document::new());
assert_eq!(
predicate.joins(),
[
forward_lookup(bson::bson!([{ "$match": { "title": { "$eq": "Autumn" } } }])),
doc! { "$match": { "__ig_keyframe_1__": { "$ne": [] } } },
dropped(),
]
);
assert_eq!(predicate.reads(), [FieldName::new("keyframe")]);
assert_eq!(ComplexComparison::conjuncts(&cover), 1);
}
#[test]
fn the_join_pipeline_holds_the_predicate_alone() {
let cover = Ref::new(KEYFRAME, Keyframe::new().with_title("Autumn"));
let predicate = at_field("cover_id", &cover);
let pipeline = predicate.joins()[0]
.get_document("$lookup")
.expect("a lookup body")
.get_array("pipeline")
.expect("a sub-pipeline");
assert_eq!(pipeline.len(), 1);
assert_eq!(
pipeline[0].as_document().expect("a stage").keys().count(),
1
);
assert!(
pipeline[0]
.as_document()
.expect("a stage")
.contains_key("$match")
);
}
#[test]
fn a_predicate_on_the_targets_id_alone_collapses_onto_the_reference() {
let collapsing = Ref::new(KEYFRAME, Keyframe::new().with_id(10));
let predicate = at_field("cover_id", &collapsing);
assert_eq!(predicate.filter(), &doc! { "cover_id": { "$eq": 10i64 } });
assert!(predicate.joins().is_empty(), "the collapse opens no join");
assert!(
predicate.reads().is_empty(),
"and reads no second collection"
);
assert_eq!(ComplexComparison::conjuncts(&collapsing), 1);
let declining = Ref::new(KEYFRAME, Keyframe::new().with_id(10).unreferential());
let predicate = at_field("cover_id", &declining);
assert_eq!(predicate.filter(), &Document::new());
assert_eq!(
predicate.joins()[0],
forward_lookup(bson::bson!([{ "$match": { "_id": { "$eq": 10i64 } } }]))
);
let narrowed = Ref::new(KEYFRAME, Keyframe::new().with_id(10).with_title("Autumn"));
assert_eq!(
at_field("cover_id", &narrowed).joins()[0],
forward_lookup(bson::bson!([{
"$match": { "_id": { "$eq": 10i64 }, "title": { "$eq": "Autumn" } }
}]))
);
}
#[test]
fn an_empty_relation_position_writes_nothing() {
let empty = Ref::new(KEYFRAME, Keyframe::new());
assert_eq!(ComplexComparison::conjuncts(&empty), 0);
assert_eq!(at_field("cover_id", &empty), MongoPredicate::default());
let position = Derived::new(KEYFRAME, SHOT_ID);
assert_eq!(position.conjuncts(), 0);
assert!(position.is_empty());
assert_eq!(
lowered(At::new(&SHOT), &position).expect("nothing to lower"),
MongoPredicate::default()
);
}
#[test]
fn a_relation_whose_address_is_wide_is_refused_and_writes_nothing() {
let frame = At::new(&SHOT).frame(FieldName::new("splice_id"));
let wide_target = MongoRefComparison::new(SPLICE, Keyframe::new().with_title("Autumn"));
assert_eq!(
lowered(frame.at(), &wide_target),
Err(MongoLoweringError::RelationWidth(RelationWidthError::new(
FieldName::new("splice"),
2
)))
);
let collapsing = MongoRefComparison::new(SPLICE, Keyframe::new().with_id(10));
assert!(lowered(frame.at(), &collapsing).is_err());
let reverse = Derived::new(KEYFRAME, SHOT_ID).with_any(Keyframe::new());
assert_eq!(
lowered(At::new(SPLICE.address()), &reverse),
Err(MongoLoweringError::RelationWidth(RelationWidthError::new(
FieldName::new("splice"),
2
)))
);
}
#[test]
fn every_quantifier_opens_a_join_of_its_own() {
let autumn = bson::bson!([{ "$match": { "title": { "$eq": "Autumn" } } }]);
let any = Derived::new(KEYFRAME, SHOT_ID).with_any(Keyframe::new().with_title("Autumn"));
assert_eq!(
lowered(At::new(&SHOT), &any).unwrap().joins(),
[
reverse_lookup_with(autumn.clone()),
doc! { "$match": { "__ig_keyframe_1__": { "$ne": [] } } },
dropped(),
]
);
let none = Derived::new(KEYFRAME, SHOT_ID).with_none(Keyframe::new().with_title("Autumn"));
assert_eq!(
lowered(At::new(&SHOT), &none).unwrap().joins(),
[
reverse_lookup_with(autumn.clone()),
doc! { "$match": { "__ig_keyframe_1__": { "$eq": [] } } },
dropped(),
]
);
let all = Derived::new(KEYFRAME, SHOT_ID).with_all(Keyframe::new().with_title("Autumn"));
assert_eq!(
lowered(At::new(&SHOT), &all).unwrap().joins(),
[
reverse_lookup_with(bson::bson!([{
"$match": { "$nor": [ { "title": { "$eq": "Autumn" } } ] }
}])),
doc! { "$match": { "__ig_keyframe_1__": { "$eq": [] } } },
dropped(),
]
);
let count = Derived::new(KEYFRAME, SHOT_ID).with_count(
MongoCountComparison::try_from_filter(ScalarFilter::<usize>::new().with_gt(2)).unwrap(),
);
assert_eq!(
lowered(At::new(&SHOT), &count).unwrap().joins(),
[
reverse_lookup(),
doc! { "$match": { "$and": [ { "$expr": { "$and": [
{ "$isArray": "$__ig_keyframe_1__" },
{ "$gt": [ { "$size": "$__ig_keyframe_1__" }, 2i64 ] },
] } } ] } },
dropped(),
]
);
}
#[test]
fn the_cardinality_counts_the_position_and_an_empty_one_opens_nothing() {
let count = Derived::new(KEYFRAME, SHOT_ID).with_count(
MongoCountComparison::try_from_filter(ScalarFilter::<usize>::new().with_gt(2)).unwrap(),
);
assert!(
!lowered(At::new(&SHOT), &count).unwrap().joins()[0]
.get_document("$lookup")
.expect("a lookup body")
.contains_key("pipeline"),
"the cardinality's join runs no pipeline"
);
let unasked = Derived::new(KEYFRAME, SHOT_ID).with_count(MongoCountComparison::new());
assert_eq!(unasked.conjuncts(), 0);
assert!(
lowered(At::new(&SHOT), &unasked)
.unwrap()
.joins()
.is_empty()
);
}
#[test]
fn an_empty_predicate_is_vacuous_under_all_and_a_presence_test_under_the_others() {
let all = Derived::new(KEYFRAME, SHOT_ID).with_all(Keyframe::new());
assert_eq!(all.conjuncts(), 1);
assert_eq!(
lowered(At::new(&SHOT), &all).unwrap().joins()[0],
reverse_lookup_with(bson::bson!([{ "$match": { "$nor": [ {} ] } }]))
);
let any = Derived::new(KEYFRAME, SHOT_ID).with_any(Keyframe::new());
assert_eq!(
lowered(At::new(&SHOT), &any).unwrap().joins(),
[
reverse_lookup(),
doc! { "$match": { "__ig_keyframe_1__": { "$ne": [] } } },
dropped(),
]
);
let none = Derived::new(KEYFRAME, SHOT_ID).with_none(Keyframe::new());
assert_eq!(
lowered(At::new(&SHOT), &none).unwrap().joins()[1],
doc! { "$match": { "__ig_keyframe_1__": { "$eq": [] } } }
);
}
#[test]
fn sibling_joins_reuse_one_field_and_each_consumes_it_first() {
let both = Derived::new(KEYFRAME, SHOT_ID)
.with_any(Keyframe::new())
.with_none(Keyframe::new().with_title("Autumn"));
let joins = lowered(At::new(&SHOT), &both).unwrap();
assert_eq!(
joins.joins(),
[
reverse_lookup(),
doc! { "$match": { "__ig_keyframe_1__": { "$ne": [] } } },
dropped(),
reverse_lookup_with(bson::bson!([{ "$match": { "title": { "$eq": "Autumn" } } }])),
doc! { "$match": { "__ig_keyframe_1__": { "$eq": [] } } },
dropped(),
]
);
assert_eq!(joins.reads(), [FieldName::new("keyframe")]);
}
#[test]
fn a_self_relation_lands_its_documents_in_a_minted_field() {
let any = Derived::new(SCENE, PARENT_ID).with_any(Keyframe::new());
assert_eq!(
lowered(At::new(SCENE.address()), &any).unwrap().joins(),
[
doc! { "$lookup": {
"from": "scene",
"localField": "_id",
"foreignField": "parent_id",
"as": "__ig_scene_1__",
} },
doc! { "$match": { "__ig_scene_1__": { "$ne": [] } } },
doc! { "$project": { "__ig_scene_1__": 0 } },
]
);
}
#[test]
fn two_levels_of_one_collection_take_two_join_fields() {
let inner = Derived::new(SCENE, PARENT_ID).with_any(Keyframe::new().with_title("Autumn"));
let outer = MongoRefComparison::new(SCENE, Nesting(inner));
let frame = At::new(SCENE.address()).frame(PARENT_ID);
let predicate = lowered(frame.at(), &outer).unwrap();
assert_eq!(
predicate.joins(),
[
doc! { "$lookup": {
"from": "scene",
"localField": "parent_id",
"foreignField": "_id",
"as": "__ig_scene_1__",
"pipeline": [
{ "$lookup": {
"from": "scene",
"localField": "_id",
"foreignField": "parent_id",
"as": "__ig_scene_2__",
"pipeline": [ { "$match": { "title": { "$eq": "Autumn" } } } ],
} },
{ "$match": { "__ig_scene_2__": { "$ne": [] } } },
{ "$project": { "__ig_scene_2__": 0 } },
],
} },
doc! { "$match": { "__ig_scene_1__": { "$ne": [] } } },
doc! { "$project": { "__ig_scene_1__": 0 } },
]
);
assert_eq!(predicate.reads(), [FieldName::new("scene")]);
}
#[test]
fn a_relation_deeper_than_its_join_fields_is_refused() {
let any = Derived::new(SHALLOW, PARENT_ID).with_any(Keyframe::new());
assert_eq!(
lowered(At::new(SHALLOW.address()), &any),
Err(MongoLoweringError::RelationDepth(RelationDepthError::new(
FieldName::new("scene"),
1,
1
)))
);
let forward = MongoRefComparison::new(SHALLOW, Keyframe::new().with_title("x"));
let frame = At::new(SHALLOW.address()).frame(PARENT_ID);
assert_eq!(
lowered(frame.at(), &forward),
Err(MongoLoweringError::RelationDepth(RelationDepthError::new(
FieldName::new("scene"),
1,
1
)))
);
}
#[test]
fn a_relation_inside_all_is_refused_and_inside_its_neighbours_is_not() {
let nested = || Nesting(Derived::new(SCENE, PARENT_ID).with_any(Keyframe::new()));
let all = MongoDerivedComparison::new(KEYFRAME, SHOT_ID).with_all(nested());
assert_eq!(
lowered(At::new(&SHOT), &all),
Err(MongoLoweringError::NegatedRelation)
);
for position in [
MongoDerivedComparison::new(KEYFRAME, SHOT_ID).with_any(nested()),
MongoDerivedComparison::new(KEYFRAME, SHOT_ID).with_none(nested()),
] {
let predicate = lowered(At::new(&SHOT), &position).expect("a nested join renders");
assert_eq!(
predicate.reads(),
[FieldName::new("keyframe"), FieldName::new("scene")]
);
}
}
#[test]
fn a_relation_inside_an_element_scope_is_refused() {
let cover = Ref::new(KEYFRAME, Keyframe::new().with_title("Autumn"));
let frame = At::new(&SHOT).frame(FieldName::new("cover_id"));
let scope = cover
.apply(frame.at(), MongoConjunction::element())
.expect("the relation writes and the scope remembers");
assert_eq!(
scope.into_element_document(),
Err(MongoLoweringError::ElementRelation)
);
}
#[test]
fn a_derived_position_reads_the_document_and_leaves_the_path_unread() {
let any = Derived::new(KEYFRAME, SHOT_ID).with_any(Keyframe::new().with_title("Autumn"));
let outer = At::new(&SHOT).frame(FieldName::new("covers"));
let inner = outer.at().frame(FieldName::new("deeper"));
let top = lowered(At::new(&SHOT), &any).unwrap();
assert_eq!(top, at_field("covers", &any));
assert_eq!(top, lowered(inner.at(), &any).unwrap());
}
#[test]
fn the_refusals_say_what_is_missing() {
let messages = [
MongoLoweringError::ElementRelation.to_string(),
MongoLoweringError::NegatedRelation.to_string(),
MongoLoweringError::RelationWidth(RelationWidthError::new(FieldName::new("splice"), 2))
.to_string(),
MongoLoweringError::RelationDepth(RelationDepthError::new(FieldName::new("scene"), 9, 8))
.to_string(),
];
assert!(messages[0].contains("$lookup"), "{}", messages[0]);
assert!(messages[1].contains("$nor"), "{}", messages[1]);
assert!(
messages[2].contains("`splice` is addressed by 2 columns"),
"{}",
messages[2]
);
assert!(
messages[3].contains("nests 9 levels deep and its join fields are minted for 8"),
"{}",
messages[3]
);
}
#[test]
fn corpus() {
let autumn = || Keyframe::new().with_title("Autumn");
let cover = at_field("cover_id", &Ref::new(KEYFRAME, autumn()));
let any = lowered(
At::new(&SHOT),
&Derived::new(KEYFRAME, SHOT_ID).with_any(autumn()),
)
.unwrap();
assert_ne!(cover.joins(), any.joins());
assert_eq!(
cover.joins()[0]
.get_document("$lookup")
.unwrap()
.get_str("localField")
.expect("a local field"),
"cover_id"
);
assert_eq!(
any.joins()[0]
.get_document("$lookup")
.unwrap()
.get_str("localField")
.expect("a local field"),
"_id"
);
let all = lowered(
At::new(&SHOT),
&Derived::new(KEYFRAME, SHOT_ID).with_all(autumn()),
)
.unwrap();
let none = lowered(
At::new(&SHOT),
&Derived::new(KEYFRAME, SHOT_ID).with_none(autumn()),
)
.unwrap();
assert_eq!(
all.joins()[1],
none.joins()[1],
"both require an empty join"
);
assert_ne!(all.joins()[0], none.joins()[0], "and negate in one of them");
}