1 use crate::schema::user_aggregates;
2 use diesel::{result::Error, *};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "user_aggregates"]
7 pub struct UserAggregates {
12 pub comment_count: i64,
13 pub comment_score: i64,
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))
27 aggregates::user_aggregates::UserAggregates,
28 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
29 community::{Community, CommunityForm},
30 post::{Post, PostForm, PostLike, PostLikeForm},
31 tests::establish_unpooled_connection,
32 user::{UserForm, User_},
41 let conn = establish_unpooled_connection();
43 let new_user = UserForm {
44 name: "thommy_user_agg".into(),
45 preferred_username: None,
46 password_encrypted: "nope".into(),
56 theme: "browser".into(),
57 default_sort_type: SortType::Hot as i16,
58 default_listing_type: ListingType::Subscribed as i16,
59 lang: "browser".into(),
61 send_notifications_to_email: false,
67 last_refreshed_at: None,
70 let inserted_user = User_::create(&conn, &new_user).unwrap();
72 let another_user = UserForm {
73 name: "jerry_user_agg".into(),
74 preferred_username: None,
75 password_encrypted: "nope".into(),
85 theme: "browser".into(),
86 default_sort_type: SortType::Hot as i16,
87 default_listing_type: ListingType::Subscribed as i16,
88 lang: "browser".into(),
90 send_notifications_to_email: false,
96 last_refreshed_at: None,
99 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
101 let new_community = CommunityForm {
102 name: "TIL_site_agg".into(),
103 creator_id: inserted_user.id,
104 title: "nada".to_owned(),
115 last_refreshed_at: None,
121 let inserted_community = Community::create(&conn, &new_community).unwrap();
123 let new_post = PostForm {
124 name: "A test post".into(),
127 creator_id: inserted_user.id,
128 community_id: inserted_community.id,
136 embed_description: None,
144 let inserted_post = Post::create(&conn, &new_post).unwrap();
146 let post_like = PostLikeForm {
147 post_id: inserted_post.id,
148 user_id: inserted_user.id,
152 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
154 let comment_form = CommentForm {
155 content: "A test comment".into(),
156 creator_id: inserted_user.id,
157 post_id: inserted_post.id,
168 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
170 let comment_like = CommentLikeForm {
171 comment_id: inserted_comment.id,
172 user_id: inserted_user.id,
173 post_id: inserted_post.id,
177 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
179 let child_comment_form = CommentForm {
180 content: "A test comment".into(),
181 creator_id: inserted_user.id,
182 post_id: inserted_post.id,
186 parent_id: Some(inserted_comment.id),
193 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
195 let child_comment_like = CommentLikeForm {
196 comment_id: inserted_child_comment.id,
197 user_id: another_inserted_user.id,
198 post_id: inserted_post.id,
202 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
204 let user_aggregates_before_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
206 assert_eq!(1, user_aggregates_before_delete.post_count);
207 assert_eq!(1, user_aggregates_before_delete.post_score);
208 assert_eq!(2, user_aggregates_before_delete.comment_count);
209 assert_eq!(2, user_aggregates_before_delete.comment_score);
211 // Remove a post like
212 PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
213 let after_post_like_remove = UserAggregates::read(&conn, inserted_user.id).unwrap();
214 assert_eq!(0, after_post_like_remove.post_score);
216 // Remove a parent comment (the scores should also be removed)
217 Comment::delete(&conn, inserted_comment.id).unwrap();
218 let after_parent_comment_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
219 assert_eq!(0, after_parent_comment_delete.comment_count);
220 assert_eq!(0, after_parent_comment_delete.comment_score);
222 // This should delete all the associated rows, and fire triggers
223 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
224 assert_eq!(1, user_num_deleted);
225 User_::delete(&conn, another_inserted_user.id).unwrap();
227 // Should be none found
228 let after_delete = UserAggregates::read(&conn, inserted_user.id);
229 assert!(after_delete.is_err());