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 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_access_word_on_the_field() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(string), indexed)] tags: Vec<String>,
}
})
.expect("a persisted collection may ask for the value-led access path");
let Marker::Framework(FrameworkMarker { label, element }) = field(&model, "tags")
.marker
.as_ref()
.expect("this field declared a label")
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").indexed);
}
#[test]
fn a_set_takes_the_access_word_that_an_ordering_word_refuses() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(set(string), indexed)] marks: Vec<String>,
}
})
.expect("a set's membership is what the value-led path seeks");
assert!(field(&model, "marks").indexed);
}
#[test]
fn the_access_word_sits_beside_the_ordering_word() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), sortable, indexed)] marks: Vec<i64>,
}
})
.expect("an order and an access path are two intents about one collection");
assert!(field(&model, "marks").sortable);
assert!(field(&model, "marks").indexed);
}
#[test]
fn the_word_may_be_written_before_the_collection_word() {
for declaration in [
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(indexed, list(scalar))] marks: Vec<i64>,
}
},
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), indexed)] marks: Vec<i64>,
}
},
] {
let model: syn::DeriveInput = declaration;
let model = record(&model).expect("either order is the same declaration");
assert!(field(&model, "marks").indexed);
}
}
#[test]
fn refuses_the_access_word_on_a_field_with_no_elements() {
let e = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(scalar, indexed)] bit_rate: u64,
}
});
assert!(e.contains("is a COLLECTION's word"), "{e}");
assert!(
e.contains("#[ingraph(sqlite(indexed))]"),
"it names the seat a single column's index is written at: {e}"
);
assert!(
e.contains("#[ingraph(list(scalar), indexed)]"),
"and the shape a collection's is: {e}"
);
}
#[test]
fn a_key_side_column_is_redirected_alike() {
let e = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id, indexed)] id: u128,
#[ingraph(list(string))] tags: Vec<String>,
}
});
assert!(e.contains("is a COLLECTION's word"), "{e}");
assert!(
e.contains("#[ingraph(sqlite(indexed))]"),
"the seat is named wherever the field sits: {e}"
);
}
#[test]
fn refuses_the_access_word_beside_a_custom_label() {
let e = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(label = "a::marker::Path", indexed)] tags: Vec<String>,
}
});
assert!(e.contains("cannot classify"), "{e}");
assert!(e.contains("`list(…)` or `set(…)`"), "{e}");
}
#[test]
fn refuses_a_repeated_access_word() {
let e = refused(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), indexed, indexed)] marks: Vec<i64>,
}
});
assert!(e.contains("declared more than once"), "{e}");
}
#[test]
fn refuses_the_access_word_carrying_anything() {
for declaration in [
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), indexed = "yes")] marks: Vec<i64>,
}
},
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), indexed(sqlite))] marks: Vec<i64>,
}
},
] {
let declaration: syn::DeriveInput = declaration;
let _ = refused(&declaration);
}
}
#[test]
fn the_per_database_word_on_a_collection_names_the_field_seat() {
for declaration in [
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), sqlite(indexed))] marks: Vec<i64>,
}
},
syn::parse_quote! {
#[ingraph(postgres, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(list(scalar), postgres(indexed))] marks: Vec<i64>,
}
},
] {
let declaration: syn::DeriveInput = declaration;
let e = refused(&declaration);
assert!(e.contains("is a COLLECTION and"), "{e}");
assert!(
e.contains("#[ingraph(list(scalar), indexed)]"),
"the refusal names the seat the word does have: {e}"
);
assert!(
e.contains("folds case"),
"and why that seat cannot be a database's: {e}"
);
}
}
#[test]
fn a_scalar_keeps_its_per_database_index() {
let model = record(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: u128,
#[ingraph(scalar, sqlite(indexed))] bit_rate: u64,
}
})
.expect("a column's index is its database's own word");
assert!(
model
.persist
.as_ref()
.expect("the record stores")
.backends
.iter()
.flat_map(DatabaseBackend::columns)
.any(|column| column.source == "bit_rate" && column.indexed),
"the per-database word still reaches the column it is about"
);
}
#[test]
fn a_collection_without_the_word_is_unchanged() {
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 needs no storage word");
assert!(!field(&model, "tags").indexed);
assert!(field(&model, "tags").marker.is_some());
}