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
use jiff::{civil, tz};

use super::{LocalTimestamp, LocalTimestampFilter, NoUtcPosition};

fn wall() -> civil::DateTime {
  civil::date(2021, 8, 14).at(16, 45, 9, 0)
}

/// The three constructors name ONE type, and the `Option` route is the other two read together.
#[test]
fn the_three_constructors_agree() {
  let offset = tz::Offset::constant(9);

  assert_eq!(
    LocalTimestamp::wall(wall()),
    LocalTimestamp::new(wall(), None)
  );
  assert_eq!(
    LocalTimestamp::at(wall(), offset),
    LocalTimestamp::new(wall(), Some(offset))
  );
  assert_eq!(LocalTimestamp::wall(wall()).offset(), None);
  assert_eq!(LocalTimestamp::at(wall(), offset).offset(), Some(offset));
  assert!(!LocalTimestamp::wall(wall()).is_zoned());
  assert!(LocalTimestamp::at(wall(), offset).is_zoned());
}

/// **THE WHOLE POINT, IN TWO LINES.** A file that said `+00:00` and a file that said nothing are two
/// values, and every face keeps them apart.
///
/// Under two independent fields this is the distinction a reader has to remember; here it is
/// equality's own answer.
#[test]
fn a_stated_utc_is_not_the_same_value_as_no_zone_at_all() {
  let stated = LocalTimestamp::at(wall(), tz::Offset::UTC);
  let silent = LocalTimestamp::wall(wall());

  assert_ne!(stated, silent);
  assert_eq!(
    stated.to_utc().map(|utc| utc.to_string()).ok(),
    Some(String::from("2021-08-14T16:45:09Z"))
  );
  assert_eq!(silent.to_utc(), Err(NoUtcPosition::Unzoned));
}

/// The composition is jiff's, applied where the type has both halves — and the fixture is the one a
/// Tokyo photograph gives.
#[test]
fn a_zoned_wall_clock_names_the_instant_its_offset_makes_it() {
  let capture = LocalTimestamp::at(wall(), tz::Offset::constant(9));

  assert_eq!(
    capture.to_utc().expect("both halves are here").to_string(),
    "2021-08-14T07:45:09Z"
  );
  assert_eq!(
    capture.to_utc_nanos().expect("inside the ceiling"),
    1_628_927_109_000_000_000
  );
}

/// **THE REFUSAL, AND IT IS THE RULING** — a UTC instant is never invented for a value that named no
/// zone, at any face.
#[test]
fn an_unzoned_wall_clock_has_no_instant_and_no_position() {
  let capture = LocalTimestamp::wall(wall());

  assert_eq!(capture.to_utc(), Err(NoUtcPosition::Unzoned));
  assert_eq!(capture.to_utc_nanos(), Err(NoUtcPosition::Unzoned));
}

/// The ±292-year nanosecond ceiling, from both sides — and the value columns are untouched by it,
/// which is what the storage face's `NULL` posture rests on.
#[test]
fn a_zoned_wall_clock_past_the_nanosecond_ceiling_has_an_instant_and_no_position() {
  for local in [
    civil::date(1500, 1, 1).at(0, 0, 0, 0),
    civil::date(2500, 1, 1).at(0, 0, 0, 0),
  ] {
    let capture = LocalTimestamp::at(local, tz::Offset::UTC);

    assert!(capture.to_utc().is_ok(), "the instant exists");
    assert!(
      matches!(
        capture.to_utc_nanos(),
        Err(NoUtcPosition::NanosecondOverflow { .. })
      ),
      "and has no i64 nanosecond position"
    );
    assert_eq!(capture.local(), local, "the value is untouched by either");
  }
}

/// The far edge of the civil span composed with the far edge of the offset domain, which is the one
/// place [`NoUtcPosition::OutOfRange`] is reachable.
#[test]
fn the_civil_extremes_can_compose_past_the_instant_range() {
  let latest = LocalTimestamp::at(civil::DateTime::MAX, tz::Offset::MIN);
  assert_eq!(latest.to_utc(), Err(NoUtcPosition::OutOfRange));

  let earliest = LocalTimestamp::at(civil::DateTime::MIN, tz::Offset::MAX);
  assert_eq!(earliest.to_utc(), Err(NoUtcPosition::OutOfRange));
}

/// The declared [`Ord`] is the STORAGE one — a wall clock then a displacement — and it is
/// deliberately NOT the chronological one; see the type's own note, where the partial order is
/// argued away.
#[test]
fn the_declared_order_is_the_storage_order_and_not_the_clock() {
  let tokyo = LocalTimestamp::at(wall(), tz::Offset::constant(9));
  let london = LocalTimestamp::at(wall(), tz::Offset::constant(1));

  // Tokyo's instant is EARLIER — 07:45Z against 15:45Z — and the declared order says the opposite,
  // because it compares the offsets after the equal wall clocks.
  assert!(tokyo.to_utc().unwrap() < london.to_utc().unwrap());
  assert!(tokyo > london);

  // An unzoned value sorts BELOW every zoned one at the same clock, `None` being `Option`'s least.
  assert!(LocalTimestamp::wall(wall()) < london);
}

/// The empty filter asks nothing, and a component's own emptiness is read through rather than
/// treated as an operator.
#[test]
fn an_empty_filter_is_empty_through_its_components() {
  assert!(LocalTimestampFilter::new().is_empty());
  assert!(LocalTimestampFilter::default().is_empty());
  assert!(
    LocalTimestampFilter::new()
      .with_local(crate::ScalarFilter::new())
      .is_empty(),
    "a component slot holding an empty filter asks nothing"
  );

  assert!(!LocalTimestampFilter::new().with_zoned(false).is_empty());
  assert!(
    !LocalTimestampFilter::new()
      .with_exact(LocalTimestamp::wall(wall()))
      .is_empty()
  );
  assert!(
    !LocalTimestampFilter::new()
      .with_local(crate::ScalarFilter::new().with_eq(wall()))
      .is_empty()
  );
}