ingraph-derive 0.1.0

The attribute and derive macros for ingraph: the words a record, node, key, filter and vocabulary declaration is written in.
Documentation
//! THE `sortable` WORD — what the grammar accepts, what it refuses, and the reading it does not
//! touch.
//!
//! A suite of its own beside [`crate::tests`], for the reason the other per-word suites give: what
//! is asserted here is ONE word's grammar — where it may be written, what it refuses beside, and
//! that a declaration without it means exactly what it meant — and holding that apart from the
//! label vocabulary's own suite keeps a change to either visible.
//!
//! # The word is a STORAGE fact and not an interpretation, which the last two tests are about
//!
//! `#[ingraph(list(scalar), sortable)]` and a bare `#[ingraph(list(scalar))]` resolve to
//! the SAME marker: what a column is read as is the element word's answer, and `sortable` says what
//! the storage does with the collection. A reading that moved with the word would make one
//! declaration mean two filters.
//!
//! The RESEAT itself — the old label spelling, and the seats the word is refused at — is
//! [`crate::sortable_reseat_tests`], which is where the ruling that moved it is pinned.

use ingraph_codegen::prelude::*;

use crate::{host, parse::parse};

/// The pass under test, at the kind this word is about — a record, being the only kind that stores.
fn record(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
  let (host, options) = host::split(input);
  parse(&host, &options, NodeKind::Record)
}

/// One field of the record.
fn field<'a>(model: &'a RecordModel, source: &str) -> &'a Field {
  model
    .fields
    .iter()
    .find(|field| field.column.source == source)
    .unwrap_or_else(|| panic!("`{source}` is a field of the record"))
}

/// One field's declared marker.
fn marker<'a>(model: &'a RecordModel, source: &str) -> &'a Marker {
  field(model, source)
    .marker
    .as_ref()
    .expect("this field declared a label")
}

/// The refusal one declaration gets, as text.
fn refused(input: &syn::DeriveInput) -> String {
  record(input)
    .err()
    .map(|error| error.to_string())
    .unwrap_or_else(|| panic!("this declaration is refused"))
}

/// **THE WORD IS READ INSIDE `persist(…)`**, beside the per-database blocks, and it lands on the
/// FIELD rather than on its marker.
#[test]
fn a_collection_declares_its_storage_word_inside_persist() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(list(string), sortable)] tags: Vec<String>,
    }
  })
  .expect("a persisted collection may ask for the companion column");

  let Marker::Framework(FrameworkMarker { label, element }) = marker(&model, "tags") else {
    panic!("a word resolves to one of the framework's markers")
  };
  assert_eq!(*label, Label::List);
  assert_eq!(*element, Some(Label::String));
  assert!(field(&model, "tags").sortable);
}

/// **THE WORD COMPOSES WITH A DATABASE'S OWN BLOCK**, which is the ruling's own spelling: one
/// written intent in the common scope, and each database's storage form beside it.
#[test]
fn the_word_sits_beside_a_database_block() {
  let model = record(&syn::parse_quote! {
    #[ingraph(postgres, graphql(async_graphql, plural = "many"))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(list(scalar), sortable, postgres(edge))] marks: Vec<i64>,
    }
  })
  .expect("the intent and the storage form are two words in one block");
  assert!(field(&model, "marks").sortable);
}

/// **THE TWO ORDERS ARE ONE DECLARATION**, because the entries of `persist(…)` are a set.
///
/// The database block carries `rename` rather than the `indexed` it used to: a collection SQLite
/// keeps in a second table is refused that word by name now — there is no column of this row for an
/// index to be over — and what this test is about is the ORDER of two entries rather than which
/// entry the second one is.
#[test]
fn the_word_may_be_written_before_a_database_block() {
  for declaration in [
    syn::parse_quote! {
      #[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
      struct S {
        #[ingraph(id)] id: u128,
        #[ingraph(list(scalar), sortable, sqlite(rename = "notes"))] marks: Vec<i64>,
      }
    },
    syn::parse_quote! {
      #[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
      struct S {
        #[ingraph(id)] id: u128,
        #[ingraph(list(scalar), sqlite(rename = "notes"), sortable)] marks: Vec<i64>,
      }
    },
  ] {
    let model: syn::DeriveInput = declaration;
    let model = record(&model).expect("either order is the same declaration");
    assert!(field(&model, "marks").sortable);
  }
}

