]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/person_aggregates.rs
56d72d38b076b974725b83d975936c887ddb56b5
[lemmy.git] / crates / db_queries / src / aggregates / person_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::person_aggregates, PersonId};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "person_aggregates"]
7 pub struct PersonAggregates {
8   pub id: i32,
9   pub person_id: PersonId,
10   pub post_count: i64,
11   pub post_score: i64,
12   pub comment_count: i64,
13   pub comment_score: i64,
14 }
15
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))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::person_aggregates::PersonAggregates,
28     establish_unpooled_connection,
29     Crud,
30     Likeable,
31   };
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},
37   };
38   use serial_test::serial;
39
40   #[test]
41   #[serial]
42   fn test_crud() {
43     let conn = establish_unpooled_connection();
44
45     let new_person = PersonForm {
46       name: "thommy_user_agg".into(),
47       ..PersonForm::default()
48     };
49
50     let inserted_person = Person::create(&conn, &new_person).unwrap();
51
52     let another_person = PersonForm {
53       name: "jerry_user_agg".into(),
54       ..PersonForm::default()
55     };
56
57     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
58
59     let new_community = CommunityForm {
60       name: "TIL_site_agg".into(),
61       title: "nada".to_owned(),
62       ..CommunityForm::default()
63     };
64
65     let inserted_community = Community::create(&conn, &new_community).unwrap();
66
67     let new_post = PostForm {
68       name: "A test post".into(),
69       creator_id: inserted_person.id,
70       community_id: inserted_community.id,
71       ..PostForm::default()
72     };
73
74     let inserted_post = Post::create(&conn, &new_post).unwrap();
75
76     let post_like = PostLikeForm {
77       post_id: inserted_post.id,
78       person_id: inserted_person.id,
79       score: 1,
80     };
81
82     let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
83
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()
89     };
90
91     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
92
93     let mut comment_like = CommentLikeForm {
94       comment_id: inserted_comment.id,
95       person_id: inserted_person.id,
96       post_id: inserted_post.id,
97       score: 1,
98     };
99
100     let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
101
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()
108     };
109
110     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
111
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,
116       score: 1,
117     };
118
119     let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
120
121     let person_aggregates_before_delete =
122       PersonAggregates::read(&conn, inserted_person.id).unwrap();
123
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);
128
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);
133
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);
139
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);
149
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);
156
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();
161
162     // Delete the community
163     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
164     assert_eq!(1, community_num_deleted);
165
166     // Should be none found
167     let after_delete = PersonAggregates::read(&conn, inserted_person.id);
168     assert!(after_delete.is_err());
169   }
170 }