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 std::sync::Arc;

use super::{Deferred, FulltextQuery, VectorQuery};
use crate::Embedding;

const DIM: usize = 4;

/// The floor is the FIRST thing a query has, and the constructor is the only door: there is no
/// `FulltextQuery::new(text)` to reach past it.
#[test]
fn a_full_text_query_carries_its_text_and_its_floor() {
  let query = FulltextQuery::new("sunset", 0.75_f32);

  assert_eq!(query.text(), "sunset");
  assert_eq!(query.floor(), &0.75);
}

/// Both doors into the crate's one string type reach the text, which is what keeps a wire argument,
/// an owned `String` and a literal from being three signatures.
#[test]
fn the_query_text_arrives_from_either_string() {
  assert_eq!(
    FulltextQuery::new(String::from("Sonnenuntergang"), 0.5_f32).text(),
    "Sonnenuntergang"
  );
  assert_eq!(FulltextQuery::new("sunset", 0.5_f32).text(), "sunset");
}

/// The floor is the engine's own currency and NOT an `f32` the framework picked: a query over a
/// face scoring in `f64`, or in a shape of its own, is the same type.
#[test]
fn the_floor_is_whatever_the_engine_scores_in() {
  #[derive(Debug, PartialEq, PartialOrd)]
  struct Distance(f64);

  let query = FulltextQuery::new("heron", Distance(0.125));

  assert_eq!(query.floor(), &Distance(0.125));
  assert_eq!(FulltextQuery::new("heron", 3_u32).floor(), &3);
}

/// The vector query carries a WIDTH-CHECKED input: the coordinates it hands the seam are the ones
/// [`Embedding::new`] already accepted, so the seam's unchecked `&[E]` is fed a proof.
#[test]
fn a_vector_query_hands_the_seam_a_width_checked_input() {
  let input =
    Embedding::<f32, DIM>::new(Arc::from(vec![0.5; DIM])).expect("the fixture declares DIM");
  let query = VectorQuery::new(input, 0.2_f32);

  assert_eq!(query.coordinates(), [0.5; DIM]);
  assert_eq!(query.coordinates().len(), Embedding::<f32, DIM>::DIM);
  assert_eq!(query.input().coordinates().len(), DIM);
  assert_eq!(query.floor(), &0.2);
}

/// **THE FLOORLESS FORM DEFERS**, and the deferral is in the TYPE: what comes back is floored by
/// [`Deferred`](super::Deferred) rather than by a number, so a door naming the engine's own score
/// cannot be reached with it.
#[test]
fn a_floorless_vector_query_carries_its_question_and_no_number() {
  let input =
    Embedding::<f32, DIM>::new(Arc::from(vec![0.5; DIM])).expect("the fixture declares DIM");
  let deferred: VectorQuery<f32, Deferred, DIM> = VectorQuery::of(input);

  assert_eq!(deferred.coordinates(), [0.5; DIM]);
  assert_eq!(deferred.floor(), &Deferred::new());
}

/// The chain's second step: the resolved query is the SAME question at the floor somebody supplied,
/// and it is now the type an engine-facing door takes.
#[test]
fn resolving_a_deferred_floor_gives_the_query_the_explicit_form_has() {
  let input =
    Embedding::<f32, DIM>::new(Arc::from(vec![0.25; DIM])).expect("the fixture declares DIM");

  let resolved = VectorQuery::of(input.clone()).floored(0.4_f32);

  assert_eq!(resolved, VectorQuery::new(input, 0.4));
  assert_eq!(resolved.floor(), &0.4);
}