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 ..PersonForm::default()
50 let inserted_person = Person::create(&conn, &new_person).unwrap();
52 let another_person = PersonForm {
53 name: "jerry_user_agg".into(),
54 ..PersonForm::default()
57 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
59 let new_community = CommunityForm {
60 name: "TIL_site_agg".into(),
61 title: "nada".to_owned(),
62 ..CommunityForm::default()
65 let inserted_community = Community::create(&conn, &new_community).unwrap();
67 let new_post = PostForm {
68 name: "A test post".into(),
69 creator_id: inserted_person.id,
70 community_id: inserted_community.id,
74 let inserted_post = Post::create(&conn, &new_post).unwrap();
76 let post_like = PostLikeForm {
77 post_id: inserted_post.id,
78 person_id: inserted_person.id,
82 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
84 let comment_form = CommentForm {
85 content: "A test comment".into(),
86 creator_id: inserted_person.id,
87 post_id: inserted_post.id,
88 ..CommentForm::default()
91 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
93 let mut comment_like = CommentLikeForm {
94 comment_id: inserted_comment.id,
95 person_id: inserted_person.id,
96 post_id: inserted_post.id,
100 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
102 let mut child_comment_form = CommentForm {
103 content: "A test comment".into(),
104 creator_id: inserted_person.id,
105 post_id: inserted_post.id,
106 parent_id: Some(inserted_comment.id),
107 ..CommentForm::default()
110 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
112 let child_comment_like = CommentLikeForm {
113 comment_id: inserted_child_comment.id,
114 person_id: another_inserted_person.id,
115 post_id: inserted_post.id,
119 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
121 let person_aggregates_before_delete =
122 PersonAggregates::read(&conn, inserted_person.id).unwrap();
124 assert_eq!(1, person_aggregates_before_delete.post_count);
125 assert_eq!(1, person_aggregates_before_delete.post_score);
126 assert_eq!(2, person_aggregates_before_delete.comment_count);
127 assert_eq!(2, person_aggregates_before_delete.comment_score);
129 // Remove a post like
130 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
131 let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
132 assert_eq!(0, after_post_like_remove.post_score);
134 // Remove a parent comment (the scores should also be removed)
135 Comment::delete(&conn, inserted_comment.id).unwrap();
136 let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
137 assert_eq!(0, after_parent_comment_delete.comment_count);
138 assert_eq!(0, after_parent_comment_delete.comment_score);
140 // Add in the two comments again, then delete the post.
141 let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
142 child_comment_form.parent_id = Some(new_parent_comment.id);
143 Comment::create(&conn, &child_comment_form).unwrap();
144 comment_like.comment_id = new_parent_comment.id;
145 CommentLike::like(&conn, &comment_like).unwrap();
146 let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
147 assert_eq!(2, after_comment_add.comment_count);
148 assert_eq!(1, after_comment_add.comment_score);
150 Post::delete(&conn, inserted_post.id).unwrap();
151 let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
152 assert_eq!(0, after_post_delete.comment_score);
153 assert_eq!(0, after_post_delete.comment_count);
154 assert_eq!(0, after_post_delete.post_score);
155 assert_eq!(0, after_post_delete.post_count);
157 // This should delete all the associated rows, and fire triggers
158 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
159 assert_eq!(1, person_num_deleted);
160 Person::delete(&conn, another_inserted_person.id).unwrap();
162 // Delete the community
163 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
164 assert_eq!(1, community_num_deleted);
166 // Should be none found
167 let after_delete = PersonAggregates::read(&conn, inserted_person.id);
168 assert!(after_delete.is_err());