add test for comment view languages

This commit is contained in:
Felix Ableitner 2022-08-23 23:20:00 +02:00 committed by Dessalines
parent f5b1ee6c34
commit bc7450ae3e
2 changed files with 64 additions and 2 deletions

View file

@ -233,7 +233,7 @@ impl<'a> CommentQuery<'a> {
.inner_join(language::table) .inner_join(language::table)
.left_join( .left_join(
local_user_language::table.on( local_user_language::table.on(
post::language_id comment::language_id
.eq(local_user_language::language_id) .eq(local_user_language::language_id)
.and(local_user_language::local_user_id.eq(local_user_id_join)), .and(local_user_language::local_user_id.eq(local_user_id_join)),
), ),
@ -391,7 +391,9 @@ mod tests {
source::{ source::{
comment::*, comment::*,
community::*, community::*,
language::Language,
local_user::LocalUserForm, local_user::LocalUserForm,
local_user_language::LocalUserLanguage,
person::*, person::*,
person_block::PersonBlockForm, person_block::PersonBlockForm,
post::*, post::*,
@ -479,10 +481,12 @@ mod tests {
let inserted_comment_1 = let inserted_comment_1 =
Comment::create(&conn, &comment_form_1, Some(&inserted_comment_0.path)).unwrap(); 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 { let comment_form_2 = CommentForm {
content: "Comment 2".into(), content: "Comment 2".into(),
creator_id: inserted_person.id, creator_id: inserted_person.id,
post_id: inserted_post.id, post_id: inserted_post.id,
language_id: Some(finnish_id),
..CommentForm::default() ..CommentForm::default()
}; };
@ -499,10 +503,12 @@ mod tests {
let _inserted_comment_3 = let _inserted_comment_3 =
Comment::create(&conn, &comment_form_3, Some(&inserted_comment_1.path)).unwrap(); 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 { let comment_form_4 = CommentForm {
content: "Comment 4".into(), content: "Comment 4".into(),
creator_id: inserted_person.id, creator_id: inserted_person.id,
post_id: inserted_post.id, post_id: inserted_post.id,
language_id: Some(polish_id),
..CommentForm::default() ..CommentForm::default()
}; };
@ -679,6 +685,62 @@ mod tests {
cleanup(data, &conn); 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) { fn cleanup(data: Data, conn: &PgConnection) {
CommentLike::remove(&conn, data.inserted_person.id, data.inserted_comment_0.id).unwrap(); CommentLike::remove(&conn, data.inserted_person.id, data.inserted_comment_0.id).unwrap();
Comment::delete(&conn, data.inserted_comment_0.id).unwrap(); Comment::delete(&conn, data.inserted_comment_0.id).unwrap();

View file

@ -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 # so to load the config we need to traverse to the repo root
export LEMMY_CONFIG_LOCATION=../../config/config.hjson export LEMMY_CONFIG_LOCATION=../../config/config.hjson
RUST_BACKTRACE=1 \ 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 # Add this to do printlns: -- --nocapture