use quote::ToTokens as _;
use super::Journal;
fn parse(input: &syn::DeriveInput) -> syn::Result<Journal> {
let (host, arguments) = crate::host::split(input);
super::parse(&host, &arguments)
}
fn accepted(input: proc_macro2::TokenStream) -> Journal {
parse(&syn::parse2(input).expect("the declaration parses")).expect("the declaration is a journal")
}
fn refused(input: proc_macro2::TokenStream) -> String {
parse(&syn::parse2(input).expect("the declaration parses"))
.err()
.expect("this declaration breaks a rule")
.into_iter()
.map(|error| error.to_string())
.collect::<Vec<_>>()
.join("\n")
}
fn columns(journal: &Journal) -> Vec<String> {
journal
.model
.record
.fields
.iter()
.map(|field| {
format!(
"{}: {}",
field.column.name,
field
.column
.ty
.to_token_stream()
.to_string()
.replace(' ', "")
)
})
.collect()
}
#[test]
fn the_ruled_line_mounts_the_canonical_record() {
let journal = accepted(quote::quote! {
#[ingraph(root = "MediaId", sqlite)]
pub struct VideoJournal;
});
assert_eq!(journal.model.record.record, "VideoJournal");
assert_eq!(
journal.model.root.to_token_stream().to_string(),
"MediaId",
"the root travels to the row contract as the type the declaration named"
);
assert_eq!(
columns(&journal),
[
"root: MediaId",
"node: ::std::string::String",
"station: ::std::string::String",
"at: ::ingraph::__private::primitives::Timestamp",
]
);
}
#[test]
fn the_key_is_the_pair_an_entry_is_replaced_on() {
let journal = accepted(quote::quote! {
#[ingraph(root = "MediaId", sqlite)]
pub struct VideoJournal;
});
let key = &journal.model.record.key;
assert_eq!(
key
.path
.iter()
.map(|column| column.name.to_string())
.collect::<Vec<_>>(),
["root"],
"the lineage is the root, and only the root"
);
assert_eq!(
key.id.name, "node",
"and the id is the node, so the pair is `(root, node)`"
);
}
#[test]
fn the_persist_block_passes_through_whole() {
let journal = accepted(quote::quote! {
#[ingraph(
root = "MediaId",
table = "video_journal", sqlite(alias(depth = 12)), postgres)]
pub struct VideoJournal;
});
let persist = journal
.model
.record
.persist
.as_ref()
.expect("the block reached the record grammar");
assert_eq!(
persist.table.as_ref().map(ToString::to_string),
Some("video_journal".to_owned())
);
assert_eq!(
persist
.backends
.iter()
.map(|db| db.kind().as_str())
.collect::<Vec<_>>(),
["sqlite", "postgres"]
);
}
#[test]
fn a_journal_may_name_no_database() {
let journal = accepted(quote::quote! {
#[ingraph(root = "MediaId")]
pub struct VideoJournal;
});
assert!(journal.model.record.persist.is_none());
assert_eq!(columns(&journal).len(), 4, "and the four columns are there");
}
#[test]
fn a_journal_can_name_the_crate() {
let journal = accepted(quote::quote! {
#[ingraph(crate = "crate::shim", root = "MediaId")]
pub struct VideoJournal;
});
assert_eq!(
journal.model.record.krate.to_token_stream().to_string(),
"crate :: shim"
);
assert!(
columns(&journal)
.last()
.is_some_and(|at| at == "at: crate::shim::__private::primitives::Timestamp"),
"{:?}",
columns(&journal)
);
}
#[test]
fn the_authors_own_attributes_reach_the_mounted_host() {
let journal = accepted(quote::quote! {
#[derive(Debug, Clone)]
#[ingraph(root = "MediaId")]
pub struct VideoJournal;
});
let attrs = journal
.host
.attrs
.iter()
.map(|attr| attr.to_token_stream().to_string().replace(' ', ""))
.collect::<Vec<_>>();
assert_eq!(attrs.len(), 2, "{attrs:?}");
assert!(attrs[0].contains("Whatthedeploymentcallsit."), "{attrs:?}");
assert_eq!(attrs[1], "#[derive(Debug,Clone)]");
assert!(
!attrs.iter().any(|attr| attr.contains("ingraph")),
"the word's own arguments are taken out, so the record pass does not report them twice: \
{attrs:?}"
);
assert_eq!(journal.host.vis.to_token_stream().to_string(), "pub");
}
#[test]
fn a_journal_names_its_root() {
let refusal = refused(quote::quote! {
#[ingraph(sqlite)]
pub struct VideoJournal;
});
assert!(
refusal.contains("a journal needs `root = \"…\"`"),
"{refusal}"
);
}
#[test]
fn a_host_carrying_fields_is_sent_to_the_hand_path() {
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId")]
pub struct VideoJournal {
detail: Option<String>,
}
});
assert!(
refusal.contains("a journal's host is a MARKER"),
"{refusal}"
);
assert!(refusal.contains("HAND path"), "{refusal}");
assert!(
refusal.contains("implement `JournalRow` on it"),
"{refusal}"
);
}
#[test]
fn a_positional_host_is_refused_the_same_way() {
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId")]
pub struct VideoJournal(String);
});
assert!(
refusal.contains("a journal's host is a MARKER"),
"{refusal}"
);
}
#[test]
fn an_enum_host_is_refused() {
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId")]
pub enum VideoJournal { A }
});
assert!(
refusal.contains("a journal's host is a marker STRUCT"),
"{refusal}"
);
}
#[test]
fn a_generic_host_is_refused() {
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId")]
pub struct VideoJournal<Root>;
});
assert!(
refusal.contains("a journal takes no generic parameter"),
"{refusal}"
);
}
#[test]
fn the_words_a_canonical_shape_has_already_decided_are_refused_by_name() {
for (word, written) in [
("lineage", quote::quote!(lineage(fields(root)))),
("key", quote::quote!(key(rename = "Address"))),
("graphql", quote::quote!(graphql)),
("implements", quote::quote!(implements(media::Media))),
("subscribe", quote::quote!(subscribe = "media::Media")),
("upstream", quote::quote!(upstream(node = "media::Media"))),
] {
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId", #written)]
pub struct VideoJournal;
});
assert!(
refusal.contains(&format!("`{word}` decides something a CANONICAL journal")),
"{word}: {refusal}"
);
assert!(refusal.contains("HAND path"), "{word}: {refusal}");
}
}
#[test]
fn the_other_storages_are_refused_as_nodes_of_their_own() {
for word in ["fulltext", "vector", "blob"] {
let entry = syn::Ident::new(word, proc_macro2::Span::call_site());
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId", #entry(tantivy))]
pub struct VideoJournal;
});
assert!(
refusal.contains("a journal holds ROWS") && refusal.contains("its OWN node"),
"{word}: {refusal}"
);
assert!(
refusal.contains(&format!("the `{word}` macro")),
"{word}: {refusal}"
);
}
}
#[test]
fn an_unknown_word_names_what_a_journal_takes() {
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId", retention = "30d")]
pub struct VideoJournal;
});
assert!(
refusal.contains("unknown `ingraph` journal attribute"),
"{refusal}"
);
assert!(
refusal.contains("which is what CANONICAL means"),
"{refusal}"
);
}
#[test]
fn a_root_that_is_not_a_type_is_refused_at_the_string() {
let refusal = refused(quote::quote! {
#[ingraph(root = "not a type")]
pub struct VideoJournal;
});
assert!(
refusal.contains("`root = \"…\"` names the chain root"),
"{refusal}"
);
}
#[test]
fn each_word_is_declared_once() {
let root = refused(quote::quote! {
#[ingraph(root = "MediaId", root = "OtherId")]
pub struct VideoJournal;
});
assert!(root.contains("`root` is declared more than once"), "{root}");
let store = refused(quote::quote! {
#[ingraph(root = "MediaId", sqlite, sqlite)]
pub struct VideoJournal;
});
assert!(
store.contains("`sqlite` is declared more than once"),
"{store}"
);
let krate = refused(quote::quote! {
#[ingraph(crate = "a::b", crate = "c::d", root = "MediaId")]
pub struct VideoJournal;
});
assert!(
krate.contains("`crate` is declared more than once"),
"{krate}"
);
}
#[test]
fn persist_is_refused_in_every_spelling() {
for written in [
quote::quote!(persist = "sqlite"),
quote::quote!(persist(sqlite)),
quote::quote!(persist),
] {
let refusal = refused(quote::quote! {
#[ingraph(root = "MediaId", #written)]
pub struct VideoJournal;
});
assert!(
refusal.contains("`persist(…)` is gone — a journal names its stores BARE"),
"{refusal}"
);
}
}
#[test]
fn one_compile_reports_the_whole_declaration() {
let refusal = refused(quote::quote! {
#[ingraph(lineage(fields(media)))]
pub struct VideoJournal {
media: MediaId,
}
});
assert!(refusal.contains("`lineage` decides something"), "{refusal}");
assert!(
refusal.contains("a journal's host is a MARKER"),
"{refusal}"
);
assert!(
refusal.contains("a journal needs `root = \"…\"`"),
"{refusal}"
);
}