use ingraph_codegen::prelude::*;
use crate::host;
fn model(block: &str) -> Result<RecordModel, Vec<String>> {
let input: syn::DeriveInput = syn::parse_str(&format!(
"#[ingraph(sqlite, graphql({block}))]
struct Reel {{
#[ingraph(id)] id: i64,
#[ingraph(string)] title: String,
}}"
))
.expect("a parseable declaration");
let (host, options) = host::split(&input);
crate::parse::parse(&host, &options, NodeKind::Record)
.map_err(|e| e.into_iter().map(|e| e.to_string()).collect())
}
fn words(block: &str) -> (Option<String>, Option<String>) {
let model = model(block).expect("the declaration settles");
let graphql = model.graphql.expect("the wire face");
(graphql.connection, graphql.edge)
}
fn refusal(block: &str) -> String {
model(block)
.expect_err("the declaration is refused")
.join(" | ")
}
#[test]
fn the_two_entries_reach_the_model_as_written() {
assert_eq!(
words(r#"async_graphql, connection(rename = "SpliceReel"), edge(rename = "SpliceReelItem")"#),
(
Some("SpliceReel".to_owned()),
Some("SpliceReelItem".to_owned())
)
);
}
#[test]
fn either_word_may_be_written_without_the_other() {
assert_eq!(
words(r#"async_graphql, connection(rename = "Page")"#),
(Some("Page".to_owned()), None)
);
assert_eq!(
words(r#"async_graphql, edge(rename = "Item")"#),
(None, Some("Item".to_owned()))
);
}
#[test]
fn a_declaration_that_writes_neither_carries_neither() {
assert_eq!(words("async_graphql"), (None, None));
}
#[test]
fn the_records_rust_rename_does_not_move_the_page_words() {
assert_eq!(
words(r#"async_graphql, rename = "Spool", connection(rename = "Page")"#),
(Some("Page".to_owned()), None),
"the page's word is its own"
);
}
#[test]
fn a_repeated_word_is_refused_once_per_entry() {
let repeated = refusal(r#"async_graphql, connection(rename = "A", rename = "B")"#);
assert!(
repeated.contains("more than once") && repeated.contains("connection(…)"),
"{repeated}"
);
let edge = refusal(r#"async_graphql, edge(rename = "A", rename = "B")"#);
assert!(
edge.contains("more than once") && edge.contains("edge(…)"),
"{edge}"
);
}
#[test]
fn a_block_with_nothing_in_it_is_refused() {
for block in [
"async_graphql, connection",
"async_graphql, connection()",
"async_graphql, edge",
"async_graphql, edge()",
] {
let refused = refusal(block);
assert!(
refused.contains("rename = ") && refused.contains("naming the type"),
"{block}: {refused}"
);
}
}
#[test]
fn an_unknown_entry_inside_the_block_is_refused() {
let refused = refusal(r#"async_graphql, connection(name = "Page")"#);
assert!(
refused.contains("unknown entry in `connection(…)`"),
"{refused}"
);
}
#[test]
fn a_word_outside_graphqls_name_grammar_is_refused() {
let refused = refusal(r#"async_graphql, connection(rename = "débit")"#);
assert!(refused.contains("is not a GraphQL name"), "{refused}");
}
#[test]
fn the_blocks_own_sentence_lists_the_two_new_entries() {
let refused = refusal(r#"async_graphql, connexion(rename = "Page")"#);
assert!(
refused.contains("`connection`") && refused.contains("`edge`"),
"{refused}"
);
}