1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::person_aggregates, PersonId};
3 use serde::{Deserialize, Serialize};
6 Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
8 #[table_name = "person_aggregates"]
9 pub struct PersonAggregates {
11 pub person_id: PersonId,
14 pub comment_count: i64,
15 pub comment_score: i64,
18 impl PersonAggregates {
19 pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
20 person_aggregates::table
21 .filter(person_aggregates::person_id.eq(person_id))
29 aggregates::person_aggregates::PersonAggregates,
30 establish_unpooled_connection,
34 use lemmy_db_schema::source::{
35 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36 community::{Community, CommunityForm},
37 person::{Person, PersonForm},
38 post::{Post, PostForm, PostLike, PostLikeForm},
40 use serial_test::serial;
45 let conn = establish_unpooled_connection();
47 let new_person = PersonForm {
48 name: "thommy_user_agg".into(),
49 ..PersonForm::default()
52 let inserted_person = Person::create(&conn, &new_person).unwrap();
54 let another_person = PersonForm {
55 name: "jerry_user_agg".into(),
56 ..PersonForm::default()
59 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
61 let new_community = CommunityForm {
62 name: "TIL_site_agg".into(),
63 title: "nada".to_owned(),
64 ..CommunityForm::default()
67 let inserted_community = Community::create(&conn, &new_community).unwrap();
69 let new_post = PostForm {
70 name: "A test post".into(),
71 creator_id: inserted_person.id,
72 community_id: inserted_community.id,
76 let inserted_post = Post::create(&conn, &new_post).unwrap();
78 let post_like = PostLikeForm {
79 post_id: inserted_post.id,
80 person_id: inserted_person.id,
84 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
86 let comment_form = CommentForm {
87 content: "A test comment".into(),
88 creator_id: inserted_person.id,
89 post_id: inserted_post.id,
90 ..CommentForm::default()
93 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
95 let mut comment_like = CommentLikeForm {
96 comment_id: inserted_comment.id,
97 person_id: inserted_person.id,
98 post_id: inserted_post.id,
102 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
104 let mut child_comment_form = CommentForm {
105 content: "A test comment".into(),
106 creator_id: inserted_person.id,
107 post_id: inserted_post.id,
108 parent_id: Some(inserted_comment.id),
109 ..CommentForm::default()
112 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
114 let child_comment_like = CommentLikeForm {
115 comment_id: inserted_child_comment.id,
116 person_id: another_inserted_person.id,
117 post_id: inserted_post.id,
121 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
123 let person_aggregates_before_delete =
124 PersonAggregates::read(&conn, inserted_person.id).unwrap();
126 assert_eq!(1, person_aggregates_before_delete.post_count);
127 assert_eq!(1, person_aggregates_before_delete.post_score);
128 assert_eq!(2, person_aggregates_before_delete.comment_count);
129 assert_eq!(2, person_aggregates_before_delete.comment_score);
131 // Remove a post like
132 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
133 let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
134 assert_eq!(0, after_post_like_remove.post_score);
136 // Remove a parent comment (the scores should also be removed)
137 Comment::delete(&conn, inserted_comment.id).unwrap();
138 let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
139 assert_eq!(0, after_parent_comment_delete.comment_count);
140 assert_eq!(0, after_parent_comment_delete.comment_score);
142 // Add in the two comments again, then delete the post.
143 let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
144 child_comment_form.parent_id = Some(new_parent_comment.id);
145 Comment::create(&conn, &child_comment_form).unwrap();
146 comment_like.comment_id = new_parent_comment.id;
147 CommentLike::like(&conn, &comment_like).unwrap();
148 let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
149 assert_eq!(2, after_comment_add.comment_count);
150 assert_eq!(1, after_comment_add.comment_score);
152 Post::delete(&conn, inserted_post.id).unwrap();
153 let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
154 assert_eq!(0, after_post_delete.comment_score);
155 assert_eq!(0, after_post_delete.comment_count);
156 assert_eq!(0, after_post_delete.post_score);
157 assert_eq!(0, after_post_delete.post_count);
159 // This should delete all the associated rows, and fire triggers
160 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
161 assert_eq!(1, person_num_deleted);
162 Person::delete(&conn, another_inserted_person.id).unwrap();
164 // Delete the community
165 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
166 assert_eq!(1, community_num_deleted);
168 // Should be none found
169 let after_delete = PersonAggregates::read(&conn, inserted_person.id);
170 assert!(after_delete.is_err());