]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/comment_aggregates.rs
Support plain `cargo test` and disable unused doctests for speed
[lemmy.git] / crates / db_queries / src / aggregates / comment_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::comment_aggregates;
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: i32,
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: i32) -> 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     ListingType,
32     SortType,
33   };
34   use lemmy_db_schema::source::{
35     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36     community::{Community, CommunityForm},
37     post::{Post, PostForm},
38     user::{UserForm, User_},
39   };
40   use serial_test::serial;
41
42   #[test]
43   #[serial]
44   fn test_crud() {
45     let conn = establish_unpooled_connection();
46
47     let new_user = UserForm {
48       name: "thommy_comment_agg".into(),
49       preferred_username: None,
50       password_encrypted: "nope".into(),
51       email: None,
52       matrix_user_id: None,
53       avatar: None,
54       banner: None,
55       admin: false,
56       banned: Some(false),
57       published: None,
58       updated: None,
59       show_nsfw: false,
60       theme: "browser".into(),
61       default_sort_type: SortType::Hot as i16,
62       default_listing_type: ListingType::Subscribed as i16,
63       lang: "browser".into(),
64       show_avatars: true,
65       send_notifications_to_email: false,
66       actor_id: None,
67       bio: None,
68       local: true,
69       private_key: None,
70       public_key: None,
71       last_refreshed_at: None,
72       inbox_url: None,
73       shared_inbox_url: None,
74     };
75
76     let inserted_user = User_::create(&conn, &new_user).unwrap();
77
78     let another_user = UserForm {
79       name: "jerry_comment_agg".into(),
80       preferred_username: None,
81       password_encrypted: "nope".into(),
82       email: None,
83       matrix_user_id: None,
84       avatar: None,
85       banner: None,
86       admin: false,
87       banned: Some(false),
88       published: None,
89       updated: None,
90       show_nsfw: false,
91       theme: "browser".into(),
92       default_sort_type: SortType::Hot as i16,
93       default_listing_type: ListingType::Subscribed as i16,
94       lang: "browser".into(),
95       show_avatars: true,
96       send_notifications_to_email: false,
97       actor_id: None,
98       bio: None,
99       local: true,
100       private_key: None,
101       public_key: None,
102       last_refreshed_at: None,
103       inbox_url: None,
104       shared_inbox_url: None,
105     };
106
107     let another_inserted_user = User_::create(&conn, &another_user).unwrap();
108
109     let new_community = CommunityForm {
110       name: "TIL_comment_agg".into(),
111       creator_id: inserted_user.id,
112       title: "nada".to_owned(),
113       description: None,
114       nsfw: false,
115       removed: None,
116       deleted: None,
117       updated: None,
118       actor_id: None,
119       local: true,
120       private_key: None,
121       public_key: None,
122       last_refreshed_at: None,
123       published: None,
124       icon: None,
125       banner: None,
126       followers_url: None,
127       inbox_url: None,
128       shared_inbox_url: None,
129     };
130
131     let inserted_community = Community::create(&conn, &new_community).unwrap();
132
133     let new_post = PostForm {
134       name: "A test post".into(),
135       url: None,
136       body: None,
137       creator_id: inserted_user.id,
138       community_id: inserted_community.id,
139       removed: None,
140       deleted: None,
141       locked: None,
142       stickied: None,
143       nsfw: false,
144       updated: None,
145       embed_title: None,
146       embed_description: None,
147       embed_html: None,
148       thumbnail_url: None,
149       ap_id: None,
150       local: true,
151       published: None,
152     };
153
154     let inserted_post = Post::create(&conn, &new_post).unwrap();
155
156     let comment_form = CommentForm {
157       content: "A test comment".into(),
158       creator_id: inserted_user.id,
159       post_id: inserted_post.id,
160       removed: None,
161       deleted: None,
162       read: None,
163       parent_id: None,
164       published: None,
165       updated: None,
166       ap_id: None,
167       local: true,
168     };
169
170     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
171
172     let child_comment_form = CommentForm {
173       content: "A test comment".into(),
174       creator_id: inserted_user.id,
175       post_id: inserted_post.id,
176       removed: None,
177       deleted: None,
178       read: None,
179       parent_id: Some(inserted_comment.id),
180       published: None,
181       updated: None,
182       ap_id: None,
183       local: true,
184     };
185
186     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
187
188     let comment_like = CommentLikeForm {
189       comment_id: inserted_comment.id,
190       post_id: inserted_post.id,
191       user_id: inserted_user.id,
192       score: 1,
193     };
194
195     CommentLike::like(&conn, &comment_like).unwrap();
196
197     let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
198
199     assert_eq!(1, comment_aggs_before_delete.score);
200     assert_eq!(1, comment_aggs_before_delete.upvotes);
201     assert_eq!(0, comment_aggs_before_delete.downvotes);
202
203     // Add a post dislike from the other user
204     let comment_dislike = CommentLikeForm {
205       comment_id: inserted_comment.id,
206       post_id: inserted_post.id,
207       user_id: another_inserted_user.id,
208       score: -1,
209     };
210
211     CommentLike::like(&conn, &comment_dislike).unwrap();
212
213     let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
214
215     assert_eq!(0, comment_aggs_after_dislike.score);
216     assert_eq!(1, comment_aggs_after_dislike.upvotes);
217     assert_eq!(1, comment_aggs_after_dislike.downvotes);
218
219     // Remove the first comment like
220     CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
221     let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
222     assert_eq!(-1, after_like_remove.score);
223     assert_eq!(0, after_like_remove.upvotes);
224     assert_eq!(1, after_like_remove.downvotes);
225
226     // Remove the parent post
227     Post::delete(&conn, inserted_post.id).unwrap();
228
229     // Should be none found, since the post was deleted
230     let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
231     assert!(after_delete.is_err());
232
233     // This should delete all the associated rows, and fire triggers
234     User_::delete(&conn, another_inserted_user.id).unwrap();
235     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
236     assert_eq!(1, user_num_deleted);
237   }
238 }