use ingraph_codegen::prelude::*;
use quote::ToTokens as _;
fn parse(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
under(input, NodeKind::Record)
}
fn operator(input: &syn::DeriveInput) -> syn::Result<RecordModel> {
under(input, NodeKind::Operator)
}
fn under(input: &syn::DeriveInput, kind: NodeKind) -> syn::Result<RecordModel> {
let (host, options) = crate::host::split(input);
crate::parse::parse(&host, &options, kind)
}
fn lineage_names(model: &RecordModel) -> Vec<String> {
model
.lineage
.iter()
.flat_map(|lineage| lineage.path().iter())
.map(|column| column.name.to_string())
.collect()
}
fn field<'a>(model: &'a RecordModel, name: &str) -> &'a Field {
model
.fields
.iter()
.find(|field| field.column.name == name)
.unwrap_or_else(|| panic!("`{name}` is a field of the record"))
}
fn rendered(path: &syn::Path) -> String {
path.to_token_stream().to_string().replace(' ', "")
}
fn rendered_type(ty: &syn::Type) -> String {
ty.to_token_stream().to_string().replace(' ', "")
}
#[test]
fn video_track_model_orders_lineage_root_first() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id, video_id)))]
struct VideoTrack { #[ingraph(id)] id: Uuid7, media_id: Uuid7, video_id: Uuid7, bit_rate: u64 }
})
.expect("the grandchild shape is the one T1 exists to prove");
assert_eq!(m.record, "VideoTrack");
assert_eq!(m.key.id.name, "id");
assert_eq!(lineage_names(&m), ["media_id", "video_id"], "root-first");
assert!(
m.lineage
.iter()
.flat_map(|lineage| lineage.path().iter())
.all(|column| matches!(column.ty, syn::Type::Path(_))),
"each lineage field carries the declared type, copied verbatim"
);
}
#[test]
fn media_has_no_lineage() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct Media { #[ingraph(id)] id: Uuid7, files: Vec<Uuid7>, video_id: Option<Uuid7> }
})
.expect("a root record omits the lineage block");
assert!(m.lineage.is_none(), "absence by omission");
assert_eq!(
m.key.id.name, "id",
"the `Option<Uuid7>` FK and the `Vec<Uuid7>` reverse lookup are ignored entirely"
);
}
#[test]
fn emitted_names_default_to_the_suffix_convention() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)))]
struct Video { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.expect("naming nothing is the ordinary case");
assert_eq!(m.key.name, "VideoKey");
assert_eq!(
m.lineage.expect("a child has a lineage").name,
"VideoLineage"
);
}
#[test]
fn both_renames_override_the_convention() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite,
lineage(fields(media_id, video_id), rename = "TrackScope"),
key(rename = "TrackAddress"),
)]
struct VideoTrack { #[ingraph(id)] id: Uuid7, media_id: Uuid7, video_id: Uuid7 }
})
.expect("both blocks may name their type");
assert_eq!(m.key.name, "TrackAddress");
assert_eq!(
m.lineage.expect("renamed, still present").name,
"TrackScope"
);
}
#[test]
fn a_rename_may_be_a_raw_identifier() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, key(rename = "r#type"))]
struct S { #[ingraph(id)] id: Uuid7 }
})
.expect("`r#type` is a legal type name");
assert_eq!(m.key.name.to_string(), "r#type");
}
#[test]
fn a_key_column_may_be_marked_copy() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id, video_id)))]
struct VideoTrack {
#[ingraph(id)] id: Uuid7,
#[ingraph(copy)] media_id: Uuid7,
video_id: Uuid7,
}
})
.expect("a marked lineage column");
let lineage = m.lineage.expect("a grandchild has a path");
assert_eq!(
lineage.path()[0].project,
Projection::Copied,
"`media_id` was marked"
);
assert_eq!(
lineage.path()[1].project,
Projection::Borrowed,
"`video_id` was not"
);
assert_eq!(m.key.id.project, Projection::Borrowed, "nor was the id");
}
#[test]
fn id_and_copy_may_share_one_attribute() {
for input in [
syn::parse_quote! { #[ingraph(sqlite)] struct S { #[ingraph(id, copy)] id: Uuid7 } },
syn::parse_quote! { #[ingraph(sqlite)] struct S { #[ingraph(id)] #[ingraph(copy)] id: Uuid7 } },
] {
let m = parse(&input).expect("both spellings mark the same field twice over");
assert_eq!(m.key.id.project, Projection::Copied);
}
}
#[test]
fn refuses_a_repeated_field_marker() {
for input in [
syn::parse_quote! { struct S { #[ingraph(id, id)] id: Uuid7 } },
syn::parse_quote! { struct S { #[ingraph(id)] #[ingraph(id)] id: Uuid7 } },
syn::parse_quote! { struct S { #[ingraph(id, copy, copy)] id: Uuid7 } },
syn::parse_quote! { struct S { #[ingraph(id)] #[ingraph(copy)] #[ingraph(copy)] id: Uuid7 } },
] {
let e = parse(&input).unwrap_err();
assert!(e.to_string().contains("declared more than once"), "{e}");
}
}
#[test]
fn any_field_may_be_marked_copy_now_that_the_record_is_emitted_too() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)))]
struct S {
#[ingraph(id)] id: Uuid7,
media_id: Uuid7,
#[ingraph(copy)] checksum: u64,
chapters: Vec<Uuid7>,
}
})
.expect("a data field carries the marker too");
let checksum = field(&m, "checksum");
assert_eq!(
checksum.column.project,
Projection::Copied,
"the marker reached the data field"
);
assert!(!checksum.identity, "and it bears no identity");
assert_eq!(field(&m, "chapters").column.project, Projection::Borrowed);
}
#[test]
fn every_field_reaches_the_model_and_knows_whether_it_bears_identity() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)))]
struct Video {
#[ingraph(id)] id: Uuid7,
media_id: Uuid7,
total_scenes: u32,
tracks: Vec<Uuid7>,
}
})
.expect("a child with data fields");
assert_eq!(
m.fields
.iter()
.map(|f| f.column.name.to_string())
.collect::<Vec<_>>(),
["id", "media_id", "total_scenes", "tracks"],
"declared order, data fields included"
);
assert_eq!(
m.fields
.iter()
.filter(|f| f.identity)
.map(|f| f.column.name.to_string())
.collect::<Vec<_>>(),
["id", "media_id"],
"the id and the lineage step, and nothing else"
);
}
#[test]
fn the_model_carries_the_source_visibility() {
for (declaration, expected) in [
(
syn::parse_quote! { #[ingraph(sqlite)] pub struct S { #[ingraph(id)] id: Uuid7 } },
"pub",
),
(
syn::parse_quote! { #[ingraph(sqlite)] pub(crate) struct S { #[ingraph(id)] id: Uuid7 } },
"pub(crate)",
),
(
syn::parse_quote! { #[ingraph(sqlite)] struct S { #[ingraph(id)] id: Uuid7 } },
"",
),
] {
let m = parse(&declaration).expect("visibility is not a rule, only a fact to carry");
assert_eq!(
m.vis.to_token_stream().to_string().replace(' ', ""),
expected
);
}
}
#[test]
fn crate_path_defaults_and_overrides() {
let d = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct A { #[ingraph(id)] id: Uuid7 }
})
.expect("no `crate` option is the ordinary case");
assert_eq!(rendered(&d.krate), "::ingraph");
let o = parse(&syn::parse_quote! {
#[ingraph(sqlite, crate = "shim::vendored::ingraph")]
struct B { #[ingraph(id)] id: Uuid7 }
})
.expect("a re-export shim names itself");
assert_eq!(rendered(&o.krate), "shim::vendored::ingraph");
}
#[test]
fn refuses_a_crate_value_that_is_not_a_path() {
let e = parse(&syn::parse_quote! {
#[ingraph(crate = "not a path!")]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("must be a path"), "{e}");
}
#[test]
fn refuses_a_crate_value_that_is_not_a_string() {
let e = parse(&syn::parse_quote! {
#[ingraph(crate = 7)]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("must be a path"), "{e}");
}
#[test]
fn refuses_a_repeated_crate_option() {
let e = parse(&syn::parse_quote! {
#[ingraph(crate = "a::b", crate = "c::d")]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("declared more than once"), "{e}");
}
#[test]
fn refuses_a_repeated_block() {
for input in [
syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), lineage(fields(video_id)))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7, video_id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(key(rename = "A"), key(rename = "B"))]
struct S { #[ingraph(id)] id: Uuid7 }
},
] {
let e = parse(&input).unwrap_err();
assert!(e.to_string().contains("declared more than once"), "{e}");
}
}
#[test]
fn refuses_a_repeated_option_inside_a_block() {
let e = parse(&syn::parse_quote! {
#[ingraph(key(rename = "A", rename = "B"))]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("declared more than once"), "{e}");
}
#[test]
fn refuses_an_unknown_entry_inside_a_block() {
for input in [
syn::parse_quote! {
#[ingraph(key(mystery = "x"))]
struct S { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(lineage(fields(media_id), mystery))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
},
] {
let e = parse(&input).unwrap_err();
assert!(e.to_string().contains("unknown entry in"), "{e}");
}
}
#[test]
fn refuses_the_old_flat_lineage_shorthand() {
let e = parse(&syn::parse_quote! {
#[ingraph(lineage(media_id, video_id))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7, video_id: Uuid7 }
})
.unwrap_err();
let message = e.to_string();
assert!(message.contains("unknown entry in"), "{message}");
assert!(
message.contains("fields(…)"),
"it names the replacement: {message}"
);
}
#[test]
fn refuses_a_lineage_block_without_fields() {
for input in [
syn::parse_quote! {
#[ingraph(lineage(rename = "TrackScope"))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(lineage())]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
},
] {
let e = parse(&input).unwrap_err();
assert!(e.to_string().contains("needs `fields(…)`"), "{e}");
}
}
#[test]
fn refuses_an_empty_fields_list() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields()))]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(messages.len(), 1, "{messages:?}");
assert!(messages[0].contains("at least one field"), "{messages:?}");
}
#[test]
fn refuses_a_rename_that_is_not_an_identifier() {
for input in [
syn::parse_quote! {
#[ingraph(key(rename = "Not An Ident"))]
struct S { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(key(rename = 7))]
struct S { #[ingraph(id)] id: Uuid7 }
},
] {
let e = parse(&input).unwrap_err();
assert!(e.to_string().contains("must be a valid identifier"), "{e}");
}
}
#[test]
fn refuses_colliding_key_and_lineage_names() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id), rename = "Both"), key(rename = "Both"))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("already the key type"), "{e}");
}
#[test]
fn refuses_a_rename_that_collides_with_the_others_default() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id), rename = "SKey"))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.unwrap_err();
assert!(
e.to_string().contains("already the key type"),
"a rename onto another emitted type's DEFAULT is the same comparison as onto a rename — a \
defaulted type's name is simply the name it has: {e}"
);
}
#[test]
fn refuses_a_rename_equal_to_the_records_own_name() {
for input in [
syn::parse_quote! {
#[ingraph(sqlite, key(rename = "Video"))]
struct Video { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id), rename = "Video"))]
struct Video { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
},
] {
let e = parse(&input).unwrap_err();
assert!(e.to_string().contains("the record's own name"), "{e}");
}
}
#[test]
fn refuses_missing_id() {
let e = parse(&syn::parse_quote! { struct S { a: u32 } }).unwrap_err();
assert!(e.to_string().contains("exactly one `#[ingraph(id)]`"));
}
#[test]
fn refuses_two_id_fields() {
let e = parse(&syn::parse_quote! {
struct S { #[ingraph(id)] a: u32, #[ingraph(id)] b: u32 }
})
.unwrap_err();
assert!(e.to_string().contains("exactly one `#[ingraph(id)]`"));
}
#[test]
fn refuses_a_lineage_name_that_is_not_a_field() {
let e = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(parent_id)))]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("names fields of this struct"), "{e}");
}
#[test]
fn refuses_the_id_field_in_lineage() {
let e = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(id)))]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(
e.to_string().contains("cannot also be a lineage field"),
"{e}"
);
}
#[test]
fn refuses_a_duplicate_lineage_name() {
let e = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id, media_id)))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("named twice"), "{e}");
}
#[test]
fn refuses_a_generic_record() {
let e = parse(&syn::parse_quote! {
struct Media<Id = Uuid7> { #[ingraph(id)] id: Id }
})
.unwrap_err();
assert!(
e.to_string().contains("generic records are not supported"),
"{e}"
);
}
#[test]
fn refuses_a_lifetime_parameter_and_a_const_parameter_too() {
for input in [
syn::parse_quote! { struct S<'a> { #[ingraph(id)] id: &'a u32 } },
syn::parse_quote! { struct S<const N: usize> { #[ingraph(id)] id: [u8; N] } },
] {
let e = parse(&input).unwrap_err();
assert!(
e.to_string().contains("generic records are not supported"),
"{e}"
);
}
}
#[test]
fn refuses_a_where_clause_even_without_parameters() {
let e = parse(&syn::parse_quote! {
struct S where u32: Copy { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(
e.to_string().contains("generic records are not supported"),
"{e}"
);
}
#[test]
fn refuses_a_tuple_struct() {
let e = parse(&syn::parse_quote! { struct S(u32, u32); }).unwrap_err();
assert!(e.to_string().contains("named fields"), "{e}");
}
#[test]
fn refuses_a_unit_struct() {
let e = parse(&syn::parse_quote! { struct S; }).unwrap_err();
assert!(e.to_string().contains("named fields"), "{e}");
}
#[test]
fn refuses_an_enum() {
let e = parse(&syn::parse_quote! { enum E { A, B } }).unwrap_err();
assert!(e.to_string().contains("structs only"), "{e}");
}
#[test]
fn refuses_a_union() {
let e = parse(&syn::parse_quote! { union U { a: u32 } }).unwrap_err();
assert!(e.to_string().contains("structs only"), "{e}");
}
#[test]
fn refuses_an_unknown_ingraph_attribute() {
let e = parse(&syn::parse_quote! {
#[ingraph(mystery)]
struct S { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err();
assert!(e.to_string().contains("unknown"), "{e}");
}
#[test]
fn two_independent_violations_are_both_reported_in_one_pass() {
let e = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(parent_id)))]
struct S { a: u32 }
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(messages.len(), 2, "{messages:?}");
assert!(
messages
.iter()
.any(|m| m.contains("names fields of this struct")),
"the unknown lineage name: {messages:?}"
);
assert!(
messages
.iter()
.any(|m| m.contains("exactly one `#[ingraph(id)]`")),
"and the missing id: {messages:?}"
);
}
#[test]
fn violations_in_two_different_blocks_are_both_reported() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id), mystery), key(rename = "Not An Ident"))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(messages.len(), 2, "{messages:?}");
assert!(messages.iter().any(|m| m.contains("unknown entry in")));
assert!(
messages
.iter()
.any(|m| m.contains("must be a valid identifier"))
);
}
#[test]
fn a_repeated_field_marker_and_an_record_level_violation_are_both_reported() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(parent_id)))]
struct S { #[ingraph(id, id)] id: Uuid7 }
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(messages.len(), 2, "{messages:?}");
assert!(
messages
.iter()
.any(|m| m.contains("declared more than once")),
"the repeated marker: {messages:?}"
);
assert!(
messages
.iter()
.any(|m| m.contains("names fields of this struct")),
"and the unknown lineage name: {messages:?}"
);
}
#[test]
fn every_offending_lineage_name_is_reported_not_only_the_first() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(nope, id, media_id, media_id)))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.unwrap_err();
assert_eq!(
e.into_iter().count(),
3,
"an unknown name, the id itself, and a repeat"
);
}
#[test]
fn refuses_a_column_inside_the_frameworks_namespace() {
for reserved in ["__seq__", "__x__", "__", "___", "____"] {
let name = syn::Ident::new(reserved, proc_macro2::Span::call_site());
let e = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(#name)))]
struct S { #[ingraph(id)] id: Uuid7, #name: Uuid7 }
})
.unwrap_err();
assert!(
e.to_string().contains("framework's namespace"),
"`{reserved}` is inside the fence: {e}"
);
}
}
#[test]
fn accepts_a_column_that_only_looks_reserved() {
for allowed in ["__internal", "internal__", "_internal_"] {
let name = syn::Ident::new(allowed, proc_macro2::Span::call_site());
parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(#name)))]
struct S { #[ingraph(id)] id: Uuid7, #name: Uuid7 }
})
.unwrap_or_else(|e| panic!("`{allowed}` is outside the fence: {e}"));
}
}
#[test]
fn refuses_a_rename_into_the_frameworks_namespace() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(key(rename = "__seq__"))] media_id: Uuid7,
}
})
.unwrap_err();
assert!(e.to_string().contains("framework's namespace"), "{e}");
}
#[test]
fn a_skipped_field_may_carry_a_reserved_shape() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(skip)] __value__: Vec<u8> }
})
.expect("`__value__` is not a column of any emitted type here");
assert_eq!(columns(&m, DatabaseKind::Sqlite), ["id"]);
}
#[test]
fn a_field_may_be_renamed_per_scope() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id, video_id)))]
struct VideoTrack {
#[ingraph(id)] id: Uuid7,
#[ingraph(lineage(rename = "media"))] media_id: Uuid7,
#[ingraph(key(rename = "video"))] video_id: Uuid7,
}
})
.expect("each scope names its own column");
assert_eq!(
m.key
.columns()
.map(|column| column.name.to_string())
.collect::<Vec<_>>(),
["media_id", "video", "id"],
"the key takes the `key` rename and leaves the `lineage` one alone"
);
assert_eq!(
m.lineage
.as_ref()
.expect("a grandchild has a path")
.path()
.iter()
.map(|column| column.name.to_string())
.collect::<Vec<_>>(),
["media", "video_id"],
"and the lineage takes the other"
);
assert_eq!(
m.key
.columns()
.map(|column| column.source.to_string())
.collect::<Vec<_>>(),
["media_id", "video_id", "id"],
"both keep the record field they project, for the conversions"
);
assert_eq!(
m.fields
.iter()
.map(|f| f.column.name.to_string())
.collect::<Vec<_>>(),
["id", "media_id", "video_id"],
"and the record keeps its own Rust names"
);
}
#[test]
fn one_name_may_be_used_in_both_scopes() {
parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(key(rename = "media"), lineage(rename = "media"))] media_id: Uuid7,
}
})
.expect("two types, one name each");
}
#[test]
fn refuses_a_scoped_rename_with_no_subject() {
for input in [
syn::parse_quote! {
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(key(rename = "c"))] checksum: u64 }
},
syn::parse_quote! {
#[ingraph(lineage(fields(media_id)))]
struct S { #[ingraph(id, lineage(rename = "x"))] id: Uuid7, media_id: Uuid7 }
},
syn::parse_quote! {
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(lineage(rename = "c"))] checksum: u64 }
},
] {
let e = parse(&input).unwrap_err();
assert!(e.to_string().contains("has none there"), "{e}");
assert!(
e.to_string().contains("persisted row"),
"it names the future scope: {e}"
);
}
}
#[test]
fn a_bare_field_rename_names_the_column_in_every_database() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "bitRate")] bit_rate: u64,
}
})
.expect("the storage rename parses");
for db in [DatabaseKind::Sqlite, DatabaseKind::Postgres] {
assert_eq!(stored(&m, db, "bit_rate").name.external(), "bitRate");
}
}
#[test]
fn refuses_a_repeated_field_rename() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "a", rename = "b")] bit_rate: u64,
}
});
assert!(e.contains("`rename` is declared more than once"), "{e}");
}
#[test]
fn refuses_two_columns_of_one_type_resolving_to_one_name() {
let e = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id, video_id)))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(key(rename = "same"))] media_id: Uuid7,
#[ingraph(key(rename = "same"))] video_id: Uuid7,
}
})
.unwrap_err();
assert!(
e.to_string().contains("would have two columns called"),
"{e}"
);
}
#[test]
fn refuses_a_column_whose_emitted_method_is_already_taken() {
let cases: [(syn::DeriveInput, &str); 4] = [
(
syn::parse_quote! { #[ingraph(sqlite)] struct S { #[ingraph(id, copy)] new: Uuid7 } },
"`SKey` would declare `new` twice",
),
(
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, x: u64, #[ingraph(copy)] set_x: u64 }
},
"`S` would declare `set_x` twice",
),
(
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, x_ref: u64, set_x: u64 }
},
"`S` would declare `set_x_ref` twice",
),
(
syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "a_ref", sqlite(copy))] b: u64,
#[ingraph(scalar, rename = "a")] c: u64,
}
},
"`SSqlite` would declare `a_ref` twice",
),
];
for (declaration, taken) in cases {
let e = parse(&declaration).unwrap_err();
assert!(e.to_string().contains(taken), "{e}");
}
}
#[test]
fn a_duplicate_column_name_is_reported_once() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id, video_id)))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(key(rename = "same"))] media_id: Uuid7,
#[ingraph(key(rename = "same"))] video_id: Uuid7,
}
})
.unwrap_err();
assert_eq!(
e.into_iter().count(),
1,
"the column rule, and no restatement of it"
);
}
#[test]
fn the_method_collision_names_the_column_on_each_side() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, x: u64, #[ingraph(copy)] set_x: u64 }
})
.unwrap_err();
let reported = e.to_string();
assert!(reported.contains("the `x` column's setter"), "{reported}");
assert!(
reported.contains("the `set_x` column's value accessor"),
"{reported}"
);
}
#[test]
fn accepts_columns_that_only_look_like_an_emitted_method() {
let declarations: [syn::DeriveInput; 4] = [
syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
new_thing: u64,
set: u64,
with: u64,
x_reference: u64,
}
},
syn::parse_quote! { #[ingraph(sqlite)] struct S { #[ingraph(id)] new: Uuid7 } },
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(copy)] new: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, set_x: u64, y: u64 }
},
];
for declaration in declarations {
parse(&declaration).unwrap_or_else(|e| panic!("a near miss is not a collision: {e}"));
}
}
#[test]
fn refuses_a_repeated_scoped_rename() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(key(rename = "a", rename = "b"))] media_id: Uuid7,
}
})
.unwrap_err();
assert!(e.to_string().contains("declared more than once"), "{e}");
}
#[test]
fn the_projection_markers_reach_the_model() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct Media {
#[ingraph(id)] id: Uuid7,
#[ingraph(repeated)] files: Ids,
#[ingraph(optional)] duration: MaybeDuration,
checksum: u64,
}
})
.expect("both markers are field-level");
assert_eq!(field(&m, "files").column.project, Projection::Repeated);
assert_eq!(field(&m, "duration").column.project, Projection::Optional);
assert_eq!(
field(&m, "checksum").column.project,
Projection::Borrowed,
"nothing is inferred — an unmarked field keeps the plain borrow"
);
}
#[test]
fn refuses_two_projection_markers_on_one_field() {
for input in [
syn::parse_quote! { struct S { #[ingraph(id)] id: Uuid7, #[ingraph(repeated, optional)] a: T } },
syn::parse_quote! { struct S { #[ingraph(id)] id: Uuid7, #[ingraph(copy, repeated)] a: T } },
syn::parse_quote! { struct S { #[ingraph(id)] id: Uuid7, #[ingraph(copy, optional)] a: T } },
syn::parse_quote! { struct S { #[ingraph(id)] id: Uuid7, #[ingraph(optional)] #[ingraph(copy)] a: T } },
] {
let e = parse(&input).unwrap_err();
assert!(
e.to_string()
.contains("each names a different way to hand the column out"),
"{e}"
);
}
}
#[test]
fn refuses_a_repeated_projection_marker() {
for marker in ["repeated", "optional"] {
let name = syn::Ident::new(marker, proc_macro2::Span::call_site());
let e = parse(&syn::parse_quote! {
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(#name, #name)] a: T }
})
.unwrap_err();
assert!(e.to_string().contains("declared more than once"), "{e}");
}
}
fn database(model: &RecordModel, kind: DatabaseKind) -> &DatabaseBackend {
model
.persist
.as_ref()
.unwrap_or_else(|| panic!("the record declares a `persist(…)` block"))
.backends
.iter()
.find(|database| database.kind() == kind)
.unwrap_or_else(|| panic!("`{}` is declared", kind.as_str()))
}
fn stored<'a>(model: &'a RecordModel, kind: DatabaseKind, source: &str) -> &'a PersistColumn {
database(model, kind)
.columns()
.iter()
.find(|column| column.source == source)
.unwrap_or_else(|| panic!("`{source}` is stored in {}", kind.as_str()))
}
fn columns(model: &RecordModel, kind: DatabaseKind) -> Vec<String> {
database(model, kind)
.columns()
.iter()
.map(|column| column.name.to_string())
.collect()
}
fn declared() -> RecordModel {
parse(&syn::parse_quote! {
#[ingraph(
lineage(fields(media_id, video_id)),
table = "video_tracks",
sqlite,
postgres(schema = "public", row(rename = "VideoTrackRecord")),
mongo(collection = "tracks"))]
pub struct VideoTrack {
#[ingraph(id)] id: Uuid7,
media_id: Uuid7,
video_id: Uuid7,
#[ingraph(scalar,
rename = "bitrate",
sqlite(indexed),
postgres(indexed, type = "BeU64", copy,
rename = "bit_rate_v2",
try_from = "path::to::try_from",
into = "path::to::into"),
)]
bit_rate: u64,
#[ingraph(scalar, optional)] stream_index: Option<u32>,
#[ingraph(skip)] cached_probe: OnceCell<Probe>,
}
})
.expect("the documented declaration parses")
}
#[test]
fn the_declared_databases_are_the_rows_that_get_emitted() {
let m = declared();
let persist = m.persist.as_ref().expect("declared");
assert_eq!(
persist
.backends
.iter()
.map(|database| (database.kind(), database.name().to_string()))
.collect::<Vec<_>>(),
[
(DatabaseKind::Sqlite, "VideoTrackSqlite".to_owned()),
(DatabaseKind::Postgres, "VideoTrackRecord".to_owned()),
(DatabaseKind::Mongo, "VideoTrackMongo".to_owned()),
],
"a bare block takes the suffix convention; a `rename` names the emitted STRUCT"
);
assert!(
!persist
.backends
.iter()
.any(|database| database.kind() == DatabaseKind::Mysql),
"a database nobody declared emits nothing"
);
}
#[test]
fn every_unskipped_field_is_persisted() {
let m = declared();
for kind in [DatabaseKind::Sqlite, DatabaseKind::Postgres] {
assert_eq!(
database(&m, kind)
.columns()
.iter()
.map(|column| column.source.to_string())
.collect::<Vec<_>>(),
["id", "media_id", "video_id", "bit_rate", "stream_index"],
"`cached_probe` wears `skip` and is the only field missing"
);
}
}
#[test]
fn a_column_takes_the_common_rename_unless_its_database_overrides_it() {
let m = declared();
assert_eq!(
columns(&m, DatabaseKind::Sqlite),
["id", "media_id", "video_id", "bitrate", "stream_index"]
);
assert_eq!(
columns(&m, DatabaseKind::Postgres),
["id", "media_id", "video_id", "bit_rate_v2", "stream_index"],
"postgres names its own column and leaves the other alone"
);
assert_eq!(
columns(&m, DatabaseKind::Mongo),
["id", "media_id", "video_id", "bitrate", "stream_index"]
);
}
#[test]
fn a_key_field_may_take_a_common_rename() {
let m = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), sqlite, postgres, mongo)]
struct Clip {
#[ingraph(id, rename = "clip")] id: Uuid7,
#[ingraph(rename = "media")] media_id: Uuid7,
#[ingraph(scalar)] bit_rate: u64,
}
})
.expect("a common rename is one name for every database");
for kind in [
DatabaseKind::Sqlite,
DatabaseKind::Postgres,
DatabaseKind::Mongo,
] {
assert_eq!(columns(&m, kind), ["clip", "media", "bit_rate"]);
}
assert_eq!(
(m.key.id.name.to_string(), m.key.path[0].name.to_string()),
("id".to_owned(), "media_id".to_owned()),
"the KEY type's columns are the `key` scope's, untouched by a `persist` rename"
);
}
#[test]
fn a_key_field_stores_the_type_it_declares() {
let m = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), sqlite, postgres)]
struct Clip {
#[ingraph(id)] id: Uuid7,
media_id: Uuid7,
#[ingraph(scalar, postgres(type = "BeU64"))] bit_rate: u64,
}
})
.expect("a key column stores the type its field declares");
for source in ["id", "media_id"] {
for kind in [DatabaseKind::Sqlite, DatabaseKind::Postgres] {
let column = stored(&m, kind, source);
assert_eq!(rendered_type(column.ty()), "Uuid7");
assert!(
column.stored.is_none(),
"nothing was written, so nothing is recorded — `ty()` fell through to the domain type"
);
}
}
assert_eq!(
rendered_type(stored(&m, DatabaseKind::Postgres, "bit_rate").ty()),
"BeU64",
"and a column that DID override one is stored as it asked"
);
}
#[test]
fn a_key_field_takes_each_databases_own_name_and_stored_type() {
let m = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), sqlite, postgres)]
struct Clip {
#[ingraph(id, sqlite(rename = "clip"), postgres(rename = "pk", type = "BeU128"))]
id: Uuid7,
#[ingraph(rename = "media", sqlite(type = "Vec<u8>"), postgres(rename = "media_uuid"))]
media_id: Uuid7,
#[ingraph(scalar)] bit_rate: u64,
}
})
.expect("a key column is refined per database like any other");
assert_eq!(
columns(&m, DatabaseKind::Sqlite),
["clip", "media", "bit_rate"],
"sqlite names the id its own way and takes the common rename for the lineage step"
);
assert_eq!(
columns(&m, DatabaseKind::Postgres),
["pk", "media_uuid", "bit_rate"],
"postgres names both of them its own way, overriding the common rename on the second"
);
assert_eq!(
[
rendered_type(stored(&m, DatabaseKind::Sqlite, "id").ty()),
rendered_type(stored(&m, DatabaseKind::Postgres, "id").ty()),
rendered_type(stored(&m, DatabaseKind::Sqlite, "media_id").ty()),
rendered_type(stored(&m, DatabaseKind::Postgres, "media_id").ty()),
],
["Uuid7", "BeU128", "Vec<u8>", "Uuid7"],
"and each row stores the type ITS database asked for, the domain type where none did"
);
assert_eq!(
(
m.key.id.name.to_string(),
rendered_type(&m.key.id.ty),
m.key.path[0].name.to_string(),
rendered_type(&m.key.path[0].ty),
),
(
"id".to_owned(),
"Uuid7".to_owned(),
"media_id".to_owned(),
"Uuid7".to_owned()
),
"the KEY type is untouched by all four: its columns are the record's Rust fields, carrying the \
types the record declared"
);
}
#[test]
fn the_stored_type_is_the_type_override_or_the_domain_type() {
let m = declared();
assert_eq!(
rendered_type(stored(&m, DatabaseKind::Postgres, "bit_rate").ty()),
"BeU64"
);
assert_eq!(
rendered_type(stored(&m, DatabaseKind::Sqlite, "bit_rate").ty()),
"u64",
"no override, so the row carries the domain type"
);
assert_eq!(
rendered_type(&stored(&m, DatabaseKind::Postgres, "bit_rate").domain),
"u64",
"and the domain type is kept beside it, for the conversions"
);
}
#[test]
fn indexed_and_copy_are_per_database() {
let m = declared();
assert!(stored(&m, DatabaseKind::Sqlite, "bit_rate").indexed);
assert!(stored(&m, DatabaseKind::Postgres, "bit_rate").indexed);
assert!(!stored(&m, DatabaseKind::Mongo, "bit_rate").indexed);
assert!(
stored(&m, DatabaseKind::Postgres, "bit_rate").copy,
"the STORED type is `Copy`, which the domain field's own marker cannot imply"
);
assert!(!stored(&m, DatabaseKind::Sqlite, "bit_rate").copy);
}
#[test]
fn flatten_is_one_databases_naming_decision() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct Clip {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(flatten))] rate: Timebase,
#[ingraph(string)] title: String,
}
})
.expect("a marked field parses");
assert!(stored(&m, DatabaseKind::Sqlite, "rate").flatten);
assert!(
!stored(&m, DatabaseKind::Postgres, "rate").flatten,
"the other database keeps the prefix"
);
assert!(
!stored(&m, DatabaseKind::Sqlite, "title").flatten,
"and the word is the field's, not the record's"
);
}
#[test]
fn the_dialect_free_flatten_reaches_every_sql_database() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, postgres, mongo)]
struct Clip {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, flatten)] rate: Timebase,
#[ingraph(string)] title: String,
}
})
.expect("one word for every database parses");
assert!(stored(&m, DatabaseKind::Sqlite, "rate").flatten);
assert!(stored(&m, DatabaseKind::Postgres, "rate").flatten);
assert!(
!stored(&m, DatabaseKind::Mongo, "rate").flatten,
"a document store has no prefix to drop, so one written word mints nothing there"
);
assert!(
!stored(&m, DatabaseKind::Sqlite, "title").flatten,
"and the word is the field's, not the record's"
);
}
#[test]
fn the_dialect_free_flatten_is_a_bare_word() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct Clip {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, flatten = "yes")] rate: Timebase,
}
});
assert!(e.contains("`flatten` is a BARE word"), "{e}");
}
#[test]
fn refuses_flatten_on_the_document_database() {
let e = refused(syn::parse_quote! {
#[ingraph(mongo)]
struct Clip {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, mongo(flatten))] rate: Timebase,
}
});
assert!(e.contains("has no columns"), "{e}");
assert!(e.contains("nested subdocument"), "{e}");
}
#[test]
fn refuses_a_repeated_flatten() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct Clip {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(flatten, flatten))] rate: Timebase,
}
});
assert!(e.contains("`flatten` is declared more than once"), "{e}");
}
#[test]
fn nullability_follows_the_optional_marker() {
let m = declared();
for kind in [
DatabaseKind::Sqlite,
DatabaseKind::Postgres,
DatabaseKind::Mongo,
] {
assert!(
stored(&m, kind, "stream_index").nullable,
"`#[ingraph( optional)]`"
);
assert!(!stored(&m, kind, "bit_rate").nullable, "`` is NOT NULL");
}
}
#[test]
fn a_spelled_option_without_the_marker_is_not_nullable() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] cover: Option<Uuid7> }
})
.expect("an unmarked `Option<_>` is an ordinary column");
assert!(
!stored(&m, DatabaseKind::Sqlite, "cover").nullable,
"the type's spelling says nothing — a type alias for `Option<_>` is the case that proves it"
);
}
#[test]
fn the_conversions_collapse_into_three_states() {
let m = declared();
let column = stored(&m, DatabaseKind::Postgres, "bit_rate");
assert!(
matches!(&column.from, Conversion::Fallible(leg) if rendered(&leg.path) == "path::to::try_from"
&& leg.escape.is_none()),
"{:?}",
column.from
);
assert!(
matches!(&column.into, Conversion::Infallible(path) if rendered(path) == "path::to::into"),
"{:?}",
column.into
);
let plain = stored(&m, DatabaseKind::Sqlite, "bit_rate");
assert_eq!(plain.from, Conversion::Default);
assert_eq!(plain.into, Conversion::Default);
}
#[test]
fn a_fallible_conversion_needs_no_type_override() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(try_from = "validate"))] bit_rate: u64,
}
})
.expect("a fallible read of the same type is a validation, not a mistake");
let column = stored(&m, DatabaseKind::Sqlite, "bit_rate");
assert!(column.stored.is_none());
assert!(column.from.fallible());
}
#[test]
fn the_table_name_is_common_and_overridable() {
let m = declared();
let persist = m.persist.as_ref().expect("declared");
assert_eq!(
persist.table.as_ref().map(ToString::to_string),
Some("video_tracks".to_owned())
);
assert_eq!(
persist
.table_of(database(&m, DatabaseKind::Sqlite))
.map(ToString::to_string),
Some("video_tracks".to_owned()),
"a database that named none takes the common one"
);
assert_eq!(
persist
.table_of(database(&m, DatabaseKind::Mongo))
.map(ToString::to_string),
Some("tracks".to_owned()),
"and `collection` is mongo's word for the same option"
);
assert_eq!(
database(&m, DatabaseKind::Postgres).schema(),
Some("public")
);
}
#[test]
fn a_common_rename_is_the_base_every_row_suffixes() {
let m = parse(&syn::parse_quote! {
#[ingraph(rename = "Track", sqlite, postgres, mysql, mongo)]
struct VideoTrack { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] bit_rate: u64 }
})
.expect("`rename` names the thing its block is about, at its level");
assert_eq!(
m.persist
.expect("declared")
.backends
.iter()
.map(|database| database.name().to_string())
.collect::<Vec<_>>(),
["TrackSqlite", "TrackPostgres", "TrackMysql", "TrackMongo"],
"sqlx's own spellings"
);
}
#[test]
fn an_entity_is_persisted_nowhere() {
let m = under(
&syn::parse_quote! { struct S { #[ingraph(id)] id: Uuid7, checksum: u64 } },
NodeKind::Entity,
)
.expect("a pure-compute node");
assert!(m.persist.is_none());
}
#[test]
fn a_column_may_be_named_something_rust_cannot_spell() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "bitRate")] bit_rate: u64,
#[ingraph(scalar, rename = "type")] kind: u32,
}
})
.expect("`bitRate` and `type` are ordinary column names");
let names: Vec<(String, String)> = database(&m, DatabaseKind::Sqlite)
.columns()
.iter()
.map(|column| (column.name.to_string(), column.name.safe().to_string()))
.collect();
assert_eq!(
names,
[
("id".to_owned(), "id".to_owned()),
("bitRate".to_owned(), "bit_rate".to_owned()),
("type".to_owned(), "type_".to_owned()),
],
"the mangling is the FIELD's: `bitRate` is folded to snake case and `type` gains an underscore"
);
}
#[test]
fn the_wire_name_rule_is_graphqls_name_grammar() {
fn grammar(name: &str) -> bool {
let start = |c: char| matches!(c, 'a'..='z' | 'A'..='Z' | '_');
let continues = |c: char| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '_');
let mut chars = name.chars();
match chars.next() {
Some(first) if start(first) => chars.all(continues),
_ => false,
}
}
let long = "b".repeat(4096);
let cases: Vec<(&str, bool)> = vec![
("bitRate", true),
("bit_rate", true),
("_", true),
("__typename", true),
("a1", true),
("Z9_", true),
(long.as_str(), true),
("", false),
("9lives", false),
("0", false),
("bit-rate", false),
("-", false),
("bit rate", false),
("bit.rate", false),
("$where", false),
("a!b", false),
("a:b", false),
("débit", false),
("é", false),
("a\0", false),
("\"quoted\"", false),
];
for (name, expected) in cases {
assert_eq!(
grammar(name),
expected,
"the grammar written out is wrong about {name:?}"
);
assert_eq!(
crate::wire::is_graphql_name(name),
grammar(name),
"the derive's rule and GraphQL's Name grammar disagree about {name:?}"
);
}
}
#[test]
fn a_wire_name_the_sdl_grammar_forbids_is_refused_once() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(rename = "bit-rate"))] bit_rate: u64,
}
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(
messages.len(),
1,
"one name, one mistake, one refusal: {messages:?}"
);
assert!(
messages[0].contains("`bit-rate` is not a GraphQL name"),
"{messages:?}"
);
assert!(
messages[0].contains("[_A-Za-z][_0-9A-Za-z]*"),
"the message names the grammar: {messages:?}"
);
}
#[test]
fn a_persist_rename_does_not_supply_the_wire_name() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "debit")] débit: u64,
}
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(messages.len(), 1, "{messages:?}");
assert!(
messages[0].contains("`débit` is not a GraphQL name"),
"the refusal names the field's own spelling, not the rename: {messages:?}"
);
}
#[test]
fn each_position_carries_its_own_wire_name() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(
input(rename = "in-name"),
output(rename = "out-name"),
)))]
bit_rate: u64,
}
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(messages.len(), 2, "{messages:?}");
assert!(
messages.iter().any(|m| m.contains("`in-name`")),
"{messages:?}"
);
assert!(
messages.iter().any(|m| m.contains("`out-name`")),
"{messages:?}"
);
}
#[test]
fn a_column_that_reaches_no_wire_face_answers_to_no_grammar() {
parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "bitRate")] bit_rate: u64,
}
})
.expect("a name every store holds, exposed on no wire face");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(rename = "9lives"))] bit_rate: u64,
}
});
assert!(e.contains("`9lives` is not a GraphQL name"), "{e}");
}
fn refused(input: syn::DeriveInput) -> String {
parse(&input).unwrap_err().to_string()
}
fn refusals(input: syn::DeriveInput) -> Vec<String> {
parse(&input)
.unwrap_err()
.into_iter()
.map(|e| e.to_string())
.collect()
}
fn names(messages: &[String], rule: &str) -> bool {
messages.iter().any(|message| message.contains(rule))
}
#[test]
fn refuses_both_spellings_of_one_conversion_direction() {
for (plain, fallible) in [("from", "try_from"), ("into", "try_into")] {
let plain = syn::Ident::new(plain, proc_macro2::Span::call_site());
let fallible = syn::Ident::new(fallible, proc_macro2::Span::call_site());
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(#plain = "a", #fallible = "b"))] bit_rate: u64,
}
});
assert!(e.contains("there is no third answer"), "{e}");
assert!(
e.contains(&plain.to_string()) && e.contains(&fallible.to_string()),
"both spellings are named: {e}"
);
}
}
#[test]
fn refuses_both_spellings_on_the_container_too() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(from = "a", try_from = "b"))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] bit_rate: u64 }
});
assert!(e.contains("there is no third answer"), "{e}");
}
#[test]
fn refuses_a_field_block_for_an_undeclared_database() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, mongo)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, postgres(indexed))] bit_rate: u64,
}
});
assert!(e.contains("not one of the databases"), "{e}");
assert!(
e.contains("`sqlite`, `mongo`"),
"it names the declared set: {e}"
);
}
#[test]
fn refuses_a_per_database_option_in_the_common_scope() {
for input in [
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, unique)] a: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, type = "BeU64")] a: u64 }
},
] {
let e = refused(input);
assert!(e.contains("is a PER-DATABASE option"), "{e}");
assert!(
e.contains("postgres("),
"it names the shape it belongs in: {e}"
);
}
for input in [
syn::parse_quote! {
#[ingraph(indexed, sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
syn::parse_quote! {
#[ingraph(schema = "public", sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
] {
let e = refused(input);
assert!(
e.contains("unknown `ingraph` record attribute — a record takes its STORE WORDS bare"),
"the head answers for the scope it absorbed: {e}"
);
}
}
#[test]
fn refuses_a_field_persist_on_a_record_that_names_no_store() {
let bare = refusals(syn::parse_quote! {
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, persist)] a: u64 }
});
assert!(names(&bare, "`persist` is retired at a field"), "{bare:?}");
assert!(names(&bare, "holds rows and names no store"), "{bare:?}");
assert!(
!names(&bare, "declares no `persist(…)` block"),
"the block it would have named does not exist to be missing: {bare:?}"
);
let refining = refused(syn::parse_quote! {
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, rename = "b")] a: u64 }
});
assert!(
refining.contains("holds rows and names no store"),
"a block that REFINES a column is answered wherever the columns were to have been: {refining}"
);
}
#[test]
fn refuses_a_head_that_names_no_database() {
for input in [
syn::parse_quote! {
#[ingraph(rename = "T")]
struct S { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(table = "s")]
struct S { #[ingraph(id)] id: Uuid7 }
},
] {
let e = refused(input);
assert!(e.contains("holds rows and names no store"), "{e}");
assert!(
e.contains("declare it an `entity`"),
"and the sentence names the other exit: {e}"
);
}
}
#[test]
fn refuses_a_repeated_database_block() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(
e.contains("`sqlite` is declared more than once — one block per database"),
"the store words are the head's now, so the sentence names no block around them: {e}"
);
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, postgres, sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(m.is_err(), "the repeat is refused");
}
#[test]
fn refuses_a_repeated_graphql_backend_block() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, async_graphql))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(
e.contains(
"`async_graphql` is declared more than once in `graphql(…)` — one block per backend"
),
"{e}"
);
}
#[test]
fn refuses_a_repeated_persist_block_or_option() {
for input in [
syn::parse_quote! {
#[ingraph(sqlite, sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
syn::parse_quote! {
#[ingraph(table = "a", table = "b", sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "a", rename = "b")] a: u64,
}
},
syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(indexed, indexed))] a: u64,
}
},
] {
let e = refused(input);
assert!(e.contains("declared more than once"), "{e}");
}
}
#[test]
fn refuses_a_persist_column_inside_the_frameworks_namespace() {
for input in [
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] __seq__: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, rename = "__seq__")] a: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, postgres(rename = "__seq__"))] a: u64,
}
},
] {
let e = refused(input);
assert!(e.contains("framework's namespace"), "{e}");
}
}
#[test]
fn refuses_a_table_inside_the_frameworks_namespace() {
for input in [
syn::parse_quote! {
#[ingraph(table = "__ig_scene_0__", sqlite)]
struct S { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(sqlite(table = "__ignore"))]
struct S { #[ingraph(id)] id: Uuid7 }
},
syn::parse_quote! {
#[ingraph(mongo(collection = "__ig_shots"))]
struct S { #[ingraph(id)] id: Uuid7 }
},
] {
let e = refused(input);
assert!(e.contains("framework's namespace"), "{e}");
}
}
#[test]
fn a_name_in_both_namespaces_is_refused_once() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] __ig_x__: u64 }
});
assert_eq!(e.matches("framework's namespace").count(), 1, "{e}");
assert!(e.contains("`__` at both ends"), "{e}");
}
#[test]
fn the_reserved_prefix_is_the_one_the_framework_mints() {
assert_eq!(
ingraph_codegen::alias::ALIAS_PREFIX,
ingraph::comparison::sql::ALIAS_PREFIX,
"the mint and the engine disagree about the reserved namespace",
);
assert!(
crate::alias::Fence::default()
.caught(ingraph_codegen::alias::ALIAS_PREFIX)
.is_some(),
"the mint writes `{}`, which the derive does not fence",
ingraph_codegen::alias::ALIAS_PREFIX,
);
}
#[test]
fn each_database_mints_in_its_own_namespace() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "__mine", depth = 16)), postgres)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
})
.expect("each database answers for itself");
let sqlite = &database(&m, DatabaseKind::Sqlite).storage().aliases;
assert_eq!(sqlite.prefix_in(&EmissionPrefix::default()), "__mine");
assert_eq!(sqlite.depth(), 16);
let postgres = &database(&m, DatabaseKind::Postgres).storage().aliases;
assert_eq!(postgres.prefix, None, "an absent prefix stays absent");
assert_eq!(
postgres.prefix_in(&EmissionPrefix::default()),
ingraph_codegen::alias::ALIAS_PREFIX
);
assert_eq!(postgres.depth(), ingraph_codegen::alias::DEPTH_CEILING);
}
#[test]
fn a_knob_written_alone_moves_only_itself() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite(alias(depth = 12)), postgres(alias(prefix = "__pg")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
})
.expect("both halves are optional");
let sqlite = &database(&m, DatabaseKind::Sqlite).storage().aliases;
assert_eq!(
(sqlite.prefix_in(&EmissionPrefix::default()), sqlite.depth()),
("__ig", 12)
);
let postgres = &database(&m, DatabaseKind::Postgres).storage().aliases;
assert_eq!(
(
postgres.prefix_in(&EmissionPrefix::default()),
postgres.depth()
),
("__pg", ingraph_codegen::alias::DEPTH_CEILING)
);
}
#[test]
fn the_fence_is_the_union_of_every_declared_prefix() {
let declared = refused(syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "__mine")), postgres)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] __mine_x: u64 }
});
assert!(declared.contains("names beginning `__mine`"), "{declared}");
let defaulted = refused(syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "__mine")), postgres)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] __ig_x: u64 }
});
assert!(defaulted.contains("names beginning `__ig`"), "{defaulted}");
}
#[test]
fn overriding_every_database_returns_the_default_namespace() {
parse(&syn::parse_quote! {
#[ingraph(
sqlite(alias(prefix = "__mine")),
postgres(alias(prefix = "__mine"))
)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] __ignore: u64 }
})
.expect("no database mints in `__ig`, so nothing reserves it");
let e = refused(syn::parse_quote! {
#[ingraph(
sqlite(alias(prefix = "__mine")),
postgres(alias(prefix = "__mine"))
)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] __mine_x: u64 }
});
assert!(e.contains("names beginning `__mine`"), "{e}");
}
#[test]
fn the_union_fence_reaches_a_declared_table() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "__mine"), table = "__mine_legacy"), postgres)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("table `__mine_legacy`"), "{e}");
assert!(e.contains("names beginning `__mine`"), "{e}");
}
#[test]
fn the_mints_rules_reach_the_declaration() {
for (declaration, expected) in [
(
syn::parse_quote! {
#[ingraph(sqlite(alias(depth = 0)))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"aliases nothing",
),
(
syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "mine")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"does not begin `__`",
),
(
syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "__my-name")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"which an alias cannot carry",
),
] {
let e = refused(declaration);
assert!(e.contains(expected), "{e}");
}
}
#[test]
fn an_alias_block_naming_no_knob_is_refused() {
for declaration in [
syn::parse_quote! {
#[ingraph(sqlite(alias()))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite(alias))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
] {
let e = refused(declaration);
assert!(e.contains("names neither knob"), "{e}");
assert!(e.contains("`prefix = \"…\"` and `depth = N`"), "{e}");
}
}
#[test]
fn refuses_a_repeated_alias_block_or_knob() {
for (declaration, expected) in [
(
syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "__a"), alias(prefix = "__b")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"`alias(…)` is declared more than once",
),
(
syn::parse_quote! {
#[ingraph(sqlite(alias(prefix = "__a", prefix = "__b")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"`prefix` is declared more than once",
),
(
syn::parse_quote! {
#[ingraph(sqlite(alias(depth = 9, depth = 10)))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"`depth` is declared more than once",
),
(
syn::parse_quote! {
#[ingraph(sqlite(alias(namespace = "__a")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"unknown entry in `alias(…)`",
),
] {
let e = refused(declaration);
assert!(e.contains(expected), "{e}");
}
}
#[test]
fn the_mint_fills_the_depth_ceiling() {
use ingraph_codegen::alias::{ALIAS_PREFIX, DEPTH_CEILING, aliases};
assert_eq!(DEPTH_CEILING, ingraph::query::sql::DEPTH_CEILING);
assert_eq!(
aliases(ALIAS_PREFIX, "scene", DEPTH_CEILING).len(),
ingraph::query::sql::DEPTH_CEILING,
);
}
#[test]
fn refuses_two_rows_resolving_to_one_name() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(row(rename = "Row")), postgres(row(rename = "Row")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("already the `sqlite` row"), "{e}");
assert!(
e.contains("the `postgres` row would be named `Row`"),
"it names both: {e}"
);
}
#[test]
fn refuses_a_row_named_after_a_type_the_record_already_emits() {
for (rename, what) in [
("S", "already the record's own name"),
("SKey", "already the key type"),
("SLineage", "already the lineage type"),
] {
let rename = syn::LitStr::new(rename, proc_macro2::Span::call_site());
let e = refused(syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), sqlite(row(rename = #rename)))]
struct S { #[ingraph(id)] id: Uuid7, media_id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains(what), "{e}");
}
}
#[test]
fn refuses_two_columns_of_one_database_resolving_to_one_name() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(rename = "same"))] a: u64,
#[ingraph(scalar, sqlite(rename = "same"))] b: u64,
}
});
assert!(e.contains("as one column called `same`"), "{e}");
assert!(e.contains("`a`") && e.contains("`b`"), "it names both: {e}");
}
#[test]
fn refuses_two_columns_whose_rust_faces_collide() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, rename = "bitRate")] a: u64,
#[ingraph(scalar, rename = "bit_rate")] b: u64,
}
});
assert!(e.contains("as one column"), "{e}");
}
#[test]
fn one_column_name_may_be_used_in_two_databases() {
parse(&syn::parse_quote! {
#[ingraph(sqlite, postgres)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(rename = "same"))] a: u64,
#[ingraph(scalar, postgres(rename = "same"))] b: u64,
}
})
.expect("two databases, one name each");
}
#[test]
fn refuses_required_and_nullable_as_options() {
for name in ["required", "nullable"] {
let name = syn::Ident::new(name, proc_macro2::Span::call_site());
for input in [
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, #name)] a: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, sqlite(#name))] a: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite(#name))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
] {
let e = refused(input);
assert!(e.contains("nullability follows the `optional`"), "{e}");
}
}
}
#[test]
fn refuses_a_container_conversion_beside_a_field_one() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(from = "whole_row"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(from = "one_column"))] a: u64,
}
});
assert!(e.contains("replaces the field-wise body"), "{e}");
}
#[test]
fn a_container_conversion_leaves_the_other_direction_alone() {
parse(&syn::parse_quote! {
#[ingraph(sqlite(from = "whole_row"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(into = "one_column"))] a: u64,
}
})
.expect("the read is the container's and the write is field-wise — neither is ambiguous");
}
#[test]
fn refuses_the_other_familys_word_for_a_table() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(collection = "x"))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("`sqlite` names it `table"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(mongo(table = "x"))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("`mongo` names it `collection"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(mongo(schema = "x"))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("`schema` is a SQL notion"), "{e}");
}
#[test]
fn refuses_an_option_in_the_wrong_half_of_the_declaration() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(indexed))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("describes one COLUMN"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, sqlite(table = "x"))] a: u64 }
});
assert!(e.contains("names where the whole ROW lives"), "{e}");
}
#[test]
fn independent_persist_violations_are_all_reported_in_one_pass() {
let e = parse(&syn::parse_quote! {
#[ingraph(sqlite(row(rename = "S")))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar,
unique,
mongo(indexed),
sqlite(from = "a", try_from = "b"),
)]
bit_rate: u64,
}
})
.unwrap_err();
let messages: Vec<String> = e.into_iter().map(|e| e.to_string()).collect();
assert_eq!(messages.len(), 4, "{messages:?}");
for expected in [
"is a PER-DATABASE option",
"already the record's own name",
"not one of the databases",
"there is no third answer",
] {
assert!(
messages.iter().any(|m| m.contains(expected)),
"{expected}: {messages:?}"
);
}
}
fn wire(model: &RecordModel) -> &GraphqlBackend {
&model
.graphql
.as_ref()
.unwrap_or_else(|| panic!("the record declares a `graphql(…)` block"))
.backends[0]
}
fn wire_field<'a>(model: &'a RecordModel, source: &str) -> &'a GraphqlField {
wire(model)
.fields()
.iter()
.find(|field| field.source == source)
.unwrap_or_else(|| panic!("`{source}` is published"))
}
fn exposed() -> RecordModel {
parse(&syn::parse_quote! {
#[ingraph(
table = "video_tracks", sqlite, postgres(schema = "public"),
graphql(async_graphql(attrs(complexity = 5))))]
pub struct VideoTrack {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(rename = "bitRate",
async_graphql(output(attrs(desc = "bits per second")))))]
bit_rate: u64,
#[ingraph(enum)] codec: VideoCodec,
cached_probe: OnceCell<Probe>,
}
})
.expect("the documented wire declaration parses")
}
#[test]
fn a_wire_field_carries_both_faces() {
let m = exposed();
let backend = wire(&m);
assert_eq!(backend.kind(), GraphqlKind::AsyncGraphql);
let graphql = m.graphql.as_ref().expect("declared");
assert_eq!(graphql.output.to_string(), "VideoTrackGraphqlOutput");
assert_eq!(graphql.key_output.to_string(), "VideoTrackKeyOutput");
assert_eq!(
graphql.filter.to_string(),
"VideoTrackFilter",
"ONE per record, not one per backend — a client must never see a schema that varies with a \
deployment's storage choice"
);
assert_eq!(m.order_by.to_string(), "VideoTrackOrderBy");
assert_eq!(
backend
.fields()
.iter()
.map(|field| (field.source.to_string(), field.output.name.to_string()))
.collect::<Vec<_>>(),
[
("id".to_owned(), "id".to_owned()),
("bit_rate".to_owned(), "bitRate".to_owned()),
("codec".to_owned(), "codec".to_owned()),
("cached_probe".to_owned(), "cached_probe".to_owned()),
],
"EVERY column the record holds, and only the declared rename: a declaration naming a backend \
publishes what it holds, and a field's word refines a place it already has"
);
assert_eq!(
graphql.output.safe().to_string(),
"VideoTrackGraphqlOutput",
"one `rename` sets both faces, so a DEFAULTED type's schema name is its Rust name"
);
}
#[test]
fn attrs_reach_the_model_uninterpreted() {
let m = exposed();
let backend = wire(&m);
assert_eq!(backend.attrs().to_string().replace(' ', ""), "complexity=5");
assert_eq!(
backend.fields()[1]
.output
.attrs
.to_string()
.replace(' ', ""),
"desc=\"bitspersecond\"",
"a description comes from here and from nowhere else"
);
assert!(backend.fields()[2].output.attrs.is_empty());
assert!(
backend.fields()[1].input.attrs.is_empty(),
"an `output(attrs(…))` says nothing about the INPUT position — they are two schema positions"
);
}
#[test]
fn a_field_has_one_wire_form_and_two_names() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar)] plain: u64,
#[ingraph(enum, graphql(
type = "String",
async_graphql(input(rename = "codecFilter")),
))]
codec: VideoCodec,
}
})
.expect("the wire form is the field's own");
let plain = wire_field(&m, "plain");
let codec = wire_field(&m, "codec");
assert_eq!(rendered_type(plain.wire_ty()), "u64");
assert!(plain.wire.is_none(), "nothing declared one");
assert_eq!(rendered_type(codec.wire_ty()), "String");
assert_eq!(
codec.input.name.to_string(),
"codecFilter",
"each position carries its own NAME — an input field and an output field are different schema \
positions and may differ there"
);
assert_eq!(codec.output.name.to_string(), "codec");
}
#[test]
fn a_position_refuses_the_entries_that_moved() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(type = "String"))))] a: u64,
}
});
assert!(e.contains("`type` moved to the field's own block"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(from = "f"))))] a: u64,
}
});
assert!(e.contains("gained a DATABASE"), "{e}");
}
#[test]
fn the_input_half_admits_only_the_comparison() {
let refuse = |position: proc_macro2::TokenStream| {
refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(input(#position)))] a: u64,
}
})
};
let e = refuse(quote::quote!(orderby(sqlite(into = "f"))));
assert!(
e.contains("an order carries a COLUMN and a DIRECTION"),
"{e}"
);
let e = refuse(quote::quote!(pagination(sqlite(into = "f"))));
assert!(e.contains("a cursor is an opaque round trip"), "{e}");
}
#[test]
fn a_fallible_leg_carries_its_escape_and_a_plain_one_cannot() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(
type = "i64",
from = "widen",
try_into(fn = "positive", error_with = "wrap"),
))]
a: u64,
}
})
.expect("the structured leg is a leg");
let column = stored(&m, DatabaseKind::Sqlite, "a");
assert!(
matches!(&column.into, Conversion::Fallible(leg)
if rendered(&leg.path) == "positive"
&& leg.escape.as_ref().is_some_and(|e| rendered(e) == "wrap")),
"{:?}",
column.into
);
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(type = "i64", from(fn = "widen", error_with = "wrap")))]
a: u64,
}
});
assert!(e.contains("an INFALLIBLE leg cannot fail"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, sqlite(type = "i64", from = "widen", try_into(error_with = "wrap")))]
a: u64,
}
});
assert!(e.contains("maps an error and names no function"), "{e}");
}
#[test]
fn the_error_cells_ride_their_own_crossings_block() {
let m = parse(&syn::parse_quote! {
#[ingraph(
sqlite(
try_from(error = "crate::StoreRefusal"),
try_into(error = "crate::RowRefusal"),
),
graphql(
output(sqlite(try_from(error = "crate::WireRefusal"))),
input(comparison(sqlite(try_into(error = "crate::OperandRefusal")))),
async_graphql,
// The one fixture in this file whose id is STORED, so it is the one that answers a PAGE —
// and a one-letter name has no plural the pluraliser can find, which the plural rule
// refuses rather than publishing a page under the single's own name. The word is written
// here so this test stays about its own subject.
plural = "esses",
)
)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar)] a: u64,
}
})
.expect("every cell is at its own crossing's path");
let cell = |crossing: ingraph_codegen::model::Crossing| {
m.errors
.iter()
.find(|error| error.crossing == crossing && error.kind == DatabaseKind::Sqlite)
.map(|error| rendered(&error.path))
};
use ingraph_codegen::model::Crossing;
assert_eq!(
cell(Crossing::PersistWrite).as_deref(),
Some("crate::StoreRefusal")
);
assert_eq!(
cell(Crossing::PersistRead).as_deref(),
Some("crate::RowRefusal")
);
assert_eq!(
cell(Crossing::Output).as_deref(),
Some("crate::WireRefusal")
);
assert_eq!(
cell(Crossing::Comparison).as_deref(),
Some("crate::OperandRefusal")
);
}
#[test]
fn a_published_field_is_stored_by_default() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
})
.expect("`graphql` alone no longer leaves a field unstored");
assert!(columns(&m, DatabaseKind::Sqlite).iter().any(|c| c == "a"));
assert!(wire(&m).fields().iter().any(|field| field.source == "a"));
}
#[test]
fn refuses_filterable_and_orderable() {
for name in ["filterable", "orderable"] {
let name = syn::Ident::new(name, proc_macro2::Span::call_site());
for input in [
syn::parse_quote! {
#[ingraph(sqlite, graphql(#name, async_graphql))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql(#name)))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
] {
let e = refused(input);
assert!(
e.contains("filterable and orderable BECAUSE it is stored"),
"{e}"
);
}
}
}
#[test]
fn refuses_an_undeclared_backend_block() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(mystery(output(attrs(a)))))] a: u64,
}
});
assert!(
e.contains("unknown entry in `graphql(…)` on a field"),
"{e}"
);
}
#[test]
fn refuses_a_description_option_and_names_the_passthrough() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(desc = "x"))))] a: u64,
}
});
assert!(e.contains("passed through verbatim"), "{e}");
assert!(e.contains("deliberately NOT published"), "{e}");
}
#[test]
fn the_wire_block_follows_the_storage_blocks_rules() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(rename = "Wire"))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(
e.contains("names the backends that render the record"),
"{e}"
);
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(name = "S", async_graphql))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("`name` moved into `output(…)`"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, graphql(rename = "a"))] a: u64 }
});
assert!(e.contains("declares no `graphql(…)` block"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(rename = "T", async_graphql))]
struct TGraphqlOutput { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("already the record's own name"), "{e}");
}
#[test]
fn two_schema_names_with_one_rust_spelling_are_two_fields() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(rename = "bitRate"))] a: u64,
#[ingraph(scalar, graphql(rename = "bit_rate"))] b: u64,
}
})
.expect("two distinct schema names are two schema fields");
assert_eq!(
(
wire_field(&m, "a").input.name.to_string(),
wire_field(&m, "b").input.name.to_string()
),
("bitRate".to_owned(), "bit_rate".to_owned()),
"both wire names survive — layer 3 carries them and nothing normalises one onto the other"
);
assert_eq!(
(
wire_field(&m, "a").source.to_string(),
wire_field(&m, "b").source.to_string()
),
("a".to_owned(), "b".to_owned()),
"…standing on two members, which are the record's own field names"
);
}
#[test]
fn refuses_two_wire_fields_sharing_one_input_name() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(input(rename = "bitRate"))))] a: u64,
#[ingraph(scalar, graphql(async_graphql(input(rename = "bitRate"))))] b: u64,
}
});
assert!(e.contains("one field called `bitRate`"), "{e}");
assert!(e.contains("in its input"), "it names the POSITION: {e}");
assert!(e.contains("`a`") && e.contains("`b`"), "it names both: {e}");
}
#[test]
fn the_output_names_a_schema_field_and_a_rust_accessor_separately() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(string, graphql(async_graphql(output(rename = "titleText", field = "headline"))))]
title: String,
#[ingraph(scalar, graphql(async_graphql(output(rename = "bitRate"))))] bit_rate: u64,
#[ingraph(scalar, graphql(async_graphql(output(field = "weight"))))] mass: u64,
}
})
.expect("both axes, together and apart");
let title = wire_field(&m, "title");
assert_eq!(
(title.output.name.to_string(), title.accessor().to_string()),
("titleText".to_owned(), "headline".to_owned()),
"both written: the client sees one name and the object is read through another, and neither is \
derived from the other"
);
let bit_rate = wire_field(&m, "bit_rate");
assert_eq!(
(
bit_rate.output.name.to_string(),
bit_rate.accessor().to_string()
),
("bitRate".to_owned(), "bit_rate".to_owned()),
"a SCHEMA rename moves nothing in Rust — the accessor is the record field's own name"
);
let mass = wire_field(&m, "mass");
assert_eq!(
(mass.output.name.to_string(), mass.accessor().to_string()),
("mass".to_owned(), "weight".to_owned()),
"and an ACCESSOR rename moves nothing a client sees"
);
assert_eq!(
title.input.name.to_string(),
"title",
"`output(…)` is one schema position — the input face is untouched by either word"
);
}
#[test]
fn the_common_rename_is_a_schema_name_and_reaches_no_member() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(rename = "bitRate"))] a: u64,
}
})
.expect("a field-level rename is a schema name");
let field = wire_field(&m, "a");
assert_eq!(field.output.name.to_string(), "bitRate");
assert_eq!(field.input.name.to_string(), "bitRate");
assert_eq!(field.accessor().to_string(), "a");
}
#[test]
fn refuses_two_fields_renamed_onto_one_output_accessor() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(field = "weight"))))] a: u64,
#[ingraph(scalar, graphql(async_graphql(output(field = "weight"))))] b: u64,
}
});
assert!(e.contains("through one accessor, `weight_ref`"), "{e}");
assert!(e.contains("`a`") && e.contains("`b`"), "it names both: {e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar)] weight: u64,
#[ingraph(scalar, graphql(async_graphql(output(field = "weight"))))] b: u64,
}
});
assert!(
e.contains("through one accessor, `weight_ref`"),
"a rename onto another field's DEFAULT accessor is the same collision: {e}"
);
}
#[test]
fn refuses_an_output_accessor_that_is_not_an_identifier() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(field = "bit-rate"))))] a: u64,
}
});
assert!(e.contains("as an identifier in a string literal"), "{e}");
}
#[test]
fn refuses_an_accessor_renamed_onto_the_objects_own_address() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(field = "key"))))] a: u64,
}
});
assert!(e.contains("would declare `key_ref` twice"), "{e}");
assert!(e.contains("the output object's own address member"), "{e}");
}
#[test]
fn the_member_word_is_refused_where_nothing_would_read_it() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(input(field = "weight"))))] a: u64,
}
});
assert!(e.contains("names the OUTPUT object's accessor"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(output(field = "weight")))] a: u64,
}
});
assert!(e.contains("this block is the row-to-wire CROSSING"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(field = "a", field = "b"))))] a: u64,
}
});
assert!(e.contains("one member, one name"), "{e}");
}
#[test]
fn the_output_position_collides_on_the_schema_name_alone() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(rename = "same"))))] a: u64,
#[ingraph(scalar, graphql(async_graphql(output(rename = "same"))))] b: u64,
}
});
assert!(e.contains("in its output"), "{e}");
assert!(e.contains("one field called `same`"), "{e}");
parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(output(rename = "bitRate"))))] a: u64,
#[ingraph(scalar, graphql(async_graphql(output(rename = "bit_rate"))))] b: u64,
}
})
.expect("two distinct schema names on two distinct record fields");
}
#[test]
fn refuses_two_wire_fields_sharing_one_schema_name() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(rename = "same"))] a: u64,
#[ingraph(scalar, graphql(rename = "same"))] b: u64,
}
});
assert!(e.contains("one field called `same`"), "{e}");
assert!(
!e.contains("two schema names with one Rust spelling"),
"sharing an SDL name and colliding only after normalisation are different mistakes, and this \
is the first: {e}"
);
}
#[test]
fn refuses_a_collision_in_the_input_position_alone() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, graphql(async_graphql(input(rename = "same"), output(rename = "a"))))] a: u64,
#[ingraph(scalar, graphql(async_graphql(input(rename = "same"), output(rename = "b"))))] b: u64,
}
});
assert!(e.contains("in its input"), "{e}");
}
#[test]
fn a_camel_case_wire_name_keeps_both_faces() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar, graphql(rename = "bitRate"))] a: u64 }
})
.expect("a camelCase schema name is ordinary");
let field = wire_field(&m, "a");
assert_eq!(
field.output.name.to_string(),
"bitRate",
"what a client sees"
);
assert_eq!(
field.output.name.safe().to_string(),
"bit_rate",
"and what the emitted struct is allowed to call it"
);
}
fn spelled_marker(model: &RecordModel, source: &str) -> Option<String> {
match declared_marker(model, source)? {
Marker::Framework(marker) => Some(marker.label.as_str().to_owned()),
Marker::Path(path) => Some(rendered(path)),
}
}
fn declared_marker<'a>(model: &'a RecordModel, source: &str) -> Option<&'a Marker> {
model
.fields
.iter()
.find(|field| field.column.source == source)
.unwrap_or_else(|| panic!("`{source}` is a field of the record"))
.marker
.as_ref()
}
#[test]
fn refuses_the_older_spelling_and_names_the_word_to_write() {
for (written, expected) in [
(quote::quote!(marker = duration), "write `duration`"),
(
quote::quote!(marker = set),
"write `set(<the element's own word>)`",
),
(
quote::quote!(marker = "their::RollMarker"),
"write `label = \"their::RollMarker\"`",
),
] {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph( graphql(#written))] a: Vec<String>,
}
});
assert!(e.contains("`marker` is not an option"), "{e}");
assert!(
e.contains(expected),
"{written} migrates to {expected}: {e}"
);
}
}
#[test]
fn the_older_spelling_carrying_an_unreadable_value_is_offered_the_words() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(string), graphql(marker = 3))] a: Vec<String>,
}
});
assert!(e.contains("`marker` is not an option"), "{e}");
for label in Label::ALL {
assert!(e.contains(&format!("`{}`", label.as_str())), "{e}");
}
assert!(e.contains("`label = \"a::marker::Path\"`"), "{e}");
}
#[test]
fn the_wire_fields_entry_list_names_what_the_block_takes() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph( graphql(markers = set))] a: Vec<String>,
}
});
assert!(
e.contains("unknown entry in `graphql(…)` on a field"),
"{e}"
);
assert!(e.contains("`rename = \"…\"`"), "{e}");
assert!(e.contains("async_graphql"), "{e}");
}
fn spelled_element(model: &RecordModel, source: &str) -> Option<String> {
match declared_marker(model, source)? {
Marker::Framework(marker) => marker.element.map(|label| label.as_str().to_owned()),
Marker::Path(_) => None,
}
}
#[test]
fn a_field_writes_its_own_family_word() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(duration)] length: SignedDuration,
#[ingraph(bytes)] checksum: Vec<u8>,
}
})
.expect("a field names its own family, bare and at the top level");
assert_eq!(spelled_marker(&m, "length").as_deref(), Some("duration"));
assert_eq!(spelled_marker(&m, "checksum").as_deref(), Some("bytes"));
}
#[test]
fn every_value_word_is_written_bare() {
for label in Label::ALL.into_iter().filter(|label| !label.collection()) {
let word = syn::Ident::new(label.as_str(), proc_macro2::Span::call_site());
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(#word)] a: Vec<String>,
}
})
.unwrap_or_else(|e| panic!("`{}` is a word this grammar names: {e}", label.as_str()));
assert_eq!(spelled_marker(&m, "a").as_deref(), Some(label.as_str()));
}
}
#[test]
fn the_keyword_word_is_written_bare_and_escaped() {
for spelled in [
syn::Ident::new("enum", proc_macro2::Span::call_site()),
syn::Ident::new_raw("enum", proc_macro2::Span::call_site()),
] {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(#spelled)] grade: Grade,
}
})
.unwrap_or_else(|e| panic!("`{spelled}` is a spelling of a word this grammar names: {e}"));
assert_eq!(spelled_marker(&m, "grade").as_deref(), Some("enum"));
}
}
#[test]
fn a_collection_label_names_its_element() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(string))] speakers: Vec<String>,
#[ingraph(set(scalar))] waveform: Vec<u8>,
#[ingraph(quantified(scalar))] scores: Vec<u64>,
}
})
.expect("a collection label names its element");
assert_eq!(spelled_marker(&m, "speakers").as_deref(), Some("list"));
assert_eq!(spelled_element(&m, "speakers").as_deref(), Some("string"));
assert_eq!(spelled_marker(&m, "waveform").as_deref(), Some("set"));
assert_eq!(spelled_element(&m, "waveform").as_deref(), Some("scalar"));
assert_eq!(spelled_marker(&m, "scores").as_deref(), Some("quantified"));
assert_eq!(spelled_element(&m, "scores").as_deref(), Some("scalar"));
}
#[test]
fn the_custom_escape_is_written_at_the_field() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(label = "their::RollMarker")] roll: Roll<Tag>,
}
})
.expect("a path in a string literal is the field's escape");
assert_eq!(
spelled_marker(&m, "roll").as_deref(),
Some("their::RollMarker"),
"carried verbatim — the emitter applies the field type to it"
);
assert_eq!(
spelled_element(&m, "roll"),
None,
"a path has no element word; its family is a fact about its trait impls"
);
}
#[test]
fn the_two_label_rules_the_macro_still_answers_for() {
let key_side = refusals(syn::parse_quote! {
#[ingraph(sqlite, lineage(fields(media_id)), graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id, scalar)] id: Uuid7,
#[ingraph(list(string))] media_id: Uuid7,
}
});
for field in [
"`id` is a key-side column",
"`media_id` is a key-side column",
] {
assert!(
names(&key_side, field),
"the id and every lineage step: {key_side:?}"
);
}
assert!(
names(&key_side, "a key's vocabulary is the sealed `Id` trait"),
"{key_side:?}"
);
let unorderable = refusals(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(sortable)] lines: Vec<String>,
}
});
assert!(
names(&unorderable, "`sortable` orders a COLLECTION"),
"the one cell a declaration alone can fill: {unorderable:?}"
);
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
lines: Vec<String>,
}
})
.expect("an unlabelled column is read through its type's own marker");
assert_eq!(spelled_marker(&m, "lines"), None);
}
#[test]
fn a_field_with_no_label_carries_no_marker() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(string)] title: String,
scratch: Vec<String>,
}
})
.expect("a field declaring nothing is a plain Rust field with a column");
assert_eq!(spelled_marker(&m, "scratch"), None);
assert!(
columns(&m, DatabaseKind::Sqlite)
.iter()
.any(|c| c == "scratch")
);
}
#[test]
fn a_key_side_column_carries_a_face_and_no_label() {
parse(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), sqlite, graphql(async_graphql))]
struct Clip {
#[ingraph(id)] id: Uuid7,
media_id: Uuid7,
#[ingraph(scalar)] bit_rate: u64,
}
})
.expect("a key column's vocabulary is the sealed `Id`, so it writes no word");
}
#[test]
fn refuses_a_collection_with_no_element_label() {
for word in ["list", "set", "quantified"] {
let spelled = syn::Ident::new(word, proc_macro2::Span::call_site());
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(#spelled)] a: Vec<String>,
}
});
assert!(e.contains(&format!("`{word}` names a COLLECTION")), "{e}");
assert!(e.contains("Every level of a label names its family"), "{e}");
}
}
#[test]
fn refuses_a_collection_of_collections() {
for nested in [
quote::quote!(list(list(scalar))),
quote::quote!(list(set)),
quote::quote!(set(list(string))),
] {
let all = refusals(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(#nested)] a: Vec<Vec<String>>,
}
});
assert!(
names(&all, "no backend carries a collection of collections"),
"{nested}: {all:?}"
);
assert_eq!(
all.len(),
1,
"one mistake, one sentence — the element's own complaint is not the one that answers: {all:?}"
);
}
}
#[test]
fn refuses_the_reserved_map_word() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(map(string, scalar))] a: BTreeMap<String, i64>,
}
});
assert!(e.contains("`map` is RESERVED with the map family"), "{e}");
}
#[test]
fn refuses_an_element_level_custom_marker() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(label = "their::RollMarker"))] a: Vec<Roll<Tag>>,
}
});
assert!(
e.contains("an element-level custom marker is RESERVED"),
"{e}"
);
assert!(e.contains("is the FIELD's escape"), "{e}");
}
#[test]
fn refuses_an_element_label_on_a_value_family() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(string(scalar))] a: String,
}
});
assert!(e.contains("`string` names a VALUE family"), "{e}");
}
#[test]
fn refuses_a_half_declared_collection_crossing() {
let neither = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(string), sqlite(type = "String"))] a: Vec<String>,
}
});
assert!(
neither.contains("`a` is a COLLECTION and `sqlite` stores its ELEMENTS"),
"{neither}"
);
assert!(
neither.contains("`from`/`try_from` and `into`/`try_into` are missing"),
"{neither}"
);
let half = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(set(string), sqlite(type = "String", from = "w"))] a: Vec<String>,
}
});
assert!(half.contains("`into`/`try_into` is missing"), "{half}");
}
#[test]
fn refuses_a_storage_strategy_where_the_dialect_has_none_to_choose() {
for (db, instead) in [
(
"sqlite",
"every collection it stores is already an edge table",
),
(
"mysql",
"every collection it stores is already an edge table",
),
("mongo", "a document holds its own array"),
] {
let spelled = syn::Ident::new(db, proc_macro2::Span::call_site());
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, mysql, mongo)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(string), #spelled(edge))] a: Vec<String>,
}
});
assert!(
e.contains(&format!(
"`edge` names a storage STRATEGY and `{db}` has none to choose"
)),
"{e}"
);
assert!(e.contains(instead), "{e}");
}
assert!(
parse(&syn::parse_quote! {
#[ingraph(postgres)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(string), postgres(edge, type = "String", from = "w", into = "r"))]
a: Vec<String>,
}
})
.is_ok(),
"and the one dialect with a choice takes the word"
);
}
#[test]
fn refuses_a_storage_strategy_on_a_column_that_is_not_a_collection() {
let e = refused(syn::parse_quote! {
#[ingraph(postgres)]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(string, postgres(edge))] a: String,
}
});
assert!(
e.contains("`a` is not a COLLECTION and `edge` names how one is STORED"),
"{e}"
);
}
#[test]
fn the_collection_crossing_rule_asks_for_a_declared_element_type() {
assert!(
parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(scalar))] a: Vec<u8>,
}
})
.is_ok(),
"a collection naming no stored type owes no crossing"
);
let scalar = parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(string, sqlite(type = "String"))] a: String,
}
});
assert!(
scalar.is_ok(),
"and a column no word called a collection is not asked either"
);
}
#[test]
fn refuses_a_collection_naming_two_elements() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(string, scalar))] a: Vec<String>,
}
});
assert!(e.contains("a collection's label names ONE element"), "{e}");
}
#[test]
fn refuses_a_family_word_carrying_a_value() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar = 3)] a: i64,
}
});
assert!(e.contains("a family label is a bare WORD"), "{e}");
}
#[test]
fn refuses_an_unknown_element_word() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(list(bag))] a: Vec<String>,
}
});
assert!(e.contains("`bag` is not an interpretation"), "{e}");
assert!(
e.contains("an element-level custom marker is reserved"),
"{e}"
);
assert!(
!e.contains("in a string literal"),
"the field's door is not the element's: {e}"
);
}
#[test]
fn refuses_a_repeated_label() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar, string)] a: i64,
}
});
assert!(
e.contains("a family label is declared more than once"),
"{e}"
);
}
#[test]
fn refuses_a_family_label_on_a_key_side_field() {
for declaration in [
quote::quote! {
#[ingraph(sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id, scalar)] id: Uuid7,
#[ingraph(scalar)] a: i64,
}
},
quote::quote! {
#[ingraph(lineage(fields(media_id)), sqlite, graphql(async_graphql))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar)] media_id: Uuid7,
#[ingraph(scalar)] a: i64,
}
},
] {
let all = refusals(syn::parse_quote!(#declaration));
assert!(
names(&all, "a key's vocabulary is the sealed `Id` trait"),
"{all:?}"
);
}
}
#[test]
fn every_emitted_type_is_renamable_through_its_own_block() {
let m = parse(&syn::parse_quote! {
#[ingraph(
sqlite(row(rename = "TrackRow"), comparison(rename = "TrackWhere")),
graphql(
output(rename = "TrackWire"),
key(rename = "TrackKeyWire"),
filter(rename = "TrackFilter"),
order_by(rename = "TrackSort"),
async_graphql,
))]
struct VideoTrack { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] bit_rate: u64 }
})
.expect("six blocks, six names");
let database = database(&m, DatabaseKind::Sqlite);
assert_eq!(database.name().to_string(), "TrackRow");
assert_eq!(database.comparison_name().to_string(), "TrackWhere");
let graphql = m.graphql.as_ref().expect("declared");
assert_eq!(graphql.output.to_string(), "TrackWire");
assert_eq!(graphql.key_output.to_string(), "TrackKeyWire");
assert_eq!(graphql.filter.to_string(), "TrackFilter");
assert_eq!(m.order_by.to_string(), "TrackSort");
assert_eq!(
graphql.output.safe().to_string(),
"TrackWire",
"one `rename` sets BOTH faces — a type name is Rust, so neither face is mangled"
);
}
#[test]
fn a_rename_overrides_only_its_own_type() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite(row(rename = "TrackRow")), graphql(filter(rename = "TrackWhere"), async_graphql))]
struct VideoTrack { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] bit_rate: u64 }
})
.expect("one block each");
let database = database(&m, DatabaseKind::Sqlite);
assert_eq!(database.name().to_string(), "TrackRow");
assert_eq!(
database.comparison_name().to_string(),
"TrackRowComparison",
"the comparison defaults FROM the row's name, so one rename moves both"
);
let graphql = m.graphql.as_ref().expect("declared");
assert_eq!(graphql.filter.to_string(), "TrackWhere");
assert_eq!(graphql.output.to_string(), "VideoTrackGraphqlOutput");
assert_eq!(graphql.key_output.to_string(), "VideoTrackKeyOutput");
assert_eq!(m.order_by.to_string(), "VideoTrackOrderBy");
}
#[test]
fn refuses_two_emitted_types_renamed_onto_one_name() {
for (input, expected) in [
(
syn::parse_quote! {
#[ingraph(sqlite(row(rename = "Row")), postgres(row(rename = "Row")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"already the `sqlite` row",
),
(
syn::parse_quote! {
#[ingraph(sqlite, postgres(comparison(rename = "SSqlite")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"already the `sqlite` row",
),
(
syn::parse_quote! {
#[ingraph(sqlite, graphql(filter(rename = "SGraphqlOutput"), async_graphql))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"already the wire output",
),
(
syn::parse_quote! {
#[ingraph(sqlite(row(rename = "SKey")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"already the key type",
),
(
syn::parse_quote! {
#[ingraph(sqlite(row(rename = "S")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
},
"already the record's own name",
),
] {
let e = refused(input);
assert!(e.contains(expected), "{expected}: {e}");
}
}
#[test]
fn refuses_a_repeated_or_absent_rename_in_a_type_block() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(row(rename = "A", rename = "B")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("declared more than once in `row(…)`"), "{e}");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(comparison))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("the only thing it is for"), "{e}");
}
#[test]
fn refuses_a_type_rename_that_is_not_an_identifier() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite(row(rename = "Track Row")))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(e.contains("must be a valid identifier"), "{e}");
}
#[test]
fn a_ledger_is_derived_from_the_storage() {
let m = parse(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), sqlite, postgres)]
pub struct Keyframe {
#[ingraph(id)] id: Uuid7,
media_id: Uuid7,
#[ingraph(scalar)] width: u64,
}
})
.expect("the documented declaration parses");
assert_eq!(
m.progress
.as_ref()
.expect("derived from the storage")
.hosts
.iter()
.map(ProgressHost::kind)
.collect::<Vec<_>>(),
[DatabaseKind::Sqlite, DatabaseKind::Postgres],
"one host per PERSIST database, in the order `persist(…)` wrote them"
);
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
})
.expect("a `persist`-only record");
assert_eq!(
m.progress
.expect("every stored record has a close to certify")
.hosts
.iter()
.map(ProgressHost::kind)
.collect::<Vec<_>>(),
[DatabaseKind::Sqlite]
);
let m = under(
&syn::parse_quote! { struct S { #[ingraph(id)] id: Uuid7 } },
NodeKind::Entity,
)
.expect("a pure-compute node");
assert!(
m.progress.is_none(),
"the control tables live in the `persist` pool, and this node has none"
);
let m = parse(&syn::parse_quote! {
#[ingraph(mongo)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
})
.expect("mongo stores rows, and no rule here refuses it");
assert!(m.progress.is_none());
}
#[test]
fn refuses_a_declaration_that_is_derived() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, progress(sqlite))]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(string)] a: SmolStr }
});
assert!(
e.contains("`progress(…)` is not a declaration — it is derived from the storage families")
&& e.contains("A ledger's control tables live in"),
"{e}"
);
}
#[test]
fn a_renamed_column_still_satisfies_the_storage_rule() {
parse(&syn::parse_quote! {
#[ingraph(sqlite, graphql(async_graphql, plural = "many"))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(string, rename = "caption")] description: SmolStr,
}
})
.expect("the column's NAME is not what the rule is about");
}
#[test]
fn refuses_full_text_at_either_scope() {
for declaration in ["fulltext", "fulltext(tantivy)", "fulltext(tantivy, sqlite)"] {
let block: proc_macro2::TokenStream = declaration.parse().expect("a family");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, #block)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(string)] a: SmolStr }
});
assert!(
e.contains("a record owns no full-text index — an index is its OWN node")
&& e.contains("`fulltext` macro, on a type whose `upstream(…)` names this record"),
"{declaration}: {e}"
);
assert!(
e.contains("One node holds ONE storage") && e.contains("carries no `id` of its own"),
"the refusal carries the ruling it follows from: {e}"
);
}
for marker in [
"fulltext",
r#"fulltext = "text::render""#,
r#"fulltext(tantivy(from = "text::render"))"#,
r#"fulltext(tantivy(each), sqlite(join(sep = ", ")))"#,
] {
let marker: proc_macro2::TokenStream = marker.parse().expect("a marker");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(string, #marker)] a: SmolStr }
});
assert!(
e.contains("`fulltext` marks a field for an index that is its OWN node")
&& e.contains("per-engine conversion its block carried"),
"{e}"
);
}
}
#[test]
fn refuses_the_artifact_store_at_either_scope() {
for declaration in ["blob", "blob(fs)", r#"blob(fs, extension = "jpg")"#] {
let block: proc_macro2::TokenStream = declaration.parse().expect("a family");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, #block)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(
e.contains("a record owns no artifact store — a blob family is its OWN node")
&& e.contains("`blob` macro, on a type whose `upstream(…)` names this record"),
"{declaration}: {e}"
);
assert!(
e.contains("`extension` is part of the ADDRESS that node's key renders"),
"the address option travels with the stores: {e}"
);
assert!(
e.contains("A column of BYTES is a different thing and stays"),
"the fence around the column family: {e}"
);
}
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(bytes, blob)] b: Vec<u8> }
});
assert!(
e.contains("`blob` marks a field as an artifact's payload")
&& e.contains("A `Vec<u8>` COLUMN is a different thing and stays"),
"{e}"
);
}
#[test]
fn a_bytes_column_is_not_the_artifact_family() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(bytes)] thumbnail: Vec<u8> }
})
.expect("a byte column is a column");
assert_eq!(
stored(&m, DatabaseKind::Sqlite, "thumbnail")
.name
.external(),
"thumbnail",
"the bytes are a column of the row, exactly as they were"
);
}
#[test]
fn refuses_the_embedding_index_at_either_scope() {
for declaration in [
"vector",
"vector(lance)",
r#"vector(lance, element = "element::F16", filter = "KeyframeScope")"#,
] {
let block: proc_macro2::TokenStream = declaration.parse().expect("a family");
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, #block)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(scalar)] a: u64 }
});
assert!(
e.contains("a record owns no embedding index — an index is its OWN node")
&& e.contains("`vector` macro, on a type whose `upstream(…)` names this record"),
"{declaration}: {e}"
);
assert!(
e.contains("`element` and `filter` describe the EMBEDDINGS rather than these rows"),
"the two record types travel with the engines: {e}"
);
}
let e = refused(syn::parse_quote! {
#[ingraph(sqlite)]
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(string, vector)] e: Arc<[f32]> }
});
assert!(
e.contains("`vector` marks a field as an embedding, and an embedding index is its OWN node")
&& e.contains("One node holds ONE storage"),
"{e}"
);
}
#[test]
fn every_misplaced_storage_declaration_is_reported_at_once() {
let e = refusals(syn::parse_quote! {
#[ingraph(
sqlite,
fulltext(tantivy),
vector(lance),
blob(fs, extension = "png"),
progress(sqlite))]
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(scalar)] a: u64,
}
});
for rule in [
"a record owns no full-text index",
"a record owns no embedding index",
"a record owns no artifact store",
"`progress(…)` is not a declaration",
] {
assert!(names(&e, rule), "missing `{rule}` in: {e:?}");
}
}
#[test]
fn a_node_names_its_upstream_and_the_path_reaches_the_model_verbatim() {
let m = operator(&syn::parse_quote! {
#[ingraph(subscribe = "asr::TranscriptSegment")]
struct Attribution { #[ingraph(id)] id: Uuid7, speaker: String }
})
.expect("a subscriber");
assert_eq!(
rendered(m.subscribe.as_ref().expect("the upstream")),
"asr::TranscriptSegment"
);
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct Media { #[ingraph(id)] id: Uuid7 }
})
.expect("a source node");
assert!(
m.subscribe.is_none(),
"and a node driven by nothing says so by omission"
);
}
#[test]
fn a_node_may_opt_its_value_into_the_shutdown_s_goodbye() {
let model = operator(&syn::parse_quote! {
#[ingraph(teardown)]
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.expect("a bare word");
assert!(model.teardown);
}
#[test]
fn a_node_that_says_nothing_is_torn_down_by_drop() {
let model = operator(&syn::parse_quote! {
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.expect("nothing to refuse");
assert!(!model.teardown);
}
#[test]
fn the_goodbye_word_takes_neither_a_value_nor_a_block() {
for spelling in [
quote::quote!(teardown = "flush"),
quote::quote!(teardown(flush)),
] {
let e = operator(&syn::parse_quote! {
#[ingraph(#spelling)]
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(
e.contains("`teardown` is a bare word")
&& e.contains("the goodbye itself is the `AsyncTeardown` impl"),
"{e}"
);
}
}
#[test]
fn the_goodbye_word_is_refused_twice_over() {
let e = operator(&syn::parse_quote! {
#[ingraph(teardown, teardown)]
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(e.contains("`teardown` is declared more than once"), "{e}");
}
#[test]
fn a_node_may_opt_its_outputs_into_the_root_projection() {
let model = operator(&syn::parse_quote! {
#[ingraph(attributed)]
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.expect("a bare word");
assert!(model.attributed);
}
#[test]
fn a_node_that_says_nothing_attributes_nothing() {
let model = operator(&syn::parse_quote! {
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.expect("nothing to refuse");
assert!(!model.attributed);
}
#[test]
fn the_attribution_word_takes_neither_a_value_nor_a_block() {
for spelling in [
quote::quote!(attributed = "media"),
quote::quote!(attributed(media)),
] {
let e = operator(&syn::parse_quote! {
#[ingraph(#spelling)]
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(
e.contains("`attributed` is a bare word")
&& e.contains("the projection is the `Attributes` impl"),
"{e}"
);
}
}
#[test]
fn the_attribution_word_is_refused_twice_over() {
let e = operator(&syn::parse_quote! {
#[ingraph(attributed, attributed)]
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(e.contains("`attributed` is declared more than once"), "{e}");
}
#[test]
fn the_attribution_word_composes_with_the_body_words() {
let model = operator(&syn::parse_quote! {
#[ingraph(stream, async, attributed, teardown)]
struct Decode { #[ingraph(id)] id: Uuid7 }
})
.expect("four words on four axes");
assert!(model.attributed && model.teardown);
assert!(model.streaming.is_some() && model.asynchronous.is_some());
}
#[test]
fn a_storage_declaration_may_attribute_as_well() {
let model = under(
&syn::parse_quote! {
#[ingraph(sqlite, attributed)]
struct Frames { #[ingraph(id)] id: Uuid7 }
},
NodeKind::Record,
)
.expect("a bare word");
assert!(model.attributed);
}
#[test]
fn a_storage_declaration_may_opt_in_as_well() {
let model = under(
&syn::parse_quote! {
#[ingraph(sqlite, teardown)]
struct Frames { #[ingraph(id)] id: Uuid7 }
},
NodeKind::Record,
)
.expect("a bare word");
assert!(model.teardown);
}
#[test]
fn a_fusion_source_names_an_upstream_and_projects_nothing() {
let e = operator(&syn::parse_quote! {
#[ingraph(upstream(node = "a::A", key, speaker = "name"), lineage(fields(media_id)))]
struct Attribution { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(
e.contains("`upstream(…)` on an operator names ONE upstream of a fusion")
&& e.contains("It projects nothing")
&& e.contains("`fulltext`, `vector` or `blob` macro"),
"{e}"
);
}
#[test]
fn a_declaration_that_designates_none_fans_in_instead() {
let model = operator(&syn::parse_quote! {
#[ingraph(upstream(node = "media::Frame"), upstream(node = "asr::Segment"))]
struct Caption { #[ingraph(id)] id: Uuid7, text: String }
})
.expect("no designation is the other mode, not an error");
let fanned = model
.upstreams
.as_ref()
.and_then(Upstreams::fanned)
.expect("the mode reaches the model");
assert_eq!(
fanned
.kinds
.iter()
.map(|kind| kind.variant.to_string())
.collect::<Vec<_>>(),
["Frame", "Segment"],
"the variant's word is read off the path's last segment, never declared"
);
let lineage = model.lineage.as_ref().expect("a fan-in has an ancestry");
assert!(
matches!(lineage.shape, Shape::Kinds(_)) && lineage.path().is_empty(),
"and it is a choice rather than a path"
);
assert_eq!(
model.key.path.len(),
1,
"the key leads with that choice and nothing else"
);
assert_eq!(model.key.path[0].name, fanned_lineage_column());
assert_eq!(
model.key.path[0].source,
fanned_lineage(&EmissionPrefix::default())
);
}
#[test]
fn two_upstreams_cannot_share_a_variant_word() {
let messages = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "asr::Segment"), upstream(node = "vad::Segment"))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "both end in `Segment`") && names(&messages, "`asr::Segment`"),
"{messages:?}"
);
}
#[test]
fn a_fan_in_has_no_path_to_declare() {
let messages = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A"), upstream(node = "b::B"), lineage(fields(media_id)))]
struct Caption { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
});
assert!(
names(&messages, "its ancestry decided per ROW")
&& names(&messages, "Designate one upstream with `key`"),
"{messages:?}"
);
}
#[test]
fn a_fan_in_that_persists_waits_on_the_read_and_says_which_one() {
let messages = refusals(syn::parse_quote! {
#[ingraph(sqlite, upstream(node = "a::A"), upstream(node = "b::B"))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(
&messages,
"the READ that raises a common row back into a record"
) && names(&messages, "a choice has no empty state to decode into")
&& names(&messages, "Designate one upstream with `key`"),
"{messages:?}"
);
}
#[test]
fn a_fan_in_that_stores_nothing_is_not_waiting_on_anything() {
operator(&syn::parse_quote! {
#[ingraph(upstream(node = "a::A"), upstream(node = "b::B"))]
struct Caption { #[ingraph(id)] id: Uuid7 }
})
.expect("a node with no rows has no layout to wait for");
}
#[test]
fn two_designated_parents_are_refused_naming_the_first() {
let messages = refusals(syn::parse_quote! {
#[ingraph(
upstream(node = "a::A", key),
upstream(node = "b::B", key),
lineage(fields(media_id))
)]
struct Caption { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
});
assert!(
names(&messages, "two upstreams are designated `key`") && names(&messages, "`a::A` already"),
"{messages:?}"
);
}
#[test]
fn the_designation_is_written_bare() {
let messages = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", key = "media"), lineage(fields(media_id)))]
struct Caption { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
});
assert!(names(&messages, "`key` is a DESIGNATION"), "{messages:?}");
}
#[test]
fn a_node_does_not_name_its_upstreams_twice() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe = "a::A", upstream(node = "a::A", key), lineage(fields(media_id)))]
struct Caption { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
});
assert!(
names(&messages, "names its upstreams twice"),
"{messages:?}"
);
}
#[test]
fn a_fusion_declares_the_lineage_it_mirrors() {
let messages = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", key))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "has no lineage to check against"),
"{messages:?}"
);
}
#[test]
fn one_upstream_may_still_designate_its_key_parent() {
let model = operator(&syn::parse_quote! {
#[ingraph(upstream(node = "media::Frame", key), lineage(fields(media_id)))]
struct Caption { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
})
.expect("one designated upstream is a fusion");
let fusion = model
.upstreams
.as_ref()
.and_then(Upstreams::fused)
.expect("the designation reaches the model");
assert_eq!(fusion.upstreams().count(), 1);
assert!(fusion.others.is_empty());
assert_eq!(
fusion
.mirrors
.iter()
.map(|mirror| (mirror.column.to_string(), mirror.from.to_string()))
.collect::<Vec<_>>(),
[("media_id".to_owned(), "media_id".to_owned())],
"a same-name mirror costs no words"
);
}
#[test]
fn a_diverging_mirror_names_the_parent_field_it_equals() {
let model = operator(&syn::parse_quote! {
#[ingraph(
upstream(node = "media::Frame", key),
upstream(node = "asr::Segment"),
lineage(fields(media, frame(source = "id")))
)]
struct Caption {
#[ingraph(id)] id: Uuid7,
#[ingraph(lineage(rename = "media_id"))] media: Uuid7,
frame: Uuid7,
}
})
.expect("a fusion over two upstreams");
let fusion = model
.upstreams
.as_ref()
.and_then(Upstreams::fused)
.expect("the designation reaches the model");
assert_eq!(
fusion
.others
.iter()
.map(|path| quote::ToTokens::to_token_stream(path).to_string())
.collect::<Vec<_>>(),
["asr :: Segment"],
"the upstream that is not the parent is still an upstream"
);
assert_eq!(
fusion
.mirrors
.iter()
.map(|mirror| (mirror.column.to_string(), mirror.from.to_string()))
.collect::<Vec<_>>(),
[
("media_id".to_owned(), "media".to_owned()),
("frame".to_owned(), "id".to_owned()),
],
"the column is the RENAMED one, and the mapping is onto the parent's own field"
);
}
#[test]
fn a_mirror_without_a_designated_parent_is_refused_by_name() {
let messages = refusals(syn::parse_quote! {
#[ingraph(lineage(fields(media(source = "id"))))]
struct Caption { #[ingraph(id)] id: Uuid7, media: Uuid7 }
});
assert!(
names(&messages, "has no designated parent to map onto")
&& names(&messages, "its lineage is its own columns"),
"{messages:?}"
);
}
#[test]
fn the_mapping_word_is_written_inside_the_field() {
let messages = refusals(syn::parse_quote! {
#[ingraph(upstream(node = "a::A", key), lineage(fields(media = "id")))]
struct Caption { #[ingraph(id)] id: Uuid7, media: Uuid7 }
});
assert!(
names(
&messages,
"the parent key's field it mirrors is written inside it"
),
"{messages:?}"
);
}
#[test]
fn the_edge_is_one_path_declared_once() {
let e = operator(&syn::parse_quote! {
#[ingraph(subscribe = "a::A", subscribe = "b::B")]
struct Attribution { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(e.contains("`subscribe` is declared more than once"), "{e}");
let e = operator(&syn::parse_quote! {
#[ingraph(subscribe = "not a path")]
struct Attribution { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(e.contains("names the upstream node, as a type path"), "{e}");
}
#[test]
fn an_operator_declares_its_key_exactly_as_a_record_does() {
let m = operator(&syn::parse_quote! {
#[ingraph(lineage(fields(media_id)), key(rename = "TurnAddress"))]
struct SpeakerTurn {
#[ingraph(id)] id: Uuid7,
#[ingraph(copy)] media_id: Uuid7,
speaker: String,
}
})
.expect("an operator carries a key");
assert_eq!(
m.kind,
NodeKind::Operator,
"the derive it was written under"
);
assert_eq!(m.key.name, "TurnAddress");
assert_eq!(lineage_names(&m), ["media_id"]);
assert!(m.persist.is_none(), "and it stores nothing");
}
#[test]
fn refuses_rows_on_a_node_that_stores_none() {
let e = operator(&syn::parse_quote! {
#[ingraph(sqlite)]
struct SpeakerTurn { #[ingraph(id)] id: Uuid7, #[ingraph(string)] speaker: String }
})
.unwrap_err()
.to_string();
assert!(
e.contains("an operator stores no rows")
&& e.contains("a node whose messages land in a database is a `record`"),
"{e}"
);
}
#[test]
fn a_row_word_on_a_node_that_stores_none_is_refused_in_the_same_words() {
let e: Vec<String> = operator(&syn::parse_quote! {
struct SpeakerTurn {
#[ingraph(id)] id: Uuid7,
#[ingraph(string, rename = "who")] speaker: String,
#[ingraph(skip)] cache: Vec<u8>,
}
})
.unwrap_err()
.into_iter()
.map(|e| e.to_string())
.collect();
assert!(
names(
&e,
"`speaker` refines a stored column and an operator has no rows"
),
"{e:?}"
);
assert!(
names(
&e,
"`cache` declares that no row holds it and an operator has no rows"
),
"{e:?}"
);
assert!(
!names(&e, "declares no `persist(…)` block"),
"and not the missing-block sentence too: {e:?}"
);
}
#[test]
fn refuses_a_wire_face_on_the_node_family_s_base() {
let e = operator(&syn::parse_quote! {
#[ingraph(graphql(async_graphql))]
struct SpeakerTurn { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(
e.contains("an operator publishes nothing")
&& e.contains("the rowless tier that publishes its address is an `entity`"),
"{e}"
);
under(
&syn::parse_quote! {
#[ingraph(graphql(async_graphql, plural = "turns"))]
struct SpeakerTurn { #[ingraph(id)] id: Uuid7 }
},
NodeKind::Entity,
)
.expect("and one rung up the same block is the tier's whole point");
}
#[test]
fn the_storage_refusals_name_the_node_they_were_written_on() {
let e = operator(&syn::parse_quote! {
#[ingraph(fulltext(tantivy))]
struct SpeakerTurn { #[ingraph(id)] id: Uuid7 }
})
.unwrap_err()
.to_string();
assert!(
e.contains("an operator owns no full-text index")
&& e.contains("on a type whose `upstream(…)` names this operator")
&& e.contains("an operator's is none at all"),
"{e}"
);
}
#[test]
fn an_operator_mints_its_own_id() {
let e = operator(&syn::parse_quote! { struct SpeakerTurn { speaker: String } })
.unwrap_err()
.to_string();
assert!(
e.contains("an operator needs exactly one `#[ingraph(id)]` field"),
"{e}"
);
}
#[test]
fn the_shape_refusals_speak_the_derive_that_was_written() {
let e = operator(&syn::parse_quote! { enum SpeakerTurn { A } })
.unwrap_err()
.to_string();
assert!(
e.contains("`#[ingraph::operator]` supports structs only"),
"{e}"
);
let e = operator(&syn::parse_quote! { struct SpeakerTurn<T> { #[ingraph(id)] id: T } })
.unwrap_err()
.to_string();
assert!(e.contains("generic operators are not supported"), "{e}");
}
#[test]
fn a_ref_reaches_the_model_as_the_path_it_names() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct VideoTrack {
#[ingraph(id)] id: Uuid7,
#[ingraph(ref = "media::Media")] media: MediaId,
#[ingraph(ref(type = "crate::media::Video"))] video: VideoId,
bit_rate: u64,
}
})
.expect("both forms of the forward word");
let target = |name: &str| {
rendered(
&field(&m, name)
.reference
.as_ref()
.unwrap_or_else(|| panic!("`{name}` declares a `ref`"))
.target,
)
};
assert_eq!(target("media"), "media::Media", "the shorthand");
assert_eq!(target("video"), "crate::media::Video", "the full block");
assert!(
field(&m, "bit_rate").reference.is_none(),
"and a field writing neither refers to nothing"
);
}
#[test]
fn the_declarations_word_says_where_the_referring_value_sits() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite)]
struct Video {
#[ingraph(id)] id: Uuid7,
#[ingraph(ref = "Media")] media: MediaId,
#[ingraph(optional, ref = "Keyframe")] cover: MaybeCover,
#[ingraph(repeated, ref = "Scene")] scenes: Scenes,
#[ingraph(list(scalar), ref = "Take")] takes: Takes,
}
})
.expect("the three container words beside a bare one");
let through = |name: &str| field(&m, name).reference.as_ref().expect("a `ref`").through;
assert_eq!(through("media"), Through::Value);
assert_eq!(through("cover"), Through::Held, "`optional`");
assert_eq!(through("scenes"), Through::Held, "`repeated`");
assert_eq!(through("takes"), Through::Held, "a collection label");
}
#[test]
fn refuses_a_ref_that_names_no_record() {
for written in [
quote::quote!(ref = ""),
quote::quote!(ref),
quote::quote!(ref()),
] {
let e = refused(syn::parse_quote! {
struct S { #[ingraph(id)] id: Uuid7, #[ingraph(#written)] media: MediaId }
});
assert!(
e.contains("`ref` names the record a field refers to")
&& e.contains("ref(type = \"path::to::Type\")"),
"{e}"
);
}
}
#[test]
fn refuses_a_repeated_ref() {
let e = refused(syn::parse_quote! {
struct S {
#[ingraph(id)] id: Uuid7,
#[ingraph(ref = "Media", ref = "Video")] media: MediaId,
}
});
assert!(
e.contains("`ref` is declared more than once on this field"),
"{e}"
);
}
#[test]
fn a_derived_block_reaches_the_model_as_its_positions() {
let m = parse(&syn::parse_quote! {
#[ingraph(sqlite, derived(
tracks = "media::VideoTrack",
covers(type = "media::Cover", field = "video_id"),
))]
struct Video { #[ingraph(id)] id: Uuid7 }
})
.expect("both forms of the reverse block");
let named: Vec<String> = m
.derived
.iter()
.map(|entry| entry.name.to_string())
.collect();
assert_eq!(
named,
["tracks", "covers"],
"in the order they were written"
);
assert_eq!(rendered(&m.derived[0].source), "media::VideoTrack");
assert!(m.derived[0].field.is_none(), "the shorthand names no field");
assert_eq!(
m.derived[1]
.field
.as_ref()
.map(syn::LitStr::value)
.as_deref(),
Some("video_id"),
"and the full form does"
);
}
#[test]
fn refuses_a_derived_block_naming_no_position() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, derived())]
struct Video { #[ingraph(id)] id: Uuid7 }
});
assert!(
e.contains("`derived(…)` names the records that point back at this one, and it names none"),
"{e}"
);
}
#[test]
fn refuses_a_derived_position_that_takes_a_fields_name() {
let e = refused(syn::parse_quote! {
#[ingraph(sqlite, derived(tracks = "VideoTrack"))]
struct Video { #[ingraph(id)] id: Uuid7, tracks: u32 }
});
assert!(
e.contains("`tracks` is already a field of this record")
&& e.contains("One namespace per face"),
"{e}"
);
}
#[test]
fn refuses_a_reverse_position_this_grammar_cannot_read() {
let e = refusals(syn::parse_quote! {
#[ingraph(derived(tracks = "VideoTrack", tracks = "Clip", covers(field = "x"), takes(rename = "y")))]
struct Video { #[ingraph(id)] id: Uuid7 }
});
for rule in [
"`tracks` is declared more than once in `derived(…)`",
"a reverse position is `<name> = \"path::to::Source\"`",
"unknown entry in a reverse position",
] {
assert!(names(&e, rule), "missing `{rule}` in: {e:?}");
}
}
#[test]
fn the_subscribe_container_reads_a_mixed_fan_in() {
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(
upstream(node = "media::Frame", key),
upstream(node = "asr::Segment", key),
upstream(node = "media::Scene"),
upstream(node = "vision::Detector", out = "Boxes"),
))]
struct Caption { #[ingraph(id)] id: Uuid7 }
})
.expect("a mixed container is legal");
let fan_in = model
.subscriptions
.as_ref()
.expect("the container was read");
assert!(fan_in.is_sum(), "four upstreams are a sum");
assert_eq!(
fan_in
.upstreams
.iter()
.map(|source| source.variant.to_string())
.collect::<Vec<_>>(),
["Frame", "Segment", "Scene", "Boxes"],
"the variant is the upstream's last segment, or the product's own word where one was named"
);
assert_eq!(
fan_in
.keyed()
.map(|source| rendered(&source.node))
.collect::<Vec<_>>(),
["media::Frame", "asr::Segment"],
"`key` joins THE group, in declaration order"
);
assert_eq!(
fan_in
.straight()
.map(|source| source.variant.to_string())
.collect::<Vec<_>>(),
["Scene", "Boxes"],
);
assert_eq!(
fan_in.upstreams[3]
.out
.as_ref()
.map(ToString::to_string)
.as_deref(),
Some("Boxes"),
);
}
#[test]
fn one_straight_source_is_not_a_sum() {
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "media::Frame")))]
struct Caption { #[ingraph(id)] id: Uuid7 }
})
.expect("a one-entry container is legal");
assert!(
!model
.subscriptions
.as_ref()
.expect("the container was read")
.is_sum(),
"one upstream arriving on its own needs no enum around it"
);
}
#[test]
fn a_keyed_group_is_a_sum_at_any_size() {
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "media::Frame", key)))]
struct Caption { #[ingraph(id)] id: Uuid7 }
})
.expect("a keyed entry is legal");
assert!(
model
.subscriptions
.as_ref()
.expect("the container was read")
.is_sum()
);
}
#[test]
fn a_node_declares_its_fan_in_once() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe = "a::A", subscribe(upstream(node = "b::B")))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`subscribe` is declared more than once"),
"{messages:?}"
);
}
#[test]
fn the_container_does_not_stand_beside_the_identity_entries() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "a::A")), upstream(node = "a::A", key), lineage(fields(media_id)))]
struct Caption { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
});
assert!(
names(&messages, "names its upstreams twice"),
"{messages:?}"
);
}
#[test]
fn two_edges_cannot_share_a_variant_name() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "asr::Segment"), upstream(node = "dia::Segment")))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "both come out as `Segment`") && names(&messages, "`name = \"…\"` word"),
"{messages:?}"
);
}
#[test]
fn two_products_of_one_upstream_are_named_by_their_products() {
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(
upstream(node = "vision::Detector", out = "Boxes"),
upstream(node = "vision::Detector", out = "Masks"),
))]
struct Caption { #[ingraph(id)] id: Uuid7 }
})
.expect("two products of one upstream are two edges");
assert_eq!(
model
.subscriptions
.expect("the container was read")
.upstreams
.iter()
.map(|source| source.variant.to_string())
.collect::<Vec<_>>(),
["Boxes", "Masks"],
);
}
#[test]
fn the_container_entry_takes_three_words_and_refuses_the_rest() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe(
upstream(key = "media"),
upstream(node = "a::A", listen(channel)),
lineage(fields(media_id)),
))]
struct Caption { #[ingraph(id)] id: Uuid7, media_id: Uuid7 }
});
for rule in [
"`key` is written bare",
"`upstream(…)` needs `node = \"path::To::Upstream\"`",
"unknown entry in `upstream(…)`",
"unknown entry in `subscribe(…)`",
] {
assert!(names(&messages, rule), "missing `{rule}` in: {messages:?}");
}
}
#[test]
fn an_empty_container_is_refused_by_name() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe())]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`subscribe(…)` holds at least one"),
"{messages:?}"
);
}
#[test]
fn emits_declares_the_products_in_order() {
let model = operator(&syn::parse_quote! {
#[ingraph(emits = ["Caption", "Summary"])]
struct Describe { #[ingraph(id)] id: Uuid7 }
})
.expect("a product list is legal");
let products = model.products.as_ref().expect("the list was read");
assert!(products.is_sum());
assert_eq!(
products
.products
.iter()
.map(|product| product.variant.to_string())
.collect::<Vec<_>>(),
["Caption", "Summary"],
);
}
#[test]
fn one_declared_product_is_not_a_sum() {
let model = operator(&syn::parse_quote! {
#[ingraph(emits = "Caption")]
struct Describe { #[ingraph(id)] id: Uuid7 }
})
.expect("naming the one product is legal");
assert!(!model.products.expect("the list was read").is_sum());
}
#[test]
fn the_product_list_refuses_what_it_cannot_name() {
let messages = refusals(syn::parse_quote! {
#[ingraph(emits = ["Caption", "media::Caption", "1 + 1"])]
struct Describe { #[ingraph(id)] id: Uuid7 }
});
for rule in ["both end in `Caption`", "so each value is a type path"] {
assert!(names(&messages, rule), "missing `{rule}` in: {messages:?}");
}
let messages = refusals(syn::parse_quote! {
#[ingraph(emits = [])]
struct Describe { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "`emits` names at least one product"),
"{messages:?}"
);
}
#[test]
fn a_straight_source_cannot_take_the_groups_word() {
let messages = refusals(syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "a::A", key), upstream(node = "b::Paired")))]
struct Caption { #[ingraph(id)] id: Uuid7 }
});
assert!(
names(&messages, "is the variant that group takes"),
"{messages:?}"
);
let model = operator(&syn::parse_quote! {
#[ingraph(subscribe(upstream(node = "a::A"), upstream(node = "b::Paired")))]
struct Caption { #[ingraph(id)] id: Uuid7 }
})
.expect("with no group, the word is nobody's");
assert_eq!(
model
.subscriptions
.expect("the container was read")
.upstreams
.iter()
.map(|source| source.variant.to_string())
.collect::<Vec<_>>(),
["A", PAIRED],
);
}