From bc7450ae3ecdb575b9d75fbab9c0a4dd59698ad3 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 23 Aug 2022 23:20:00 +0200 Subject: [PATCH] add test for comment view languages --- crates/db_views/src/comment_view.rs | 64 ++++++++++++++++++++++++++++- scripts/test.sh | 2 +- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 4730e9b7..137fcbe1 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -233,7 +233,7 @@ impl<'a> CommentQuery<'a> { .inner_join(language::table) .left_join( local_user_language::table.on( - post::language_id + comment::language_id .eq(local_user_language::language_id) .and(local_user_language::local_user_id.eq(local_user_id_join)), ), @@ -391,7 +391,9 @@ mod tests { source::{ comment::*, community::*, + language::Language, local_user::LocalUserForm, + local_user_language::LocalUserLanguage, person::*, person_block::PersonBlockForm, post::*, @@ -479,10 +481,12 @@ mod tests { let inserted_comment_1 = Comment::create(&conn, &comment_form_1, Some(&inserted_comment_0.path)).unwrap(); + let finnish_id = Language::read_id_from_code(&conn, "fi").unwrap(); let comment_form_2 = CommentForm { content: "Comment 2".into(), creator_id: inserted_person.id, post_id: inserted_post.id, + language_id: Some(finnish_id), ..CommentForm::default() }; @@ -499,10 +503,12 @@ mod tests { let _inserted_comment_3 = Comment::create(&conn, &comment_form_3, Some(&inserted_comment_1.path)).unwrap(); + let polish_id = Language::read_id_from_code(&conn, "pl").unwrap(); let comment_form_4 = CommentForm { content: "Comment 4".into(), creator_id: inserted_person.id, post_id: inserted_post.id, + language_id: Some(polish_id), ..CommentForm::default() }; @@ -679,6 +685,62 @@ mod tests { cleanup(data, &conn); } + #[test] + #[serial] + fn test_languages() { + let conn = establish_unpooled_connection(); + let data = init_data(&conn); + + // by default, user has all languages enabled and should see all comments + // (except from blocked user) + let all_languages = CommentQuery::builder() + .conn(&conn) + .local_user(Some(&data.inserted_local_user)) + .build() + .list() + .unwrap(); + assert_eq!(5, all_languages.len()); + + // change user lang to finnish, should only show single finnish comment + let finnish_id = Language::read_id_from_code(&conn, "fi").unwrap(); + LocalUserLanguage::update_user_languages( + &conn, + Some(vec![finnish_id]), + data.inserted_local_user.id, + ) + .unwrap(); + let finnish_comment = CommentQuery::builder() + .conn(&conn) + .local_user(Some(&data.inserted_local_user)) + .build() + .list() + .unwrap(); + assert_eq!(1, finnish_comment.len()); + assert_eq!( + data.inserted_comment_2.content, + finnish_comment[0].comment.content + ); + assert_eq!(finnish_id, finnish_comment[0].comment.language_id); + + // now show all comments with undetermined language (which is the default value) + let undetermined_id = Language::read_id_from_code(&conn, "und").unwrap(); + LocalUserLanguage::update_user_languages( + &conn, + Some(vec![undetermined_id]), + data.inserted_local_user.id, + ) + .unwrap(); + let undetermined_comment = CommentQuery::builder() + .conn(&conn) + .local_user(Some(&data.inserted_local_user)) + .build() + .list() + .unwrap(); + assert_eq!(3, undetermined_comment.len()); + + cleanup(data, &conn); + } + fn cleanup(data: Data, conn: &PgConnection) { CommentLike::remove(&conn, data.inserted_person.id, data.inserted_comment_0.id).unwrap(); Comment::delete(&conn, data.inserted_comment_0.id).unwrap(); diff --git a/scripts/test.sh b/scripts/test.sh index d6b87a69..71a8b8ee 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -9,5 +9,5 @@ export LEMMY_DATABASE_URL=postgres://lemmy:password@localhost:5432/lemmy # so to load the config we need to traverse to the repo root export LEMMY_CONFIG_LOCATION=../../config/config.hjson RUST_BACKTRACE=1 \ - cargo test -p lemmy_db_views --no-fail-fast --all-features + cargo test -p lemmy_db_views --no-fail-fast --all-features -- --nocapture # Add this to do printlns: -- --nocapture -- 2.44.1