+ let _inserted_comment_like = CommentLike::like(pool, &comment_like_form).await.unwrap();
+
+ let local_user_view = LocalUserView {
+ local_user: inserted_local_user.clone(),
+ person: inserted_person.clone(),
+ counts: Default::default(),
+ };
+ Data {
+ inserted_instance,
+ inserted_comment_0,
+ inserted_comment_1,
+ inserted_comment_2,
+ inserted_post,
+ local_user_view,
+ inserted_person_2,
+ inserted_community,
+ }
+ }
+
+ #[tokio::test]
+ #[serial]
+ async fn test_crud() {
+ let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
+ let data = init_data(pool).await;
+
+ let expected_comment_view_no_person = expected_comment_view(&data, pool).await;
+
+ let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
+ expected_comment_view_with_person.my_vote = Some(1);
+
+ let read_comment_views_no_person = CommentQuery {
+ sort: (Some(CommentSortType::Old)),
+ post_id: (Some(data.inserted_post.id)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ assert_eq!(
+ expected_comment_view_no_person,
+ read_comment_views_no_person[0]
+ );
+
+ let read_comment_views_with_person = CommentQuery {
+ sort: (Some(CommentSortType::Old)),
+ post_id: (Some(data.inserted_post.id)),
+ local_user: (Some(&data.local_user_view)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ assert_eq!(
+ expected_comment_view_with_person,
+ read_comment_views_with_person[0]
+ );
+
+ // Make sure its 1, not showing the blocked comment
+ assert_eq!(5, read_comment_views_with_person.len());
+
+ let read_comment_from_blocked_person = CommentView::read(
+ pool,
+ data.inserted_comment_1.id,
+ Some(data.local_user_view.person.id),
+ )
+ .await
+ .unwrap();
+
+ // Make sure block set the creator blocked
+ assert!(read_comment_from_blocked_person.creator_blocked);
+
+ let read_liked_comment_views = CommentQuery {
+ local_user: (Some(&data.local_user_view)),
+ liked_only: (Some(true)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ assert_eq!(
+ expected_comment_view_with_person,
+ read_liked_comment_views[0]
+ );
+
+ assert_eq!(1, read_liked_comment_views.len());
+
+ let read_disliked_comment_views: Vec<CommentView> = CommentQuery {
+ local_user: (Some(&data.local_user_view)),
+ disliked_only: (Some(true)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ assert!(read_disliked_comment_views.is_empty());
+
+ cleanup(data, pool).await;
+ }
+
+ #[tokio::test]
+ #[serial]
+ async fn test_comment_tree() {
+ let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
+ let data = init_data(pool).await;
+
+ let top_path = data.inserted_comment_0.path.clone();
+ let read_comment_views_top_path = CommentQuery {
+ post_id: (Some(data.inserted_post.id)),
+ parent_path: (Some(top_path)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ let child_path = data.inserted_comment_1.path.clone();
+ let read_comment_views_child_path = CommentQuery {
+ post_id: (Some(data.inserted_post.id)),
+ parent_path: (Some(child_path)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ // Make sure the comment parent-limited fetch is correct
+ assert_eq!(6, read_comment_views_top_path.len());
+ assert_eq!(4, read_comment_views_child_path.len());
+
+ // Make sure it contains the parent, but not the comment from the other tree
+ let child_comments = read_comment_views_child_path
+ .into_iter()
+ .map(|c| c.comment)
+ .collect::<Vec<Comment>>();
+ assert!(child_comments.contains(&data.inserted_comment_1));
+ assert!(!child_comments.contains(&data.inserted_comment_2));
+
+ let read_comment_views_top_max_depth = CommentQuery {
+ post_id: (Some(data.inserted_post.id)),
+ max_depth: (Some(1)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ // Make sure a depth limited one only has the top comment
+ assert_eq!(
+ expected_comment_view(&data, pool).await,
+ read_comment_views_top_max_depth[0]
+ );
+ assert_eq!(1, read_comment_views_top_max_depth.len());
+
+ let child_path = data.inserted_comment_1.path.clone();
+ let read_comment_views_parent_max_depth = CommentQuery {
+ post_id: (Some(data.inserted_post.id)),
+ parent_path: (Some(child_path)),
+ max_depth: (Some(1)),
+ sort: (Some(CommentSortType::New)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+
+ // Make sure a depth limited one, and given child comment 1, has 3
+ assert!(read_comment_views_parent_max_depth[2]
+ .comment
+ .content
+ .eq("Comment 3"));
+ assert_eq!(3, read_comment_views_parent_max_depth.len());
+
+ cleanup(data, pool).await;
+ }
+
+ #[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.local_user_view)),
+ ..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.local_user_view.local_user.id)
+ .await
+ .unwrap();
+ let finnish_comments = CommentQuery {
+ local_user: (Some(&data.local_user_view)),
+ ..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.local_user_view.local_user.id,
+ )
+ .await
+ .unwrap();
+ let undetermined_comment = CommentQuery {
+ local_user: (Some(&data.local_user_view)),
+ ..Default::default()
+ }
+ .list(pool)
+ .await
+ .unwrap();
+ assert_eq!(1, undetermined_comment.len());