use bson::doc;
use super::{MongoOrder, MongoSortError};
use crate::{
FieldName, OrderDirection, OrderTerm, RecordAddress, TermName, Walk,
comparison::mongo::Mongo,
query::{
Unpushable,
mongo::{
MongoQuery,
tests::{ShotMongo, ShotOrder, by_cover_strip_label, by_cover_title, subjects, terms},
},
partition,
},
};
const ADDRESS: RecordAddress = <ShotMongo as MongoQuery>::ADDRESS;
#[test]
fn a_completed_order_ends_with_the_id_ascending() {
let terms = terms(&[ShotOrder::Title(OrderDirection::Asc)]);
let order = MongoOrder::new(&terms).expect("two terms");
assert_eq!(
order.sort(&ADDRESS, Walk::Forward),
doc! { "title": 1, "_id": 1 }
);
}
#[test]
fn each_term_renders_its_own_direction() {
let terms = terms(&[
ShotOrder::Title(OrderDirection::Desc),
ShotOrder::Score(OrderDirection::Asc),
]);
let order = MongoOrder::new(&terms).expect("three terms");
assert_eq!(
order.sort(&ADDRESS, Walk::Forward),
doc! { "title": -1, "score": 1, "_id": 1 }
);
}
#[test]
fn a_backward_walk_inverts_every_term() {
let terms = terms(&[
ShotOrder::Title(OrderDirection::Desc),
ShotOrder::Score(OrderDirection::Asc),
]);
let order = MongoOrder::new(&terms).expect("three terms");
assert_eq!(
order.sort(&ADDRESS, Walk::Backward),
doc! { "title": 1, "score": -1, "_id": -1 }
);
}
#[test]
fn an_order_naming_the_id_keeps_one_entry() {
let terms = terms(&[ShotOrder::Id(OrderDirection::Desc)]);
let order = MongoOrder::new(&terms).expect("one term");
assert_eq!(order.sort(&ADDRESS, Walk::Forward), doc! { "_id": 1 });
assert_eq!(order.terms().len(), 1);
}
#[test]
fn an_order_over_no_terms_is_refused() {
assert_eq!(MongoOrder::new(&[]), Err(MongoSortError::Empty));
}
#[test]
fn two_terms_naming_one_field_are_refused() {
let repeated = [
OrderTerm::new(
TermName::new("title"),
FieldName::new("title"),
OrderDirection::Asc,
),
OrderTerm::new(
TermName::new("caption"),
FieldName::new("title"),
OrderDirection::Desc,
),
];
assert_eq!(
MongoOrder::new(&repeated),
Err(MongoSortError::Repeated(TermName::new("caption")))
);
}
#[test]
fn the_collision_is_over_the_field_and_not_the_identity() {
let distinct = [
OrderTerm::new(
TermName::new("title"),
FieldName::new("title"),
OrderDirection::Asc,
),
OrderTerm::new(
TermName::new("title"),
FieldName::new("caption"),
OrderDirection::Desc,
),
];
assert!(MongoOrder::new(&distinct).is_ok());
}
#[test]
fn the_renderer_refuses_exactly_what_the_capability_row_splits() {
let repeated = [
OrderTerm::new(
TermName::new("title"),
FieldName::new("title"),
OrderDirection::Asc,
),
OrderTerm::new(
TermName::new("caption"),
FieldName::new("title"),
OrderDirection::Desc,
),
];
let split = partition::<Mongo>(&repeated);
assert_eq!(split.cause(), Some(Unpushable::Repetition));
assert!(MongoOrder::new(&repeated).is_err());
assert!(
MongoOrder::new(split.pushable()).is_ok(),
"the prefix the frame leaves is exactly what this renders — the DEGRADE entry"
);
}
#[test]
fn a_nullable_term_needs_no_clause_and_no_refusal() {
let terms = terms(&[ShotOrder::Score(OrderDirection::Desc)]);
assert!(partition::<Mongo>(&terms).is_whole());
assert_eq!(
MongoOrder::new(&terms)
.expect("two terms")
.sort(&ADDRESS, Walk::Forward),
doc! { "score": -1, "_id": 1 }
);
}
#[test]
fn a_nested_term_sorts_by_the_materialized_field() {
let written = by_cover_title();
let terms = terms(&written);
let order = MongoOrder::new(&terms).expect("two terms");
assert_eq!(
order.sort_with(&ADDRESS, Walk::Forward, &subjects(&written)),
doc! { "__ig_ord0__": 1, "_id": 1 }
);
}
#[test]
fn a_deeper_nesting_sorts_by_the_same_one_field() {
let written = by_cover_strip_label();
let terms = terms(&written);
let order = MongoOrder::new(&terms).expect("two terms");
assert_eq!(
order.sort_with(&ADDRESS, Walk::Backward, &subjects(&written)),
doc! { "__ig_ord0__": -1, "_id": -1 }
);
}
#[test]
fn a_collection_is_refused_by_the_rule_it_disagrees_with() {
let terms = [
OrderTerm::array(
TermName::new("tags"),
FieldName::new("tags"),
OrderDirection::Asc,
),
OrderTerm::new(
TermName::new("id"),
FieldName::new("_id"),
OrderDirection::Asc,
),
];
assert_eq!(
MongoOrder::new(&terms),
Err(MongoSortError::Collection(TermName::new("tags")))
);
assert_eq!(
partition::<Mongo>(&terms).cause(),
Some(Unpushable::CollectionSemantics),
"and the refusal is the frame's verdict rather than a scan of this renderer's own"
);
let sentence = MongoSortError::Collection(TermName::new("tags")).to_string();
assert!(
sentence.contains("smallest element ascending") && sentence.contains("largest descending"),
"the sentence names the rule this engine uses, not `unsupported`: {sentence}"
);
}