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 core::convert::Infallible;

use super::SqlBooleanComparison;
use crate::{BooleanFilter, comparison::TryFromFilter};

/// The `'Y'`/`'N'` column — the storage form that makes `V` worth having on a kind whose filter has
/// no parameter at all.
fn yes_no(value: bool) -> Result<String, Infallible> {
  Ok(if value { "Y" } else { "N" }.to_owned())
}

#[test]
fn a_parameterless_filter_crosses_into_a_parameterised_comparison() {
  let filter = BooleanFilter::new().with_eq(true).with_in_([true, false]);
  let crossed = SqlBooleanComparison::<String, ()>::try_from_filter_with(filter, yes_no).unwrap();

  assert_eq!(crossed.eq().map(String::as_str), Some("Y"));
  assert_eq!(crossed.in_().unwrap(), ["Y".to_owned(), "N".to_owned()]);
}

/// The native column needs no converter, and its crossing cannot fail.
#[test]
fn the_native_crossing_is_the_identity() {
  let crossed: Result<SqlBooleanComparison<bool, ()>, Infallible> =
    SqlBooleanComparison::try_from_filter(BooleanFilter::new().with_ne(false));
  assert_eq!(crossed.unwrap().ne(), Some(&false));
}

/// `in_: []` asks for no rows and is not the same as an absent `in_`, which asks for all of them —
/// so it survives the crossing as a present, empty slot.
#[test]
fn an_empty_membership_list_is_not_an_absent_operator() {
  let empty = SqlBooleanComparison::<bool, ()>::try_from_filter(
    BooleanFilter::new().with_in_(Vec::<bool>::new()),
  )
  .unwrap();
  let absent = SqlBooleanComparison::<bool, ()>::try_from_filter(BooleanFilter::new()).unwrap();

  assert_eq!(empty.conjuncts(), 1);
  assert_eq!(absent.conjuncts(), 0);
  assert!(absent.is_empty());
}

#[cfg(feature = "sqlite")]
mod sqlite {
  use core::{cell::RefCell, convert::Infallible};

  use super::SqlBooleanComparison;
  use crate::{
    BooleanFilter,
    comparison::{
      TryFromFilter,
      sql::tests::{Probe, render, threaded},
    },
  };

  /// The stored form is invisible in the SQL: a native `BOOLEAN` and a `0`/`1` `INTEGER` render the
  /// same text and bind the same count, which is why the crossing has to be asserted on the
  /// operand.
  ///
  /// The `'Y'`/`'N'` form above is crossed but not rendered here — `String` has no carrier row yet,
  /// and the text kinds are a separate slice.
  #[test]
  fn a_stored_form_renders_the_same_sql_as_the_native_one() {
    let native = SqlBooleanComparison::<bool, sqlx::Sqlite>::try_from_filter(
      BooleanFilter::new().with_eq(true),
    )
    .unwrap();
    let integer = SqlBooleanComparison::<i32, sqlx::Sqlite>::try_from_filter_with(
      BooleanFilter::new().with_eq(true),
      |value: bool| Ok::<_, Infallible>(i32::from(value)),
    )
    .unwrap();

    assert_eq!(integer.eq(), Some(&1));
    assert_eq!(
      render::<sqlx::Sqlite, _>("flag", &native),
      ("\"flag\" = ?".to_owned(), 1)
    );
    assert_eq!(
      render::<sqlx::Sqlite, _>("flag", &native),
      render::<sqlx::Sqlite, _>("flag", &integer)
    );
  }

  /// The threaded lowering emits the borrowed one's conjuncts, joined to the clause keyword the
  /// accumulator opened with.
  #[test]
  fn the_threaded_lowering_renders_the_same_conjuncts() {
    let crossed = SqlBooleanComparison::<bool, sqlx::Sqlite>::try_from_filter(
      BooleanFilter::new()
        .with_eq(true)
        .with_ne(false)
        .with_in_([true, false])
        .with_not_in([true]),
    )
    .unwrap();

    assert_eq!(
      threaded::<sqlx::Sqlite, _>("flag", &crossed),
      "SELECT \"id\" FROM \"t\" WHERE \"flag\" = ? AND NOT ( \"flag\" = ? ) \
       AND \"flag\" IN (?, ?) AND NOT ( \"flag\" IN (?) )"
    );
  }

  /// Every operand the comparison holds reaches the builder, in slot order. It matters more here
  /// than anywhere: a boolean's whole operand vocabulary is two values, so a swapped slot renders
  /// the same text with the same bind count and asks the opposite question.
  #[test]
  fn every_operand_the_comparison_holds_is_bound() {
    let bound = RefCell::new(Vec::new());
    let crossed = SqlBooleanComparison::<Probe<'_>, sqlx::Sqlite>::try_from_filter_with(
      BooleanFilter::new()
        .with_eq(true)
        .with_ne(false)
        .with_in_([false, true]),
      |value: bool| {
        Ok::<_, Infallible>(Probe {
          value: i64::from(value),
          bound: &bound,
        })
      },
    )
    .unwrap();

    threaded::<sqlx::Sqlite, _>("flag", &crossed);

    assert_eq!(*bound.borrow(), [1, 0, 0, 1]);
  }
}