use crate::{
alias::sort_column,
model::RecordModel,
prefix::EmissionPrefix,
tests::{gathered, gathered_in_mysql, gathered_in_postgres},
};
pub(super) fn sortable(mut model: RecordModel) -> RecordModel {
let field = model
.fields
.iter_mut()
.find(|field| field.column.source == "chapters")
.expect("the collection column is a field of the record");
assert!(
field.marker.is_some(),
"the collection declared one of the framework's words"
);
field.sortable = true;
model
}
fn emitted(model: &RecordModel) -> String {
model.emit().to_string()
}
#[test]
fn a_sortable_collection_emits_its_companion() {
let tokens = emitted(&sortable(gathered()));
assert!(
tokens.contains("const SHADOWS"),
"the write plan states the companion: {tokens}"
);
assert!(
tokens.contains("\"__ig_chapters_sort__\""),
"under the synthesized-column law: {tokens}"
);
assert!(
tokens.contains("collection_sort_key"),
"filled from the framework's own encoding: {tokens}"
);
}
#[test]
fn a_sortable_collection_orders_by_the_companion() {
let tokens = emitted(&sortable(gathered()));
assert!(
tokens.contains("OrderTerm :: shadow"),
"the subject is the companion's: {tokens}"
);
assert!(
tokens.contains("TermName :: new (\"chapters\")"),
"and the identity is still the field's: {tokens}"
);
}
#[test]
fn the_other_edge_dialect_answers_alike() {
let tokens = emitted(&sortable(gathered_in_mysql()));
assert!(tokens.contains("const SHADOWS"), "{tokens}");
assert!(tokens.contains("OrderTerm :: shadow"), "{tokens}");
}
#[test]
fn an_array_stored_collection_emits_no_companion() {
let tokens = emitted(&sortable(gathered_in_postgres()));
assert!(
!tokens.contains("const SHADOWS"),
"a native array orders itself: {tokens}"
);
assert!(
!tokens.contains("__ig_chapters_sort__"),
"so no column is created, written or ordered by: {tokens}"
);
assert!(
tokens.contains("OrderTerm :: array"),
"and the term is the array's, as it was: {tokens}"
);
}
#[test]
fn a_collection_without_the_word_emits_what_it_always_did() {
let plain = emitted(&gathered());
assert!(!plain.contains("const SHADOWS"), "{plain}");
assert!(!plain.contains("collection_sort_key"), "{plain}");
assert!(
plain.contains("OrderTerm :: edge"),
"the term is the remainder it was: {plain}"
);
}
#[test]
fn the_companion_is_named_from_the_stored_column() {
let mut model = sortable(gathered());
let Some(persist) = model.persist.as_mut() else {
panic!("the model stores its columns")
};
let columns = match &mut persist.backends[0] {
crate::model::DatabaseBackend::Sqlite(db) => &mut db.storage.columns,
_ => panic!("this model's first backend is SQLite"),
};
let at = columns
.iter()
.position(|column| column.source == "chapters")
.expect("the collection column is stored");
columns[at].name = crate::name::RustSafeName::new("acts", proc_macro2::Span::call_site());
let tokens = emitted(&model);
assert!(
tokens.contains("\"__ig_acts_sort__\"") && !tokens.contains("__ig_chapters_sort__"),
"the companion wears the column's stored name: {tokens}"
);
}
#[test]
fn the_companion_is_minted_in_the_namespace_its_database_mints_in() {
let mut model = sortable(gathered());
let Some(persist) = model.persist.as_mut() else {
panic!("the model stores its columns")
};
match &mut persist.backends[0] {
crate::model::DatabaseBackend::Sqlite(db) => {
db.storage.aliases.prefix = Some(String::from("__mine"));
}
_ => panic!("this model's first backend is SQLite"),
}
let tokens = emitted(&model);
assert!(
tokens.contains("\"__mine_chapters_sort__\""),
"the companion is a column of THIS database, so it wears this database's mark: {tokens}"
);
assert!(
!tokens.contains("__ig_chapters_sort__"),
"and the namespace the fence handed back is not written into: {tokens}"
);
}
#[test]
fn a_database_that_names_nothing_falls_back_to_the_models_own_mark() {
let mut model = sortable(gathered());
model.prefix = EmissionPrefix::new("__mg");
let tokens = emitted(&model);
assert!(
tokens.contains("\"__mg_chapters_sort__\"") && !tokens.contains("__ig_chapters_sort__"),
"an absent `prefix = …` resolves to whichever framework built the model: {tokens}"
);
}
#[test]
fn two_fields_cannot_reach_one_companion() {
let mark = EmissionPrefix::default();
assert_eq!(sort_column(mark.mark(), "tags"), "__ig_tags_sort__");
assert_eq!(
sort_column(mark.mark(), "tags_sort"),
"__ig_tags_sort_sort__"
);
assert_ne!(
sort_column(mark.mark(), "tags"),
sort_column(mark.mark(), "tags_sort")
);
}