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 super::*;
use crate::primitives::mediatime::tests::rate;

/// The reduction lands on lowest terms over the whole constructible domain, and is idempotent — so a
/// value crossing the boundary more than once cannot creep.
#[test]
fn reduce_puts_every_rate_in_lowest_terms() {
  for (declared, reduced) in [
    ((0, 1), (0, 1)),
    ((0, 90_000), (0, 1)),
    ((30, 1), (30, 1)),
    ((30_000, 1001), (30_000, 1001)),
    ((60_000, 2002), (30_000, 1001)),
    ((48_000, 1), (48_000, 1)),
    ((2, 4), (1, 2)),
    ((i32::MAX, i32::MAX), (1, 1)),
  ] {
    let (num, den) = rate(declared.0, declared.1).reduced();
    assert_eq!((num, den.get()), reduced, "{}/{}", declared.0, declared.1);

    let (again_num, again_den) = rate(num, den.get()).reduced();
    assert_eq!((again_num, again_den.get()), reduced, "idempotent");
  }
}

/// Rates the domain calls equal reduce to ONE pair — what makes an index on the two stored columns
/// answer equality exactly.
#[test]
fn equal_rates_reduce_to_the_same_pair() {
  let ntsc = rate(30_000, 1001);
  let doubled = rate(60_000, 2002);

  assert_eq!(ntsc, doubled, "mediatime compares rates by value");
  assert_eq!(ntsc.reduced(), doubled.reduced());
}

/// The three layers each do the right DIFFERENT thing: the type compares by value, `Display`
/// preserves the pair the stream declared, and the storage form canonicalises.
#[test]
fn the_rendering_keeps_what_the_reduction_discards() {
  let declared = rate(60_000, 2002);

  assert_eq!(declared.to_string(), "60000/2002");
  assert_eq!(declared.reduced().0, 30_000);
  assert_eq!(declared.reduced().1.get(), 1001);
  assert_eq!(declared, rate(30_000, 1001));
}

/// **The value stored is the RATE's reading, and the reciprocal is one call away** — the relation to
/// the timebase citizen, pinned so that a column convention cannot drift into the other reading.
#[test]
fn a_rate_and_its_timebase_are_one_value_read_two_ways() {
  let ntsc = rate(30_000, 1001);

  assert_eq!(
    ntsc.reduced().0,
    30_000,
    "the stored numerator is the rate's"
  );
  let timebase = ntsc
    .get()
    .checked_to_timebase()
    .expect("a non-zero rate has a reciprocal");
  assert_eq!(timebase, mt::Timebase::NTSC_VIDEO);
  assert_eq!(timebase.num(), 1001, "the reciprocal swaps the pair");

  // Both rosters name the same value in their own reading, and neither door reads the other's names.
  assert_eq!(mt::Rate::from_name("FPS_29_97"), Some(ntsc.get()));
  assert_eq!(mt::Rate::from_name("NTSC_VIDEO"), None);
  assert_eq!(mt::Timebase::from_name("FPS_29_97"), None);

  // A zero rate has no reciprocal, which is the one case the wire face publishes as null.
  assert_eq!(rate(0, 1).get().checked_to_timebase(), None);
}

/// `per_second` is the division, total and lossy; `29.97` is NOT `30000/1001`, which is the whole
/// reason a bare decimal is refused at the door.
#[test]
fn the_per_second_reading_is_the_division() {
  assert_eq!(rate(30, 1).per_second(), 30.0);
  assert_eq!(rate(0, 1).per_second(), 0.0);

  let ntsc = rate(30_000, 1001).per_second();
  assert!((ntsc - 29.970_029_97).abs() < 1e-9);
  assert_ne!(ntsc, 29.97, "a client's `29.97` names a different rational");
}

/// The canonical spelling names exactly the rates that exist, and each refusal says which half or
/// rule broke.
#[test]
fn the_canonical_form_names_exactly_the_rates_that_exist() {
  assert_eq!("30000/1001".parse(), Ok(rate(30_000, 1001)));
  assert_eq!("0/1".parse(), Ok(rate(0, 1)));

  assert_eq!("1/0".parse::<Rate>(), Err(ParseRateError::ZeroDenominator));
  assert_eq!("-1/2".parse::<Rate>(), Err(ParseRateError::NegativeHalf));
  assert_eq!("1/-2".parse::<Rate>(), Err(ParseRateError::NegativeHalf));
  assert_eq!("a/b".parse::<Rate>(), Err(ParseRateError::Numerator));
  assert_eq!("1/b".parse::<Rate>(), Err(ParseRateError::Denominator));
  assert_eq!("1/2/3".parse::<Rate>(), Err(ParseRateError::Denominator));

  // A BARE NUMBER has no separator, so the strict parser refuses it here as well as at the door.
  assert_eq!("30".parse::<Rate>(), Err(ParseRateError::MissingSeparator));
  assert_eq!(
    "29.97".parse::<Rate>(),
    Err(ParseRateError::MissingSeparator)
  );
  assert_eq!("".parse::<Rate>(), Err(ParseRateError::MissingSeparator));

  // Whitespace is not trimmed: an operand comes off a wire, not out of a person's hand.
  assert_eq!(" 30/1".parse::<Rate>(), Err(ParseRateError::Numerator));
  assert_eq!("30 / 1".parse::<Rate>(), Err(ParseRateError::Numerator));
}

