use core::{
future::Future,
num::NonZeroUsize,
pin::pin,
task::{Context, Poll, Waker},
};
use super::{
AsyncFulltextRead, AsyncFulltextWrite, Connected, FulltextStore, FulltextSubset,
SEARCHABLE_FIELD, Scored, TantivyDoc, TantivyFulltext, TantivyFulltextError, TantivyStore,
scoped_fields,
};
use crate::FulltextColumn;
const ROOMY: NonZeroUsize = NonZeroUsize::new(10).expect("10 is not zero");
const SLOT: &str = "subset_captions";
const HEAP: usize = TantivyStore::MIN_WRITER_HEAP_BYTES;
fn block_on<F: Future>(future: F) -> F::Output {
let mut future = pin!(future);
let mut cx = Context::from_waker(Waker::noop());
loop {
if let Poll::Ready(value) = future.as_mut().poll(&mut cx) {
return value;
}
}
}
fn open_store() -> (tempfile::TempDir, TantivyStore) {
let dir = tempfile::tempdir().expect("a temporary directory");
let store = TantivyStore::open(dir.path()).expect("the store opens");
(dir, store)
}
fn commit(store: &TantivyStore, face: &TantivyFulltext, entries: &[(i64, &str)]) {
{
let mut writer = store
.writer(face.slot(), HEAP)
.expect("the slot's writer opens");
for (key, body) in entries {
let doc = TantivyDoc::new(face.slot())
.with_identity(key)
.with_address(key)
.with_body(*body);
block_on(face.upsert(Connected::new(store, &mut writer), doc)).expect("the document queues");
}
writer.commit().expect("the batch commits");
}
store
.reload(face.slot())
.expect("the slot's reader reloads");
}
#[test]
fn the_whole_subject_resolves_to_the_one_searchable_field() {
let (_dir, store) = open_store();
let opened = store.slot_index(SLOT, false).expect("the slot opens");
assert_eq!(
scoped_fields(FulltextSubset::All, &opened.fields).expect("`All` is always held"),
vec![opened.fields.body()],
"one field, and it is the body",
);
}
#[test]
fn a_subset_naming_the_held_column_answers_what_the_whole_subject_answers() {
let (_dir, store) = open_store();
let face = store.fulltext(SLOT);
commit(
&store,
&face,
&[(1, "a heron at dawn"), (2, "a car in the rain")],
);
let ask = |subset| {
block_on(face.search(Connected::new(&store, ()), subset, "heron", ROOMY, None))
.expect("the search runs")
};
let whole = ask(FulltextSubset::All);
let named = ask(FulltextSubset::These(&[FulltextColumn::new(
SEARCHABLE_FIELD,
)]));
assert_eq!(whole.len(), 1, "the corpus holds one heron");
assert_eq!(
whole.iter().map(Scored::score).collect::<Vec<_>>(),
named.iter().map(Scored::score).collect::<Vec<_>>(),
"the two spellings of one subject score identically",
);
}
#[test]
fn a_column_this_slot_does_not_hold_is_refused_by_name() {
let (_dir, store) = open_store();
let face = store.fulltext(SLOT);
let refused = block_on(face.search(
Connected::new(&store, ()),
FulltextSubset::These(&[FulltextColumn::new("title")]),
"heron",
ROOMY,
None,
));
assert!(
matches!(
refused,
Err(TantivyFulltextError::UnheldColumn { column }) if column.as_str() == "title"
),
"the refusal names the column: {refused:?}",
);
assert!(
refused.unwrap_err().to_string().contains(SEARCHABLE_FIELD),
"and says which one this slot does hold",
);
}
#[test]
fn a_mixed_subset_names_the_first_column_that_is_not_held() {
let (_dir, store) = open_store();
let opened = store.slot_index(SLOT, false).expect("the slot opens");
let refused = scoped_fields(
FulltextSubset::These(&[
FulltextColumn::new(SEARCHABLE_FIELD),
FulltextColumn::new("standfirst"),
FulltextColumn::new("byline"),
]),
&opened.fields,
);
assert!(
matches!(
refused,
Err(TantivyFulltextError::UnheldColumn { column }) if column.as_str() == "standfirst"
),
"the FIRST unheld column, and a held one before it did not absorb the check: {refused:?}",
);
}
#[test]
fn an_empty_subset_is_refused_rather_than_answered() {
let (_dir, store) = open_store();
let opened = store.slot_index(SLOT, false).expect("the slot opens");
let refused = scoped_fields(FulltextSubset::These(&[]), &opened.fields);
assert!(
matches!(refused, Err(TantivyFulltextError::EmptySubset)),
"an empty subset has no answer to give: {refused:?}",
);
}
#[test]
fn the_refusal_precedes_the_search() {
let (_dir, store) = open_store();
let face = store.fulltext(SLOT);
let refused = block_on(face.search(
Connected::new(&store, ()),
FulltextSubset::These(&[FulltextColumn::new("title")]),
"heron",
ROOMY,
None,
));
assert!(
refused.is_err(),
"an empty slot answers the refusal, not an empty hit list",
);
}