Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unittest for Search by title only #5033

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<()> {
Expand Down