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 {
15 impl CommentAggregates {
16 pub fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
17 comment_aggregates::table
18 .filter(comment_aggregates::comment_id.eq(comment_id))
26 aggregates::comment_aggregates::CommentAggregates,
28 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
29 community::{Community, CommunityForm},
30 post::{Post, PostForm},
31 user::{UserForm, User_},
33 tests::establish_unpooled_connection,
42 let conn = establish_unpooled_connection();
44 let new_user = UserForm {
45 name: "thommy_comment_agg".into(),
46 preferred_username: None,
47 password_encrypted: "nope".into(),
57 theme: "browser".into(),
58 default_sort_type: SortType::Hot as i16,
59 default_listing_type: ListingType::Subscribed as i16,
60 lang: "browser".into(),
62 send_notifications_to_email: false,
68 last_refreshed_at: None,
71 let inserted_user = User_::create(&conn, &new_user).unwrap();
73 let another_user = UserForm {
74 name: "jerry_comment_agg".into(),
75 preferred_username: None,
76 password_encrypted: "nope".into(),
86 theme: "browser".into(),
87 default_sort_type: SortType::Hot as i16,
88 default_listing_type: ListingType::Subscribed as i16,
89 lang: "browser".into(),
91 send_notifications_to_email: false,
97 last_refreshed_at: None,
100 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
102 let new_community = CommunityForm {
103 name: "TIL_comment_agg".into(),
104 creator_id: inserted_user.id,
105 title: "nada".to_owned(),
116 last_refreshed_at: None,
122 let inserted_community = Community::create(&conn, &new_community).unwrap();
124 let new_post = PostForm {
125 name: "A test post".into(),
128 creator_id: inserted_user.id,
129 community_id: inserted_community.id,
137 embed_description: None,
145 let inserted_post = Post::create(&conn, &new_post).unwrap();
147 let comment_form = CommentForm {
148 content: "A test comment".into(),
149 creator_id: inserted_user.id,
150 post_id: inserted_post.id,
161 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
163 let child_comment_form = CommentForm {
164 content: "A test comment".into(),
165 creator_id: inserted_user.id,
166 post_id: inserted_post.id,
170 parent_id: Some(inserted_comment.id),
177 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
179 let comment_like = CommentLikeForm {
180 comment_id: inserted_comment.id,
181 post_id: inserted_post.id,
182 user_id: inserted_user.id,
186 CommentLike::like(&conn, &comment_like).unwrap();
188 let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
190 assert_eq!(1, comment_aggs_before_delete.score);
191 assert_eq!(1, comment_aggs_before_delete.upvotes);
192 assert_eq!(0, comment_aggs_before_delete.downvotes);
194 // Add a post dislike from the other user
195 let comment_dislike = CommentLikeForm {
196 comment_id: inserted_comment.id,
197 post_id: inserted_post.id,
198 user_id: another_inserted_user.id,
202 CommentLike::like(&conn, &comment_dislike).unwrap();
204 let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
206 assert_eq!(0, comment_aggs_after_dislike.score);
207 assert_eq!(1, comment_aggs_after_dislike.upvotes);
208 assert_eq!(1, comment_aggs_after_dislike.downvotes);
210 // Remove the first comment like
211 CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
212 let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
213 assert_eq!(-1, after_like_remove.score);
214 assert_eq!(0, after_like_remove.upvotes);
215 assert_eq!(1, after_like_remove.downvotes);
217 // Remove the parent post
218 Post::delete(&conn, inserted_post.id).unwrap();
220 // Should be none found, since the post was deleted
221 let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
222 assert!(after_delete.is_err());
224 // This should delete all the associated rows, and fire triggers
225 User_::delete(&conn, another_inserted_user.id).unwrap();
226 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
227 assert_eq!(1, user_num_deleted);