2018-12-02 16:37:51 +00:00
|
|
|
mod query;
|
2019-03-20 16:56:17 +00:00
|
|
|
mod searcher;
|
2018-12-02 16:37:51 +00:00
|
|
|
mod tokenizer;
|
|
|
|
pub use self::query::PlumeQuery as Query;
|
2019-03-20 16:56:17 +00:00
|
|
|
pub use self::searcher::*;
|
2018-12-02 16:37:51 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::{Query, Searcher};
|
2019-03-20 16:56:17 +00:00
|
|
|
use diesel::Connection;
|
2018-12-02 16:37:51 +00:00
|
|
|
use std::env::temp_dir;
|
2019-03-19 13:37:56 +00:00
|
|
|
use std::str::FromStr;
|
2018-12-02 16:37:51 +00:00
|
|
|
|
2019-03-20 16:56:17 +00:00
|
|
|
use blogs::tests::fill_database;
|
2018-12-02 16:37:51 +00:00
|
|
|
use plume_common::utils::random_hex;
|
|
|
|
use post_authors::*;
|
2019-03-20 16:56:17 +00:00
|
|
|
use posts::{NewPost, Post};
|
2018-12-02 16:37:51 +00:00
|
|
|
use safe_string::SafeString;
|
|
|
|
use tests::db;
|
|
|
|
|
|
|
|
pub(crate) fn get_searcher() -> Searcher {
|
|
|
|
let dir = temp_dir().join("plume-test");
|
|
|
|
if dir.exists() {
|
|
|
|
Searcher::open(&dir)
|
|
|
|
} else {
|
|
|
|
Searcher::create(&dir)
|
2019-03-20 16:56:17 +00:00
|
|
|
}
|
|
|
|
.unwrap()
|
2018-12-02 16:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_first_token() {
|
|
|
|
let vector = vec![
|
|
|
|
("+\"my token\" other", ("+\"my token\"", " other")),
|
|
|
|
("-\"my token\" other", ("-\"my token\"", " other")),
|
|
|
|
(" \"my token\" other", ("\"my token\"", " other")),
|
|
|
|
("\"my token\" other", ("\"my token\"", " other")),
|
|
|
|
("+my token other", ("+my", " token other")),
|
|
|
|
("-my token other", ("-my", " token other")),
|
|
|
|
(" my token other", ("my", " token other")),
|
|
|
|
("my token other", ("my", " token other")),
|
|
|
|
("+\"my token other", ("+\"my token other", "")),
|
|
|
|
("-\"my token other", ("-\"my token other", "")),
|
|
|
|
(" \"my token other", ("\"my token other", "")),
|
|
|
|
("\"my token other", ("\"my token other", "")),
|
|
|
|
];
|
|
|
|
for (source, res) in vector {
|
|
|
|
assert_eq!(Query::get_first_token(source), res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_str() {
|
|
|
|
let vector = vec![
|
|
|
|
("", ""),
|
|
|
|
("a query", "a query"),
|
|
|
|
("\"a query\"", "\"a query\""),
|
|
|
|
("+a -\"query\"", "+a -query"),
|
|
|
|
("title:\"something\" a query", "a query title:something"),
|
|
|
|
("-title:\"something\" a query", "a query -title:something"),
|
|
|
|
("author:user@domain", "author:user@domain"),
|
|
|
|
("-author:@user@domain", "-author:user@domain"),
|
|
|
|
("before:2017-11-05 before:2018-01-01", "before:2017-11-05"),
|
|
|
|
("after:2017-11-05 after:2018-01-01", "after:2018-01-01"),
|
|
|
|
];
|
|
|
|
for (source, res) in vector {
|
2019-03-19 13:37:56 +00:00
|
|
|
assert_eq!(&Query::from_str(source).unwrap().to_string(), res);
|
2018-12-02 16:37:51 +00:00
|
|
|
assert_eq!(Query::new().parse_query(source).to_string(), res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn setters() {
|
|
|
|
let vector = vec![
|
|
|
|
("something", "title:something"),
|
|
|
|
("+something", "+title:something"),
|
|
|
|
("-something", "-title:something"),
|
|
|
|
("+\"something\"", "+title:something"),
|
|
|
|
("+some thing", "+title:\"some thing\""),
|
|
|
|
];
|
|
|
|
for (source, res) in vector {
|
|
|
|
assert_eq!(&Query::new().title(source, None).to_string(), res);
|
|
|
|
}
|
|
|
|
|
|
|
|
let vector = vec![
|
|
|
|
("something", "author:something"),
|
|
|
|
("+something", "+author:something"),
|
|
|
|
("-something", "-author:something"),
|
|
|
|
("+\"something\"", "+author:something"),
|
|
|
|
("+@someone@somewhere", "+author:someone@somewhere"),
|
|
|
|
];
|
|
|
|
for (source, res) in vector {
|
|
|
|
assert_eq!(&Query::new().author(source, None).to_string(), res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn open() {
|
2019-03-20 16:56:17 +00:00
|
|
|
{
|
|
|
|
get_searcher()
|
|
|
|
}; //make sure $tmp/plume-test-tantivy exist
|
2018-12-02 16:37:51 +00:00
|
|
|
|
|
|
|
let dir = temp_dir().join("plume-test");
|
|
|
|
Searcher::open(&dir).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create() {
|
|
|
|
let dir = temp_dir().join(format!("plume-test-{}", random_hex()));
|
|
|
|
|
|
|
|
assert!(Searcher::open(&dir).is_err());
|
2019-03-20 16:56:17 +00:00
|
|
|
{
|
|
|
|
Searcher::create(&dir).unwrap();
|
|
|
|
}
|
|
|
|
Searcher::open(&dir).unwrap(); //verify it's well created
|
2018-12-02 16:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn search() {
|
|
|
|
let conn = &db();
|
|
|
|
conn.test_transaction::<_, (), _>(|| {
|
|
|
|
let searcher = get_searcher();
|
2018-12-09 17:44:26 +00:00
|
|
|
let blog = &fill_database(conn).1[0];
|
2018-12-29 08:36:07 +00:00
|
|
|
let author = &blog.list_authors(conn).unwrap()[0];
|
2018-12-02 16:37:51 +00:00
|
|
|
|
|
|
|
let title = random_hex()[..8].to_owned();
|
|
|
|
|
2019-03-20 16:56:17 +00:00
|
|
|
let mut post = Post::insert(
|
|
|
|
conn,
|
|
|
|
NewPost {
|
|
|
|
blog_id: blog.id,
|
|
|
|
slug: title.clone(),
|
|
|
|
title: title.clone(),
|
|
|
|
content: SafeString::new(""),
|
|
|
|
published: true,
|
|
|
|
license: "CC-BY-SA".to_owned(),
|
|
|
|
ap_url: "".to_owned(),
|
|
|
|
creation_date: None,
|
|
|
|
subtitle: "".to_owned(),
|
|
|
|
source: "".to_owned(),
|
|
|
|
cover_id: None,
|
|
|
|
},
|
|
|
|
&searcher,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
PostAuthor::insert(
|
|
|
|
conn,
|
|
|
|
NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: author.id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-12-02 16:37:51 +00:00
|
|
|
|
|
|
|
searcher.commit();
|
2019-03-20 16:56:17 +00:00
|
|
|
assert_eq!(
|
|
|
|
searcher.search_document(conn, Query::from_str(&title).unwrap(), (0, 1))[0].id,
|
|
|
|
post.id
|
|
|
|
);
|
2018-12-02 16:37:51 +00:00
|
|
|
|
|
|
|
let newtitle = random_hex()[..8].to_owned();
|
|
|
|
post.title = newtitle.clone();
|
2018-12-29 08:36:07 +00:00
|
|
|
post.update(conn, &searcher).unwrap();
|
2018-12-02 16:37:51 +00:00
|
|
|
searcher.commit();
|
2019-03-20 16:56:17 +00:00
|
|
|
assert_eq!(
|
|
|
|
searcher.search_document(conn, Query::from_str(&newtitle).unwrap(), (0, 1))[0].id,
|
|
|
|
post.id
|
|
|
|
);
|
|
|
|
assert!(searcher
|
|
|
|
.search_document(conn, Query::from_str(&title).unwrap(), (0, 1))
|
|
|
|
.is_empty());
|
2018-12-02 16:37:51 +00:00
|
|
|
|
2019-04-17 17:31:47 +00:00
|
|
|
post.delete(conn, &searcher).unwrap();
|
2018-12-02 16:37:51 +00:00
|
|
|
searcher.commit();
|
2019-03-20 16:56:17 +00:00
|
|
|
assert!(searcher
|
|
|
|
.search_document(conn, Query::from_str(&newtitle).unwrap(), (0, 1))
|
|
|
|
.is_empty());
|
2018-12-02 16:37:51 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn drop_writer() {
|
|
|
|
let searcher = get_searcher();
|
|
|
|
searcher.drop_writer();
|
|
|
|
get_searcher();
|
|
|
|
}
|
|
|
|
}
|