1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::comment_aggregates, CommentId};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "comment_aggregates"]
7 pub struct CommentAggregates {
9 pub comment_id: CommentId,
13 pub published: chrono::NaiveDateTime,
16 impl CommentAggregates {
17 pub fn read(conn: &PgConnection, comment_id: CommentId) -> 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,
32 use lemmy_db_schema::source::{
33 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
34 community::{Community, CommunityForm},
35 person::{Person, PersonForm},
36 post::{Post, PostForm},
38 use serial_test::serial;
43 let conn = establish_unpooled_connection();
45 let new_person = PersonForm {
46 name: "thommy_comment_agg".into(),
47 preferred_username: None,
59 last_refreshed_at: None,
61 shared_inbox_url: None,
65 let inserted_person = Person::create(&conn, &new_person).unwrap();
67 let another_person = PersonForm {
68 name: "jerry_comment_agg".into(),
69 preferred_username: None,
81 last_refreshed_at: None,
83 shared_inbox_url: None,
87 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
89 let new_community = CommunityForm {
90 name: "TIL_comment_agg".into(),
91 creator_id: inserted_person.id,
92 title: "nada".to_owned(),
102 last_refreshed_at: None,
108 shared_inbox_url: None,
111 let inserted_community = Community::create(&conn, &new_community).unwrap();
113 let new_post = PostForm {
114 name: "A test post".into(),
117 creator_id: inserted_person.id,
118 community_id: inserted_community.id,
126 embed_description: None,
134 let inserted_post = Post::create(&conn, &new_post).unwrap();
136 let comment_form = CommentForm {
137 content: "A test comment".into(),
138 creator_id: inserted_person.id,
139 post_id: inserted_post.id,
150 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
152 let child_comment_form = CommentForm {
153 content: "A test comment".into(),
154 creator_id: inserted_person.id,
155 post_id: inserted_post.id,
159 parent_id: Some(inserted_comment.id),
166 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
168 let comment_like = CommentLikeForm {
169 comment_id: inserted_comment.id,
170 post_id: inserted_post.id,
171 person_id: inserted_person.id,
175 CommentLike::like(&conn, &comment_like).unwrap();
177 let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
179 assert_eq!(1, comment_aggs_before_delete.score);
180 assert_eq!(1, comment_aggs_before_delete.upvotes);
181 assert_eq!(0, comment_aggs_before_delete.downvotes);
183 // Add a post dislike from the other person
184 let comment_dislike = CommentLikeForm {
185 comment_id: inserted_comment.id,
186 post_id: inserted_post.id,
187 person_id: another_inserted_person.id,
191 CommentLike::like(&conn, &comment_dislike).unwrap();
193 let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
195 assert_eq!(0, comment_aggs_after_dislike.score);
196 assert_eq!(1, comment_aggs_after_dislike.upvotes);
197 assert_eq!(1, comment_aggs_after_dislike.downvotes);
199 // Remove the first comment like
200 CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
201 let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
202 assert_eq!(-1, after_like_remove.score);
203 assert_eq!(0, after_like_remove.upvotes);
204 assert_eq!(1, after_like_remove.downvotes);
206 // Remove the parent post
207 Post::delete(&conn, inserted_post.id).unwrap();
209 // Should be none found, since the post was deleted
210 let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
211 assert!(after_delete.is_err());
213 // This should delete all the associated rows, and fire triggers
214 Person::delete(&conn, another_inserted_person.id).unwrap();
215 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
216 assert_eq!(1, person_num_deleted);