]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/comment_aggregates.rs
898c57da392e3b8b1eed89077e56629d9a1b10e5
[lemmy.git] / crates / db_queries / src / aggregates / comment_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::comment_aggregates, CommentId};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "comment_aggregates"]
7 pub struct CommentAggregates {
8   pub id: i32,
9   pub comment_id: CommentId,
10   pub score: i64,
11   pub upvotes: i64,
12   pub downvotes: i64,
13   pub published: chrono::NaiveDateTime,
14 }
15
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))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::comment_aggregates::CommentAggregates,
28     establish_unpooled_connection,
29     Crud,
30     Likeable,
31   };
32   use lemmy_db_schema::source::{
33     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
34     community::{Community, CommunityForm},
35     person::{Person, PersonForm},
36     post::{Post, PostForm},
37   };
38   use serial_test::serial;
39
40   #[test]
41   #[serial]
42   fn test_crud() {
43     let conn = establish_unpooled_connection();
44
45     let new_person = PersonForm {
46       name: "thommy_comment_agg".into(),
47       ..PersonForm::default()
48     };
49
50     let inserted_person = Person::create(&conn, &new_person).unwrap();
51
52     let another_person = PersonForm {
53       name: "jerry_comment_agg".into(),
54       ..PersonForm::default()
55     };
56
57     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
58
59     let new_community = CommunityForm {
60       name: "TIL_comment_agg".into(),
61       title: "nada".to_owned(),
62       ..CommunityForm::default()
63     };
64
65     let inserted_community = Community::create(&conn, &new_community).unwrap();
66
67     let new_post = PostForm {
68       name: "A test post".into(),
69       creator_id: inserted_person.id,
70       community_id: inserted_community.id,
71       ..PostForm::default()
72     };
73
74     let inserted_post = Post::create(&conn, &new_post).unwrap();
75
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()
81     };
82
83     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
84
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()
91     };
92
93     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
94
95     let comment_like = CommentLikeForm {
96       comment_id: inserted_comment.id,
97       post_id: inserted_post.id,
98       person_id: inserted_person.id,
99       score: 1,
100     };
101
102     CommentLike::like(&conn, &comment_like).unwrap();
103
104     let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
105
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);
109
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,
115       score: -1,
116     };
117
118     CommentLike::like(&conn, &comment_dislike).unwrap();
119
120     let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
121
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);
125
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);
132
133     // Remove the parent post
134     Post::delete(&conn, inserted_post.id).unwrap();
135
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());
139
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);
144
145     // Delete the community
146     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
147     assert_eq!(1, community_num_deleted);
148   }
149 }