surrealpatch/lib/tests/matches.rs
2023-06-19 19:41:13 +01:00

93 lines
2.2 KiB
Rust

mod parse;
use parse::Parse;
use surrealdb::dbs::Session;
use surrealdb::err::Error;
use surrealdb::kvs::Datastore;
use surrealdb::sql::Value;
#[tokio::test]
async fn select_where_matches_using_index() -> Result<(), Error> {
let sql = r"
CREATE blog:1 SET title = 'Hello World!';
DEFINE ANALYZER simple TOKENIZERS blank,class;
DEFINE INDEX blog_title ON blog FIELDS title SEARCH ANALYZER simple BM25(1.2,0.75);
SELECT id,title FROM blog WHERE title @@ 'Hello' EXPLAIN;
";
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
assert_eq!(res.len(), 4);
//
let _ = res.remove(0).result?;
let _ = res.remove(0).result?;
let _ = res.remove(0).result?;
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: blog:1,
title: 'Hello World!'
},
{
explain:
[
{
detail: {
plan: {
index: 'blog_title',
operator: '@@',
value: 'Hello'
},
table: 'blog',
},
operation: 'Iterate Index'
}
]
}
]",
);
assert_eq!(tmp, val);
Ok(())
}
#[tokio::test]
async fn select_where_matches_without_using_index_iterator() -> Result<(), Error> {
let sql = r"
CREATE blog:1 SET title = 'Hello World!';
CREATE blog:2 SET title = 'Foo Bar!';
DEFINE ANALYZER simple TOKENIZERS blank,class FILTERS lowercase;
DEFINE INDEX blog_title ON blog FIELDS title SEARCH ANALYZER simple BM25(1.2,0.75);
SELECT id,title FROM blog WHERE (title @@ 'hello' AND id>0) OR (title @@ 'world' AND id<99) EXPLAIN;
";
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
assert_eq!(res.len(), 5);
//
let _ = res.remove(0).result?;
let _ = res.remove(0).result?;
let _ = res.remove(0).result?;
let _ = res.remove(0).result?;
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: blog:1,
title: 'Hello World!'
},
{
explain:
[
{
detail: {
table: 'blog',
},
operation: 'Iterate Table'
}
]
}
]",
);
assert_eq!(tmp, val);
Ok(())
}