]> Untitled Git - lemmy.git/commitdiff
add test for comment view languages
authorFelix Ableitner <me@nutomic.com>
Tue, 23 Aug 2022 21:20:00 +0000 (23:20 +0200)
committerDessalines <dessalines@users.noreply.github.com>
Fri, 2 Sep 2022 14:49:54 +0000 (10:49 -0400)
crates/db_views/src/comment_view.rs
scripts/test.sh

index 4730e9b7df73a08cc6bbdb7dc29080dad5ad3b61..137fcbe199fae758489a6524dcce2cf4d4ee1bee 100644 (file)
@@ -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();
index d6b87a6932155e8f1736cb08ed5bc4676afd9ab4..71a8b8eee9b351739f5ff14960479170337f4bf1 100755 (executable)
@@ -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