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
//! What a cardinality renders, and the pin on the form that was chosen over `$size`.

use bson::doc;

use super::{MongoCountComparison, push_count_slots};
use crate::{
  ScalarFilter,
  comparison::{TryFromFilter, mongo::MongoConjunction},
};

/// Cross a `usize` cardinality filter the way a collection crossing does, and render it at `field`.
fn rendered(filter: ScalarFilter<usize>, field: &str) -> bson::Document {
  let comparison = MongoCountComparison::<i64>::try_from_filter(filter).expect("a small operand");
  let mut accumulator = MongoConjunction::new();
  push_count_slots(&comparison, field, &mut accumulator);
  accumulator.into_document()
}

/// **The decision this kind exists to record.** `$size` answers `eq` and nothing else, so all eight
/// operators go through one guarded `$expr` — and `gt` is the operator that has no other spelling at
/// all.
#[test]
fn an_ordering_operator_renders_as_a_guarded_expression() {
  assert_eq!(
    rendered(ScalarFilter::new().with_gt(3), "tags"),
    doc! {
      "$and": [ {
        "$expr": {
          "$and": [
            { "$isArray": "$tags" },
            { "$gt": [ { "$size": "$tags" }, 3i64 ] },
          ],
        },
      } ],
    }
  );
}

/// `eq` takes the SAME form as the other seven rather than the `$size` it could have taken, so one
/// question has one rendering and `eq: 0` cannot disagree with `lt: 1`. Measured against 8.2.12, the
/// guarded expression and `{$size: 0}` return the same documents.
#[test]
fn equality_takes_the_same_form_as_the_other_seven() {
  assert_eq!(
    rendered(ScalarFilter::new().with_eq(0), "tags"),
    doc! {
      "$and": [ {
        "$expr": {
          "$and": [
            { "$isArray": "$tags" },
            { "$eq": [ { "$size": "$tags" }, 0i64 ] },
          ],
        },
      } ],
    }
  );
}

/// There is no aggregation `$nin`, so the negated membership is `$not` around `$in` — and the
/// membership operand is one array rather than a bind per element, which is what BSON already is.
#[test]
fn negated_membership_wraps_the_positive_form() {
  assert_eq!(
    rendered(ScalarFilter::new().with_not_in([1, 2]), "tags"),
    doc! {
      "$and": [ {
        "$expr": {
          "$and": [
            { "$isArray": "$tags" },
            { "$not": { "$in": [ { "$size": "$tags" }, [ 1i64, 2i64 ] ] } },
          ],
        },
      } ],
    }
  );
}

/// An EMPTY membership list needs no special case, and it is the same truth table the field
/// operators have: `in_: []` asks for no documents and `not_in: []` for every one, where an ABSENT
/// operator asks nothing at all.
#[test]
fn an_empty_membership_list_is_the_operator_present() {
  let asks_none = rendered(ScalarFilter::new().with_in_([]), "tags");
  let asks_all = rendered(ScalarFilter::new().with_not_in([]), "tags");
  assert_ne!(asks_none, asks_all);
  assert!(rendered(ScalarFilter::new(), "tags").is_empty());
}

/// Two operators are two `$and` branches rather than one `$expr` holding both, and the `$isArray`
/// guard is repeated inside each: it is what makes its own expression evaluable rather than a
/// separate predicate.
#[test]
fn two_operators_are_two_guarded_branches() {
  let filter = ScalarFilter::<i32>::new().with_gt(1).with_lt(5);
  let comparison = MongoCountComparison::<i64>::try_from_filter(filter).expect("a small operand");
  assert_eq!(comparison.conjuncts(), 2);

  let mut accumulator = MongoConjunction::new();
  push_count_slots(&comparison, "tags", &mut accumulator);
  assert_eq!(
    accumulator.into_document(),
    doc! {
      "$and": [
        {
          "$expr": {
            "$and": [
              { "$isArray": "$tags" },
              { "$gt": [ { "$size": "$tags" }, 1i64 ] },
            ],
          },
        },
        {
          "$expr": {
            "$and": [
              { "$isArray": "$tags" },
              { "$lt": [ { "$size": "$tags" }, 5i64 ] },
            ],
          },
        },
      ],
    }
  );
}

/// A composite's cardinality names a dotted path, and the expression takes the same `$` prefix a
/// bare name does.
#[test]
fn a_nested_path_is_one_field_reference() {
  assert_eq!(
    rendered(ScalarFilter::new().with_gte(1), "audio.tags"),
    doc! {
      "$and": [ {
        "$expr": {
          "$and": [
            { "$isArray": "$audio.tags" },
            { "$gte": [ { "$size": "$audio.tags" }, 1i64 ] },
          ],
        },
      } ],
    }
  );
}

/// A cardinality is a `usize` on one side and this family's `i64` on the other, so an operand past
/// the width is REFUSED rather than saturated — a substituted operand is a wrong result set reported
/// as a right one.
#[test]
#[cfg(target_pointer_width = "64")]
fn an_operand_past_the_width_is_refused() {
  let filter = ScalarFilter::<usize>::new().with_eq(usize::MAX);
  assert!(MongoCountComparison::<i64>::try_from_filter(filter).is_err());
}

/// The walk is a `pub(super)` function, so nothing outside this family can reach a cardinality's
/// rendering at all — and the type itself refuses the OTHER way in. That half is stated on the type,
/// where rustdoc actually runs it.
#[test]
fn a_cardinality_reaches_a_filter_only_through_the_walk() {
  let comparison =
    MongoCountComparison::<i64>::try_from_filter(ScalarFilter::<usize>::new().with_eq(3))
      .expect("a small operand");
  let mut accumulator = MongoConjunction::new();
  push_count_slots(&comparison, "tags", &mut accumulator);
  assert!(!accumulator.into_document().is_empty());
}