use core::{
future::Future,
num::NonZeroUsize,
pin::pin,
task::{Context, Poll, Waker},
};
use super::{
AsyncFulltextRead, AsyncFulltextWrite, AsyncVectorRead, AsyncVectorWrite, FulltextRead,
FulltextWrite, VectorRead, VectorWrite,
};
use crate::{Connected, FulltextColumn, FulltextSubset, Hits, VectorColumn};
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;
}
}
}
const K: NonZeroUsize = NonZeroUsize::new(10).expect("10 is not zero");
const COLUMN: VectorColumn = VectorColumn::new("embedding");
fn wears_every_face<E>()
where
E: VectorRead + AsyncVectorRead + VectorWrite + AsyncVectorWrite,
E: FulltextRead + AsyncFulltextRead + FulltextWrite + AsyncFulltextWrite,
{
}
const _: fn() = wears_every_face::<()>;
#[test]
fn the_absent_vector_read_face_answers_empty() {
let hits = VectorRead::knn(&(), Connected::new(&(), ()), COLUMN, &[], K, None)
.expect("the absent face is infallible");
assert_eq!(hits, Hits::new());
let vectors = VectorRead::get(&(), Connected::new(&(), ()), COLUMN, &())
.expect("the absent face is infallible");
assert!(vectors.is_empty(), "{vectors:?}");
}
#[test]
fn the_absent_vector_write_face_accepts() {
VectorWrite::upsert(&(), Connected::new(&(), ()), ()).expect("the absent face is infallible");
}
#[test]
fn the_absent_fulltext_read_face_answers_empty_for_every_subset() {
let whole = FulltextRead::search(
&(),
Connected::new(&(), ()),
FulltextSubset::All,
"car",
K,
None,
)
.expect("the absent face is infallible");
assert_eq!(whole, Hits::new());
let scoped = FulltextRead::search(
&(),
Connected::new(&(), ()),
FulltextSubset::These(&[FulltextColumn::new("body")]),
"car",
K,
None,
)
.expect("the absent face is infallible");
assert_eq!(scoped, whole);
}
#[test]
fn the_absent_fulltext_write_face_accepts() {
FulltextWrite::upsert(&(), Connected::new(&(), ()), ()).expect("the absent face is infallible");
}
#[test]
fn both_vector_read_faces_answer_alike() {
let blocking = VectorRead::knn(&(), Connected::new(&(), ()), COLUMN, &[], K, None)
.expect("the absent face is infallible");
let asynchronous = block_on(AsyncVectorRead::knn(
&(),
Connected::new(&(), ()),
COLUMN,
&[],
K,
None,
))
.expect("the absent face is infallible");
assert_eq!(blocking, asynchronous);
let blocking = VectorRead::get(&(), Connected::new(&(), ()), COLUMN, &())
.expect("the absent face is infallible");
let asynchronous = block_on(AsyncVectorRead::get(
&(),
Connected::new(&(), ()),
COLUMN,
&(),
))
.expect("the absent face is infallible");
assert_eq!(blocking, asynchronous);
}
#[test]
fn both_fulltext_read_faces_answer_alike() {
let subset = FulltextSubset::These(&[FulltextColumn::new("body")]);
let blocking = FulltextRead::search(&(), Connected::new(&(), ()), subset, "car", K, None)
.expect("the absent face is infallible");
let asynchronous = block_on(AsyncFulltextRead::search(
&(),
Connected::new(&(), ()),
subset,
"car",
K,
None,
))
.expect("the absent face is infallible");
assert_eq!(blocking, asynchronous);
}
#[test]
fn both_write_faces_accept_alike() {
let blocking = VectorWrite::upsert(&(), Connected::new(&(), ()), ());
let asynchronous = block_on(AsyncVectorWrite::upsert(&(), Connected::new(&(), ()), ()));
assert_eq!(blocking.is_ok(), asynchronous.is_ok());
let blocking = FulltextWrite::upsert(&(), Connected::new(&(), ()), ());
let asynchronous = block_on(AsyncFulltextWrite::upsert(&(), Connected::new(&(), ()), ()));
assert_eq!(blocking.is_ok(), asynchronous.is_ok());
}