use super::{
CollectionBoundary, CollectionOrdering, NullPlacement, OrderCapability, PagingOrderError,
ShadowBoundary, ShadowOrdering, TermRepetition, Unpushable, partition,
};
use crate::{FieldName, OrderDirection, OrderTerm, TermName};
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 Wordless;
impl OrderCapability for Wordless {
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::Unbindable;
}
struct ListsOnly;
impl OrderCapability for ListsOnly {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Lexicographic;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Bindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Bytewise;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Unbindable;
}
const COMPANION: &FieldName = FieldName::new("__ig_tags_sort__");
fn shadow(direction: OrderDirection) -> OrderTerm {
OrderTerm::shadow(TermName::new("tags"), COMPANION, direction)
}
fn array(direction: OrderDirection) -> OrderTerm {
OrderTerm::array(
TermName::new("chapters"),
FieldName::new("chapters"),
direction,
)
}
fn term(name: &'static str, direction: OrderDirection) -> OrderTerm {
OrderTerm::new(TermName::new(name), FieldName::new(name), direction)
}
#[test]
fn a_page_resumes_at_a_companion_term() {
let terms = [shadow(OrderDirection::Asc), term("id", OrderDirection::Asc)];
assert_eq!(
partition::<Bytes>(&terms)
.paging::<Bytes>()
.expect("a companion's bytes are a boundary this backend binds"),
&terms,
"the whole order, in the sequence it runs"
);
}
#[test]
fn either_polarity_is_the_same_answer() {
for direction in [OrderDirection::Asc, OrderDirection::Desc] {
let terms = [shadow(direction), term("id", direction)];
assert!(
partition::<Bytes>(&terms).paging::<Bytes>().is_ok(),
"a boundary is a value, and which way the walk runs is the request's"
);
}
}
#[test]
fn a_backend_that_cannot_bind_a_companion_refuses_by_name() {
let terms = [shadow(OrderDirection::Asc), term("id", OrderDirection::Asc)];
let refused: PagingOrderError = partition::<Wordless>(&terms)
.paging::<Wordless>()
.expect_err("this backend's keyset binds no blob");
assert_eq!(refused.term(), TermName::new("tags"));
assert_eq!(refused.cause(), Unpushable::ShadowCursor);
let sentence = refused.to_string();
assert!(
sentence.contains("`tags`") && sentence.contains("companion column"),
"the sentence names the term and the mechanism: {sentence}"
);
}
#[test]
fn the_scan_is_whole_where_the_page_is_refused() {
let terms = [shadow(OrderDirection::Asc)];
assert!(
partition::<Wordless>(&terms).is_whole(),
"the ORDER BY carries it — that is the word's own deliverable"
);
assert!(partition::<Wordless>(&terms).paging::<Wordless>().is_err());
}
#[test]
fn a_companion_boundary_is_not_an_array_boundary() {
let companion = [shadow(OrderDirection::Asc)];
let list = [array(OrderDirection::Asc)];
assert_eq!(
partition::<ListsOnly>(&companion)
.paging::<ListsOnly>()
.expect_err("it binds lists, not blobs")
.cause(),
Unpushable::ShadowCursor
);
assert!(
partition::<ListsOnly>(&list).paging::<ListsOnly>().is_ok(),
"and the array row still answers for the array"
);
assert!(partition::<Bytes>(&companion).paging::<Bytes>().is_ok());
}
#[test]
fn the_named_term_is_the_first_one_the_page_could_not_take() {
struct Neither;
impl OrderCapability for Neither {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Lexicographic;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Unbindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Bytewise;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Unbindable;
}
let array_first = [array(OrderDirection::Asc), shadow(OrderDirection::Asc)];
let shadow_first = [shadow(OrderDirection::Asc), array(OrderDirection::Asc)];
let first = partition::<Neither>(&array_first)
.paging::<Neither>()
.expect_err("neither boundary binds");
assert_eq!(first.term(), TermName::new("chapters"));
assert_eq!(first.cause(), Unpushable::CollectionCursor);
let second = partition::<Neither>(&shadow_first)
.paging::<Neither>()
.expect_err("neither boundary binds");
assert_eq!(second.term(), TermName::new("tags"));
assert_eq!(second.cause(), Unpushable::ShadowCursor);
}
#[test]
fn a_page_with_no_companion_term_is_untouched() {
let terms = [term("id", OrderDirection::Asc)];
assert_eq!(
partition::<Wordless>(&terms)
.paging::<Wordless>()
.expect("nothing about this order names a companion"),
&terms
);
}
#[test]
fn the_statements_refusal_outranks_the_boundarys() {
struct Divergent;
impl OrderCapability for Divergent {
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;
}
let terms = [shadow(OrderDirection::Asc)];
assert_eq!(
partition::<Divergent>(&terms)
.paging::<Divergent>()
.expect_err("the ORDER BY could not carry it")
.cause(),
Unpushable::CollectionShadow,
"the narrower sentence is the statement's, and it is asked first"
);
}
#[test]
fn the_shipped_rows_answer_as_the_table_says() {
#[cfg(feature = "sqlite")]
assert_eq!(
<sqlx::Sqlite as OrderCapability>::SHADOW_BOUNDARY,
ShadowBoundary::Bindable
);
#[cfg(feature = "postgres")]
assert_eq!(
<sqlx::Postgres as OrderCapability>::SHADOW_BOUNDARY,
ShadowBoundary::Bindable
);
#[cfg(feature = "mysql")]
assert_eq!(
<sqlx::MySql as OrderCapability>::SHADOW_BOUNDARY,
ShadowBoundary::Bindable
);
#[cfg(feature = "mongo")]
assert_eq!(
<crate::Mongo as OrderCapability>::SHADOW_BOUNDARY,
ShadowBoundary::Unbindable
);
}