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::NormCoord;

fn reading(text: &str) -> TextDetection<NormCoord> {
  TextDetection::new(
    text,
    Score::new(0.91).expect("the fixture's confidence is in range"),
    BoundingBox::try_unit(0.10, 0.42, 0.30, 0.06).expect("the fixture's rectangle is in frame"),
  )
}

/// **THE TEXT REFUSES NOTHING**, where a label refuses the empty word — the difference between a
/// PAYLOAD and a vocabulary word, stated at the two doors.
#[test]
fn a_reading_of_no_characters_is_an_observation_rather_than_a_refusal() {
  let nothing = reading("");

  assert_eq!(nothing.text(), "");
  assert_eq!(
    nothing.bbox().width(),
    crate::NormCoord::new(0.30).expect("in range"),
    "…and the region it was read from survives, which is the whole reason not to refuse"
  );
  assert!(super::super::Label::new("").is_err(), "the CONTRAST");
}

/// **SEVEN COLUMNS, SIX OF THEM THE VALUE** — the box's derived leaf inherited and counted apart.
#[test]
fn a_reading_states_its_columns_and_which_of_them_hold_the_value() {
  type Read = TextDetection<NormCoord>;

  assert_eq!(crate::width(<Read as ColumnKind>::SEGMENTS), 7);
  assert_eq!(crate::value_width(<Read as ColumnKind>::SEGMENTS), 6);

  let names: Vec<_> = <Read as ColumnKind>::SEGMENTS
    .iter()
    .map(|segment| segment.field().expect("named").declared().as_str())
    .collect();
  assert_eq!(names, ["text", "confidence", "bbox"]);
}

/// **SIX LEVELS OVER SEVEN LEAVES**, and the seventh answering `None` — the inherited index leaf
/// kept out of every structural judgment.
#[test]
fn a_readings_order_names_its_six_value_leaves_and_never_the_boxs_index() {
  let levels: Vec<_> = <TextDetection<NormCoord> as ColumnKind>::ORDER
    .iter()
    .map(|level| level.leaf())
    .collect();
  assert_eq!(levels, [0, 1, 2, 3, 4, 5]);

  let line = reading("Gate B14");
  assert!(matches!(
    crate::OrderLeaves::leaf(&line, 0),
    Some(TextDetectionLeaf::Text(_))
  ));
  assert!(matches!(
    crate::OrderLeaves::leaf(&line, 2),
    Some(TextDetectionLeaf::Bbox(BoundingBoxLeaf::X(_)))
  ));
  assert_eq!(crate::OrderLeaves::leaf(&line, 6), None);
}

/// **THE CURSOR ROUND TRIP**, and the two ways a forged position is refused: a member that is not
/// there, and bytes no member owns.
#[test]
fn a_reading_round_trips_through_a_cursor_and_refuses_a_forged_one() {
  use crate::CursorValue as _;

  let line = reading("Gate B14 · 19:40");
  let mut bytes = Vec::new();
  line.write_cursor(&mut bytes);

  assert_eq!(
    <TextDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes),
    Some(line)
  );
  assert_eq!(
    <TextDetection<NormCoord> as crate::CursorValue>::read_cursor(&bytes[..bytes.len() - 1]),
    None,
    "a frame this type did not write is refused rather than read as a shorter one"
  );

  let mut trailing = bytes.clone();
  trailing.push(0);
  assert_eq!(
    <TextDetection<NormCoord> as crate::CursorValue>::read_cursor(&trailing),
    None,
    "…and so are bytes past the last member"
  );
}

/// EMPTINESS is the conjunction of the three facets', and the geometry facet descends into the
/// box's own five.
#[test]
fn a_filter_is_empty_until_one_of_its_three_facets_is_set() {
  type Face = TextDetectionFilter<crate::ScalarFilter<NormCoord>>;

  assert!(crate::GraphqlFilter::is_empty(&Face::new()));
  assert!(
    crate::GraphqlFilter::is_empty(&Face::new().with_bbox(BoundingBoxFilter::new())),
    "a facet holding an empty filter asks nothing"
  );
  assert!(!crate::GraphqlFilter::is_empty(&Face::new().with_bbox(
    BoundingBoxFilter::new().with_area(
      crate::ScalarFilter::new().with_gte(crate::NormCoord::new(0.05).expect("in range"))
    )
  )));
}