- 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();
- Comment::delete(&conn, data.inserted_comment_1.id).unwrap();
- Post::delete(&conn, data.inserted_post.id).unwrap();
- Community::delete(&conn, data.inserted_community.id).unwrap();
- Person::delete(&conn, data.inserted_person.id).unwrap();
- Person::delete(&conn, data.inserted_person_2.id).unwrap();
+ #[tokio::test]
+ #[serial]
+ async fn test_languages() {
+ let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
+ let data = init_data(pool).await;
+
+ // by default, user has all languages enabled and should see all comments
+ // (except from blocked user)
+ let all_languages = CommentQuery {
+ local_user: (Some(&data.inserted_local_user)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+ assert_eq!(5, all_languages.len());
+
+ // change user lang to finnish, should only show one post in finnish and one undetermined
+ let finnish_id = Language::read_id_from_code(pool, Some("fi"))
+ .await
+ .unwrap()
+ .unwrap();
+ LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id)
+ .await
+ .unwrap();
+ let finnish_comments = CommentQuery {
+ local_user: (Some(&data.inserted_local_user)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+ assert_eq!(2, finnish_comments.len());
+ let finnish_comment = finnish_comments
+ .iter()
+ .find(|c| c.comment.language_id == finnish_id);
+ assert!(finnish_comment.is_some());
+ assert_eq!(
+ data.inserted_comment_2.content,
+ finnish_comment.unwrap().comment.content
+ );
+
+ // now show all comments with undetermined language (which is the default value)
+ LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
+ .await
+ .unwrap();
+ let undetermined_comment = CommentQuery {
+ local_user: (Some(&data.inserted_local_user)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+ assert_eq!(1, undetermined_comment.len());
+
+ cleanup(data, pool).await;
+ }
+
+ async fn cleanup(data: Data, pool: &mut DbPool<'_>) {
+ CommentLike::remove(pool, data.inserted_person.id, data.inserted_comment_0.id)
+ .await
+ .unwrap();
+ Comment::delete(pool, data.inserted_comment_0.id)
+ .await
+ .unwrap();
+ Comment::delete(pool, data.inserted_comment_1.id)
+ .await
+ .unwrap();
+ Post::delete(pool, data.inserted_post.id).await.unwrap();
+ Community::delete(pool, data.inserted_community.id)
+ .await
+ .unwrap();
+ Person::delete(pool, data.inserted_person.id).await.unwrap();
+ Person::delete(pool, data.inserted_person_2.id)
+ .await
+ .unwrap();
+ Instance::delete(pool, data.inserted_instance.id)
+ .await
+ .unwrap();