1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::person_aggregates;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "person_aggregates"]
7 pub struct PersonAggregates {
12 pub comment_count: i64,
13 pub comment_score: i64,
16 impl PersonAggregates {
17 pub fn read(conn: &PgConnection, person_id: i32) -> 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,
34 use lemmy_db_schema::source::{
35 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36 community::{Community, CommunityForm},
37 post::{Post, PostForm, PostLike, PostLikeForm},
38 person::{PersonForm, Person},
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,
64 let inserted_person = Person::create(&conn, &new_person).unwrap();
66 let another_person = PersonForm {
67 name: "jerry_user_agg".into(),
68 preferred_username: None,
80 last_refreshed_at: None,
82 shared_inbox_url: None,
85 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
87 let new_community = CommunityForm {
88 name: "TIL_site_agg".into(),
89 creator_id: inserted_person.id,
90 title: "nada".to_owned(),
100 last_refreshed_at: None,
106 shared_inbox_url: None,
109 let inserted_community = Community::create(&conn, &new_community).unwrap();
111 let new_post = PostForm {
112 name: "A test post".into(),
115 creator_id: inserted_person.id,
116 community_id: inserted_community.id,
124 embed_description: None,
132 let inserted_post = Post::create(&conn, &new_post).unwrap();
134 let post_like = PostLikeForm {
135 post_id: inserted_post.id,
136 person_id: inserted_person.id,
140 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
142 let comment_form = CommentForm {
143 content: "A test comment".into(),
144 creator_id: inserted_person.id,
145 post_id: inserted_post.id,
156 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
158 let mut comment_like = CommentLikeForm {
159 comment_id: inserted_comment.id,
160 person_id: inserted_person.id,
161 post_id: inserted_post.id,
165 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
167 let mut child_comment_form = CommentForm {
168 content: "A test comment".into(),
169 creator_id: inserted_person.id,
170 post_id: inserted_post.id,
174 parent_id: Some(inserted_comment.id),
181 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
183 let child_comment_like = CommentLikeForm {
184 comment_id: inserted_child_comment.id,
185 person_id: another_inserted_person.id,
186 post_id: inserted_post.id,
190 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
192 let person_aggregates_before_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
194 assert_eq!(1, person_aggregates_before_delete.post_count);
195 assert_eq!(1, person_aggregates_before_delete.post_score);
196 assert_eq!(2, person_aggregates_before_delete.comment_count);
197 assert_eq!(2, person_aggregates_before_delete.comment_score);
199 // Remove a post like
200 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
201 let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
202 assert_eq!(0, after_post_like_remove.post_score);
204 // Remove a parent comment (the scores should also be removed)
205 Comment::delete(&conn, inserted_comment.id).unwrap();
206 let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
207 assert_eq!(0, after_parent_comment_delete.comment_count);
208 assert_eq!(0, after_parent_comment_delete.comment_score);
210 // Add in the two comments again, then delete the post.
211 let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
212 child_comment_form.parent_id = Some(new_parent_comment.id);
213 Comment::create(&conn, &child_comment_form).unwrap();
214 comment_like.comment_id = new_parent_comment.id;
215 CommentLike::like(&conn, &comment_like).unwrap();
216 let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
217 assert_eq!(2, after_comment_add.comment_count);
218 assert_eq!(1, after_comment_add.comment_score);
220 Post::delete(&conn, inserted_post.id).unwrap();
221 let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
222 assert_eq!(0, after_post_delete.comment_score);
223 assert_eq!(0, after_post_delete.comment_count);
224 assert_eq!(0, after_post_delete.post_score);
225 assert_eq!(0, after_post_delete.post_count);
227 // This should delete all the associated rows, and fire triggers
228 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
229 assert_eq!(1, person_num_deleted);
230 Person::delete(&conn, another_inserted_person.id).unwrap();
232 // Should be none found
233 let after_delete = PersonAggregates::read(&conn, inserted_person.id);
234 assert!(after_delete.is_err());