]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/user_aggregates.rs
Support plain `cargo test` and disable unused doctests for speed
[lemmy.git] / crates / db_queries / src / aggregates / user_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::user_aggregates;
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "user_aggregates"]
7 pub struct UserAggregates {
8   pub id: i32,
9   pub user_id: i32,
10   pub post_count: i64,
11   pub post_score: i64,
12   pub comment_count: i64,
13   pub comment_score: i64,
14 }
15
16 impl UserAggregates {
17   pub fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
18     user_aggregates::table
19       .filter(user_aggregates::user_id.eq(user_id))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::user_aggregates::UserAggregates,
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, PostLike, PostLikeForm},
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_user_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_user_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_site_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 post_like = PostLikeForm {
157       post_id: inserted_post.id,
158       user_id: inserted_user.id,
159       score: 1,
160     };
161
162     let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
163
164     let comment_form = CommentForm {
165       content: "A test comment".into(),
166       creator_id: inserted_user.id,
167       post_id: inserted_post.id,
168       removed: None,
169       deleted: None,
170       read: None,
171       parent_id: None,
172       published: None,
173       updated: None,
174       ap_id: None,
175       local: true,
176     };
177
178     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
179
180     let mut comment_like = CommentLikeForm {
181       comment_id: inserted_comment.id,
182       user_id: inserted_user.id,
183       post_id: inserted_post.id,
184       score: 1,
185     };
186
187     let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
188
189     let mut child_comment_form = CommentForm {
190       content: "A test comment".into(),
191       creator_id: inserted_user.id,
192       post_id: inserted_post.id,
193       removed: None,
194       deleted: None,
195       read: None,
196       parent_id: Some(inserted_comment.id),
197       published: None,
198       updated: None,
199       ap_id: None,
200       local: true,
201     };
202
203     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
204
205     let child_comment_like = CommentLikeForm {
206       comment_id: inserted_child_comment.id,
207       user_id: another_inserted_user.id,
208       post_id: inserted_post.id,
209       score: 1,
210     };
211
212     let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
213
214     let user_aggregates_before_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
215
216     assert_eq!(1, user_aggregates_before_delete.post_count);
217     assert_eq!(1, user_aggregates_before_delete.post_score);
218     assert_eq!(2, user_aggregates_before_delete.comment_count);
219     assert_eq!(2, user_aggregates_before_delete.comment_score);
220
221     // Remove a post like
222     PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
223     let after_post_like_remove = UserAggregates::read(&conn, inserted_user.id).unwrap();
224     assert_eq!(0, after_post_like_remove.post_score);
225
226     // Remove a parent comment (the scores should also be removed)
227     Comment::delete(&conn, inserted_comment.id).unwrap();
228     let after_parent_comment_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
229     assert_eq!(0, after_parent_comment_delete.comment_count);
230     assert_eq!(0, after_parent_comment_delete.comment_score);
231
232     // Add in the two comments again, then delete the post.
233     let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
234     child_comment_form.parent_id = Some(new_parent_comment.id);
235     Comment::create(&conn, &child_comment_form).unwrap();
236     comment_like.comment_id = new_parent_comment.id;
237     CommentLike::like(&conn, &comment_like).unwrap();
238     let after_comment_add = UserAggregates::read(&conn, inserted_user.id).unwrap();
239     assert_eq!(2, after_comment_add.comment_count);
240     assert_eq!(1, after_comment_add.comment_score);
241
242     Post::delete(&conn, inserted_post.id).unwrap();
243     let after_post_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
244     assert_eq!(0, after_post_delete.comment_score);
245     assert_eq!(0, after_post_delete.comment_count);
246     assert_eq!(0, after_post_delete.post_score);
247     assert_eq!(0, after_post_delete.post_count);
248
249     // This should delete all the associated rows, and fire triggers
250     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
251     assert_eq!(1, user_num_deleted);
252     User_::delete(&conn, another_inserted_user.id).unwrap();
253
254     // Should be none found
255     let after_delete = UserAggregates::read(&conn, inserted_user.id);
256     assert!(after_delete.is_err());
257   }
258 }