use super::{
CollectionBoundary, CollectionOrdering, NullPlacement, OrderCapability, ShadowBoundary,
ShadowOrdering, TermRepetition, Unpushable, compare_collections, partition,
};
use crate::{
FieldName, OrderDirection, OrderTerm, TermName, TermSubject, collection_sort_key,
comparison::sql::SqlOrderError,
};
struct Bytes;
impl OrderCapability for Bytes {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Divergent;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Unbindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Bytewise;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Bindable;
}
struct OtherBytes;
impl OrderCapability for OtherBytes {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Divergent;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Unbindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Divergent;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Bindable;
}
const COMPANION: &FieldName = FieldName::new("__ig_tags_sort__");
fn shadow(direction: OrderDirection) -> OrderTerm {
OrderTerm::shadow(TermName::new("tags"), COMPANION, direction)
}
fn term(name: &'static str, direction: OrderDirection) -> OrderTerm {
OrderTerm::new(TermName::new(name), FieldName::new(name), direction)
}
#[test]
fn the_word_moves_a_collection_out_of_the_remainder() {
let edge = [OrderTerm::edge(
TermName::new("tags"),
FieldName::new("tags"),
OrderDirection::Asc,
)];
assert_eq!(
partition::<Bytes>(&edge).cause(),
Some(Unpushable::CollectionTable),
"without the word the row has no column to order by"
);
let sortable = [shadow(OrderDirection::Asc)];
assert!(
partition::<Bytes>(&sortable).is_whole(),
"with it the order lives entirely in the statement"
);
}
#[test]
fn the_two_names_come_apart_exactly_once() {
let term = shadow(OrderDirection::Asc);
assert_eq!(term.identity(), TermName::new("tags"));
assert_eq!(term.column(), COMPANION);
assert_eq!(term.subject(), TermSubject::Shadow);
assert!(!term.is_nullable(), "a collection has no null state");
}
#[test]
fn a_companion_boundary_is_not_an_array_boundary() {
assert!(TermSubject::Shadow.is_collection());
assert!(!TermSubject::Shadow.binds_the_collection());
assert!(TermSubject::Array.binds_the_collection());
}
#[test]
fn the_bytes_row_is_what_decides_a_companion_term() {
let terms = [shadow(OrderDirection::Asc)];
assert!(partition::<Bytes>(&terms).is_whole());
assert_eq!(
partition::<OtherBytes>(&terms).cause(),
Some(Unpushable::CollectionShadow)
);
}
#[test]
fn the_shipped_rows_answer_as_the_table_says() {
#[cfg(feature = "sqlite")]
assert_eq!(
<sqlx::Sqlite as OrderCapability>::SHADOWS,
ShadowOrdering::Bytewise
);
#[cfg(feature = "postgres")]
assert_eq!(
<sqlx::Postgres as OrderCapability>::SHADOWS,
ShadowOrdering::Bytewise
);
#[cfg(feature = "mysql")]
assert_eq!(
<sqlx::MySql as OrderCapability>::SHADOWS,
ShadowOrdering::Bytewise
);
#[cfg(feature = "mongo")]
assert_eq!(
<crate::Mongo as OrderCapability>::SHADOWS,
ShadowOrdering::Divergent
);
}
#[test]
fn a_page_at_a_companion_term_is_served() {
let terms = [shadow(OrderDirection::Asc), term("id", OrderDirection::Asc)];
assert!(
partition::<Bytes>(&terms).is_whole(),
"the statement carries the whole order — the word's own deliverable"
);
assert_eq!(
partition::<Bytes>(&terms)
.paging::<Bytes>()
.expect("and a companion's bytes are a boundary this dialect binds"),
&terms
);
}
#[test]
fn a_page_with_no_companion_term_is_untouched() {
let terms = [term("id", OrderDirection::Asc)];
assert_eq!(
partition::<Bytes>(&terms)
.paging::<Bytes>()
.expect("nothing about this order names a companion"),
&terms
);
}
#[cfg(feature = "sqlite")]
#[test]
fn a_dialect_renders_a_companion_term_as_a_plain_column() {
use crate::{
Walk,
comparison::sql::{QueryBuilder, SelectClause, SqlOrder},
};
let terms = [shadow(OrderDirection::Asc), term("id", OrderDirection::Asc)];
let order = SqlOrder::<sqlx::Sqlite>::new(&terms).expect("a companion column is a column");
let rendered = order
.push_order_by(
QueryBuilder::<SelectClause, sqlx::Sqlite>::select()
.push_field("*")
.from("t"),
Walk::Forward,
)
.finalize()
.into_string();
assert_eq!(
rendered,
r#"SELECT * FROM t ORDER BY "__ig_tags_sort__" ASC, "id" ASC"#
);
}
#[test]
fn the_render_names_the_bytes_row() {
let terms = [shadow(OrderDirection::Asc)];
let refused = partition::<OtherBytes>(&terms);
assert_eq!(refused.cause(), Some(Unpushable::CollectionShadow));
let message = SqlOrderError::CollectionShadow(TermName::new("tags")).to_string();
assert!(
message.contains("companion column") && message.contains("bytewise"),
"the sentence names the mechanism: {message}"
);
}
#[test]
fn the_companion_order_and_the_pin_are_one_sequence() {
let corpus: Vec<Vec<i64>> = vec![
vec![],
vec![1],
vec![1, 2],
vec![1, 2, 3],
vec![2],
vec![9],
vec![10],
];
let mut by_bytes = corpus.clone();
by_bytes.sort_by_key(|collection| collection_sort_key(collection));
let mut by_pin = corpus;
by_pin.sort_by(|left, right| compare_collections(left, right));
assert_eq!(
by_bytes, by_pin,
"the companion column's bytewise order is the framework's collection order"
);
}