ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
//! What a page reports about itself, and the ceilings a builder clamps to.

use super::{Edge, Page};
use crate::{
  Cursor, FieldName, OrderDirection, OrderTerm, TermName,
  query::{DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE},
};

/// A one-term order to mint cursors under. Its content does not matter here — a page says nothing
/// about what its cursors mean — but a real one is what keeps the assertions from passing over a
/// shape no engine produces.
const ORDER: &[OrderTerm] = &[OrderTerm::new(
  TermName::new("id"),
  FieldName::new("id"),
  OrderDirection::Asc,
)];

fn page(rows: &[i64]) -> Page<i64, i64> {
  let edges: Vec<Edge<i64, i64>> = rows
    .iter()
    .map(|row| Edge::new(*row, Cursor::at(ORDER, [Some(*row)])))
    .collect();
  Page::new(edges, false, false)
}

/// The end cursors are the page's own first and last, and an empty page has neither — which is what
/// a client past the end is handed rather than a cursor pointing at nothing.
#[test]
fn the_end_cursors_are_the_first_and_last_rows() {
  let page = page(&[10, 20, 30]);
  assert_eq!(page.len(), 3);
  assert_eq!(page.start_cursor(), Some(&Cursor::at(ORDER, [Some(10)])));
  assert_eq!(page.end_cursor(), Some(&Cursor::at(ORDER, [Some(30)])));

  let empty = page_of_nothing();
  assert!(empty.is_empty());
  assert_eq!(empty.start_cursor(), None);
  assert_eq!(empty.end_cursor(), None);
}

fn page_of_nothing() -> Page<i64, i64> {
  page(&[])
}

/// An edge's two halves come apart in the shape they went in, so a caller holding a page can take
/// the records out without cloning the cursors it does not need.
#[test]
fn an_edge_gives_back_what_it_was_built_from() {
  let edge = Edge::new(7i64, Cursor::at(ORDER, [Some(7i64)]));
  assert_eq!(edge.node(), &7);
  assert_eq!(edge.cursor().values(), [Some(7)]);
  assert_eq!(edge.into_parts(), (7, Cursor::at(ORDER, [Some(7i64)])));
}

/// The two booleans are carried rather than derived: which one the extra row answered is the WALK's
/// business, and by the time a page exists the walk is over.
#[test]
fn a_page_reports_the_sides_it_was_told_about() {
  let page = Page::new(page(&[1]).into_edges(), true, false);
  assert!(page.has_next_page());
  assert!(!page.has_previous_page());
}

/// The ceilings, as numbers a reader can see. The DEFAULT is what a client asking to continue and
/// nothing else gets; the MAX is what the widest ask is clamped to — and the clamp is the property,
/// which the engine's own tests assert on the rendered `LIMIT`.
///
/// That the one fits under the other is not asserted here: it is a `const` beside the two, so a
/// default raised past the ceiling is a build that does not finish rather than a test that fails.
#[test]
fn the_ceilings_are_the_documented_numbers() {
  assert_eq!(DEFAULT_PAGE_SIZE, 100);
  assert_eq!(MAX_PAGE_SIZE, 1000);
}