use ingraph_codegen::prelude::*;
use crate::{host, parse::parse};
fn record(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
let (host, options) = host::split(input);
parse(&host, &options, NodeKind::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"))
}
fn marker<'a>(model: &'a RecordModel, source: &str) -> &'a Marker {
field(model, source)
.marker
.as_ref()
.expect("this field declared a label")
}
fn refused(input: &syn::DeriveInput) -> String {
record(input)
.err()
.map(|error| error.to_string())
.unwrap_or_else(|| panic!("this declaration is refused"))
}
#[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);
}
#[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);
}
#[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);
}
}
#[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);
}
#[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);
}
#[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"
);
}
#[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}"
);
}
#[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}"
);
}
#[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}"
);
}
#[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}"
);
}
#[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}"
);
}