use ingraph_codegen::aggregate::{AggregateModel, Measure};
use ingraph_codegen::prefix::EmissionPrefix;
use quote::ToTokens as _;
fn parse(input: &syn::DeriveInput) -> syn::Result<AggregateModel> {
let (host, arguments) = crate::host::split(input);
super::parse(&host, &arguments)
}
fn accepted(input: proc_macro2::TokenStream) -> AggregateModel {
parse(&syn::parse2(input).expect("the declaration parses"))
.expect("the declaration is an aggregate")
}
fn refusals(input: proc_macro2::TokenStream) -> Vec<String> {
parse(&syn::parse2(input).expect("the declaration parses"))
.expect_err("this declaration breaks a rule")
.into_iter()
.map(|error| error.to_string())
.collect()
}
fn refused(input: proc_macro2::TokenStream) -> String {
refusals(input).join("\n")
}
fn ruled() -> proc_macro2::TokenStream {
quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(count = counting),
poll(recount, every = "10m", light, async(inline)),
sqlite,
)]
pub struct CodecCensus {
#[ingraph(group)]
codec: Codec,
#[ingraph(measure)]
count: Count,
}
}
}
fn tight(tokens: &proc_macro2::TokenStream) -> String {
tokens.to_string().replace(' ', "")
}
#[test]
fn the_ruled_line_parses_and_every_entry_reaches_the_model() {
let model = accepted(ruled());
assert_eq!(model.aggregate, "CodecCensus");
assert_eq!(
model
.upstream
.to_token_stream()
.to_string()
.replace(' ', ""),
"demux::Track",
"the upstream travels as the path the declaration named"
);
assert_eq!(
model.krate.to_token_stream().to_string().replace(' ', ""),
"::ingraph"
);
assert!(model.krate_spelled.is_none(), "no `crate` was written");
assert_eq!(model.groups.len(), 1);
assert_eq!(model.groups[0].field, "codec");
assert_eq!(model.groups[0].from, "codec");
assert_eq!(model.groups[0].ty.to_token_stream().to_string(), "Codec");
assert_eq!(model.measures.len(), 1);
assert_eq!(model.measures[0].field, "count");
assert!(matches!(model.measures[0].by, Measure::Counting));
assert_eq!(model.measures[0].ty.to_token_stream().to_string(), "Count");
assert_eq!(
tight(&model.triggers),
"poll(recount,every=\"10m\",light,async(inline))",
"the clock is carried VERBATIM — its grammar is the record pass's, execution word and all"
);
assert_eq!(tight(&model.stores), "sqlite");
assert!(model.graphql.is_none());
}
#[test]
fn the_row_is_its_groups_then_its_measures() {
let model = accepted(quote::quote! {
#[ingraph(
measure(count = counting, bytes(fold = summing, from = "size")),
group(codec = "codec", lang = "language"),
upstream(node = "demux::Track"),
poll(recount, every = "10m", light),
sqlite,
)]
pub struct TrackCensus {
#[ingraph(measure)] count: Count,
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
#[ingraph(group)] lang: Language,
}
});
assert_eq!(
model.columns().map(ToString::to_string).collect::<Vec<_>>(),
["codec", "lang", "count", "bytes"],
);
assert_eq!(
model.groups[0].from, "codec",
"a group entry carries the UPSTREAM's field name, not this declaration's"
);
assert_eq!(model.groups[1].from, "language");
}
#[test]
fn a_measure_takes_a_shipped_word_bare_and_a_declared_fold_quoted() {
let model = accepted(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(count = counting, bytes(fold = summing, from = "size"), median = "app::Median"),
poll(recount, every = "10m", light),
sqlite,
)]
pub struct TrackCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
#[ingraph(measure)] bytes: u64,
#[ingraph(measure)] median: f64,
}
});
assert!(matches!(model.measures[0].by, Measure::Counting));
assert!(matches!(model.measures[1].by, Measure::Summing));
match &model.measures[2].by {
Measure::Declared(path) => assert_eq!(
path.to_token_stream().to_string().replace(' ', ""),
"app::Median",
),
other => panic!("a quoted fold is a declared one: {other:?}"),
}
}
#[test]
fn the_record_grammars_words_are_carried_verbatim() {
let model = accepted(quote::quote! {
#[ingraph(
crate = "framework::ingraph",
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(count = counting),
poll(recount, every = "10m", light),
poll(nightly, every = "1d", light),
sqlite,
postgres(schema = "media"),
table = "codec_census",
rename = "CodecCensusRow",
graphql(async_graphql),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert_eq!(
tight(&model.triggers),
"poll(recount,every=\"10m\",light),poll(nightly,every=\"1d\",light)",
"both clocks are carried, comma-joined, in written order"
);
assert_eq!(
tight(&model.stores),
"sqlite,postgres(schema=\"media\"),table=\"codec_census\",rename=\"CodecCensusRow\"",
"the store words and their two common entries travel together and unread"
);
assert_eq!(
tight(model.graphql.as_ref().expect("a wire face was named")),
"graphql(async_graphql)",
);
assert_eq!(
model.krate.to_token_stream().to_string().replace(' ', ""),
"framework::ingraph",
);
assert_eq!(
model
.krate_spelled
.as_ref()
.expect("the literal is kept beside the path")
.value(),
"framework::ingraph",
"both halves are carried so the record pass is handed the string that was written",
);
}
#[test]
fn a_column_carries_the_projection_its_marker_declared() {
let model = accepted(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(count = counting),
poll(recount, every = "10m", light),
sqlite,
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure, copy)] count: Count,
}
});
assert!(matches!(
model.groups[0].projection,
ingraph_codegen::model::Projection::Borrowed
));
assert!(matches!(
model.measures[0].projection,
ingraph_codegen::model::Projection::Copied
));
}
#[test]
fn an_accepted_aggregate_mounts_a_record_keyed_by_its_group_projection() {
let input: syn::DeriveInput = syn::parse2(ruled()).expect("the declaration parses");
let (host, arguments) = crate::host::split(&input);
let model = super::parse(&host, &arguments).expect("the declaration is an aggregate");
let mounted = super::mount(&host, model).expect("and it mounts a record");
assert_eq!(
mounted.record.kind,
ingraph_codegen::model::NodeKind::Aggregate
);
assert_eq!(mounted.record.key.id.name.to_string(), "codec");
assert!(
mounted.record.lineage.is_none(),
"one group column is a ROOT record: the key is the id alone",
);
assert!(
mounted.record.persist.is_some(),
"the carried store word reached the record grammar",
);
assert_eq!(
mounted
.record
.polls
.as_ref()
.map(|polls| polls.entries.len()),
Some(1),
"and so did the clock",
);
}
#[test]
fn a_refused_aggregate_is_not_told_it_is_early() {
let refusals = refused(quote::quote! {
#[ingraph(group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(refusals.contains("`upstream(…)`"), "{refusals}");
assert!(!refusals.contains("cannot be emitted yet"), "{refusals}");
}
#[test]
fn an_aggregate_with_no_clock_is_refused_by_name() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), sqlite)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(refusals.contains("needs a `poll(…)` clock"), "{refusals}");
assert!(
refusals.contains("stays empty and answers nothing forever"),
"the refusal says what silence would have cost: {refusals}"
);
assert!(
refusals.contains("notify edge used as an ALARM"),
"the recorded freshness door is named rather than left out: {refusals}"
);
}
#[test]
fn an_aggregate_folds_exactly_one_upstream() {
let missing = refused(quote::quote! {
#[ingraph(group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
missing.contains("folds the rows of ONE upstream"),
"{missing}"
);
assert!(
missing.contains("reads its upstream's TABLE rather than its messages"),
"{missing}"
);
let twice = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
upstream(node = "demux::Chapter"),
group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
twice.contains("`upstream` is declared more than once"),
"{twice}"
);
assert!(
twice.contains("single-upstream by ruling rather than by omission"),
"the blocked seat is named: {twice}"
);
}
#[test]
fn a_projection_inside_the_upstream_is_sent_to_the_group_block() {
let refusals = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track", codec = "codec"),
group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains("names the EDGE on an aggregate"),
"{refusals}"
);
assert!(
refusals.contains("`group(codec = \"<upstream field>\")`"),
"{refusals}"
);
assert!(
refusals.contains("a measure's column is FOLDED into being"),
"the sentence says WHY the projection is not total here: {refusals}"
);
}
#[test]
fn an_aggregate_groups_something_and_measures_something() {
let absent = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), poll(p, every = "10m", light))]
pub struct CodecCensus {
codec: Codec,
}
});
assert!(
absent.contains("needs at least one `group(…)` entry"),
"{absent}"
);
assert!(
absent.contains("needs at least one `measure(…)` entry"),
"{absent}"
);
let empty = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(), measure(), poll(p, every = "10m", light))]
pub struct CodecCensus {
codec: Codec,
}
});
assert!(
empty.contains("needs at least one `group(…)` entry"),
"{empty}"
);
assert!(
empty.contains("needs at least one `measure(…)` entry"),
"{empty}"
);
}
#[test]
fn a_field_is_named_once_in_a_block() {
let refusals = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec", codec = "codec_name"),
measure(count = counting, count(fold = summing, from = "size")),
poll(p, every = "10m", light),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains("`codec` is named more than once in `group(…)`"),
"{refusals}"
);
assert!(
refusals.contains("`count` is named more than once in `measure(…)`"),
"{refusals}"
);
}
#[test]
fn every_field_wears_exactly_one_role() {
let none = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
note: String,
}
});
assert!(
none.contains("`note` wears neither `#[ingraph(group)]` nor `#[ingraph(measure)]`"),
"{none}"
);
let both = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group, measure)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
both.contains("wears both `#[ingraph(group)]` and `#[ingraph(measure)]`"),
"{both}"
);
let twice = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group, group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
twice.contains("`group` is declared more than once on `codec` — one marker, one field"),
"{twice}"
);
}
#[test]
fn a_role_marker_carries_nothing() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group(rename = "c"))] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(refusals.contains("`group` is a BARE marker"), "{refusals}");
}
#[test]
fn a_head_entry_and_a_marker_that_disagree_are_refused() {
let crossed = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec", count = "n"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
crossed.contains("`count` is named in `group(…)` and wears `#[ingraph(measure)]`"),
"{crossed}"
);
let unnamed = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(group)] lang: Language,
#[ingraph(measure)] count: Count,
}
});
assert!(
unnamed.contains("`lang` wears `#[ingraph(group)]` and `group(…)` has no entry for it"),
"{unnamed}"
);
assert!(
unnamed.contains("`lang = \"<upstream field>\"`"),
"the missing half is spelled out: {unnamed}"
);
let stranger = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting, bytes(fold = summing, from = "size")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
stranger.contains("`bytes` is not one of `CodecCensus`'s"),
"{stranger}"
);
}
#[test]
fn an_unshipped_bare_fold_is_refused_with_the_open_half_named() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = countng), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains("is no fold this framework ships"),
"{refusals}"
);
assert!(refusals.contains("`counting` and `summing`"), "{refusals}");
assert!(refusals.contains("\"path::to::Fold\""), "{refusals}");
assert!(
refusals.contains("did you mean `counting`?"),
"the near miss reaches the closed half: {refusals}"
);
}
#[test]
fn a_measure_entrys_full_form_carries_the_fold_and_the_column_it_reads() {
let model = accepted(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(
bytes(fold = summing, from = "size"),
median(fold = "app::Median", from = "duration_ms"),
),
poll(recount, every = "10m", light),
sqlite,
)]
pub struct TrackCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
#[ingraph(measure)] median: f64,
}
});
assert!(matches!(model.measures[0].by, Measure::Summing));
assert_eq!(
model.measures[0]
.from
.as_ref()
.expect("the full form named the column"),
"size",
"a measure entry carries the UPSTREAM's column name, not this declaration's field name"
);
match &model.measures[1].by {
Measure::Declared(path) => assert_eq!(
path.to_token_stream().to_string().replace(' ', ""),
"app::Median",
),
other => panic!("a quoted fold is a declared one: {other:?}"),
}
assert_eq!(
model.measures[1]
.from
.as_ref()
.expect("a declared fold may read a column too"),
"duration_ms",
);
}
#[test]
fn the_sugar_states_a_whole_entry_and_reads_no_column() {
let model = accepted(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(count = counting, median = "app::Median"),
poll(recount, every = "10m", light),
sqlite,
)]
pub struct TrackCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
#[ingraph(measure)] median: f64,
}
});
assert!(matches!(model.measures[0].by, Measure::Counting));
assert!(
model.measures[0].from.is_none(),
"a counted column reads no column of the rows it counts"
);
assert!(matches!(model.measures[1].by, Measure::Declared(_)));
assert!(model.measures[1].from.is_none());
}
#[test]
fn one_measure_block_holds_both_spellings() {
let model = accepted(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(count = counting, bytes(fold = summing, from = "size")),
poll(recount, every = "10m", light),
sqlite,
)]
pub struct TrackCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
#[ingraph(measure)] bytes: u64,
}
});
assert!(model.measures[0].from.is_none());
assert_eq!(model.measures[1].from.as_ref().expect("named"), "size");
}
#[test]
fn a_fold_that_reads_a_column_is_refused_the_short_spelling() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes = summing), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert!(
refusals.contains("`bytes = summing` leaves out the column `summing` folds"),
"{refusals}"
);
assert!(
refusals.contains("`bytes(fold = summing, from = \"<upstream column>\")`"),
"the full spelling is written out: {refusals}"
);
assert!(
refusals.contains("`<field> = counting` is a whole entry"),
"the sentence says where the short spelling stays: {refusals}"
);
}
#[test]
fn a_full_form_whose_fold_reads_a_column_must_name_one() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing)), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert!(
refusals.contains("`summing` folds an upstream COLUMN and `bytes(…)` names none"),
"{refusals}"
);
assert!(
refusals.contains("It is not filled in from `bytes`"),
"the refused convenience is named rather than left to be assumed: {refusals}"
);
assert!(
refusals.contains("would fold the wrong column wherever they did not"),
"the sentence says what the default would have cost: {refusals}"
);
}
#[test]
fn a_fold_that_reads_no_column_is_refused_a_column() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count(fold = counting, from = "n")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains("`from` names the upstream column a fold READS, and `counting` reads none"),
"{refusals}"
);
assert!(
refusals.contains("`count(fold = counting)` is the entry entire"),
"{refusals}"
);
}
#[test]
fn a_declared_fold_is_refused_neither_spelling() {
let with_column = accepted(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(median(fold = "app::Median", from = "ms")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] median: f64,
}
});
assert_eq!(with_column.measures[0].from.as_ref().expect("named"), "ms");
let without = accepted(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(median(fold = "app::Median")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] median: f64,
}
});
assert!(without.measures[0].from.is_none());
}
#[test]
fn an_unknown_property_names_the_entrys_roster_and_the_nearest_word() {
let near = refusals(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing, frm = "size")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert_eq!(near.len(), 1, "one mistake, one error: {near:?}");
let near = near.join("\n");
assert!(near.contains("unknown property in `bytes(…)`"), "{near}");
assert!(
near.contains("a measure entry knows `fold` and `from`"),
"the roster is printed off the table the walk reads: {near}"
);
assert!(near.contains("did you mean `from`?"), "{near}");
let far = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing, hippopotamus = "size")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert!(
far.contains("a measure entry knows `fold` and `from`"),
"the sentence still teaches where nothing is near: {far}"
);
assert!(!far.contains("did you mean"), "{far}");
}
#[test]
fn a_column_that_could_not_be_read_is_never_reported_as_absent() {
let refusals = refusals(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summing, from = 7)), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
assert!(
refusals[0].contains("`from = \"<upstream column>\"` names the upstream column"),
"{refusals:?}"
);
assert!(
!refusals[0].contains("names none"),
"an author who wrote the column is never told they left it out: {refusals:?}"
);
}
#[test]
fn a_full_form_that_names_no_fold_is_refused_once() {
let none = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(from = "size")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert!(
none.contains("`bytes(…)` names no fold, and a measure column is what a FOLD fills"),
"{none}"
);
let refused_fold = refusals(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes(fold = summin, from = "size")), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert_eq!(
refused_fold.len(),
1,
"a fold written and refused BY NAME must not also be told it was never written: {refused_fold:?}"
);
assert!(
refused_fold[0].contains("is no fold this framework ships"),
"{refused_fold:?}"
);
}
#[test]
fn an_empty_measure_entry_is_refused_with_both_spellings() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(bytes()), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert!(
refusals.contains("`bytes()` says nothing, which is not a measure"),
"{refusals}"
);
assert!(refusals.contains("FULL FORM"), "{refusals}");
assert!(
refusals.contains("the sugar `bytes = <fold>`"),
"{refusals}"
);
}
#[test]
fn a_property_written_twice_in_one_entry_is_refused() {
let refusals = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"), group(codec = "codec"),
measure(bytes(fold = summing, fold = counting, from = "size", from = "n")),
poll(p, every = "10m", light),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] bytes: u64,
}
});
assert!(
refusals.contains("`fold` is declared more than once in `bytes(…)`"),
"{refusals}"
);
assert!(
refusals.contains("`from` is declared more than once in `bytes(…)`"),
"{refusals}"
);
assert!(
refusals.contains("a column of its own takes an entry of its own"),
"the sentence says what a second answer is not: {refusals}"
);
}
#[test]
fn the_sketchs_fold_call_earns_one_error_and_never_the_false_clock() {
let refusals = refusals(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(total_ms = summing("duration_ms")),
poll(recount, every = "10m", light),
sqlite,
)]
pub struct TrackCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] total_ms: u64,
}
});
assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
assert!(
refusals[0].contains("`total_ms = summing` leaves out the column `summing` folds"),
"and it is the rule rather than the punctuation: {refusals:?}"
);
assert!(
!refusals[0].contains("poll"),
"the clock this declaration DOES carry is never refused as missing: {refusals:?}"
);
}
#[test]
fn a_counting_column_of_another_width_is_refused_at_its_own_type() {
let over = |ty: proc_macro2::TokenStream| {
refusals(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(count = counting),
poll(recount, every = "10m", light, async(inline)),
sqlite,
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: #ty,
}
})
};
for narrowing in [quote::quote!(u32), quote::quote!(u64)] {
let spelled = narrowing.to_string();
let refusals = over(narrowing);
assert_eq!(
refusals.len(),
1,
"one mistake, one error at `{spelled}`: {refusals:?}"
);
assert!(
refusals[0].contains("which answers HOW MANY ROWS a group holds"),
"the sentence names what the fold answers at `{spelled}`: {refusals:?}"
);
assert!(
refusals[0].contains("carries every cardinality at `ingraph::Count`"),
"and the TYPE it answers at, which is no longer a width at `{spelled}`: {refusals:?}"
);
assert!(
refusals[0].contains("Write `count: ingraph::Count`"),
"and it says what to write instead at `{spelled}`: {refusals:?}"
);
}
}
#[test]
fn a_width_this_pass_cannot_know_is_left_to_the_emitted_call() {
let over = |measure: proc_macro2::TokenStream, ty: proc_macro2::TokenStream| {
accepted(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(#measure),
poll(recount, every = "10m", light, async(inline)),
sqlite,
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: #ty,
}
})
};
over(
quote::quote!(count(fold = summing, from = "size")),
quote::quote!(u32),
);
over(quote::quote!(count = counting), quote::quote!(Tally));
over(
quote::quote!(count = counting),
quote::quote!(ingraph::Count),
);
over(
quote::quote!(count = "app::Median"),
quote::quote!(::core::primitive::f64),
);
}
#[test]
fn a_column_named_after_the_emissions_own_method_is_refused() {
for projection in [quote::quote!(measure), quote::quote!(measure, copy)] {
let refusals = refusals(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"),
group(codec = "codec"),
measure(fold(fold = summing, from = "size")),
poll(recount, every = "10m", light, async(inline)),
sqlite,
)]
pub struct Colliding {
#[ingraph(group)] codec: Codec,
#[ingraph(#projection)] fold: u64,
}
});
assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
assert!(
refusals[0].contains("is the method an aggregate's expansion MINTS"),
"{refusals:?}"
);
}
}
#[test]
fn a_head_that_could_not_be_read_to_the_end_earns_no_absence_refusals() {
let refusals = refusals(quote::quote! {
#[ingraph(
upstream(node = "demux::Track") group(codec = "codec"),
measure(count = counting),
poll(recount, every = "10m", light),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert_eq!(refusals.len(), 1, "one mistake, one error: {refusals:?}");
for false_absence in [
"needs a `poll(…)` clock",
"needs at least one `group(…)` entry",
"needs at least one `measure(…)` entry",
"has no entry for it",
] {
assert!(
!refusals[0].contains(false_absence),
"{false_absence} is a claim about tokens the reader never reached: {refusals:?}"
);
}
}
#[test]
fn a_truncated_head_still_reports_what_the_fields_said() {
let refusals = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track") group(codec = "codec"),
measure(count = counting),
poll(recount, every = "10m", light),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
note: String,
}
});
assert!(
refusals.contains("`note` wears neither `#[ingraph(group)]` nor `#[ingraph(measure)]`"),
"{refusals}"
);
assert!(!refusals.contains("needs a `poll(…)` clock"), "{refusals}");
}
#[test]
fn an_aggregate_takes_no_declared_id() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group, id)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains("mints its key from its GROUP COLUMNS"),
"{refusals}"
);
}
#[test]
fn an_election_marker_is_sent_to_the_kind_that_elects() {
for (word, owner) in [("knn", "vector"), ("searchable", "fulltext")] {
let marker = syn::Ident::new(word, proc_macro2::Span::call_site());
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group, #marker)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains(&format!("The marker is the `{owner}` kind's own")),
"{refusals}"
);
assert!(
refusals.contains("an aggregate FOLDS its upstream's rows"),
"{refusals}"
);
}
}
#[test]
fn the_record_words_an_aggregate_has_already_decided_are_refused_by_name() {
for (word, clause) in [
("lineage", "the path is the GROUP columns"),
("key", "the key is the group columns"),
(
"subscribe",
"reads its upstream's TABLE rather than its messages",
),
] {
let entry = syn::Ident::new(word, proc_macro2::Span::call_site());
let refusals = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting),
poll(p, every = "10m", light), #entry = "x",
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(refusals.contains(clause), "{word}: {refusals}");
}
}
#[test]
fn another_storage_word_is_refused_with_the_one_storage_law() {
for (word, held) in [
("fulltext", "full-text index"),
("vector", "embedding index"),
("blob", "artifact family"),
] {
let entry = syn::Ident::new(word, proc_macro2::Span::call_site());
let refusals = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting),
poll(p, every = "10m", light), #entry(tantivy),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains(&format!("an aggregate owns no {held}")),
"{refusals}"
);
assert!(
refusals.contains("an aggregate's is the folded rows it keeps"),
"the law's own clause for this kind: {refusals}"
);
}
}
#[test]
fn an_unknown_head_entry_names_the_grammar_and_the_nearest_word() {
let refusals = refused(quote::quote! {
#[ingraph(
upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting),
poll(p, every = "10m", light), measur(x = counting),
)]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains("unknown `ingraph` aggregate attribute"),
"{refusals}"
);
assert!(refusals.contains("did you mean `measure`?"), "{refusals}");
}
#[test]
fn an_unknown_field_entry_names_the_roles_and_the_nearest_word() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(grup)] codec: Codec,
#[ingraph(measure)] count: Count,
}
});
assert!(
refusals.contains("a field of an aggregate takes its ROLE"),
"{refusals}"
);
assert!(refusals.contains("did you mean `group`?"), "{refusals}");
}
#[test]
fn an_aggregate_is_a_concrete_struct_with_named_fields() {
let generic = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus<T> {
#[ingraph(group)] codec: T,
#[ingraph(measure)] count: Count,
}
});
assert!(
generic.contains("generic aggregates are not supported"),
"{generic}"
);
let positional = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus(Codec, u64);
});
assert!(
positional.contains("must be a struct with named fields"),
"{positional}"
);
let variant = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub enum CodecCensus { A }
});
assert!(
variant.contains("`#[ingraph::aggregate]` supports structs only"),
"{variant}"
);
let bare = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {}
});
assert!(
bare.contains("an aggregate with no fields folds nothing"),
"{bare}"
);
}
#[test]
fn a_declaration_carrying_an_injected_key_is_a_second_storage() {
let injected = ingraph_codegen::model::borrowed_key(&EmissionPrefix::default());
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"), group(codec = "codec"), measure(count = counting), poll(p, every = "10m", light))]
pub struct CodecCensus {
#[ingraph(group)] codec: Codec,
#[ingraph(measure)] count: Count,
#injected: Key,
}
});
assert!(
refusals.contains("a second storage macro is being asked to fold a row that has one"),
"{refusals}"
);
}
#[test]
fn the_role_words_are_refused_by_name_on_every_other_kind() {
let head = |source: proc_macro2::TokenStream, kind| {
let input: syn::DeriveInput = syn::parse2(source).expect("the declaration parses");
let (host, arguments) = crate::host::split(&input);
let refused = match kind {
ingraph_codegen::model::NodeKind::Record => crate::parse::parse(&host, &arguments, kind)
.err()
.map(|e| e.into_iter().map(|e| e.to_string()).collect::<Vec<_>>()),
_ => crate::index::parse(&host, &arguments, kind)
.err()
.map(|e| e.into_iter().map(|e| e.to_string()).collect::<Vec<_>>()),
};
refused.expect("this declaration breaks a rule").join("\n")
};
let record = head(
quote::quote! {
#[ingraph(sqlite, group(codec = "codec"))]
pub struct Rows {
#[ingraph(id)] id: u128,
#[ingraph(measure)] count: Count,
}
},
ingraph_codegen::model::NodeKind::Record,
);
assert!(
record.contains(
"`group(…)` says which column a folded row is keyed by, and a record folds \
nothing"
),
"{record}"
);
assert!(
record.contains(
"`measure` marks this column as the one that says how a group's rows are \
folded"
),
"{record}"
);
assert!(
record.contains("declare it with `#[ingraph::aggregate(…)]`"),
"{record}"
);
let index = head(
quote::quote! {
#[ingraph(upstream(node = "Segment", body = "text"), dedup(hash), measure(count = counting))]
pub struct Near {
#[ingraph(searchable, group)] body: String,
}
},
ingraph_codegen::model::NodeKind::Fulltext,
);
assert!(
index.contains(
"`measure(…)` says how a group's rows are folded into one column, and a \
full-text index folds nothing"
),
"{index}"
);
assert!(
index.contains(
"`group` marks this column as the one that says which column a folded row is \
keyed by"
),
"{index}"
);
assert!(
!index.contains("reads as an ENGINE"),
"a word this grammar can answer is never read as somebody's engine: {index}"
);
}
#[test]
fn one_compile_reports_the_whole_declaration() {
let refusals = refused(quote::quote! {
#[ingraph(upstream(node = "demux::Track"))]
pub struct CodecCensus {
codec: Codec,
#[ingraph(measure)] count: Count,
}
});
for rule in [
"wears neither",
"needs at least one `group(…)` entry",
"has no entry for it",
"needs a `poll(…)` clock",
] {
assert!(
refusals.contains(rule),
"{rule} is missing from: {refusals}"
);
}
}