use super::NodeKind;
const ALL: [NodeKind; 7] = [
NodeKind::Operator,
NodeKind::Entity,
NodeKind::Record,
NodeKind::Aggregate,
NodeKind::Fulltext,
NodeKind::Vector,
NodeKind::Blob,
];
const ROW_HOLDING: [NodeKind; 2] = [NodeKind::Record, NodeKind::Aggregate];
const DERIVED: [NodeKind; 3] = [NodeKind::Fulltext, NodeKind::Vector, NodeKind::Blob];
struct Words {
macro_word: &'static str,
noun: &'static str,
plural: &'static str,
article: &'static str,
}
struct Answers {
storage_block: Option<&'static str>,
holds_rows: bool,
election: Option<&'static str>,
derives_its_key: bool,
dressable: bool,
stores: bool,
}
#[test]
fn the_roster_holds_every_kind() {
const fn rostered(kind: NodeKind) -> bool {
match kind {
NodeKind::Operator
| NodeKind::Entity
| NodeKind::Record
| NodeKind::Aggregate
| NodeKind::Fulltext
| NodeKind::Vector
| NodeKind::Blob => true,
}
}
for kind in ALL {
assert!(rostered(kind), "{kind:?} is missing from the roster");
}
}
#[test]
fn every_kind_carries_its_words() {
const fn words(kind: NodeKind) -> Words {
match kind {
NodeKind::Operator => Words {
macro_word: "operator",
noun: "operator",
plural: "operators",
article: "an",
},
NodeKind::Entity => Words {
macro_word: "entity",
noun: "entity",
plural: "entities",
article: "an",
},
NodeKind::Record => Words {
macro_word: "record",
noun: "record",
plural: "records",
article: "a",
},
NodeKind::Aggregate => Words {
macro_word: "aggregate",
noun: "aggregate",
plural: "aggregates",
article: "an",
},
NodeKind::Fulltext => Words {
macro_word: "fulltext",
noun: "full-text index",
plural: "full-text indexes",
article: "a",
},
NodeKind::Vector => Words {
macro_word: "vector",
noun: "embedding index",
plural: "embedding indexes",
article: "an",
},
NodeKind::Blob => Words {
macro_word: "blob",
noun: "artifact family",
plural: "artifact families",
article: "an",
},
}
}
for kind in ALL {
let want = words(kind);
assert_eq!(kind.as_str(), want.macro_word, "{kind:?} macro word");
assert_eq!(kind.noun(), want.noun, "{kind:?} noun");
assert_eq!(kind.plural(), want.plural, "{kind:?} plural");
assert_eq!(kind.article(), want.article, "{kind:?} article");
}
}
#[test]
fn every_kind_answers_every_question() {
const fn answers(kind: NodeKind) -> Answers {
match kind {
NodeKind::Operator => Answers {
storage_block: None,
holds_rows: false,
election: None,
derives_its_key: false,
dressable: false,
stores: false,
},
NodeKind::Entity => Answers {
storage_block: None,
holds_rows: false,
election: None,
derives_its_key: false,
dressable: true,
stores: false,
},
NodeKind::Record => Answers {
storage_block: None,
holds_rows: true,
election: None,
derives_its_key: false,
dressable: true,
stores: true,
},
NodeKind::Aggregate => Answers {
storage_block: None,
holds_rows: true,
election: None,
derives_its_key: false,
dressable: true,
stores: true,
},
NodeKind::Fulltext => Answers {
storage_block: Some("fulltext"),
holds_rows: false,
election: Some("searchable"),
derives_its_key: true,
dressable: false,
stores: true,
},
NodeKind::Vector => Answers {
storage_block: Some("vector"),
holds_rows: false,
election: Some("knn"),
derives_its_key: true,
dressable: false,
stores: true,
},
NodeKind::Blob => Answers {
storage_block: Some("blob"),
holds_rows: false,
election: None,
derives_its_key: true,
dressable: false,
stores: true,
},
}
}
for kind in ALL {
let want = answers(kind);
assert_eq!(kind.storage_block(), want.storage_block, "{kind:?} block");
assert_eq!(kind.holds_rows(), want.holds_rows, "{kind:?} rows");
assert_eq!(kind.election(), want.election, "{kind:?} election");
assert_eq!(kind.derives_its_key(), want.derives_its_key, "{kind:?} key");
assert_eq!(kind.dressable(), want.dressable, "{kind:?} dressing");
assert_eq!(kind.stores(), want.stores, "{kind:?} storing");
}
}
#[test]
fn the_words_name_one_kind_each() {
for (at, one) in ALL.iter().enumerate() {
for other in &ALL[at + 1..] {
assert_ne!(one.as_str(), other.as_str(), "{one:?} vs {other:?}");
assert_ne!(one.noun(), other.noun(), "{one:?} vs {other:?}");
assert_ne!(one.plural(), other.plural(), "{one:?} vs {other:?}");
}
}
}
#[test]
fn the_article_agrees_with_its_noun() {
for kind in ALL {
let vowel = matches!(
kind.noun().as_bytes().first(),
Some(b'a' | b'e' | b'i' | b'o' | b'u')
);
let want = if vowel { "an" } else { "a" };
assert_eq!(
kind.article(),
want,
"{kind:?} says `{article} {noun}`",
article = kind.article(),
noun = kind.noun()
);
}
}
#[test]
fn a_row_holding_kind_stores_and_wraps_nothing() {
for kind in ALL {
assert_eq!(
kind.holds_rows(),
ROW_HOLDING.contains(&kind),
"{kind:?} against the row-holding roster"
);
if kind.holds_rows() {
assert!(kind.stores(), "{kind:?} holds rows and must store");
assert!(
kind.storage_block().is_none(),
"{kind:?} holds rows, so its store words are bare at the head"
);
}
}
}
#[test]
fn only_a_derived_index_carries_its_upstreams_key() {
for kind in ALL {
assert_eq!(
kind.derives_its_key(),
DERIVED.contains(&kind),
"{kind:?} against the derived roster"
);
if kind.derives_its_key() {
assert!(
kind.storage_block().is_some(),
"{kind:?} borrows a key, so its storage is its own macro's block"
);
}
if kind.election().is_some() {
assert!(
kind.derives_its_key(),
"{kind:?} elects a subject, which only a derived index has"
);
}
}
}
#[test]
fn an_aggregate_stores_and_mints_its_own_key() {
assert!(NodeKind::Aggregate.stores());
assert!(!NodeKind::Aggregate.derives_its_key());
assert!(NodeKind::Aggregate.holds_rows());
}
#[test]
fn an_aggregates_storage_answers_are_a_records() {
let (aggregate, record) = (NodeKind::Aggregate, NodeKind::Record);
assert_eq!(aggregate.storage_block(), record.storage_block());
assert_eq!(aggregate.holds_rows(), record.holds_rows());
assert_eq!(aggregate.derives_its_key(), record.derives_its_key());
assert_eq!(aggregate.dressable(), record.dressable());
assert_eq!(aggregate.stores(), record.stores());
assert_eq!(aggregate.election(), record.election());
assert_ne!(aggregate.as_str(), record.as_str());
assert_ne!(aggregate.noun(), record.noun());
}
#[test]
fn an_aggregate_elects_no_index_subject() {
assert_eq!(NodeKind::Aggregate.election(), None);
assert_eq!(NodeKind::Aggregate.storage_block(), None);
}