use std::sync::Arc;
use bson::{Document, doc};
use super::{MongoFields, joins, sort_alias, sort_key};
use crate::{
FieldName, OrderDirection, RecordAddress,
comparison::mongo::MongoLoweringError,
query::{
DEPTH_CEILING,
mongo::{
MongoOrdering, MongoQuery, MongoSubjects,
tests::{
DeepSubjects, KeyframeOrder, ShotMongo, ShotOrder, StripOrder, by_cover_strip_label,
by_cover_title, subjects, terms,
},
},
},
};
const ADDRESS: RecordAddress = <ShotMongo as MongoQuery>::ADDRESS;
fn stages(written: &[ShotOrder]) -> Vec<Document> {
joins(&ADDRESS, &terms(written), &subjects(written))
.expect("this nesting is inside the ceiling")
.into_stages()
}
#[test]
fn one_hop_is_a_lookup_an_add_fields_and_a_project() {
assert_eq!(
stages(&by_cover_title()),
[
doc! { "$lookup": {
"from": "keyframe",
"localField": "cover_id",
"foreignField": "_id",
"as": "__ig_keyframe_1__",
} },
doc! { "$addFields": { "__ig_ord0__": { "$first": "$__ig_keyframe_1__.title" } } },
doc! { "$project": { "__ig_keyframe_1__": 0 } },
]
);
}
#[test]
fn two_hops_chain_the_second_lookup_through_the_firsts_array() {
assert_eq!(
stages(&by_cover_strip_label()),
[
doc! { "$lookup": {
"from": "keyframe",
"localField": "cover_id",
"foreignField": "_id",
"as": "__ig_keyframe_1__",
} },
doc! { "$lookup": {
"from": "strip",
"localField": "__ig_keyframe_1__.strip_id",
"foreignField": "_id",
"as": "__ig_strip_2__",
} },
doc! { "$addFields": { "__ig_ord0__": { "$first": "$__ig_strip_2__.label" } } },
doc! { "$project": { "__ig_keyframe_1__": 0, "__ig_strip_2__": 0 } },
]
);
}
#[test]
fn a_flat_order_adds_no_stages() {
assert!(stages(&[ShotOrder::Title(OrderDirection::Asc)]).is_empty());
}
#[test]
fn a_mixed_order_aliases_by_position_and_leaves_its_plain_levels_alone() {
let written = [
ShotOrder::Title(OrderDirection::Asc),
ShotOrder::Cover(Arc::new(KeyframeOrder::Title(OrderDirection::Asc))),
];
let terms = terms(&written);
let subjects = subjects(&written);
assert_eq!(
stages(&written),
[
doc! { "$lookup": {
"from": "keyframe",
"localField": "cover_id",
"foreignField": "_id",
"as": "__ig_keyframe_1__",
} },
doc! { "$addFields": { "__ig_ord1__": { "$first": "$__ig_keyframe_1__.title" } } },
doc! { "$project": { "__ig_keyframe_1__": 0 } },
]
);
assert_eq!(sort_key(&ADDRESS, &terms, 0, &subjects), "title");
assert_eq!(sort_key(&ADDRESS, &terms, 1, &subjects), "__ig_ord1__");
assert_eq!(sort_key(&ADDRESS, &terms, 2, &subjects), "_id");
}
#[test]
fn two_terms_reaching_one_collection_share_a_name_and_consume_it() {
let written = [
ShotOrder::Cover(Arc::new(KeyframeOrder::Title(OrderDirection::Asc))),
ShotOrder::Cover(Arc::new(KeyframeOrder::Strip(Arc::new(StripOrder::Label(
OrderDirection::Asc,
))))),
];
assert_eq!(
stages(&written),
[
doc! { "$lookup": {
"from": "keyframe",
"localField": "cover_id",
"foreignField": "_id",
"as": "__ig_keyframe_1__",
} },
doc! { "$addFields": { "__ig_ord0__": { "$first": "$__ig_keyframe_1__.title" } } },
doc! { "$project": { "__ig_keyframe_1__": 0 } },
doc! { "$lookup": {
"from": "keyframe",
"localField": "cover_id",
"foreignField": "_id",
"as": "__ig_keyframe_1__",
} },
doc! { "$lookup": {
"from": "strip",
"localField": "__ig_keyframe_1__.strip_id",
"foreignField": "_id",
"as": "__ig_strip_2__",
} },
doc! { "$addFields": { "__ig_ord1__": { "$first": "$__ig_strip_2__.label" } } },
doc! { "$project": { "__ig_keyframe_1__": 0, "__ig_strip_2__": 0 } },
]
);
}
#[test]
fn the_sort_alias_carries_the_terms_position() {
assert_eq!(sort_alias(0), "__ig_ord0__");
assert_eq!(sort_alias(12), "__ig_ord12__");
}
#[test]
fn a_minted_word_can_be_no_instance_of_the_join_frame() {
for level in [0, 1, 9, 10, 512] {
let alias = sort_alias(level);
let body = alias
.strip_prefix("__ig_")
.and_then(|body| body.strip_suffix("__"))
.expect("a minted word wears the prefix and the terminator");
assert!(
!body.contains('_'),
"`{alias}` has a body a `<name>_<depth>` split could reach"
);
}
}
#[test]
fn a_user_column_spelled_the_old_way_is_never_written() {
let rendered = format!("{:?}", stages(&by_cover_title()));
assert!(
!rendered.contains("__ord_"),
"the mint no longer claims a name outside the fence: {rendered}"
);
}
#[test]
fn a_plain_term_sorts_by_its_own_key() {
let terms = terms(&[ShotOrder::Score(OrderDirection::Desc)]);
assert_eq!(sort_key(&ADDRESS, &terms, 0, &MongoFields), "score");
assert_eq!(sort_key(&ADDRESS, &terms, 1, &MongoFields), "_id");
}
#[test]
fn a_nesting_past_the_ceiling_is_refused_by_name() {
let terms = terms(&by_cover_title());
let refused = joins(&ADDRESS, &terms, &DeepSubjects::crossing(DEPTH_CEILING))
.err()
.expect("the eighth hop has no name left");
assert!(matches!(
refused,
MongoLoweringError::RelationDepth(depth)
if depth.table() == FieldName::new("shot")
&& depth.depth() == DEPTH_CEILING
&& depth.ceiling() == DEPTH_CEILING
));
}
#[test]
fn a_nesting_at_the_last_minted_name_still_renders() {
let terms = terms(&by_cover_title());
let stages = joins(&ADDRESS, &terms, &DeepSubjects::crossing(DEPTH_CEILING - 1))
.expect("seven hops have seven names")
.into_stages();
assert_eq!(
stages.len(),
DEPTH_CEILING + 1,
"one per hop, plus the two the run finishes with"
);
}
#[test]
fn a_documents_own_subjects_answer_for_its_nested_levels() {
let written = by_cover_title();
let terms = terms(&written);
let subjects =
<ShotMongo as MongoOrdering>::subjects(&<ShotMongo as MongoOrdering>::complete(&written));
assert!(subjects.nesting(&terms, 0).is_some());
assert!(subjects.nesting(&terms, 1).is_none());
}