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