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 ..PersonForm::default()
50 let inserted_person = Person::create(&conn, &new_person).unwrap();
52 let another_person = PersonForm {
53 name: "jerry_comment_agg".into(),
54 ..PersonForm::default()
57 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
59 let new_community = CommunityForm {
60 name: "TIL_comment_agg".into(),
61 title: "nada".to_owned(),
62 ..CommunityForm::default()
65 let inserted_community = Community::create(&conn, &new_community).unwrap();
67 let new_post = PostForm {
68 name: "A test post".into(),
69 creator_id: inserted_person.id,
70 community_id: inserted_community.id,
74 let inserted_post = Post::create(&conn, &new_post).unwrap();
76 let comment_form = CommentForm {
77 content: "A test comment".into(),
78 creator_id: inserted_person.id,
79 post_id: inserted_post.id,
80 ..CommentForm::default()
83 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
85 let child_comment_form = CommentForm {
86 content: "A test comment".into(),
87 creator_id: inserted_person.id,
88 post_id: inserted_post.id,
89 parent_id: Some(inserted_comment.id),
90 ..CommentForm::default()
93 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
95 let comment_like = CommentLikeForm {
96 comment_id: inserted_comment.id,
97 post_id: inserted_post.id,
98 person_id: inserted_person.id,
102 CommentLike::like(&conn, &comment_like).unwrap();
104 let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
106 assert_eq!(1, comment_aggs_before_delete.score);
107 assert_eq!(1, comment_aggs_before_delete.upvotes);
108 assert_eq!(0, comment_aggs_before_delete.downvotes);
110 // Add a post dislike from the other person
111 let comment_dislike = CommentLikeForm {
112 comment_id: inserted_comment.id,
113 post_id: inserted_post.id,
114 person_id: another_inserted_person.id,
118 CommentLike::like(&conn, &comment_dislike).unwrap();
120 let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
122 assert_eq!(0, comment_aggs_after_dislike.score);
123 assert_eq!(1, comment_aggs_after_dislike.upvotes);
124 assert_eq!(1, comment_aggs_after_dislike.downvotes);
126 // Remove the first comment like
127 CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
128 let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
129 assert_eq!(-1, after_like_remove.score);
130 assert_eq!(0, after_like_remove.upvotes);
131 assert_eq!(1, after_like_remove.downvotes);
133 // Remove the parent post
134 Post::delete(&conn, inserted_post.id).unwrap();
136 // Should be none found, since the post was deleted
137 let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
138 assert!(after_delete.is_err());
140 // This should delete all the associated rows, and fire triggers
141 Person::delete(&conn, another_inserted_person.id).unwrap();
142 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
143 assert_eq!(1, person_num_deleted);
145 // Delete the community
146 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
147 assert_eq!(1, community_num_deleted);