use super::{OrderSignature, OrderTerm, TermName, Walk};
use crate::{ColumnName, ColumnShape, FieldName, OrderDirection, OrderLevel};
const CREATED: &TermName = TermName::new("created_at");
const TITLE: &TermName = TermName::new("title");
const ID: &TermName = TermName::new("id");
fn sqlite_order() -> [OrderTerm; 2] {
[
OrderTerm::new(CREATED, FieldName::new("created_at"), OrderDirection::Desc),
OrderTerm::new(ID, FieldName::new("id"), OrderDirection::Asc),
]
}
fn postgres_order() -> [OrderTerm; 2] {
[
OrderTerm::new(CREATED, FieldName::new("created"), OrderDirection::Desc),
OrderTerm::new(ID, FieldName::new("id"), OrderDirection::Asc),
]
}
#[test]
fn a_rename_does_not_split_one_order_into_two_signatures() {
assert_ne!(sqlite_order()[0].column(), postgres_order()[0].column());
assert_eq!(sqlite_order()[0].identity(), postgres_order()[0].identity());
assert_eq!(
OrderSignature::of(&sqlite_order()),
OrderSignature::of(&postgres_order())
);
}
#[test]
fn crossed_renames_do_not_alias_two_orders_into_one() {
let crossed = [
OrderTerm::new(TITLE, FieldName::new("created"), OrderDirection::Desc),
OrderTerm::new(ID, FieldName::new("id"), OrderDirection::Asc),
];
assert_eq!(crossed[0].column(), postgres_order()[0].column());
assert_ne!(
OrderSignature::of(&crossed),
OrderSignature::of(&postgres_order())
);
}
#[test]
fn a_direction_is_part_of_the_signature() {
let ascending = [OrderTerm::new(
CREATED,
FieldName::new("created_at"),
OrderDirection::Asc,
)];
let descending = [OrderTerm::new(
CREATED,
FieldName::new("created_at"),
OrderDirection::Desc,
)];
assert_ne!(
OrderSignature::of(&ascending),
OrderSignature::of(&descending)
);
}
#[test]
fn the_sequence_is_part_of_the_signature() {
let title_first = [
OrderTerm::new(TITLE, FieldName::new("title"), OrderDirection::Asc),
OrderTerm::new(CREATED, FieldName::new("created_at"), OrderDirection::Asc),
];
let created_first = [
OrderTerm::new(CREATED, FieldName::new("created_at"), OrderDirection::Asc),
OrderTerm::new(TITLE, FieldName::new("title"), OrderDirection::Asc),
];
assert_ne!(
OrderSignature::of(&title_first),
OrderSignature::of(&created_first)
);
}
#[test]
fn identities_are_length_prefixed_so_a_split_cannot_be_moved() {
let split_early = [
OrderTerm::new(
TermName::new("ab"),
FieldName::new("ab"),
OrderDirection::Asc,
),
OrderTerm::new(TermName::new("c"), FieldName::new("c"), OrderDirection::Asc),
];
let split_late = [
OrderTerm::new(TermName::new("a"), FieldName::new("a"), OrderDirection::Asc),
OrderTerm::new(
TermName::new("bc"),
FieldName::new("bc"),
OrderDirection::Asc,
),
];
assert_ne!(
OrderSignature::of(&split_early),
OrderSignature::of(&split_late)
);
}
#[test]
fn nullability_is_not_part_of_the_signature() {
let required = [OrderTerm::new(
CREATED,
FieldName::new("created_at"),
OrderDirection::Asc,
)];
let optional = [OrderTerm::nullable(
CREATED,
FieldName::new("created_at"),
OrderDirection::Asc,
)];
assert_eq!(OrderSignature::of(&required), OrderSignature::of(&optional));
}
#[test]
fn the_fingerprint_is_a_fixed_number() {
assert_eq!(OrderSignature::of(&[]).get(), 0xcbf2_9ce4_8422_2325);
assert_eq!(
OrderSignature::of(&sqlite_order()).get(),
0xdb69_1f89_3575_9e1b
);
}
#[test]
fn a_signature_round_trips_through_its_eight_bytes() {
let signature = OrderSignature::of(&sqlite_order());
assert_eq!(
signature.to_blob(),
signature.get().to_be_bytes(),
"big-endian, as every durable number in this crate is"
);
assert_eq!(
OrderSignature::from_blob(&signature.to_blob()),
Some(signature)
);
assert_eq!(OrderSignature::from_blob(&[0; 7]), None);
assert_eq!(OrderSignature::from_blob(&[0; 9]), None);
}
#[test]
fn a_backward_walk_inverts_every_term() {
assert_eq!(
Walk::Forward.direction(OrderDirection::Asc),
OrderDirection::Asc
);
assert_eq!(
Walk::Forward.direction(OrderDirection::Desc),
OrderDirection::Desc
);
assert_eq!(
Walk::Backward.direction(OrderDirection::Asc),
OrderDirection::Desc
);
assert_eq!(
Walk::Backward.direction(OrderDirection::Desc),
OrderDirection::Asc
);
}
#[test]
fn walking_back_twice_is_walking_forward() {
for written in [OrderDirection::Asc, OrderDirection::Desc] {
assert_eq!(
Walk::Backward.direction(Walk::Backward.direction(written)),
written
);
}
}
#[test]
fn a_term_keeps_its_two_names_apart() {
let term = OrderTerm::new(CREATED, FieldName::new("created"), OrderDirection::Desc);
assert_eq!(term.identity(), CREATED);
assert_eq!(term.column(), FieldName::new("created"));
assert_eq!(term.direction(), OrderDirection::Desc);
assert!(!term.is_nullable());
}
#[test]
fn the_nullable_constructor_changes_nothing_but_nullability() {
let required = OrderTerm::new(CREATED, FieldName::new("created"), OrderDirection::Desc);
let optional = OrderTerm::nullable(CREATED, FieldName::new("created"), OrderDirection::Desc);
assert!(optional.is_nullable());
assert_eq!(optional.identity(), required.identity());
assert_eq!(optional.column(), required.column());
assert_eq!(optional.direction(), required.direction());
}
const THREE_WORDS: &[ColumnShape] = &[
ColumnShape::Plain(ColumnName::new("value")),
ColumnShape::Nullable(ColumnName::new("partial")),
ColumnShape::Derived(ColumnName::new("total")),
];
const THREE_LEVELS: &[OrderLevel] = &[OrderLevel::new(0), OrderLevel::new(1), OrderLevel::new(2)];
#[test]
fn a_derived_level_is_nullable_only_when_its_own_leaf_may_be() {
let term = OrderTerm::spanning(
CREATED,
FieldName::new("span"),
THREE_WORDS,
THREE_LEVELS,
OrderDirection::Asc,
);
assert_eq!(
term
.levels()
.map(|level| level.is_nullable())
.collect::<Vec<_>>(),
[false, true, false],
"plain: required · nullable leaf: nullable · derived-and-total: required"
);
assert_eq!(
term.levels().map(|level| level.leaf()).collect::<Vec<_>>(),
[Some(0), Some(1), Some(2)],
"and each level is one column wide, which is what the compose is asked per"
);
}
#[test]
fn an_optional_holder_makes_every_level_nullable_including_the_total_index() {
let term = OrderTerm::spanning_nullable(
CREATED,
FieldName::new("span"),
THREE_WORDS,
THREE_LEVELS,
OrderDirection::Asc,
);
assert_eq!(
term
.levels()
.map(|level| level.is_nullable())
.collect::<Vec<_>>(),
[true, true, true],
"the marker sits on the field, so it reaches every one of its columns"
);
}