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

use super::{LocalTimestamp, LocalTimestampOperand, render, split_offset};

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

fn parse(text: &str) -> Result<LocalTimestamp, String> {
  crossed(<LocalTimestampOperand as ScalarType>::parse(Value::String(
    String::from(text),
  )))
}

/// One parse result as the pair a test asserts on: the value, or the sentence a client would read.
fn crossed(
  parsed: async_graphql::InputValueResult<LocalTimestampOperand>,
) -> Result<LocalTimestamp, String> {
  parsed.map(|operand| operand.get()).map_err(|refused| {
    refused
      .into_server_error(async_graphql::Pos::default())
      .message
  })
}

/// **THE TEXT ARM IS THE EXIF SPELLING**, and the offset is optional exactly where the file's is.
#[test]
fn the_text_arm_reads_a_wall_clock_with_or_without_a_zone() {
  assert_eq!(
    parse("2021-08-14T16:45:09"),
    Ok(LocalTimestamp::wall(wall()))
  );
  assert_eq!(
    parse("2021-08-14T16:45:09+09:00"),
    Ok(LocalTimestamp::at(wall(), tz::Offset::constant(9)))
  );
  assert_eq!(
    parse("2021-08-14T16:45:09-04:30"),
    Ok(LocalTimestamp::at(
      wall(),
      tz::Offset::from_seconds(-16_200).expect("inside the domain")
    ))
  );
  assert_eq!(
    parse("2021-08-14T16:45:09Z"),
    Ok(LocalTimestamp::at(wall(), tz::Offset::UTC)),
    "`Z` is a STATED zone and not the absence of one"
  );
}

/// **THE ROUND TRIP IS THE IDENTITY**, which is what a STRUCTURAL operand has to be: the value that
/// went in is the value that comes back, and the two shapes stay two shapes.
#[test]
fn every_shape_renders_back_to_a_string_that_reads_as_itself() {
  for capture in [
    LocalTimestamp::wall(wall()),
    LocalTimestamp::at(wall(), tz::Offset::UTC),
    LocalTimestamp::at(wall(), tz::Offset::constant(9)),
    LocalTimestamp::at(
      civil::date(1892, 1, 1).at(0, 0, 0, 0),
      tz::Offset::from_seconds(2_670).expect("a local mean time"),
    ),
    LocalTimestamp::at(
      civil::date(2021, 8, 14).at(16, 45, 9, 123_456_789),
      tz::Offset::constant(-4),
    ),
  ] {
    let written = render(capture);
    assert_eq!(parse(&written), Ok(capture), "`{written}`");
    assert_eq!(
      LocalTimestampOperand::new(capture).to_value(),
      Value::String(written)
    );
  }
}

/// A file that SAID `+00:00` and a file that said nothing render differently, which is the whole
/// distinction carried to the wire.
#[test]
fn the_silent_file_and_the_utc_one_render_apart() {
  assert_eq!(render(LocalTimestamp::wall(wall())), "2021-08-14T16:45:09");
  assert_eq!(
    render(LocalTimestamp::at(wall(), tz::Offset::UTC)),
    "2021-08-14T16:45:09+00"
  );
}

/// The STRUCTURE arm, and its two keys — an absent `offset` and one written `null` are ONE value,
/// which is GraphQL's own reading of an optional input field.
#[test]
fn the_structure_arm_reads_the_same_two_shapes() {
  use async_graphql::{Name, indexmap::IndexMap};

  fn object(pairs: &[(&str, Value)]) -> Result<LocalTimestamp, String> {
    let mut held = IndexMap::new();
    for (key, value) in pairs {
      held.insert(Name::new(key), value.clone());
    }
    crossed(<LocalTimestampOperand as ScalarType>::parse(Value::Object(
      held,
    )))
  }

  let clock = Value::String(String::from("2021-08-14T16:45:09"));
  assert_eq!(
    object(&[("local", clock.clone())]),
    Ok(LocalTimestamp::wall(wall()))
  );
  assert_eq!(
    object(&[("local", clock.clone()), ("offset", Value::Null)]),
    Ok(LocalTimestamp::wall(wall()))
  );
  assert_eq!(
    object(&[
      ("local", clock.clone()),
      ("offset", Value::String(String::from("+09:00")))
    ]),
    Ok(LocalTimestamp::at(wall(), tz::Offset::constant(9)))
  );

  assert!(object(&[]).is_err(), "the wall clock is not optional");
  assert!(
    object(&[("local", clock), ("zone", Value::Null)]).is_err(),
    "an unknown key is named rather than ignored"
  );
}

/// The refusals: a bare number names no clock face, and neither does a string jiff cannot read.
#[test]
fn the_operand_refuses_what_names_no_clock_face() {
  assert!(
    <LocalTimestampOperand as ScalarType>::parse(Value::Number(1_628_927_109.into())).is_err()
  );
  assert!(parse("").is_err());
  assert!(parse("yesterday").is_err());
  assert!(
    parse("2021-08-14T16:45:09+26:00").is_err(),
    "past the offset domain"
  );
  assert!(
    parse("2021-08-14T16:45:09[Asia/Tokyo]").is_err(),
    "a NAMED zone is not an offset"
  );
}

/// **A NEGATIVE YEAR IS NOT AN OFFSET**, which is the split's own hazard and the reason it looks only
/// after the date/time separator.
#[test]
fn a_negative_year_is_not_read_as_a_zone() {
  assert_eq!(
    split_offset("-009999-01-01T00:00:00"),
    ("-009999-01-01T00:00:00", None)
  );
  assert_eq!(
    split_offset("-009999-01-01T00:00:00+09:00"),
    ("-009999-01-01T00:00:00", Some("+09:00"))
  );
  assert!(parse("-009999-01-01T00:00:00").is_ok());
}

/// `is_valid` and `parse` share ONE notion of validity — a second one is how a scalar comes to accept
/// what it cannot read.
#[test]
fn validity_and_parsing_are_one_answer() {
  for text in [
    "2021-08-14T16:45:09",
    "2021-08-14T16:45:09+09:00",
    "yesterday",
    "",
  ] {
    let value = Value::String(String::from(text));
    assert_eq!(
      <LocalTimestampOperand as ScalarType>::is_valid(&value),
      <LocalTimestampOperand as ScalarType>::parse(value.clone()).is_ok(),
      "`{text}`"
    );
  }
}