/// **THE LABEL AND THE STORAGE WORD ARE TWO ENTRIES**, written in either order — the cross-entry
/// question the old seat never had to ask, because the word used to sit inside the collection's own
/// parentheses.
#[test]
fn the_label_may_be_written_after_the_storage_word() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(sortable, list(string))] tags: Vec<String>,
    }
  })
  .expect("the two words are one declaration whichever comes first");
  assert!(field(&model, "tags").sortable);
}

/// **NO WORD IS TODAY'S WORLD EXACTLY.** A collection that did not ask for the companion says so,
/// which is what makes the word cost a declaration written before it nothing at all.
#[test]
fn a_collection_without_the_word_asks_for_nothing() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(list(string))] tags: Vec<String>,
    }
  })
  .expect("a collection label needs no storage word");
  assert!(!field(&model, "tags").sortable);
}

/// **AT THE FIELD'S TOP LEVEL IT IS THE WORD**, whichever order it and the label were written in.
#[test]
fn the_word_at_the_field_is_the_seat_it_takes() {
  let model = record(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(sortable, list(string))] tags: Vec<String>,
    }
  })
  .expect("the word and the label are one attribute's two entries");
  assert!(
    field(&model, "tags").sortable,
    "the storage word and the label are one attribute's two entries, in either order"
  );
}

/// **A SET REFUSES IT BY NAME.** Order carries no meaning in a set by its own declaration, so an
/// order over one would be an order over a sequence the declaration says is not there.
#[test]
fn a_set_refuses_the_word_with_its_reason() {
  let message = refused(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(set(string), sortable)] notes: Vec<String>,
    }
  });
  assert!(
    message.contains("ORDER carries no meaning") && message.contains("`list(…)`"),
    "the refusal names the set's own declaration: {message}"
  );
}

/// **A VALUE FAMILY REFUSES IT**, and the sentence is the one `orderable`'s already is: a stored
/// scalar is orderable because it is stored.
#[test]
fn a_value_family_refuses_the_word() {
  let message = refused(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(string, sortable)] title: String,
    }
  });
  assert!(
    message.contains("orderable because it is stored"),
    "the refusal says why the word adds nothing: {message}"
  );
}

/// **ONE WORD, ONE COLLECTION.**
#[test]
fn the_word_is_declared_once() {
  let message = refused(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(list(string), sortable, sortable)] tags: Vec<String>,
    }
  });
  assert!(
    message.contains("more than once"),
    "the refusal counts: {message}"
  );
}

/// **IT IS A BARE WORD**, like every other one this grammar ships.
#[test]
fn the_word_carries_no_value() {
  let message = refused(&syn::parse_quote! {
    #[ingraph(sqlite, graphql(async_graphql))]
    struct S {
      #[ingraph(id)] id: u128,
      #[ingraph(list(string), sortable = "yes")] tags: Vec<String>,
    }
  });
  assert!(
    message.contains("bare WORD"),
    "the refusal is the vocabulary's own: {message}"
  );
}

/// **AN INTERFACE PIN REFUSES IT**, with what the widening is waiting on: an arm's collection terms
/// are always edge-form and there is no per-arm companion for a union to project.
#[test]
fn an_interface_pin_refuses_the_word() {
  let input: syn::DeriveInput = syn::parse_quote! {
    #[ingraph(fields(marks(type = "Vec<i64>", list(scalar), persist(sortable))))]
    pub enum Media { Video(Video) }
  };
  let (host, arguments) = host::split(&input);
  let refused = crate::interface::parse(&host, &arguments)
    .expect_err("a pin cannot ask for a companion column")
    .into_iter()
    .map(|error| error.to_string())
    .collect::<Vec<_>>()
    .join(" | ");

  assert!(
    refused.contains("RECORD's word"),
    "the refusal says whose word it is: {refused}"
  );
}