use core::cmp::Ordering;
use crate::{
FieldName, OrderDirection, OrderTerm, TermName, TermSubject, Timestamp,
query::compare_collections,
};
fn stamps() -> OrderTerm {
OrderTerm::edge(
TermName::new("stamps"),
FieldName::new("stamps"),
OrderDirection::Asc,
)
}
fn id() -> OrderTerm {
OrderTerm::new(
TermName::new("id"),
FieldName::new("id"),
OrderDirection::Asc,
)
}
fn at(second: i64, nanosecond: i32) -> Timestamp {
Timestamp::from_parts(second, nanosecond).expect("a legal instant")
}
#[test]
fn a_composite_collection_is_an_edge_subject() {
assert_eq!(stamps().subject(), TermSubject::Edge);
assert!(stamps().subject().is_collection());
assert_eq!(
stamps().width(),
1,
"a collection is ONE subject however many columns one of its elements is"
);
assert_eq!(id().width(), 1, "and so is a scalar");
}
#[test]
fn the_pin_over_composite_elements_is_the_elements_own_order() {
assert_eq!(
compare_collections::<[Timestamp; 0], _>([], [at(1, 0)]),
Ordering::Less
);
assert_eq!(
compare_collections([at(1, 0), at(2, 0)], [at(1, 0), at(2, 0), at(3, 0)]),
Ordering::Less
);
assert_eq!(
compare_collections([at(2, 0)], [at(1, 0), at(9, 0)]),
Ordering::Greater
);
assert_eq!(
compare_collections([at(10, 250)], [at(10, 0), at(99, 0)]),
Ordering::Greater,
"the first difference is in the SECOND segment, and it decides the whole comparison"
);
}
#[cfg(any(
feature = "sqlite",
feature = "postgres",
feature = "mysql",
feature = "mongo"
))]
mod backends {
use super::{id, stamps};
use crate::{
TermName,
query::{Unpushable, partition},
};
macro_rules! remainder_row {
($name:ident, $backend:ty, $feature:literal) => {
#[cfg(feature = $feature)]
#[test]
fn $name() {
let terms = [id(), stamps()];
let split = partition::<$backend>(&terms);
assert!(
!split.is_whole(),
"an edge-stored collection is not pushable"
);
assert_eq!(split.pushable().len(), 1, "the prefix the statement keeps");
assert_eq!(
split.remainder().len(),
1,
"and the term it could not carry"
);
assert_eq!(split.cause(), Some(Unpushable::CollectionTable));
let refused = split
.paging::<$backend>()
.expect_err("a page over an edge-stored collection has no boundary to resume at");
assert_eq!(refused.term(), TermName::new("stamps"));
assert_eq!(refused.cause(), Unpushable::CollectionTable);
}
};
}
remainder_row!(sqlite_takes_the_edge_remainder, sqlx::Sqlite, "sqlite");
remainder_row!(
postgres_takes_the_edge_remainder,
sqlx::Postgres,
"postgres"
);
remainder_row!(mysql_takes_the_edge_remainder, sqlx::MySql, "mysql");
remainder_row!(mongo_takes_the_edge_remainder, crate::Mongo, "mongo");
#[cfg(feature = "postgres")]
#[test]
fn postgres_pushes_an_array_and_not_an_edge() {
use crate::{FieldName, OrderDirection, OrderTerm};
let array = [OrderTerm::array(
TermName::new("tags"),
FieldName::new("tags"),
OrderDirection::Asc,
)];
assert!(partition::<sqlx::Postgres>(&array).is_whole());
assert!(!partition::<sqlx::Postgres>(&[stamps()]).is_whole());
}
#[cfg(feature = "sqlite")]
#[test]
fn the_remainder_sorts_the_collections_in_memory() {
use crate::Timestamp;
use super::at;
#[derive(Debug, PartialEq, Eq)]
struct Row {
id: i64,
stamps: Vec<Timestamp>,
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum Value {
Id(i64),
Stamps(Vec<Timestamp>),
}
let terms = [id(), stamps()];
let split = partition::<sqlx::Sqlite>(&terms);
let mut rows = [
Row {
id: 1,
stamps: vec![at(10, 250)],
},
Row {
id: 1,
stamps: vec![at(10, 0), at(99, 0)],
},
Row {
id: 1,
stamps: Vec::new(),
},
];
split.apply(&mut rows, |row, term| {
if term.identity() == TermName::new("id") {
return Some(Value::Id(row.id));
}
Some(Value::Stamps(row.stamps.clone()))
});
assert_eq!(
rows.iter().map(|row| row.stamps.len()).collect::<Vec<_>>(),
[0, 2, 1],
"empty is least, then the collection whose first element is lower, whatever the lengths"
);
}
#[cfg(feature = "sqlite")]
#[test]
fn an_order_without_a_collection_pages() {
let terms = [id()];
let split = partition::<sqlx::Sqlite>(&terms);
assert!(split.is_whole());
assert_eq!(
split
.paging::<sqlx::Sqlite>()
.expect("no collection, no refusal")
.len(),
1
);
}
#[cfg(feature = "sqlite")]
#[test]
fn the_paging_refusal_names_the_term_and_the_cause() {
let terms = [id(), stamps()];
let split = partition::<sqlx::Sqlite>(&terms);
let refused: crate::query::PagingOrderError = split
.paging::<sqlx::Sqlite>()
.expect_err("an edge-stored collection has no boundary");
assert_eq!(
refused.to_string(),
"a page's order lives entirely in the statement and `stamps` cannot be put in one: this \
collection is kept in a table of its own, so the row being ordered has no column for it"
);
}
}