use super::{OrderFold, OrderSignature, OrderTerm, TermName};
use crate::{FieldName, OrderDirection};
const COVER: &TermName = TermName::new("cover_keyframe_id");
const HERO: &TermName = TermName::new("hero_keyframe_id");
const TITLE: &TermName = TermName::new("title");
const STRIP: &TermName = TermName::new("strip_id");
const REEL: &TermName = TermName::new("reel_id");
const LABEL: &TermName = TermName::new("label");
fn reached_through(relation: &'static TermName, column: &'static str) -> [OrderTerm; 1] {
[OrderTerm::new(TITLE, FieldName::new(column), OrderDirection::Asc).through(relation)]
}
#[test]
fn two_relations_to_one_leaf_are_two_orders() {
assert_eq!(
reached_through(COVER, "title")[0].identity(),
reached_through(HERO, "title")[0].identity()
);
assert_ne!(
OrderSignature::of(&reached_through(COVER, "title")),
OrderSignature::of(&reached_through(HERO, "title"))
);
}
#[test]
fn a_nested_term_is_not_the_plain_column_of_the_same_name() {
let plain = [OrderTerm::new(
TITLE,
FieldName::new("title"),
OrderDirection::Asc,
)];
assert!(plain[0].relation().is_none());
assert_eq!(reached_through(COVER, "title")[0].relation(), Some(COVER));
assert_ne!(
OrderSignature::of(&plain),
OrderSignature::of(&reached_through(COVER, "title"))
);
}
#[test]
fn a_leaf_spelling_the_joined_literal_cannot_pose_as_the_pair() {
let joined = [OrderTerm::new(
TermName::new("cover_keyframe_id.title"),
FieldName::new("title"),
OrderDirection::Asc,
)];
assert_ne!(
OrderSignature::of(&joined),
OrderSignature::of(&reached_through(COVER, "title"))
);
}
#[test]
fn one_nested_term_is_not_two_plain_terms() {
let two = [
OrderTerm::new(COVER, FieldName::new("cover_id"), OrderDirection::Asc),
OrderTerm::new(TITLE, FieldName::new("title"), OrderDirection::Asc),
];
assert_ne!(
OrderSignature::of(&two),
OrderSignature::of(&reached_through(COVER, "title"))
);
}
#[test]
fn the_length_prefix_separates_the_pair_the_bytes_would_join() {
let ab_c = [
OrderTerm::new(TermName::new("c"), FieldName::new("c"), OrderDirection::Asc)
.through(TermName::new("ab")),
];
let a_bc = [OrderTerm::new(
TermName::new("bc"),
FieldName::new("c"),
OrderDirection::Asc,
)
.through(TermName::new("a"))];
assert_ne!(OrderSignature::of(&ab_c), OrderSignature::of(&a_bc));
}
#[test]
fn a_rename_beneath_a_nested_term_does_not_split_the_order() {
assert_ne!(
reached_through(COVER, "title")[0].column(),
reached_through(COVER, "caption")[0].column()
);
assert_eq!(
OrderSignature::of(&reached_through(COVER, "title")),
OrderSignature::of(&reached_through(COVER, "caption"))
);
}
#[test]
fn a_second_relation_replaces_the_first_on_one_term() {
let once = reached_through(COVER, "title");
let twice = [once[0].through(HERO)];
assert_eq!(twice[0].relation(), Some(HERO));
assert_eq!(
OrderSignature::of(&twice),
OrderSignature::of(&reached_through(HERO, "title"))
);
}
#[test]
fn the_walk_agrees_with_the_flat_fold_wherever_both_can_answer() {
let plain = OrderTerm::new(TITLE, FieldName::new("title"), OrderDirection::Asc);
assert_eq!(
OrderFold::new().leaf(&plain).finish(),
OrderSignature::of(&[plain]),
"no hop is the flat order"
);
assert_eq!(
OrderFold::new().hop(COVER).leaf(&plain).finish(),
OrderSignature::of(&reached_through(COVER, "title")),
"one hop is what `through` builds"
);
assert_eq!(
OrderFold::new()
.leaf(&OrderTerm::new(
COVER,
FieldName::new("cover_id"),
OrderDirection::Asc
))
.leaf(&plain)
.finish(),
OrderSignature::of(&[
OrderTerm::new(COVER, FieldName::new("cover_id"), OrderDirection::Asc),
plain,
]),
"and several leaves are several terms, in the order they run"
);
}
#[test]
fn every_depth_is_its_own_order() {
let leaf = OrderTerm::new(LABEL, FieldName::new("label"), OrderDirection::Asc);
let depths = [
OrderFold::new().leaf(&leaf).finish(),
OrderFold::new().hop(COVER).leaf(&leaf).finish(),
OrderFold::new().hop(COVER).hop(STRIP).leaf(&leaf).finish(),
OrderFold::new()
.hop(COVER)
.hop(STRIP)
.hop(REEL)
.leaf(&leaf)
.finish(),
];
for (at, one) in depths.iter().enumerate() {
for other in &depths[at + 1..] {
assert_ne!(one, other, "two depths share a fingerprint");
}
}
}
#[test]
fn the_hops_are_a_path_rather_than_a_set() {
let leaf = OrderTerm::new(LABEL, FieldName::new("label"), OrderDirection::Asc);
assert_ne!(
OrderFold::new().hop(COVER).hop(STRIP).leaf(&leaf).finish(),
OrderFold::new().hop(STRIP).hop(COVER).leaf(&leaf).finish(),
);
}
#[test]
fn the_length_prefix_separates_a_deep_path_the_bytes_would_join() {
let leaf = OrderTerm::new(LABEL, FieldName::new("label"), OrderDirection::Asc);
let ab_c = OrderFold::new()
.hop(TermName::new("ab"))
.hop(TermName::new("c"))
.leaf(&leaf)
.finish();
let a_bc = OrderFold::new()
.hop(TermName::new("a"))
.hop(TermName::new("bc"))
.leaf(&leaf)
.finish();
let joined = OrderFold::new()
.hop(TermName::new("abc"))
.leaf(&leaf)
.finish();
assert_ne!(ab_c, a_bc);
assert_ne!(ab_c, joined);
assert_ne!(a_bc, joined);
}
#[test]
fn a_leaf_spelling_a_deep_joined_literal_cannot_pose_as_the_path() {
let joined = [OrderTerm::new(
TermName::new("cover.strip.label"),
FieldName::new("label"),
OrderDirection::Asc,
)];
let leaf = OrderTerm::new(LABEL, FieldName::new("label"), OrderDirection::Asc);
assert_ne!(
OrderSignature::of(&joined),
OrderFold::new().hop(COVER).hop(STRIP).leaf(&leaf).finish(),
);
}
#[test]
fn a_rename_beneath_a_deep_path_does_not_split_the_order() {
let title = OrderTerm::new(LABEL, FieldName::new("label"), OrderDirection::Asc);
let renamed = OrderTerm::new(LABEL, FieldName::new("caption"), OrderDirection::Asc);
assert_ne!(title.column(), renamed.column());
assert_eq!(
OrderFold::new().hop(COVER).hop(STRIP).leaf(&title).finish(),
OrderFold::new()
.hop(COVER)
.hop(STRIP)
.leaf(&renamed)
.finish(),
);
}
#[test]
fn the_pair_is_two_static_borrows_and_builds_in_a_const() {
const NESTED: OrderTerm =
OrderTerm::new(TITLE, FieldName::new("title"), OrderDirection::Asc).through(COVER);
assert_eq!(NESTED.relation(), Some(COVER));
assert_eq!(NESTED.identity(), TITLE);
assert_eq!(
OrderSignature::of(&[NESTED]),
OrderSignature::of(&reached_through(COVER, "title"))
);
}