ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! WHAT THE `indexed` WORD EMITS — the builder appended to a collection's edge table, and the two
//! shapes that get nothing.
//!
//! A suite of its own beside [`super::sortable_tests`], for that suite's reason: what is asserted
//! here is ONE word's emission, and a build that never wrote it getting back exactly the tokens it
//! got before the word existed.
//!
//! # The three rows, which are the ruling read off the emission
//!
//! | dialect | the collection lives | what the word emits |
//! |---|---|---|
//! | SQLite, MySQL | an edge table, always | `EdgeTable::new(…).indexed()` — the second index, and SQLite's collation with it |
//! | PostgreSQL, no `edge` word | a native array on the row | NOTHING, and not for the word's sake: a record whose collection rides an array gets NO blueprint at all |
//!
//! The PostgreSQL row is worth reading carefully, because it is not `sortable`'s. There the word
//! genuinely has nothing to buy — a native array orders itself. Here it has something real to buy,
//! and the reason nothing is emitted is one that predates this word: `Vec<T>` has no `StoredAs` row,
//! so `RecordModel::describable` subtracts the database and there is no `Table` to write a list
//! onto. The framework's side of the array half is written and tested —
//! `ingraph`'s `Table::with_collection_indexes` — and the day that wall comes down is the day this
//! row changes.

use crate::{
  model::RecordModel,
  tests::{gathered, gathered_in_mysql, gathered_in_postgres},
};

/// The same model with its collection carrying the ACCESS word — `indexed`, the field's own fact
/// beside its label rather than inside it, exactly as [`sortable`](super::sortable_tests) is.
fn indexed(mut model: RecordModel) -> RecordModel {
  let field = model
    .fields
    .iter_mut()
    .find(|field| field.column.source == "chapters")
    .expect("the collection column is a field of the record");
  assert!(
    field.marker.is_some(),
    "the collection declared one of the framework's words"
  );
  field.indexed = true;
  model
}

/// The emission's tokens as text, GATES AND ALL — the road every suite beside this one reads by.
fn emitted(model: &RecordModel) -> String {
  model.emit().to_string()
}

/// **THE EDGE TABLE CARRIES THE BUILDER**, which is the whole of what the emitter owes the word: the
/// descriptor states the intent and `ingraph`'s own DDL lane renders the statements from it.
#[test]
fn an_indexed_collection_marks_its_edge_table() {
  let tokens = emitted(&indexed(gathered()));

  assert!(
    tokens.contains("EdgeTable :: new") && tokens.contains(". indexed ()"),
    "the collection's second table is declared indexed: {tokens}"
  );
}

/// **MYSQL ANSWERS THE SAME**, being the other dialect with no array type — so the row is the
/// STORAGE FORM's rather than one dialect's, which is what `seeks_by_value` reads.
#[test]
fn the_other_edge_dialect_answers_alike() {
  let tokens = emitted(&indexed(gathered_in_mysql()));

  assert!(tokens.contains(". indexed ()"), "{tokens}");
}

/// **A COLLECTION THAT NEVER WROTE THE WORD KEEPS ITS ABSENCE**, and this is the assertion the
/// word's opt-in promise rests on: the same model one field apart emits the constructor it always
/// emitted, so a schema pays for the second index only where a declaration asked for one.
#[test]
fn a_collection_without_the_word_emits_the_call_it_always_did() {
  let tokens = emitted(&gathered());

  assert!(tokens.contains("EdgeTable :: new"), "{tokens}");
  assert!(
    !tokens.contains(". indexed ()"),
    "write amplification is a choice the declaration makes: {tokens}"
  );
}

/// **AN ARRAY-STORED COLLECTION EMITS NOTHING, AND THE BLUEPRINT IS WHY.** Postgres's wordless
/// default rides the row as a native array, and a record carrying one gets no `Describe` impl at
/// all — so there is no descriptor for either half of this word to be written onto.
///
/// Asserted at the BLUEPRINT rather than at the word, because that is the fact: a test that only
/// looked for `.indexed()` would pass for a build that had started emitting a schema and forgotten
/// the word in it.
#[test]
fn an_array_stored_collection_has_no_blueprint_to_carry_the_word() {
  let tokens = emitted(&indexed(gathered_in_postgres()));

  assert!(
    !tokens.contains("EdgeTable :: new"),
    "an array-stored collection declares no edge table: {tokens}"
  );
  assert!(
    !tokens.contains(". indexed ()"),
    "and no descriptor for the word to ride: {tokens}"
  );
}