1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::post_aggregates;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "post_aggregates"]
7 pub struct PostAggregates {
14 pub published: chrono::NaiveDateTime,
15 pub newest_comment_time: chrono::NaiveDateTime,
19 pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
20 post_aggregates::table
21 .filter(post_aggregates::post_id.eq(post_id))
29 aggregates::post_aggregates::PostAggregates,
30 establish_unpooled_connection,
36 use lemmy_db_schema::source::{
37 comment::{Comment, CommentForm},
38 community::{Community, CommunityForm},
39 post::{Post, PostForm, PostLike, PostLikeForm},
40 user::{UserForm, User_},
45 let conn = establish_unpooled_connection();
47 let new_user = UserForm {
48 name: "thommy_community_agg".into(),
49 preferred_username: None,
50 password_encrypted: "nope".into(),
60 theme: "browser".into(),
61 default_sort_type: SortType::Hot as i16,
62 default_listing_type: ListingType::Subscribed as i16,
63 lang: "browser".into(),
65 send_notifications_to_email: false,
71 last_refreshed_at: None,
74 let inserted_user = User_::create(&conn, &new_user).unwrap();
76 let another_user = UserForm {
77 name: "jerry_community_agg".into(),
78 preferred_username: None,
79 password_encrypted: "nope".into(),
89 theme: "browser".into(),
90 default_sort_type: SortType::Hot as i16,
91 default_listing_type: ListingType::Subscribed as i16,
92 lang: "browser".into(),
94 send_notifications_to_email: false,
100 last_refreshed_at: None,
103 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
105 let new_community = CommunityForm {
106 name: "TIL_community_agg".into(),
107 creator_id: inserted_user.id,
108 title: "nada".to_owned(),
119 last_refreshed_at: None,
125 let inserted_community = Community::create(&conn, &new_community).unwrap();
127 let new_post = PostForm {
128 name: "A test post".into(),
131 creator_id: inserted_user.id,
132 community_id: inserted_community.id,
140 embed_description: None,
148 let inserted_post = Post::create(&conn, &new_post).unwrap();
150 let comment_form = CommentForm {
151 content: "A test comment".into(),
152 creator_id: inserted_user.id,
153 post_id: inserted_post.id,
164 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
166 let child_comment_form = CommentForm {
167 content: "A test comment".into(),
168 creator_id: inserted_user.id,
169 post_id: inserted_post.id,
173 parent_id: Some(inserted_comment.id),
180 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
182 let post_like = PostLikeForm {
183 post_id: inserted_post.id,
184 user_id: inserted_user.id,
188 PostLike::like(&conn, &post_like).unwrap();
190 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
192 assert_eq!(2, post_aggs_before_delete.comments);
193 assert_eq!(1, post_aggs_before_delete.score);
194 assert_eq!(1, post_aggs_before_delete.upvotes);
195 assert_eq!(0, post_aggs_before_delete.downvotes);
197 // Add a post dislike from the other user
198 let post_dislike = PostLikeForm {
199 post_id: inserted_post.id,
200 user_id: another_inserted_user.id,
204 PostLike::like(&conn, &post_dislike).unwrap();
206 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
208 assert_eq!(2, post_aggs_after_dislike.comments);
209 assert_eq!(0, post_aggs_after_dislike.score);
210 assert_eq!(1, post_aggs_after_dislike.upvotes);
211 assert_eq!(1, post_aggs_after_dislike.downvotes);
213 // Remove the parent comment
214 Comment::delete(&conn, inserted_comment.id).unwrap();
215 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
216 assert_eq!(0, after_comment_delete.comments);
217 assert_eq!(0, after_comment_delete.score);
218 assert_eq!(1, after_comment_delete.upvotes);
219 assert_eq!(1, after_comment_delete.downvotes);
221 // Remove the first post like
222 PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
223 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
224 assert_eq!(0, after_like_remove.comments);
225 assert_eq!(-1, after_like_remove.score);
226 assert_eq!(0, after_like_remove.upvotes);
227 assert_eq!(1, after_like_remove.downvotes);
229 // This should delete all the associated rows, and fire triggers
230 User_::delete(&conn, another_inserted_user.id).unwrap();
231 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
232 assert_eq!(1, user_num_deleted);
234 // Should be none found, since the creator was deleted
235 let after_delete = PostAggregates::read(&conn, inserted_post.id);
236 assert!(after_delete.is_err());