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_},
43 let conn = establish_unpooled_connection();
45 let new_user = UserForm {
46 name: "thommy_comment_agg".into(),
47 preferred_username: None,
48 password_encrypted: "nope".into(),
58 theme: "browser".into(),
59 default_sort_type: SortType::Hot as i16,
60 default_listing_type: ListingType::Subscribed as i16,
61 lang: "browser".into(),
63 send_notifications_to_email: false,
69 last_refreshed_at: None,
72 let inserted_user = User_::create(&conn, &new_user).unwrap();
74 let another_user = UserForm {
75 name: "jerry_comment_agg".into(),
76 preferred_username: None,
77 password_encrypted: "nope".into(),
87 theme: "browser".into(),
88 default_sort_type: SortType::Hot as i16,
89 default_listing_type: ListingType::Subscribed as i16,
90 lang: "browser".into(),
92 send_notifications_to_email: false,
98 last_refreshed_at: None,
101 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
103 let new_community = CommunityForm {
104 name: "TIL_comment_agg".into(),
105 creator_id: inserted_user.id,
106 title: "nada".to_owned(),
117 last_refreshed_at: None,
123 let inserted_community = Community::create(&conn, &new_community).unwrap();
125 let new_post = PostForm {
126 name: "A test post".into(),
129 creator_id: inserted_user.id,
130 community_id: inserted_community.id,
138 embed_description: None,
146 let inserted_post = Post::create(&conn, &new_post).unwrap();
148 let comment_form = CommentForm {
149 content: "A test comment".into(),
150 creator_id: inserted_user.id,
151 post_id: inserted_post.id,
162 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
164 let child_comment_form = CommentForm {
165 content: "A test comment".into(),
166 creator_id: inserted_user.id,
167 post_id: inserted_post.id,
171 parent_id: Some(inserted_comment.id),
178 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
180 let comment_like = CommentLikeForm {
181 comment_id: inserted_comment.id,
182 post_id: inserted_post.id,
183 user_id: inserted_user.id,
187 CommentLike::like(&conn, &comment_like).unwrap();
189 let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
191 assert_eq!(1, comment_aggs_before_delete.score);
192 assert_eq!(1, comment_aggs_before_delete.upvotes);
193 assert_eq!(0, comment_aggs_before_delete.downvotes);
195 // Add a post dislike from the other user
196 let comment_dislike = CommentLikeForm {
197 comment_id: inserted_comment.id,
198 post_id: inserted_post.id,
199 user_id: another_inserted_user.id,
203 CommentLike::like(&conn, &comment_dislike).unwrap();
205 let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
207 assert_eq!(0, comment_aggs_after_dislike.score);
208 assert_eq!(1, comment_aggs_after_dislike.upvotes);
209 assert_eq!(1, comment_aggs_after_dislike.downvotes);
211 // Remove the first comment like
212 CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
213 let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
214 assert_eq!(-1, after_like_remove.score);
215 assert_eq!(0, after_like_remove.upvotes);
216 assert_eq!(1, after_like_remove.downvotes);
218 // Remove the parent post
219 Post::delete(&conn, inserted_post.id).unwrap();
221 // Should be none found, since the post was deleted
222 let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
223 assert!(after_delete.is_err());
225 // This should delete all the associated rows, and fire triggers
226 User_::delete(&conn, another_inserted_user.id).unwrap();
227 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
228 assert_eq!(1, user_num_deleted);