]> Untitled Git - lemmy.git/blob - lemmy_db/src/aggregates/post_aggregates.rs
Adding tests for post aggregates.
[lemmy.git] / lemmy_db / src / aggregates / post_aggregates.rs
1 use crate::schema::post_aggregates;
2 use diesel::{result::Error, *};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "post_aggregates"]
7 pub struct PostAggregates {
8   pub id: i32,
9   pub post_id: i32,
10   pub comments: i64,
11   pub score: i64,
12   pub upvotes: i64,
13   pub downvotes: i64,
14   pub newest_comment_time: chrono::NaiveDateTime,
15 }
16
17 impl PostAggregates {
18   pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
19     post_aggregates::table
20       .filter(post_aggregates::post_id.eq(post_id))
21       .first::<Self>(conn)
22   }
23 }
24
25 #[cfg(test)]
26 mod tests {
27   use crate::{
28     aggregates::post_aggregates::PostAggregates,
29     comment::{Comment, CommentForm},
30     community::{Community, CommunityForm},
31     post::{Post, PostForm, PostLike, PostLikeForm},
32     tests::establish_unpooled_connection,
33     user::{UserForm, User_},
34     Crud,
35     Likeable,
36     ListingType,
37     SortType,
38   };
39
40   #[test]
41   fn test_crud() {
42     let conn = establish_unpooled_connection();
43
44     let new_user = UserForm {
45       name: "thommy_community_agg".into(),
46       preferred_username: None,
47       password_encrypted: "nope".into(),
48       email: None,
49       matrix_user_id: None,
50       avatar: None,
51       banner: None,
52       admin: false,
53       banned: Some(false),
54       published: None,
55       updated: None,
56       show_nsfw: false,
57       theme: "browser".into(),
58       default_sort_type: SortType::Hot as i16,
59       default_listing_type: ListingType::Subscribed as i16,
60       lang: "browser".into(),
61       show_avatars: true,
62       send_notifications_to_email: false,
63       actor_id: None,
64       bio: None,
65       local: true,
66       private_key: None,
67       public_key: None,
68       last_refreshed_at: None,
69     };
70
71     let inserted_user = User_::create(&conn, &new_user).unwrap();
72
73     let another_user = UserForm {
74       name: "jerry_community_agg".into(),
75       preferred_username: None,
76       password_encrypted: "nope".into(),
77       email: None,
78       matrix_user_id: None,
79       avatar: None,
80       banner: None,
81       admin: false,
82       banned: Some(false),
83       published: None,
84       updated: None,
85       show_nsfw: false,
86       theme: "browser".into(),
87       default_sort_type: SortType::Hot as i16,
88       default_listing_type: ListingType::Subscribed as i16,
89       lang: "browser".into(),
90       show_avatars: true,
91       send_notifications_to_email: false,
92       actor_id: None,
93       bio: None,
94       local: true,
95       private_key: None,
96       public_key: None,
97       last_refreshed_at: None,
98     };
99
100     let another_inserted_user = User_::create(&conn, &another_user).unwrap();
101
102     let new_community = CommunityForm {
103       name: "TIL_community_agg".into(),
104       creator_id: inserted_user.id,
105       title: "nada".to_owned(),
106       description: None,
107       category_id: 1,
108       nsfw: false,
109       removed: None,
110       deleted: None,
111       updated: None,
112       actor_id: None,
113       local: true,
114       private_key: None,
115       public_key: None,
116       last_refreshed_at: None,
117       published: None,
118       icon: None,
119       banner: None,
120     };
121
122     let inserted_community = Community::create(&conn, &new_community).unwrap();
123
124     let new_post = PostForm {
125       name: "A test post".into(),
126       url: None,
127       body: None,
128       creator_id: inserted_user.id,
129       community_id: inserted_community.id,
130       removed: None,
131       deleted: None,
132       locked: None,
133       stickied: None,
134       nsfw: false,
135       updated: None,
136       embed_title: None,
137       embed_description: None,
138       embed_html: None,
139       thumbnail_url: None,
140       ap_id: None,
141       local: true,
142       published: None,
143     };
144
145     let inserted_post = Post::create(&conn, &new_post).unwrap();
146
147     let comment_form = CommentForm {
148       content: "A test comment".into(),
149       creator_id: inserted_user.id,
150       post_id: inserted_post.id,
151       removed: None,
152       deleted: None,
153       read: None,
154       parent_id: None,
155       published: None,
156       updated: None,
157       ap_id: None,
158       local: true,
159     };
160
161     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
162
163     let child_comment_form = CommentForm {
164       content: "A test comment".into(),
165       creator_id: inserted_user.id,
166       post_id: inserted_post.id,
167       removed: None,
168       deleted: None,
169       read: None,
170       parent_id: Some(inserted_comment.id),
171       published: None,
172       updated: None,
173       ap_id: None,
174       local: true,
175     };
176
177     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
178
179     let post_like = PostLikeForm {
180       post_id: inserted_post.id,
181       user_id: inserted_user.id,
182       score: 1,
183     };
184
185     PostLike::like(&conn, &post_like).unwrap();
186
187     let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
188
189     assert_eq!(2, post_aggs_before_delete.comments);
190     assert_eq!(1, post_aggs_before_delete.score);
191     assert_eq!(1, post_aggs_before_delete.upvotes);
192     assert_eq!(0, post_aggs_before_delete.downvotes);
193
194     // Add a post dislike from the other user
195     let post_dislike = PostLikeForm {
196       post_id: inserted_post.id,
197       user_id: another_inserted_user.id,
198       score: -1,
199     };
200
201     PostLike::like(&conn, &post_dislike).unwrap();
202
203     let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
204
205     assert_eq!(2, post_aggs_after_dislike.comments);
206     assert_eq!(0, post_aggs_after_dislike.score);
207     assert_eq!(1, post_aggs_after_dislike.upvotes);
208     assert_eq!(1, post_aggs_after_dislike.downvotes);
209
210     // Remove the parent comment
211     Comment::delete(&conn, inserted_comment.id).unwrap();
212     let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
213     assert_eq!(0, after_comment_delete.comments);
214     assert_eq!(0, after_comment_delete.score);
215     assert_eq!(1, after_comment_delete.upvotes);
216     assert_eq!(1, after_comment_delete.downvotes);
217
218     // Remove the first post like
219     PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
220     let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
221     assert_eq!(0, after_like_remove.comments);
222     assert_eq!(-1, after_like_remove.score);
223     assert_eq!(0, after_like_remove.upvotes);
224     assert_eq!(1, after_like_remove.downvotes);
225
226     // This should delete all the associated rows, and fire triggers
227     User_::delete(&conn, another_inserted_user.id).unwrap();
228     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
229     assert_eq!(1, user_num_deleted);
230
231     // Should be none found, since the creator was deleted
232     let after_delete = PostAggregates::read(&conn, inserted_post.id);
233     assert!(after_delete.is_err());
234   }
235 }