1 use crate::schema::community_aggregates;
2 use diesel::{result::Error, *};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "community_aggregates"]
7 pub struct CommunityAggregates {
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))
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_},
40 let conn = establish_unpooled_connection();
42 let new_user = UserForm {
43 name: "thommy_community_agg".into(),
44 preferred_username: None,
45 password_encrypted: "nope".into(),
55 theme: "browser".into(),
56 default_sort_type: SortType::Hot as i16,
57 default_listing_type: ListingType::Subscribed as i16,
58 lang: "browser".into(),
60 send_notifications_to_email: false,
66 last_refreshed_at: None,
69 let inserted_user = User_::create(&conn, &new_user).unwrap();
71 let another_user = UserForm {
72 name: "jerry_community_agg".into(),
73 preferred_username: None,
74 password_encrypted: "nope".into(),
84 theme: "browser".into(),
85 default_sort_type: SortType::Hot as i16,
86 default_listing_type: ListingType::Subscribed as i16,
87 lang: "browser".into(),
89 send_notifications_to_email: false,
95 last_refreshed_at: None,
98 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
100 let new_community = CommunityForm {
101 name: "TIL_community_agg".into(),
102 creator_id: inserted_user.id,
103 title: "nada".to_owned(),
114 last_refreshed_at: None,
120 let inserted_community = Community::create(&conn, &new_community).unwrap();
122 let another_community = CommunityForm {
123 name: "TIL_community_agg_2".into(),
124 creator_id: inserted_user.id,
125 title: "nada".to_owned(),
136 last_refreshed_at: None,
142 let another_inserted_community = Community::create(&conn, &another_community).unwrap();
144 let first_user_follow = CommunityFollowerForm {
145 community_id: inserted_community.id,
146 user_id: inserted_user.id,
150 CommunityFollower::follow(&conn, &first_user_follow).unwrap();
152 let second_user_follow = CommunityFollowerForm {
153 community_id: inserted_community.id,
154 user_id: another_inserted_user.id,
158 CommunityFollower::follow(&conn, &second_user_follow).unwrap();
160 let another_community_follow = CommunityFollowerForm {
161 community_id: another_inserted_community.id,
162 user_id: inserted_user.id,
166 CommunityFollower::follow(&conn, &another_community_follow).unwrap();
168 let new_post = PostForm {
169 name: "A test post".into(),
172 creator_id: inserted_user.id,
173 community_id: inserted_community.id,
181 embed_description: None,
189 let inserted_post = Post::create(&conn, &new_post).unwrap();
191 let comment_form = CommentForm {
192 content: "A test comment".into(),
193 creator_id: inserted_user.id,
194 post_id: inserted_post.id,
205 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
207 let child_comment_form = CommentForm {
208 content: "A test comment".into(),
209 creator_id: inserted_user.id,
210 post_id: inserted_post.id,
214 parent_id: Some(inserted_comment.id),
221 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
223 let community_aggregates_before_delete =
224 CommunityAggregates::read(&conn, inserted_community.id).unwrap();
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);
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);
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);
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);
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);
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);
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);
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());