1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::comment_aggregates;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "comment_aggregates"]
7 pub struct CommentAggregates {
13 pub published: chrono::NaiveDateTime,
16 impl CommentAggregates {
17 pub fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
18 comment_aggregates::table
19 .filter(comment_aggregates::comment_id.eq(comment_id))
27 aggregates::comment_aggregates::CommentAggregates,
28 establish_unpooled_connection,
34 use lemmy_db_schema::source::{
35 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36 community::{Community, CommunityForm},
37 post::{Post, PostForm},
38 user::{UserForm, User_},
40 use serial_test::serial;
45 let conn = establish_unpooled_connection();
47 let new_user = UserForm {
48 name: "thommy_comment_agg".into(),
49 preferred_username: None,
50 password_encrypted: "nope".into(),
60 theme: "browser".into(),
61 default_sort_type: SortType::Hot as i16,
62 default_listing_type: ListingType::Subscribed as i16,
63 lang: "browser".into(),
65 send_notifications_to_email: false,
71 last_refreshed_at: None,
73 shared_inbox_url: None,
76 let inserted_user = User_::create(&conn, &new_user).unwrap();
78 let another_user = UserForm {
79 name: "jerry_comment_agg".into(),
80 preferred_username: None,
81 password_encrypted: "nope".into(),
91 theme: "browser".into(),
92 default_sort_type: SortType::Hot as i16,
93 default_listing_type: ListingType::Subscribed as i16,
94 lang: "browser".into(),
96 send_notifications_to_email: false,
102 last_refreshed_at: None,
104 shared_inbox_url: None,
107 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
109 let new_community = CommunityForm {
110 name: "TIL_comment_agg".into(),
111 creator_id: inserted_user.id,
112 title: "nada".to_owned(),
122 last_refreshed_at: None,
128 shared_inbox_url: None,
131 let inserted_community = Community::create(&conn, &new_community).unwrap();
133 let new_post = PostForm {
134 name: "A test post".into(),
137 creator_id: inserted_user.id,
138 community_id: inserted_community.id,
146 embed_description: None,
154 let inserted_post = Post::create(&conn, &new_post).unwrap();
156 let comment_form = CommentForm {
157 content: "A test comment".into(),
158 creator_id: inserted_user.id,
159 post_id: inserted_post.id,
170 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
172 let child_comment_form = CommentForm {
173 content: "A test comment".into(),
174 creator_id: inserted_user.id,
175 post_id: inserted_post.id,
179 parent_id: Some(inserted_comment.id),
186 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
188 let comment_like = CommentLikeForm {
189 comment_id: inserted_comment.id,
190 post_id: inserted_post.id,
191 user_id: inserted_user.id,
195 CommentLike::like(&conn, &comment_like).unwrap();
197 let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
199 assert_eq!(1, comment_aggs_before_delete.score);
200 assert_eq!(1, comment_aggs_before_delete.upvotes);
201 assert_eq!(0, comment_aggs_before_delete.downvotes);
203 // Add a post dislike from the other user
204 let comment_dislike = CommentLikeForm {
205 comment_id: inserted_comment.id,
206 post_id: inserted_post.id,
207 user_id: another_inserted_user.id,
211 CommentLike::like(&conn, &comment_dislike).unwrap();
213 let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
215 assert_eq!(0, comment_aggs_after_dislike.score);
216 assert_eq!(1, comment_aggs_after_dislike.upvotes);
217 assert_eq!(1, comment_aggs_after_dislike.downvotes);
219 // Remove the first comment like
220 CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
221 let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
222 assert_eq!(-1, after_like_remove.score);
223 assert_eq!(0, after_like_remove.upvotes);
224 assert_eq!(1, after_like_remove.downvotes);
226 // Remove the parent post
227 Post::delete(&conn, inserted_post.id).unwrap();
229 // Should be none found, since the post was deleted
230 let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
231 assert!(after_delete.is_err());
233 // This should delete all the associated rows, and fire triggers
234 User_::delete(&conn, another_inserted_user.id).unwrap();
235 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
236 assert_eq!(1, user_num_deleted);