use ingraph_codegen::{
index::IndexModel,
model::GraphqlKind,
prelude::{NodeKind, Projection},
};
use super::tests::{fulltext, refused};
fn backends(model: &IndexModel) -> Vec<&'static str> {
model.graphql.as_ref().map_or_else(Vec::new, |block| {
block
.backends
.iter()
.map(|backend| backend.kind.as_str())
.collect()
})
}
#[test]
fn a_declaration_that_wrote_no_block_names_no_backend() {
let model = fulltext(&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash))]
struct D { #[ingraph(searchable)] body: String }
})
.expect("a declaration with no wire face is legal");
assert!(
model.graphql.is_none(),
"no block is *no wire face*, and it is a form rather than an unset field",
);
assert_eq!(model.searchable.len(), 1);
assert_eq!(model.source.projections.len(), 1);
}
#[test]
fn the_backend_word_is_carried_as_the_backend_it_names() {
let model = fulltext(&syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql),
)]
struct D { #[ingraph(searchable)] body: String }
})
.expect("a declaration naming a backend is legal");
assert_eq!(backends(&model), vec![GraphqlKind::AsyncGraphql.as_str()]);
assert_eq!(model.searchable.len(), 1, "and the election is untouched");
}
#[test]
fn the_block_reaches_the_vector_kind_by_the_same_words() {
let (host, arguments) = crate::host::split(&syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", clip = "clip"),
dedup(hash),
graphql(async_graphql),
)]
struct D {
#[ingraph(embedding, knn)]
clip: ::ingraph::Embedding<f32, 4, Clip>,
}
});
let model =
super::super::index::parse(&host, &arguments, NodeKind::Vector).expect("a vector may render");
assert_eq!(backends(&model), vec![GraphqlKind::AsyncGraphql.as_str()]);
}
#[test]
fn a_bare_word_names_no_backend_and_is_refused_with_the_words_on_offer() {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash), graphql)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(e.contains("names no backend"), "{e}");
assert!(
e.contains("graphql(async_graphql)"),
"and the sentence spells the word its author is missing: {e}",
);
assert!(
e.contains("WHOLE Rust surface"),
"and says what is NOT missing without it: {e}",
);
}
#[test]
fn an_unknown_backend_is_refused_with_the_ones_this_framework_renders_through() {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", body = "text"), dedup(hash), graphql(smear))]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(e.contains("unknown backend"), "{e}");
assert!(e.contains(GraphqlKind::AsyncGraphql.as_str()), "{e}");
}
#[test]
fn one_backend_is_named_once_and_the_block_is_written_once() {
let twice = refused(
syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql, async_graphql),
)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(twice.contains("one block per backend"), "{twice}");
let two_blocks = refused(
syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql),
graphql(async_graphql),
)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(
two_blocks.contains("one block holds every backend"),
"{two_blocks}",
);
}
#[test]
fn an_artifact_family_refuses_the_block_with_its_reason() {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", payload = "pixels"), fs, graphql(async_graphql))]
struct D { #[ingraph(repeated)] payload: Vec<u8> }
},
NodeKind::Blob,
);
assert!(e.contains("mints no wire type of its own"), "{e}");
assert!(
e.contains("locate("),
"and where its address and bytes actually live: {e}",
);
assert!(
!e.contains("has not arrived"),
"the wait was retired with the round: {e}",
);
}
#[test]
fn the_word_is_claimed_before_a_blobs_open_engine_position() {
let e = refused(
syn::parse_quote! {
#[ingraph(upstream(node = "a::A", payload = "pixels"), fs, graphql(async_graphql))]
struct D { #[ingraph(repeated)] payload: Vec<u8> }
},
NodeKind::Blob,
);
assert!(
!e.contains("an engine is named by ONE word"),
"the word was read as an engine rather than as this grammar's: {e}",
);
let (host, arguments) = crate::host::split(&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", payload = "pixels"), fs)]
struct D { #[ingraph(repeated)] payload: Vec<u8> }
});
let model = super::super::index::parse(&host, &arguments, NodeKind::Blob).expect("legal");
assert_eq!(
model
.stores
.iter()
.map(|engine| engine.word.to_string())
.collect::<Vec<_>>(),
vec!["fs".to_owned()],
);
assert_eq!(model.fields[0].project, Projection::Repeated);
}
fn skin(model: &IndexModel) -> &ingraph_codegen::index::IndexBackend {
model
.graphql
.as_ref()
.expect("the declaration named a backend")
.skin(GraphqlKind::AsyncGraphql)
.expect("that backend's block")
}
pub(super) fn dressed(inside: proc_macro2::TokenStream) -> syn::Result<IndexModel> {
let (host, arguments) = crate::host::split(&syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql(#inside)),
)]
struct D { #[ingraph(searchable)] body: String }
});
super::super::index::parse(&host, &arguments, NodeKind::Fulltext)
}
#[test]
fn the_backend_block_carries_its_base_and_its_three_faces() {
let model = dressed(quote::quote! {
prefix = "shotSearch",
options(rename = "ShotKnobs"),
question(rename = "ShotAsk"),
engine(rename = "WhichStore"),
})
.expect("a backend block naming its skin is legal");
let skin = skin(&model);
assert_eq!(
skin.prefix.as_ref().map(syn::LitStr::value).as_deref(),
Some("shotSearch")
);
assert_eq!(
skin.options.as_ref().map(syn::LitStr::value).as_deref(),
Some("ShotKnobs")
);
assert_eq!(
skin.question.as_ref().map(syn::LitStr::value).as_deref(),
Some("ShotAsk")
);
assert_eq!(
skin.engine.as_ref().map(syn::LitStr::value).as_deref(),
Some("WhichStore")
);
assert_eq!(model.index.to_string(), "D");
}
#[test]
fn a_skin_word_that_is_not_a_graphql_name_is_refused_where_it_was_written() {
let e = refused(
syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql(prefix = "9lives")),
)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(e.contains("is not a GraphQL name"), "{e}");
}
#[test]
fn a_rust_keyword_is_an_ordinary_word_in_a_skins_namespace() {
let model =
dressed(quote::quote! { options(rename = "type") }).expect("`type` is a legal GraphQL name");
assert_eq!(
skin(&model)
.options
.as_ref()
.map(syn::LitStr::value)
.as_deref(),
Some("type"),
);
}
#[test]
fn the_evidence_face_is_refused_with_the_measurement_behind_it() {
let e = refused(
syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql(fused(rename = "WhoAgreed"))),
)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(e.contains("no schema name to move"), "{e}");
assert!(e.contains("GENERIC in the score"), "{e}");
assert!(
e.contains("mounts no query field at all"),
"the TIER's door is what the word runs into: {e}",
);
assert!(
!e.contains("Rust-only today"),
"the retired over-claim: a fanned election's fused answer IS published: {e}",
);
assert!(
e.contains("options(rename"),
"and what the block CAN name: {e}",
);
assert!(
e.contains("that field's own backend block"),
"and where the live `fused` word is: {e}",
);
}
#[test]
fn the_bone_axis_is_not_written_in_a_skins_block() {
let e = refused(
syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", body = "text"),
dedup(hash),
graphql(async_graphql(evidence(rename = "WhoAgreed"))),
)]
struct D { #[ingraph(searchable)] body: String }
},
NodeKind::Fulltext,
);
assert!(e.contains("unknown entry"), "{e}");
assert!(
e.contains("The RUST family is re-based by the top-level"),
"the sentence names the other axis: {e}",
);
}