/// The rendered form parses back with both halves as written — compared on the HALVES rather than
/// the values, since a rendering that reduced would satisfy an `assert_eq!` on the value.
#[test]
fn the_canonical_form_round_trips_both_halves() {
  for (num, den) in [(0, 1), (30, 1), (30_000, 1001), (i32::MAX, i32::MAX)] {
    let recovered = rate(num, den)
      .to_string()
      .parse::<Rate>()
      .expect("a rendered rate always parses");

    assert_eq!(recovered.get().num(), num, "numerator of {num}/{den}");
    assert_eq!(
      recovered.get().den().get(),
      den,
      "denominator of {num}/{den}"
    );
  }
}

/// **What the DECLARATION emits, named so the compiler proves it is there** — the two readers and
/// the conversion family the `remote = "…"` rewrite writes in place of the hand `From` pair.
///
/// ADDED with the migration rather than inherited from it: the impls this replaced were reachable
/// through no test in the crate, so a silent loss of either direction would have compiled. The
/// readers are asserted on the DECLARED pair, which is what a reader of a manifest field returns —
/// the reduction is the storage boundary's and is asserted by its own tests above.
#[test]
fn the_declaration_emits_the_readers_and_the_conversion_family() {
  let inner = mt::Rate::fps(
    60_000,
    NonZeroI32::new(2002).expect("a positive denominator"),
  );

  let wrapped = Rate::from(inner);
  assert_eq!(
    wrapped.num(),
    60_000,
    "a reader hands back the DECLARED half"
  );
  assert_eq!(wrapped.den().get(), 2002);

  let unwrapped: mt::Rate = mt::Rate::from(wrapped);
  assert_eq!(unwrapped, inner, "the crossing round trips");
  assert_eq!(unwrapped.num(), 60_000, "and keeps the spelling, unreduced");

  let borrowed: &mt::Rate = wrapped.as_ref();
  assert_eq!(borrowed.num(), 60_000);
}

/// Every slot of all three facets is reachable through its getter, and the consuming and in-place
/// setters reach the same filter.
#[test]
fn the_rate_filter_projects_its_three_facets() {
  let numerator = ScalarFilter::new().with_gte(1);
  let denominator = ScalarFilter::new().with_eq(NonZeroI32::new(1001).unwrap());

  let built = RateFilter::new()
    .with_eq(rate(30_000, 1001))
    .with_ne(rate(24, 1))
    .with_in_(vec![rate(25, 1)])
    .with_not_in(vec![rate(60, 1)])
    .with_starts_with(Utf8Bytes::from("30"))
    .with_ends_with(Utf8Bytes::from("/1001"))
    .with_contains(Utf8Bytes::from("/"))
    .with_numerator(numerator.clone())
    .with_denominator(denominator.clone());

  assert_eq!(built.eq(), Some(&rate(30_000, 1001)));
  assert_eq!(built.ne(), Some(&rate(24, 1)));
  assert_eq!(built.in_(), Some([rate(25, 1)].as_slice()));
  assert_eq!(built.not_in(), Some([rate(60, 1)].as_slice()));
  assert_eq!(built.starts_with(), Some(&Utf8Bytes::from("30")));
  assert_eq!(built.ends_with(), Some(&Utf8Bytes::from("/1001")));
  assert_eq!(built.contains(), Some(&Utf8Bytes::from("/")));
  assert_eq!(built.numerator(), Some(&numerator));
  assert_eq!(built.denominator(), Some(&denominator));

  let mut in_place = RateFilter::new();
  in_place
    .set_eq(rate(30_000, 1001))
    .set_ne(rate(24, 1))
    .set_in_(vec![rate(25, 1)])
    .set_not_in(vec![rate(60, 1)])
    .set_starts_with(Utf8Bytes::from("30"))
    .set_ends_with(Utf8Bytes::from("/1001"))
    .set_contains(Utf8Bytes::from("/"))
    .set_numerator(numerator)
    .set_denominator(denominator);
  assert_eq!(in_place, built);
}

/// Empty exactly while EVERY slot is — one case per slot, since a dropped term in the chain is
/// invisible to a case that sets a sibling of the same facet.
#[test]
fn the_rate_filter_is_empty_only_while_every_slot_is() {
  assert!(RateFilter::new().is_empty());

  for filter in [
    RateFilter::new().with_eq(rate(30, 1)),
    RateFilter::new().with_ne(rate(30, 1)),
    RateFilter::new().with_in_(vec![rate(30, 1)]),
    RateFilter::new().with_not_in(vec![rate(30, 1)]),
    RateFilter::new().with_starts_with(Utf8Bytes::from("30")),
    RateFilter::new().with_ends_with(Utf8Bytes::from("/1")),
    RateFilter::new().with_contains(Utf8Bytes::from("/")),
    RateFilter::new().with_numerator(ScalarFilter::new().with_eq(30)),
    RateFilter::new().with_denominator(ScalarFilter::new().with_eq(NonZeroI32::new(1).unwrap())),
  ] {
    assert!(!filter.is_empty(), "{filter:?}");
  }

  // An EMPTY membership list is an operand rather than an absence — it asks for no rows.
  assert!(!RateFilter::new().with_in_(Vec::new()).is_empty());
  assert_eq!(RateFilter::default(), RateFilter::new());
}