]> Untitled Git - lemmy.git/blob - lemmy_db/src/aggregates/community_aggregates.rs
Adding tests for current aggregates.
[lemmy.git] / lemmy_db / src / aggregates / community_aggregates.rs
1 use crate::schema::community_aggregates;
2 use diesel::{result::Error, *};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "community_aggregates"]
7 pub struct CommunityAggregates {
8   pub id: i32,
9   pub community_id: i32,
10   pub subscribers: i64,
11   pub posts: i64,
12   pub comments: i64,
13 }
14
15 impl CommunityAggregates {
16   pub fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
17     community_aggregates::table
18       .filter(community_aggregates::community_id.eq(community_id))
19       .first::<Self>(conn)
20   }
21 }
22
23 #[cfg(test)]
24 mod tests {
25   use crate::{
26     aggregates::community_aggregates::CommunityAggregates,
27     comment::{Comment, CommentForm},
28     community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
29     post::{Post, PostForm},
30     tests::establish_unpooled_connection,
31     user::{UserForm, User_},
32     Crud,
33     Followable,
34     ListingType,
35     SortType,
36   };
37
38   #[test]
39   fn test_crud() {
40     let conn = establish_unpooled_connection();
41
42     let new_user = UserForm {
43       name: "thommy_community_agg".into(),
44       preferred_username: None,
45       password_encrypted: "nope".into(),
46       email: None,
47       matrix_user_id: None,
48       avatar: None,
49       banner: None,
50       admin: false,
51       banned: Some(false),
52       published: None,
53       updated: None,
54       show_nsfw: false,
55       theme: "browser".into(),
56       default_sort_type: SortType::Hot as i16,
57       default_listing_type: ListingType::Subscribed as i16,
58       lang: "browser".into(),
59       show_avatars: true,
60       send_notifications_to_email: false,
61       actor_id: None,
62       bio: None,
63       local: true,
64       private_key: None,
65       public_key: None,
66       last_refreshed_at: None,
67     };
68
69     let inserted_user = User_::create(&conn, &new_user).unwrap();
70
71     let another_user = UserForm {
72       name: "jerry_community_agg".into(),
73       preferred_username: None,
74       password_encrypted: "nope".into(),
75       email: None,
76       matrix_user_id: None,
77       avatar: None,
78       banner: None,
79       admin: false,
80       banned: Some(false),
81       published: None,
82       updated: None,
83       show_nsfw: false,
84       theme: "browser".into(),
85       default_sort_type: SortType::Hot as i16,
86       default_listing_type: ListingType::Subscribed as i16,
87       lang: "browser".into(),
88       show_avatars: true,
89       send_notifications_to_email: false,
90       actor_id: None,
91       bio: None,
92       local: true,
93       private_key: None,
94       public_key: None,
95       last_refreshed_at: None,
96     };
97
98     let another_inserted_user = User_::create(&conn, &another_user).unwrap();
99
100     let new_community = CommunityForm {
101       name: "TIL_community_agg".into(),
102       creator_id: inserted_user.id,
103       title: "nada".to_owned(),
104       description: None,
105       category_id: 1,
106       nsfw: false,
107       removed: None,
108       deleted: None,
109       updated: None,
110       actor_id: None,
111       local: true,
112       private_key: None,
113       public_key: None,
114       last_refreshed_at: None,
115       published: None,
116       icon: None,
117       banner: None,
118     };
119
120     let inserted_community = Community::create(&conn, &new_community).unwrap();
121
122     let another_community = CommunityForm {
123       name: "TIL_community_agg_2".into(),
124       creator_id: inserted_user.id,
125       title: "nada".to_owned(),
126       description: None,
127       category_id: 1,
128       nsfw: false,
129       removed: None,
130       deleted: None,
131       updated: None,
132       actor_id: None,
133       local: true,
134       private_key: None,
135       public_key: None,
136       last_refreshed_at: None,
137       published: None,
138       icon: None,
139       banner: None,
140     };
141
142     let another_inserted_community = Community::create(&conn, &another_community).unwrap();
143
144     let first_user_follow = CommunityFollowerForm {
145       community_id: inserted_community.id,
146       user_id: inserted_user.id,
147       pending: false,
148     };
149
150     CommunityFollower::follow(&conn, &first_user_follow).unwrap();
151
152     let second_user_follow = CommunityFollowerForm {
153       community_id: inserted_community.id,
154       user_id: another_inserted_user.id,
155       pending: false,
156     };
157
158     CommunityFollower::follow(&conn, &second_user_follow).unwrap();
159
160     let another_community_follow = CommunityFollowerForm {
161       community_id: another_inserted_community.id,
162       user_id: inserted_user.id,
163       pending: false,
164     };
165
166     CommunityFollower::follow(&conn, &another_community_follow).unwrap();
167
168     let new_post = PostForm {
169       name: "A test post".into(),
170       url: None,
171       body: None,
172       creator_id: inserted_user.id,
173       community_id: inserted_community.id,
174       removed: None,
175       deleted: None,
176       locked: None,
177       stickied: None,
178       nsfw: false,
179       updated: None,
180       embed_title: None,
181       embed_description: None,
182       embed_html: None,
183       thumbnail_url: None,
184       ap_id: None,
185       local: true,
186       published: None,
187     };
188
189     let inserted_post = Post::create(&conn, &new_post).unwrap();
190
191     let comment_form = CommentForm {
192       content: "A test comment".into(),
193       creator_id: inserted_user.id,
194       post_id: inserted_post.id,
195       removed: None,
196       deleted: None,
197       read: None,
198       parent_id: None,
199       published: None,
200       updated: None,
201       ap_id: None,
202       local: true,
203     };
204
205     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
206
207     let child_comment_form = CommentForm {
208       content: "A test comment".into(),
209       creator_id: inserted_user.id,
210       post_id: inserted_post.id,
211       removed: None,
212       deleted: None,
213       read: None,
214       parent_id: Some(inserted_comment.id),
215       published: None,
216       updated: None,
217       ap_id: None,
218       local: true,
219     };
220
221     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
222
223     let community_aggregates_before_delete =
224       CommunityAggregates::read(&conn, inserted_community.id).unwrap();
225
226     assert_eq!(2, community_aggregates_before_delete.subscribers);
227     assert_eq!(1, community_aggregates_before_delete.posts);
228     assert_eq!(2, community_aggregates_before_delete.comments);
229
230     // Test the other community
231     let another_community_aggs =
232       CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
233     assert_eq!(1, another_community_aggs.subscribers);
234     assert_eq!(0, another_community_aggs.posts);
235     assert_eq!(0, another_community_aggs.comments);
236
237     // Unfollow test
238     CommunityFollower::unfollow(&conn, &second_user_follow).unwrap();
239     let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
240     assert_eq!(1, after_unfollow.subscribers);
241
242     // Follow again just for the later tests
243     CommunityFollower::follow(&conn, &second_user_follow).unwrap();
244     let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
245     assert_eq!(2, after_follow_again.subscribers);
246
247     // Remove a parent comment (the comment count should also be 0)
248     Post::delete(&conn, inserted_post.id).unwrap();
249     let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
250     assert_eq!(0, after_parent_post_delete.comments);
251     assert_eq!(0, after_parent_post_delete.posts);
252
253     // Remove the 2nd user
254     User_::delete(&conn, another_inserted_user.id).unwrap();
255     let after_user_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
256     assert_eq!(1, after_user_delete.subscribers);
257
258     // This should delete all the associated rows, and fire triggers
259     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
260     assert_eq!(1, user_num_deleted);
261
262     // Should be none found, since the creator was deleted
263     let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
264     assert!(after_delete.is_err());
265   }
266 }