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 each refusal SAYS, and the ceiling `$skip` is refused past.

use super::{
  MongoExtractionError, MongoQueryError, MongoTypeError, MongoWriteError, SKIP_CEILING, offset,
};
use crate::{
  FieldName,
  comparison::mongo::{MongoLoweringError, RelationWidthError},
};

/// `$skip` takes a non-negative 64-bit integer and the driver binds an `i64`, so the honest ceiling
/// is that type's — arrived at from MongoDB's own wire type rather than borrowed from the SQL
/// family's `OFFSET`, which reaches the same number by its own route.
#[test]
fn a_skip_is_bounded_by_what_the_stage_binds() {
  assert_eq!(offset(0).ok(), Some(0));
  assert_eq!(offset(SKIP_CEILING).ok(), Some(i64::MAX));
  assert!(matches!(
    offset(SKIP_CEILING + 1),
    Err(MongoQueryError::Skip(_))
  ));
}

/// A lowering refusal is carried VERBATIM, because a backend that refused a filter has said
/// something specific about the query being asked.
#[test]
fn a_lowering_refusal_keeps_its_own_sentence() {
  let refusal = MongoQueryError::from(MongoLoweringError::RelationWidth(RelationWidthError::new(
    FieldName::new("splice"),
    2,
  )));

  assert!(
    refusal
      .to_string()
      .contains("`splice` is addressed by 2 columns"),
    "{refusal}"
  );
}

/// An extraction refusal reaches a caller with the field it was reading, through the query error as
/// well as on its own — which is what makes a decode failure a schema report rather than a hunt.
#[test]
fn an_extraction_refusal_keeps_its_field_through_the_query_error() {
  let refusal = MongoQueryError::from(MongoExtractionError::new(
    "_id",
    MongoTypeError::wrong_type("long", "string"),
  ));

  assert_eq!(
    refusal.to_string(),
    "this document is not a row: `_id` did not read: this field holds a string where a long was \
     stored"
  );
}

/// Every refusal has a SOURCE except the one that carries a number, which is the same shape
/// `SqlQueryError` takes: a `skip` past the ceiling is this crate's own sentence and has nothing
/// underneath it.
#[test]
fn every_refusal_but_the_ceiling_carries_its_cause() {
  use core::error::Error;

  assert!(
    MongoQueryError::from(MongoLoweringError::ElementRelation)
      .source()
      .is_some()
  );
  assert!(MongoQueryError::Skip(1).source().is_none());
}

/// The unscoped delete NAMES THE COLLECTION, which is the sentence a caller needs: not *this filter
/// is empty* but *this is what was about to go*.
#[test]
fn the_unscoped_delete_names_the_collection_it_would_have_emptied() {
  let refusal = MongoWriteError::Unscoped(FieldName::new("shot"));

  assert_eq!(
    refusal.to_string(),
    "this delete names no document, so it would empty `shot` — a filter that lowers to `{}` is \
     refused rather than run"
  );
}

/// The join refusal names the COUNT and the reason, so a reader is told what the filter needed
/// rather than only that it was declined — and it is a fact about the COMMAND rather than about the
/// predicate, which is what the sentence has to convey.
#[test]
fn the_join_refusal_says_what_the_command_could_not_carry() {
  let refusal = MongoWriteError::Joined(3);

  assert!(
    refusal.to_string().contains("3 `$lookup` stage(s)"),
    "{refusal}"
  );
  assert!(
    refusal.to_string().contains("carries no pipeline"),
    "{refusal}"
  );
}

/// A write's lowering refusal is carried VERBATIM too — the query error's rule, one lane over, since
/// a `delete_where` lowers the same comparison a `find` does.
#[test]
fn a_writes_lowering_refusal_keeps_its_own_sentence() {
  let refusal = MongoWriteError::from(MongoLoweringError::RelationWidth(RelationWidthError::new(
    FieldName::new("splice"),
    2,
  )));

  assert!(
    refusal
      .to_string()
      .contains("`splice` is addressed by 2 columns"),
    "{refusal}"
  );
}

/// The two findings this lane makes ITSELF carry no source, exactly as the query's ceiling does:
/// both are statements about a value the caller handed over rather than anything a driver or a
/// lowering said.
#[test]
fn the_writes_own_findings_carry_no_cause() {
  use core::error::Error;

  assert!(
    MongoWriteError::from(MongoLoweringError::ElementRelation)
      .source()
      .is_some()
  );
  assert!(
    MongoWriteError::Unscoped(FieldName::new("shot"))
      .source()
      .is_none()
  );
  assert!(MongoWriteError::Joined(1).source().is_none());
}