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
//! The flags kind's seven operators on MongoDB, and the three the server answers natively.

use bson::doc;

use super::MongoFlagsComparison;
use crate::{
  At, ComplexComparison, FieldName, FlagsFilter, Mongo, RecordAddress, ToDatabaseComparison,
  comparison::mongo::MongoConjunction,
};

const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);

/// One comparison, lowered onto a field.
fn lowered(comparison: &MongoFlagsComparison<i64>) -> bson::Document {
  let frame = At::new(&ROW).frame(FieldName::new("disposition"));
  comparison
    .apply(frame.at(), MongoConjunction::new())
    .expect("a MongoDB lowering cannot refuse")
    .into_document()
}

/// **THE MASK WORDS ARE NATIVE.** Where the SQL twin builds `(col & ?) = ?`, MongoDB carries the
/// three bitwise predicates itself — so this lowering writes `$bitsAllSet`, `$bitsAnySet` and
/// `$bitsAllClear` onto the field and builds no expression at all.
#[test]
fn the_three_mask_words_lower_to_the_servers_own_bitwise_predicates() {
  let comparison = MongoFlagsComparison::<i64>::try_from_filter_with(
    FlagsFilter::new()
      .with_has_all(0b011i64)
      .with_has_any(0b100i64)
      .with_has_none(0b1000i64),
    Ok::<_, core::convert::Infallible>,
  )
  .expect("the identity conversion cannot fail");

  assert_eq!(comparison.conjuncts(), 3);
  assert_eq!(
    lowered(&comparison),
    doc! {
      "disposition": {
        "$bitsAllSet": 3i64,
        "$bitsAnySet": 4i64,
        "$bitsAllClear": 8i64,
      }
    }
  );
}

/// The four INTEGER words are four of the scalar kind's eight and lower the same way, merged under
/// one key beside the mask words.
#[test]
fn the_seven_words_merge_under_one_field_key() {
  let comparison = MongoFlagsComparison::<i64>::try_from_filter_with(
    FlagsFilter::new()
      .with_eq(1i64)
      .with_ne(2i64)
      .with_in_([1i64, 2])
      .with_not_in([4i64])
      .with_has_all(1i64)
      .with_has_any(2i64)
      .with_has_none(4i64),
    Ok::<_, core::convert::Infallible>,
  )
  .expect("the identity conversion cannot fail");

  assert_eq!(comparison.conjuncts(), 7);
  assert_eq!(
    lowered(&comparison),
    doc! {
      "disposition": {
        "$eq": 1i64,
        "$ne": 2i64,
        "$bitsAllSet": 1i64,
        "$bitsAnySet": 2i64,
        "$bitsAllClear": 4i64,
        "$in": [1i64, 2i64],
        "$nin": [4i64],
      }
    }
  );
}

/// **The empty mask keeps its three vacuous answers here too**, which is what makes the two
/// lowerings one semantics rather than two that look alike: `$bitsAllSet: 0` matches every document,
/// `$bitsAnySet: 0` matches none, `$bitsAllClear: 0` matches every one — the same TRUE, FALSE, TRUE
/// the SQL renders and `bitflags` computes.
#[test]
fn the_empty_mask_is_written_out_rather_than_dropped() {
  let comparison = MongoFlagsComparison::<i64>::try_from_filter_with(
    FlagsFilter::new().with_has_any(0i64),
    Ok::<_, core::convert::Infallible>,
  )
  .expect("the identity conversion cannot fail");

  assert_eq!(
    lowered(&comparison),
    doc! { "disposition": { "$bitsAnySet": 0i64 } },
    "a set word over no bits is a question with an answer, not an absent conjunct"
  );
}

/// The projection an expansion writes for a flags field, resolved.
#[test]
fn the_codegen_projection_resolves_for_a_flags_field() {
  type Crossed = <FlagsFilter<i64> as ToDatabaseComparison<i64, Mongo>>::Comparison;

  let _: Crossed = MongoFlagsComparison::<i64>::new();
}