From 6c1dd6608d46d7a77133a785824b35ba0e8f1c2a Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 11 Jan 2021 12:06:22 +0900 Subject: [PATCH] Add test for search actor --- plume-models/src/instance.rs | 6 -- plume-models/src/search/actor.rs | 137 +++++++++++++++++++++++++++++++ plume-models/src/users.rs | 6 -- 3 files changed, 137 insertions(+), 12 deletions(-) diff --git a/plume-models/src/instance.rs b/plume-models/src/instance.rs index 4ae62f51..a2114915 100644 --- a/plume-models/src/instance.rs +++ b/plume-models/src/instance.rs @@ -306,12 +306,6 @@ pub(crate) mod tests { res } - pub(crate) fn clear_instances(conn: &Conn) { - diesel::delete(crate::schema::instances::table) - .execute(conn) - .unwrap(); - } - #[test] fn local_instance() { let conn = &db(); diff --git a/plume-models/src/search/actor.rs b/plume-models/src/search/actor.rs index b8724f2b..6d99fda0 100644 --- a/plume-models/src/search/actor.rs +++ b/plume-models/src/search/actor.rs @@ -75,3 +75,140 @@ impl ActorFactoryArgs<(Arc, DbPool)> for SearchActor { Self { searcher, conn } } } + +#[cfg(test)] +mod tests { + use crate::diesel::Connection; + use crate::diesel::RunQueryDsl; + use crate::{ + blog_authors::{BlogAuthor, NewBlogAuthor}, + blogs::{Blog, NewBlog}, + db_conn::{DbPool, PragmaForeignKey}, + instance::{Instance, NewInstance}, + post_authors::{NewPostAuthor, PostAuthor}, + posts::{NewPost, Post}, + safe_string::SafeString, + search::{actor::SearchActor, tests::get_searcher, Query}, + users::{NewUser, User}, + Connection as Conn, CONFIG, + }; + use diesel::r2d2::ConnectionManager; + use plume_common::utils::random_hex; + use std::str::FromStr; + use std::sync::Arc; + use std::thread::sleep; + use std::time::Duration; + + #[test] + fn post_updated() { + // Need to commit so that searcher on another thread retrieve records. + // So, build DbPool instead of using DB_POOL for testing. + let manager = ConnectionManager::::new(CONFIG.database_url.as_str()); + let db_pool = DbPool::builder() + .connection_customizer(Box::new(PragmaForeignKey)) + .build(manager) + .unwrap(); + + let searcher = Arc::new(get_searcher(&CONFIG.search_tokenizers)); + SearchActor::init(searcher.clone(), db_pool.clone()); + let conn = db_pool.clone().get().unwrap(); + + let title = random_hex()[..8].to_owned(); + let mut post_id = 0; + let (instance, user, blog, post) = &conn + .transaction::<(Instance, User, Blog, Post), diesel::result::Error, _>(|| { + let (instance, user, blog) = fill_database(&conn); + let author = &blog.list_authors(&conn).unwrap()[0]; + + let 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, + }, + ) + .unwrap(); + PostAuthor::insert( + &conn, + NewPostAuthor { + post_id: post.id, + author_id: author.id, + }, + ) + .unwrap(); + post_id = post.id; + + Ok((instance, user, blog, post)) + }) + .unwrap(); + + // Wait for searcher on another thread add document asynchronously + sleep(Duration::from_millis(700)); + searcher.commit(); + assert_eq!( + searcher.search_document(&conn, Query::from_str(&title).unwrap(), (0, 1))[0].id, + post_id + ); + // TODO: Make sure records are deleted even when assertion failed + post.delete(&conn).unwrap(); + blog.delete(&conn).unwrap(); + user.delete(&conn).unwrap(); + diesel::delete(instance).execute(&conn).unwrap(); + } + + fn fill_database(conn: &Conn) -> (Instance, User, Blog) { + conn.transaction::<(Instance, User, Blog), diesel::result::Error, _>(|| { + let instance = Instance::insert( + conn, + NewInstance { + default_license: "CC-0-BY-SA".to_string(), + local: true, + long_description: SafeString::new("Good morning"), + long_description_html: "

Good morning

".to_string(), + short_description: SafeString::new("Hello"), + short_description_html: "

Hello

".to_string(), + name: random_hex().to_string(), + open_registrations: true, + public_domain: random_hex().to_string(), + }, + ) + .unwrap(); + let mut user = NewUser::default(); + user.instance_id = instance.id; + user.username = random_hex().to_string(); + user.ap_url = random_hex().to_string(); + user.inbox_url = random_hex().to_string(); + user.outbox_url = random_hex().to_string(); + user.followers_endpoint = random_hex().to_string(); + let user = User::insert(conn, user).unwrap(); + let mut blog = NewBlog::default(); + blog.instance_id = instance.id; + blog.actor_id = random_hex().to_string(); + blog.ap_url = random_hex().to_string(); + blog.inbox_url = random_hex().to_string(); + blog.outbox_url = random_hex().to_string(); + let blog = Blog::insert(conn, blog).unwrap(); + BlogAuthor::insert( + conn, + NewBlogAuthor { + blog_id: blog.id, + author_id: user.id, + is_owner: true, + }, + ) + .unwrap(); + + Ok((instance, user, blog)) + }) + .unwrap() + } +} diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 07a3051d..c4729acc 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -1171,12 +1171,6 @@ pub(crate) mod tests { vec![admin, user, other] } - pub(crate) fn clear_users(conn: &Conn) { - diesel::delete(crate::schema::users::table) - .execute(conn) - .unwrap(); - } - #[test] fn find_by() { let r = rockets();