1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::community_aggregates;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "community_aggregates"]
7 pub struct CommunityAggregates {
13 pub published: chrono::NaiveDateTime,
16 impl CommunityAggregates {
17 pub fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
18 community_aggregates::table
19 .filter(community_aggregates::community_id.eq(community_id))
27 aggregates::community_aggregates::CommunityAggregates,
28 establish_unpooled_connection,
34 use lemmy_db_schema::source::{
35 comment::{Comment, CommentForm},
36 community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
37 post::{Post, PostForm},
38 user::{UserForm, User_},
43 let conn = establish_unpooled_connection();
45 let new_user = UserForm {
46 name: "thommy_community_agg".into(),
47 preferred_username: None,
48 password_encrypted: "nope".into(),
58 theme: "browser".into(),
59 default_sort_type: SortType::Hot as i16,
60 default_listing_type: ListingType::Subscribed as i16,
61 lang: "browser".into(),
63 send_notifications_to_email: false,
69 last_refreshed_at: None,
72 let inserted_user = User_::create(&conn, &new_user).unwrap();
74 let another_user = UserForm {
75 name: "jerry_community_agg".into(),
76 preferred_username: None,
77 password_encrypted: "nope".into(),
87 theme: "browser".into(),
88 default_sort_type: SortType::Hot as i16,
89 default_listing_type: ListingType::Subscribed as i16,
90 lang: "browser".into(),
92 send_notifications_to_email: false,
98 last_refreshed_at: None,
101 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
103 let new_community = CommunityForm {
104 name: "TIL_community_agg".into(),
105 creator_id: inserted_user.id,
106 title: "nada".to_owned(),
117 last_refreshed_at: None,
123 let inserted_community = Community::create(&conn, &new_community).unwrap();
125 let another_community = CommunityForm {
126 name: "TIL_community_agg_2".into(),
127 creator_id: inserted_user.id,
128 title: "nada".to_owned(),
139 last_refreshed_at: None,
145 let another_inserted_community = Community::create(&conn, &another_community).unwrap();
147 let first_user_follow = CommunityFollowerForm {
148 community_id: inserted_community.id,
149 user_id: inserted_user.id,
153 CommunityFollower::follow(&conn, &first_user_follow).unwrap();
155 let second_user_follow = CommunityFollowerForm {
156 community_id: inserted_community.id,
157 user_id: another_inserted_user.id,
161 CommunityFollower::follow(&conn, &second_user_follow).unwrap();
163 let another_community_follow = CommunityFollowerForm {
164 community_id: another_inserted_community.id,
165 user_id: inserted_user.id,
169 CommunityFollower::follow(&conn, &another_community_follow).unwrap();
171 let new_post = PostForm {
172 name: "A test post".into(),
175 creator_id: inserted_user.id,
176 community_id: inserted_community.id,
184 embed_description: None,
192 let inserted_post = Post::create(&conn, &new_post).unwrap();
194 let comment_form = CommentForm {
195 content: "A test comment".into(),
196 creator_id: inserted_user.id,
197 post_id: inserted_post.id,
208 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
210 let child_comment_form = CommentForm {
211 content: "A test comment".into(),
212 creator_id: inserted_user.id,
213 post_id: inserted_post.id,
217 parent_id: Some(inserted_comment.id),
224 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
226 let community_aggregates_before_delete =
227 CommunityAggregates::read(&conn, inserted_community.id).unwrap();
229 assert_eq!(2, community_aggregates_before_delete.subscribers);
230 assert_eq!(1, community_aggregates_before_delete.posts);
231 assert_eq!(2, community_aggregates_before_delete.comments);
233 // Test the other community
234 let another_community_aggs =
235 CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
236 assert_eq!(1, another_community_aggs.subscribers);
237 assert_eq!(0, another_community_aggs.posts);
238 assert_eq!(0, another_community_aggs.comments);
241 CommunityFollower::unfollow(&conn, &second_user_follow).unwrap();
242 let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
243 assert_eq!(1, after_unfollow.subscribers);
245 // Follow again just for the later tests
246 CommunityFollower::follow(&conn, &second_user_follow).unwrap();
247 let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
248 assert_eq!(2, after_follow_again.subscribers);
250 // Remove a parent comment (the comment count should also be 0)
251 Post::delete(&conn, inserted_post.id).unwrap();
252 let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
253 assert_eq!(0, after_parent_post_delete.comments);
254 assert_eq!(0, after_parent_post_delete.posts);
256 // Remove the 2nd user
257 User_::delete(&conn, another_inserted_user.id).unwrap();
258 let after_user_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
259 assert_eq!(1, after_user_delete.subscribers);
261 // This should delete all the associated rows, and fire triggers
262 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
263 assert_eq!(1, user_num_deleted);
265 // Should be none found, since the creator was deleted
266 let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
267 assert!(after_delete.is_err());