From dbb8f9553ae1b0ce0d9bdcf0c2f1c0be09a96ff5 Mon Sep 17 00:00:00 2001 From: leoseg <70430884+leoseg@users.noreply.github.com> Date: Thu, 19 Sep 2024 23:00:07 +0200 Subject: [PATCH] Unittest for Search by title only (#5033) * added test for search by title only * formatted rust files --- crates/db_views/src/post_view.rs | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index acd8debf3..3eab04af5 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -780,6 +780,7 @@ mod tests { use std::{collections::HashSet, time::Duration}; use url::Url; + const POST_WITH_ANOTHER_TITLE: &str = "Another title"; const POST_BY_BLOCKED_PERSON: &str = "post by blocked person"; const POST_BY_BOT: &str = "post by bot"; const POST: &str = "post"; @@ -1028,6 +1029,63 @@ mod tests { cleanup(data, pool).await } + #[tokio::test] + #[serial] + async fn post_listing_title_only() -> LemmyResult<()> { + let pool = &build_db_pool().await?; + let pool = &mut pool.into(); + let data = init_data(pool).await?; + + // A post which contains the search them 'Post' not in the title (but in the body) + let new_post = PostInsertForm::builder() + .name(POST_WITH_ANOTHER_TITLE.to_string()) + .creator_id(data.local_user_view.person.id) + .community_id(data.inserted_community.id) + .language_id(Some(LanguageId(47))) + .body(Some("Post".to_string())) + .build(); + + let inserted_post = Post::create(pool, &new_post).await?; + + let read_post_listing_by_title_only = PostQuery { + community_id: Some(data.inserted_community.id), + local_user: None, + search_term: Some("Post".to_string()), + title_only: Some(true), + ..data.default_post_query() + } + .list(&data.site, pool) + .await?; + + let read_post_listing = PostQuery { + community_id: Some(data.inserted_community.id), + local_user: None, + search_term: Some("Post".to_string()), + ..data.default_post_query() + } + .list(&data.site, pool) + .await?; + + // Should be 4 posts when we do not search for title only + assert_eq!( + vec![ + POST_WITH_ANOTHER_TITLE, + POST_BY_BOT, + POST, + POST_BY_BLOCKED_PERSON + ], + names(&read_post_listing) + ); + + // Should be 3 posts when we search for title only + assert_eq!( + vec![POST_BY_BOT, POST, POST_BY_BLOCKED_PERSON], + names(&read_post_listing_by_title_only) + ); + Post::delete(pool, inserted_post.id).await?; + cleanup(data, pool).await + } + #[tokio::test] #[serial] async fn post_listing_block_community() -> LemmyResult<()> {