use crate::{
model::DatabaseKind,
name::RustSafeName,
standalone::{StandaloneColumn, StandaloneModel, StandaloneStored},
};
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn column(name: &str, ty: &str) -> StandaloneColumn {
let member = ident(name);
StandaloneColumn {
name: RustSafeName::from_ident(&member),
member,
domain: syn::parse_str(ty).expect("a type"),
wire: None,
marker: None,
stored: Vec::new(),
}
}
fn roll() -> StandaloneModel {
StandaloneModel {
vis: syn::Visibility::Public(syn::token::Pub::default()),
subject: ident("Roll"),
krate: syn::parse_quote!(::ingraph),
filter: RustSafeName::from_ident(&ident("RollFilter")),
order_by: RustSafeName::from_ident(&ident("RollOrderBy")),
comparison: RustSafeName::from_ident(&ident("Roll")),
databases: Vec::new(),
columns: vec![column("width", "u32"), column("title", "String")],
hook: Some(syn::parse_quote!(RollFilter)),
}
}
fn dense_of(tokens: &proc_macro2::TokenStream) -> String {
let file: syn::File = syn::parse2(tokens.clone()).expect("the emission is a parsable file");
prettyplease::unparse(&file)
.split_whitespace()
.collect::<String>()
}
fn tight(tokens: proc_macro2::TokenStream) -> String {
tokens.to_string().replace(' ', "")
}
#[test]
fn the_bare_comparison_gains_the_pair_under_a_hook() {
let emitted = dense_of(&roll().emit_comparison());
assert!(
emitted.contains("::core::default::DefaultforRollComparison<__INGRAPH_DB__>where"),
"the empty predicate, at a parameter: {emitted}"
);
assert!(
emitted.contains("TryFromFilter<RollFilter>forRollComparison<__INGRAPH_DB__>where"),
"the crossing, named from the hook rather than from this door's own minted filter: {emitted}"
);
assert!(
emitted.contains("ToCrossedComparison<u32,__INGRAPH_DB__,>"),
"the scalar column's own row, through the same machinery the fields tier's bare arm uses: \
{emitted}"
);
assert!(
emitted.contains("ToCrossedComparison<String,__INGRAPH_DB__,>"),
"and the string column's: {emitted}"
);
}
#[test]
fn two_columns_of_one_type_dedup_to_one_row() {
let mut model = roll();
model.columns = vec![column("width", "u32"), column("height", "u32")];
let clause = tight(model.crossings(&ident("__INGRAPH_DB__"), None));
assert_eq!(
clause.matches("ToCrossedComparison<u32,").count(),
1,
"one clause row for the shared kind, not two: {clause}"
);
}
#[test]
fn a_bare_comparison_with_no_hook_gains_nothing() {
let mut model = roll();
model.hook = None;
let emitted = dense_of(&model.emit_comparison());
assert!(
!emitted.contains("TryFromFilter"),
"no crossing without a seat to cross from: {emitted}"
);
assert!(
!emitted.contains("ToCrossedComparison"),
"the new machinery is unreached: {emitted}"
);
assert!(
!emitted.contains("ToDatabaseComparison<Roll,"),
"the map row stays absent exactly as it always was: {emitted}"
);
assert!(
!emitted.contains("implcore::default::Default"),
"and `Default` does not ship alone: {emitted}"
);
}
#[test]
fn the_named_comparison_gains_the_pair_under_a_hook() {
let mut model = roll();
model.databases = vec![DatabaseKind::Sqlite];
let emitted = dense_of(&model.emit_comparison());
assert!(
emitted.contains("::core::default::DefaultforRollSqliteComparison<__INGRAPH_DB__>where"),
"the empty predicate, at a parameter, for the NAMED comparison: {emitted}"
);
assert!(
emitted.contains("TryFromFilter<RollFilter>forRollSqliteComparison<__INGRAPH_DB__>where"),
"the crossing, named from the hook rather than from this door's own minted filter: {emitted}"
);
assert!(
emitted.contains("ToCrossedComparison<u32,__INGRAPH_DB__,>"),
"the scalar column's own row — no per-database override written here, so the DOMAIN type: \
{emitted}"
);
assert!(
emitted.contains("ToCrossedComparison<String,__INGRAPH_DB__,>"),
"and the string column's: {emitted}"
);
assert!(
emitted.contains("ToDatabaseComparison<Roll"),
"the pre-existing map row survives untouched: {emitted}"
);
}
#[test]
fn a_named_comparison_with_no_hook_gains_nothing() {
let mut model = roll();
model.databases = vec![DatabaseKind::Sqlite];
model.hook = None;
let emitted = dense_of(&model.emit_comparison());
assert!(
!emitted.contains("TryFromFilter"),
"no crossing without a seat to cross from, named database or not: {emitted}"
);
assert!(
!emitted.contains("ToCrossedComparison"),
"the new machinery is unreached: {emitted}"
);
assert!(
!emitted.contains("ToDatabaseComparison<Roll,"),
"the map row stays absent exactly as it always was: {emitted}"
);
assert!(
!emitted.contains("implcore::default::Default"),
"and `Default` does not ship alone: {emitted}"
);
}
#[test]
fn the_named_pair_crosses_through_the_per_database_stored_override() {
let mut model = roll();
model.databases = vec![DatabaseKind::Sqlite, DatabaseKind::Postgres];
model.columns = vec![StandaloneColumn {
stored: vec![
StandaloneStored {
ty: Some(syn::parse_str("i64").expect("a type")),
flatten: false,
},
StandaloneStored {
ty: Some(syn::parse_str("i32").expect("a type")),
flatten: false,
},
],
..column("width", "u32")
}];
let emitted = dense_of(&model.emit_comparison());
assert!(
emitted.contains("TryFromFilter<RollFilter>forRollSqliteComparison<__INGRAPH_DB__>where"),
"the SQLite arm gets the pair: {emitted}"
);
assert!(
emitted.contains("ToCrossedComparison<i64,__INGRAPH_DB__,>"),
"…crossing through ITS OWN override, `i64`, not the shared `u32` domain: {emitted}"
);
assert!(
emitted.contains("TryFromFilter<RollFilter>forRollPostgresComparison<__INGRAPH_DB__>where"),
"the Postgres arm gets the pair too: {emitted}"
);
assert!(
emitted.contains("ToCrossedComparison<i32,__INGRAPH_DB__,>"),
"…crossing through ITS OWN override, `i32` — a DIFFERENT stored type from SQLite's, at the SAME \
declared column: {emitted}"
);
assert!(
!emitted.contains("ToCrossedComparison<u32,"),
"the override is what crosses, not the domain underneath it: {emitted}"
);
}
#[test]
fn a_flatten_column_at_a_named_database_states_both_bounds() {
let model = StandaloneModel {
columns: vec![StandaloneColumn {
stored: vec![StandaloneStored {
ty: Some(syn::parse_str("i64").expect("a type")),
flatten: true,
}],
..column("width", "u32")
}],
databases: vec![DatabaseKind::Sqlite],
..roll()
};
let clause = tight(model.crossings(&ident("__INGRAPH_DB__"), Some(0)));
assert!(
clause.contains("ToCrossedComparison<i64,__INGRAPH_DB__>,"),
"the crossing itself, through the override — what `cross`/`empty` need: {clause}"
);
assert!(
clause.contains("ToFlattenedComparison<i64,__INGRAPH_DB__>,"),
"…and the flattened row beside it, which `Self::new`'s own clause still demands: {clause}"
);
}
#[test]
fn a_stored_entry_on_a_bare_column_is_inert() {
let mut model = roll();
model.columns = vec![StandaloneColumn {
stored: vec![StandaloneStored {
ty: Some(syn::parse_str("i64").expect("a type")),
flatten: true,
}],
..column("width", "u32")
}];
let emitted = dense_of(&model.emit_comparison());
assert!(
emitted.contains("ToCrossedComparison<u32,"),
"the domain type, not the inert per-database override: {emitted}"
);
assert!(
!emitted.contains("ToCrossedComparison<i64,"),
"a `stored` entry with no named database to select it is never read: {emitted}"
);
}