use ingraph_codegen::prelude::NodeKind;
use crate::parse;
fn refusals(input: syn::DeriveInput) -> String {
let (host, arguments) = crate::host::split(&input);
parse::parse(&host, &arguments, NodeKind::Record)
.expect_err("the declaration is refused")
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn model(input: syn::DeriveInput) -> ingraph_codegen::prelude::RecordModel {
let (host, arguments) = crate::host::split(&input);
match parse::parse(&host, &arguments, NodeKind::Record) {
Ok(model) => model,
Err(errors) => panic!(
"the declaration is accepted: {}",
errors
.into_iter()
.map(|e| e.to_string())
.collect::<Vec<_>>()
.join("\n")
),
}
}
#[test]
fn the_bare_word_mints_no_column_anywhere_and_carries_its_fill() {
let model = model(syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar)]
frame: u32,
#[ingraph(skip)]
clip: Vec<f32>,
}
});
let persist = model.persist.as_ref().expect("the record is stored");
for database in &persist.backends {
assert!(
!database
.columns()
.iter()
.any(|column| column.source == "clip"),
"`{}` minted a column for a skipped field",
database.kind().as_str()
);
}
let skipped = model
.fields
.iter()
.find(|field| field.column.source == "clip")
.expect("a skipped field is still a field of the record");
assert!(
skipped
.skip
.as_ref()
.expect("the word reaches the model")
.named
.is_none(),
"a bare word names no filler, and the fill is `Default::default()`"
);
}
#[test]
fn the_word_takes_the_fill_serde_spells() {
let model = model(syn::parse_quote! {
#[ingraph(sqlite)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(skip(default = "crate::fixtures::no_clip"))]
clip: Vec<f32>,
}
});
let named = model
.fields
.iter()
.find(|field| field.column.source == "clip")
.and_then(|field| field.skip.as_ref())
.and_then(|fill| fill.named.as_ref())
.expect("the named filler reaches the model");
assert_eq!(
quote::quote!(#named).to_string().replace(' ', ""),
"crate::fixtures::no_clip"
);
}
#[test]
fn identity_is_unskippable_on_the_storage_axis() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)))]
struct Shot {
#[ingraph(id, skip)]
id: u32,
#[ingraph(copy, skip)]
media_id: u32,
}
});
assert!(
refused.contains("is the record's `id`"),
"the id names what it bears: {refused}"
);
assert!(
refused.contains("is a step of the record's LINEAGE"),
"a lineage step names what it bears: {refused}"
);
assert_eq!(
refused
.matches("Identity is unskippable on both axes")
.count(),
2,
"both identity-bearing fields are refused: {refused}"
);
}
#[test]
fn identity_is_unskippable_on_the_wire_axis() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql), lineage(fields(media_id)))]
struct Shot {
#[ingraph(id, graphql(skip))]
id: u32,
#[ingraph(copy, graphql(async_graphql(skip)))]
media_id: u32,
}
});
assert!(
refused.contains("the address is what a hit answers with"),
"the wire half names its own reason: {refused}"
);
assert_eq!(
refused
.matches("Identity is unskippable on both axes")
.count(),
2,
"both seats are refused: {refused}"
);
}
#[test]
fn a_field_cannot_both_skip_and_persist() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, skip, rename = "frame_number")]
frame: u32,
}
});
assert!(
refused.contains("carries `skip` and `rename`"),
"the pair is named, and the sentence names the WORD that contradicts: {refused}"
);
assert!(
refused.contains("absent from SOME of the record's databases"),
"the near miss is offered by name — the partial absence has a spelling now: {refused}"
);
}
#[test]
fn stored_nowhere_and_published_is_refused_by_name() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, skip)]
frame: u32,
}
});
assert!(
refused.contains(
"wears `skip`, so it is a column in no database, and the wire face publishes every \
column"
),
"the shape is named: {refused}"
);
assert!(
refused.contains("resolver-computed field"),
"the census pin rides the sentence: {refused}"
);
}
#[test]
fn a_wire_skipped_field_needs_no_stored_source() {
let model = model(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(skip(default = "crate::fixtures::no_clip"), graphql(skip))]
clip: Vec<f32>,
}
});
let graphql = model.graphql.as_ref().expect("the record has a wire face");
assert!(
!graphql.publishes(&syn::parse_quote!(clip)),
"the word withholds the field from every vocabulary"
);
assert!(
graphql
.backends
.iter()
.all(|backend| !backend.fields().iter().any(|field| field.source == "clip")),
"a withheld field never becomes a wire field"
);
}
#[test]
fn a_repeated_skip_word_is_the_repeat_refusal_family() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, skip, skip)]
frame: u32,
#[ingraph(scalar, graphql(skip, skip))]
title: u32,
}
});
assert!(
refused.contains("`skip` is declared more than once on this field — one word, one field"),
"the storage seat: {refused}"
);
assert!(
refused.contains(
"`skip` is declared more than once in `graphql(…)` on this field — one word, one field"
),
"the wire seat: {refused}"
);
}
#[test]
fn the_wire_word_is_bare() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, graphql(skip(default = "crate::fixtures::zero")))]
frame: u32,
}
});
assert!(
refused.contains("`skip` on the wire is a BARE word"),
"the shape is named: {refused}"
);
assert!(
refused.contains("`#[ingraph(skip(default = \"…\"))]`"),
"the fill's own seat is shown: {refused}"
);
}
#[test]
fn the_wire_word_refuses_the_entries_it_makes_unreachable() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, graphql(skip, rename = "frameNumber"))]
frame: u32,
}
});
assert!(
refused.contains("has no name, no form and no crossing to declare"),
"the reason is stated: {refused}"
);
}
#[test]
fn a_per_database_skip_with_a_fill_masks_one_database_and_no_other() {
let model = model(syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, sqlite(skip(default = "no_frame")))]
frame: u32,
}
});
let persist = model.persist.as_ref().expect("a record stores rows");
let named = |kind: ingraph_codegen::prelude::DatabaseKind| {
persist
.backends
.iter()
.find(|backend| backend.kind() == kind)
.expect("the database is declared")
};
let sqlite = named(ingraph_codegen::prelude::DatabaseKind::Sqlite);
let postgres = named(ingraph_codegen::prelude::DatabaseKind::Postgres);
assert!(
!sqlite
.columns()
.iter()
.any(|column| column.source == "frame"),
"the masked database mints no column for it"
);
assert!(
postgres
.columns()
.iter()
.any(|column| column.source == "frame"),
"and the other one still does"
);
assert_eq!(
sqlite.storage().fills.len(),
1,
"the masked database carries the fill"
);
assert!(
postgres.storage().fills.is_empty(),
"and the database that has the column carries none"
);
}
#[test]
fn a_per_database_skip_with_no_fill_is_refused_for_the_fill() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, sqlite(skip))]
frame: u32,
}
});
assert!(
refused.contains("a read of a `sqlite` row has to hand something back"),
"the demand is the READ's, said at the word: {refused}"
);
assert!(
refused.contains(r#"#[ingraph(sqlite(skip(default = "…")))]"#),
"and the spelling that answers it is written out: {refused}"
);
}
#[test]
fn a_field_narrowed_by_a_presence_list_is_asked_for_the_fill() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, sqlite)]
frame: u32,
}
});
assert!(
refused.contains("has no `frame` to fetch and nothing to hand back instead"),
"the refusal names the READ that has no answer: {refused}"
);
assert!(
refused.contains("postgres(skip(default"),
"and names the spelling that supplies one, at the database that needs it: {refused}"
);
}
#[test]
fn a_presence_list_beside_its_fills_is_accepted() {
let model = model(syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, sqlite, postgres(skip(default = "no_frame")))]
frame: u32,
}
});
let persist = model.persist.as_ref().expect("a record stores rows");
let postgres = persist
.backends
.iter()
.find(|backend| backend.kind() == ingraph_codegen::prelude::DatabaseKind::Postgres)
.expect("the database is declared");
assert_eq!(
postgres.storage().fills.len(),
1,
"the database the list left out is filled"
);
}
#[test]
fn a_narrowed_address_step_is_refused() {
let refused = refusals(syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Shot {
#[ingraph(id, sqlite)]
id: u32,
#[ingraph(scalar)]
frame: u32,
}
});
assert!(
refused.contains("An address step is a column of every database the record declares"),
"the all-databases-present law is named: {refused}"
);
assert!(
refused.contains("a fill there would INVENT an identity"),
"and the reason is identity rather than layout: {refused}"
);
}
#[test]
fn a_presence_list_naming_every_declared_database_is_a_restatement() {
model(syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Shot {
#[ingraph(id)]
id: u32,
#[ingraph(scalar, sqlite, postgres)]
frame: u32,
}
});
}