use super::distribute;
struct Gathered {
parent: i64,
element: &'static str,
}
#[derive(Debug, PartialEq, Eq)]
struct Page {
key: i64,
tags: Vec<&'static str>,
}
impl Page {
const fn empty(key: i64) -> Self {
Self {
key,
tags: Vec::new(),
}
}
}
fn run(page: &mut [Page], gathered: &[Gathered]) {
distribute(
page,
gathered,
|row| row.key,
|row| Ok(row.parent),
|row| Ok(row.element),
|row| &mut row.tags,
)
.expect("neither half of this fixture can refuse");
}
#[test]
fn every_parent_gets_its_own_elements_in_arrival_order() {
let mut page = [Page::empty(1), Page::empty(2)];
run(
&mut page,
&[
Gathered {
parent: 1,
element: "a",
},
Gathered {
parent: 2,
element: "x",
},
Gathered {
parent: 1,
element: "b",
},
Gathered {
parent: 2,
element: "y",
},
],
);
assert_eq!(page[0].tags, ["a", "b"]);
assert_eq!(page[1].tags, ["x", "y"]);
}
#[test]
fn a_parent_with_no_gathered_row_keeps_the_empty_collection() {
let mut page = [Page::empty(1), Page::empty(2), Page::empty(3)];
run(
&mut page,
&[Gathered {
parent: 2,
element: "only",
}],
);
assert_eq!(
page,
[
Page::empty(1),
Page {
key: 2,
tags: vec!["only"]
},
Page::empty(3),
]
);
}
#[test]
fn a_repeated_element_is_kept_twice() {
let mut page = [Page::empty(7)];
run(
&mut page,
&[
Gathered {
parent: 7,
element: "dup",
},
Gathered {
parent: 7,
element: "dup",
},
],
);
assert_eq!(page[0].tags, ["dup", "dup"]);
}
#[test]
fn a_gathered_row_for_a_parent_off_the_page_is_ignored() {
let mut page = [Page::empty(1)];
run(
&mut page,
&[
Gathered {
parent: 99,
element: "elsewhere",
},
Gathered {
parent: 1,
element: "here",
},
],
);
assert_eq!(page[0].tags, ["here"]);
}
#[cfg(feature = "sqlite")]
#[test]
fn a_page_of_the_widest_size_stays_under_every_dialect_ceiling() {
use crate::{comparison::sql::BindCeiling, query::MAX_PAGE_SIZE};
const WIDEST_KEY: usize = 32;
let page = MAX_PAGE_SIZE as usize;
assert_eq!(
<sqlx::Sqlite as BindCeiling>::MAX_BIND_PARAMETERS,
32766,
"the lowest of the three, and the one the derivation is against"
);
assert!(
page * WIDEST_KEY <= <sqlx::Sqlite as BindCeiling>::MAX_BIND_PARAMETERS,
"a full page of 32-column keys binds under SQLite's ceiling"
);
assert!(
page * (WIDEST_KEY + 1) > <sqlx::Sqlite as BindCeiling>::MAX_BIND_PARAMETERS,
"and 32 is the LAST width that does, so the number in the note is the exact one"
);
#[cfg(feature = "postgres")]
assert_eq!(<sqlx::Postgres as BindCeiling>::MAX_BIND_PARAMETERS, 65535);
#[cfg(feature = "mysql")]
assert_eq!(<sqlx::MySql as BindCeiling>::MAX_BIND_PARAMETERS, 65535);
}