use ingraph_codegen::prelude::*;
use crate::host;
fn parse(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = host::split(input);
crate::parse::parse(&host, &options, NodeKind::Record)
}
fn refusals(input: &syn::DeriveInput) -> Vec<String> {
parse(input)
.expect_err("this declaration is refused")
.into_iter()
.map(|error| error.to_string())
.collect()
}
#[test]
fn a_declared_plural_reaches_the_model_verbatim() {
let model = parse(&syn::parse_quote! {
#[ingraph(table = "corpora", sqlite, graphql(async_graphql, plural = "corpora"))]
struct Corpus {
#[ingraph(id, copy)] id: i64,
#[ingraph(string)] name: String,
}
})
.expect("a declared plural is a declaration this grammar takes");
let graphql = model.graphql.expect("the record declares a wire face");
assert_eq!(graphql.plural.as_deref(), Some("corpora"));
}
#[test]
fn a_record_that_wrote_no_word_carries_none() {
let model = parse(&syn::parse_quote! {
#[ingraph(table = "reels", sqlite, graphql(async_graphql))]
struct Reel {
#[ingraph(id, copy)] id: i64,
#[ingraph(string)] title: String,
}
})
.expect("the ordinary declaration");
assert_eq!(
model
.graphql
.expect("the record declares a wire face")
.plural,
None
);
}
#[test]
fn a_plural_is_declared_once() {
let messages = refusals(&syn::parse_quote! {
#[ingraph(
table = "corpora", sqlite,
graphql(async_graphql, plural = "corpora", plural = "corpuses")
)]
struct Corpus {
#[ingraph(id, copy)] id: i64,
}
});
assert!(
messages
.iter()
.any(|message| message.contains("`plural` is declared more than once in `graphql(…)`")),
"{messages:?}"
);
}
#[test]
fn a_plural_is_a_written_word() {
let messages = refusals(&syn::parse_quote! {
#[ingraph(table = "corpora", sqlite, graphql(async_graphql, plural = corpora))]
struct Corpus {
#[ingraph(id, copy)] id: i64,
}
});
assert!(
messages
.iter()
.any(|message| message.contains("`plural` is the entity's plural as a WORD")),
"{messages:?}"
);
}
#[test]
fn a_plural_that_is_not_a_graphql_name_is_refused() {
for word in ["", "9lives", "reel list"] {
let attribute: syn::Attribute = syn::parse_quote! {
#[ingraph(table = "reels", sqlite, graphql(async_graphql, plural = #word))]
};
let messages = refusals(&syn::parse_quote! {
#attribute
struct Reel {
#[ingraph(id, copy)] id: i64,
}
});
assert!(
messages
.iter()
.any(|message| message.contains("is not a GraphQL name")),
"{word:?}: {messages:?}"
);
}
}
#[test]
fn the_unknown_entry_refusal_names_the_plural() {
let messages = refusals(&syn::parse_quote! {
#[ingraph(table = "reels", sqlite, graphql(async_graphql, plurals = "reels"))]
struct Reel {
#[ingraph(id, copy)] id: i64,
}
});
assert!(
messages
.iter()
.any(|message| message.contains("unknown entry in `graphql(…)`")
&& message.contains("`plural = \"…\"`")),
"{messages:?}"
);
}
#[test]
fn a_derived_plural_equal_to_the_singular_is_refused() {
let messages = refusals(&syn::parse_quote! {
#[ingraph(table = "media", sqlite, graphql(async_graphql))]
struct Media {
#[ingraph(id, copy)] id: i64,
#[ingraph(string)] label: String,
}
});
assert_eq!(messages.len(), 1, "one silence, one refusal: {messages:?}");
assert!(
messages[0].contains("`Media`'s page would be published as `media`"),
"the refusal quotes the name a client would have read: {messages:?}"
);
assert!(
messages[0].contains("`graphql(plural = \"…\")`"),
"…and teaches the entry that answers it: {messages:?}"
);
}
#[test]
fn a_declared_plural_equal_to_the_singular_is_allowed() {
let model = parse(&syn::parse_quote! {
#[ingraph(table = "media", sqlite, graphql(async_graphql, plural = "media"))]
struct Media {
#[ingraph(id, copy)] id: i64,
#[ingraph(string)] label: String,
}
})
.expect("a written word is a choice, and a choice is not refused");
assert_eq!(
model
.graphql
.expect("the record declares a wire face")
.plural
.as_deref(),
Some("media")
);
}
#[test]
fn a_record_with_no_page_is_asked_for_no_plural() {
let model = parse(&syn::parse_quote! {
#[ingraph(table = "media", sqlite, graphql(async_graphql, plural = "many"))]
struct Media {
#[ingraph(id)] id: u128,
#[ingraph(string)] label: String,
}
})
.expect("a record with no page owes no plural");
assert!(
model.degenerate_plural().is_none(),
"no page, nothing to refuse"
);
}