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
//! The map's own assertions: that a level is the leaf it names, that every flat reading answers the
//! declaration order, and that the one citizen with a semantic order answers with it.
//!
//! Gated on a dialect because [`ToDatabaseOrderBy`](super::ToDatabaseOrderBy) is parameterised by a
//! [`Backend`](crate::Backend) and this crate ships no ungated one. The dialect is incidental to
//! every assertion below — the rows all ignore their parameter today, which is itself one of the
//! things pinned here.

#![cfg(feature = "sqlite")]

use crate::{
  ColumnKind, ColumnName, ColumnShape, OrderLevel, ToCrossedOrderBy, ToDatabaseOrderBy,
  marker::{CompositeMarker, ScalarMarker},
};

type Db = sqlx::Sqlite;

#[test]
fn a_level_is_the_leaf_it_names() {
  assert_eq!(OrderLevel::new(3).leaf(), 3);
  assert_eq!(OrderLevel::new(0).leaf(), 0);
}

/// The DEFAULT is the declaration order, and the empty sequence is how that is spelled — the same
/// convention `ColumnKind::SEGMENTS` uses for *one column*.
#[test]
fn a_scalar_reading_states_no_sequence_of_its_own() {
  assert!(
    <ScalarMarker<i64> as ToDatabaseOrderBy<Db>>::LEVELS.is_empty(),
    "one leaf cannot sort any way but its own"
  );
  assert!(!<ScalarMarker<i64> as ToCrossedOrderBy<Db>>::reorders());
}

/// The empty sequence's reading, held in one place: level `n` is leaf `n`, which is what makes the
/// map's arrival a no-op for every reading that states nothing.
#[test]
fn the_empty_sequence_is_the_identity() {
  for at in 0..4 {
    assert_eq!(
      <ScalarMarker<i64> as ToCrossedOrderBy<Db>>::level(at),
      at,
      "a reading with no sequence sorts its leaves as declared"
    );
  }
}

/// A composite with nothing semantic about its parts is ordered lexicographically by them, and says
/// so by stating nothing — the case every citizen but the instants is in.
#[test]
fn a_plain_composite_takes_the_declaration_order() {
  struct Pair;

  impl ColumnKind for Pair {
    const SEGMENTS: &'static [ColumnShape] = &[
      ColumnShape::Plain(ColumnName::new("num")),
      ColumnShape::Plain(ColumnName::new("den")),
    ];
  }

  assert!(<CompositeMarker<Pair> as ToDatabaseOrderBy<Db>>::LEVELS.is_empty());
  assert!(!<CompositeMarker<Pair> as ToCrossedOrderBy<Db>>::reorders());
}

/// The one shape the map is not a constant for: a citizen whose STORED sequence and whose MEANT
/// sequence differ, teaching its own order through the impl it already writes.
#[test]
fn a_citizen_with_a_derived_leaf_leads_with_it() {
  struct Instant;

  impl ColumnKind for Instant {
    const SEGMENTS: &'static [ColumnShape] = &[
      ColumnShape::Plain(ColumnName::new("pts")),
      ColumnShape::Composite {
        field: Some(ColumnName::new("timebase")),
        segments: &[
          ColumnShape::Plain(ColumnName::new("num")),
          ColumnShape::Plain(ColumnName::new("den")),
        ],
      },
      ColumnShape::Nullable(ColumnName::new("nanos")),
    ];
    const ORDER: &'static [OrderLevel] = &[
      OrderLevel::new(3),
      OrderLevel::new(0),
      OrderLevel::new(1),
      OrderLevel::new(2),
    ];
  }

  // The leaf numbering is `width`'s, not the segment list's: three segments, four leaves.
  assert_eq!(crate::width(<Instant as ColumnKind>::SEGMENTS), 4);
  assert_eq!(crate::value_width(<Instant as ColumnKind>::SEGMENTS), 3);

  assert!(<CompositeMarker<Instant> as ToCrossedOrderBy<Db>>::reorders());
  let sequence: Vec<usize> = (0..4)
    .map(<CompositeMarker<Instant> as ToCrossedOrderBy<Db>>::level)
    .collect();
  assert_eq!(
    sequence,
    [3, 0, 1, 2],
    "the derived leaf leads and the value leaves are the tie-break"
  );
}

/// The shipped citizen, read through the same map — so the const in
/// `primitives::mediatime::timestamp::storage` and the map that answers with it cannot drift.
#[cfg(feature = "mediatime")]
#[test]
fn the_shipped_instant_answers_with_its_semantic_order() {
  let sequence: Vec<usize> = (0..4)
    .map(<CompositeMarker<crate::MediaTimestamp> as ToCrossedOrderBy<Db>>::level)
    .collect();
  assert_eq!(
    sequence,
    [3, 0, 1, 2],
    "nanos, then pts, then the timebase pair"
  );
}