1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::comment_aggregates, CommentId};
3 use serde::{Deserialize, Serialize};
6 Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
8 #[table_name = "comment_aggregates"]
9 pub struct CommentAggregates {
11 pub comment_id: CommentId,
15 pub published: chrono::NaiveDateTime,
18 impl CommentAggregates {
19 pub fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
20 comment_aggregates::table
21 .filter(comment_aggregates::comment_id.eq(comment_id))
29 aggregates::comment_aggregates::CommentAggregates,
30 establish_unpooled_connection,
34 use lemmy_db_schema::source::{
35 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36 community::{Community, CommunityForm},
37 person::{Person, PersonForm},
38 post::{Post, PostForm},
40 use serial_test::serial;
45 let conn = establish_unpooled_connection();
47 let new_person = PersonForm {
48 name: "thommy_comment_agg".into(),
49 ..PersonForm::default()
52 let inserted_person = Person::create(&conn, &new_person).unwrap();
54 let another_person = PersonForm {
55 name: "jerry_comment_agg".into(),
56 ..PersonForm::default()
59 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
61 let new_community = CommunityForm {
62 name: "TIL_comment_agg".into(),
63 title: "nada".to_owned(),
64 ..CommunityForm::default()
67 let inserted_community = Community::create(&conn, &new_community).unwrap();
69 let new_post = PostForm {
70 name: "A test post".into(),
71 creator_id: inserted_person.id,
72 community_id: inserted_community.id,
76 let inserted_post = Post::create(&conn, &new_post).unwrap();
78 let comment_form = CommentForm {
79 content: "A test comment".into(),
80 creator_id: inserted_person.id,
81 post_id: inserted_post.id,
82 ..CommentForm::default()
85 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
87 let child_comment_form = CommentForm {
88 content: "A test comment".into(),
89 creator_id: inserted_person.id,
90 post_id: inserted_post.id,
91 parent_id: Some(inserted_comment.id),
92 ..CommentForm::default()
95 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
97 let comment_like = CommentLikeForm {
98 comment_id: inserted_comment.id,
99 post_id: inserted_post.id,
100 person_id: inserted_person.id,
104 CommentLike::like(&conn, &comment_like).unwrap();
106 let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
108 assert_eq!(1, comment_aggs_before_delete.score);
109 assert_eq!(1, comment_aggs_before_delete.upvotes);
110 assert_eq!(0, comment_aggs_before_delete.downvotes);
112 // Add a post dislike from the other person
113 let comment_dislike = CommentLikeForm {
114 comment_id: inserted_comment.id,
115 post_id: inserted_post.id,
116 person_id: another_inserted_person.id,
120 CommentLike::like(&conn, &comment_dislike).unwrap();
122 let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
124 assert_eq!(0, comment_aggs_after_dislike.score);
125 assert_eq!(1, comment_aggs_after_dislike.upvotes);
126 assert_eq!(1, comment_aggs_after_dislike.downvotes);
128 // Remove the first comment like
129 CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
130 let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
131 assert_eq!(-1, after_like_remove.score);
132 assert_eq!(0, after_like_remove.upvotes);
133 assert_eq!(1, after_like_remove.downvotes);
135 // Remove the parent post
136 Post::delete(&conn, inserted_post.id).unwrap();
138 // Should be none found, since the post was deleted
139 let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
140 assert!(after_delete.is_err());
142 // This should delete all the associated rows, and fire triggers
143 Person::delete(&conn, another_inserted_person.id).unwrap();
144 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
145 assert_eq!(1, person_num_deleted);
147 // Delete the community
148 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
149 assert_eq!(1, community_num_deleted);