1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::person_aggregates, PersonId};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "person_aggregates"]
7 pub struct PersonAggregates {
9 pub person_id: PersonId,
12 pub comment_count: i64,
13 pub comment_score: i64,
16 impl PersonAggregates {
17 pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
18 person_aggregates::table
19 .filter(person_aggregates::person_id.eq(person_id))
27 aggregates::person_aggregates::PersonAggregates,
28 establish_unpooled_connection,
32 use lemmy_db_schema::source::{
33 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
34 community::{Community, CommunityForm},
35 person::{Person, PersonForm},
36 post::{Post, PostForm, PostLike, PostLikeForm},
38 use serial_test::serial;
43 let conn = establish_unpooled_connection();
45 let new_person = PersonForm {
46 name: "thommy_user_agg".into(),
47 preferred_username: None,
59 last_refreshed_at: None,
61 shared_inbox_url: None,
65 let inserted_person = Person::create(&conn, &new_person).unwrap();
67 let another_person = PersonForm {
68 name: "jerry_user_agg".into(),
69 preferred_username: None,
81 last_refreshed_at: None,
83 shared_inbox_url: None,
87 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
89 let new_community = CommunityForm {
90 name: "TIL_site_agg".into(),
91 creator_id: inserted_person.id,
92 title: "nada".to_owned(),
102 last_refreshed_at: None,
108 shared_inbox_url: None,
111 let inserted_community = Community::create(&conn, &new_community).unwrap();
113 let new_post = PostForm {
114 name: "A test post".into(),
117 creator_id: inserted_person.id,
118 community_id: inserted_community.id,
126 embed_description: None,
134 let inserted_post = Post::create(&conn, &new_post).unwrap();
136 let post_like = PostLikeForm {
137 post_id: inserted_post.id,
138 person_id: inserted_person.id,
142 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
144 let comment_form = CommentForm {
145 content: "A test comment".into(),
146 creator_id: inserted_person.id,
147 post_id: inserted_post.id,
158 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
160 let mut comment_like = CommentLikeForm {
161 comment_id: inserted_comment.id,
162 person_id: inserted_person.id,
163 post_id: inserted_post.id,
167 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
169 let mut child_comment_form = CommentForm {
170 content: "A test comment".into(),
171 creator_id: inserted_person.id,
172 post_id: inserted_post.id,
176 parent_id: Some(inserted_comment.id),
183 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
185 let child_comment_like = CommentLikeForm {
186 comment_id: inserted_child_comment.id,
187 person_id: another_inserted_person.id,
188 post_id: inserted_post.id,
192 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
194 let person_aggregates_before_delete =
195 PersonAggregates::read(&conn, inserted_person.id).unwrap();
197 assert_eq!(1, person_aggregates_before_delete.post_count);
198 assert_eq!(1, person_aggregates_before_delete.post_score);
199 assert_eq!(2, person_aggregates_before_delete.comment_count);
200 assert_eq!(2, person_aggregates_before_delete.comment_score);
202 // Remove a post like
203 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
204 let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
205 assert_eq!(0, after_post_like_remove.post_score);
207 // Remove a parent comment (the scores should also be removed)
208 Comment::delete(&conn, inserted_comment.id).unwrap();
209 let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
210 assert_eq!(0, after_parent_comment_delete.comment_count);
211 assert_eq!(0, after_parent_comment_delete.comment_score);
213 // Add in the two comments again, then delete the post.
214 let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
215 child_comment_form.parent_id = Some(new_parent_comment.id);
216 Comment::create(&conn, &child_comment_form).unwrap();
217 comment_like.comment_id = new_parent_comment.id;
218 CommentLike::like(&conn, &comment_like).unwrap();
219 let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
220 assert_eq!(2, after_comment_add.comment_count);
221 assert_eq!(1, after_comment_add.comment_score);
223 Post::delete(&conn, inserted_post.id).unwrap();
224 let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
225 assert_eq!(0, after_post_delete.comment_score);
226 assert_eq!(0, after_post_delete.comment_count);
227 assert_eq!(0, after_post_delete.post_score);
228 assert_eq!(0, after_post_delete.post_count);
230 // This should delete all the associated rows, and fire triggers
231 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
232 assert_eq!(1, person_num_deleted);
233 Person::delete(&conn, another_inserted_person.id).unwrap();
235 // Should be none found
236 let after_delete = PersonAggregates::read(&conn, inserted_person.id);
237 assert!(after_delete.is_err());