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 {
15 pub published: chrono::NaiveDateTime,
16 pub newest_comment_time: chrono::NaiveDateTime,
20 pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
21 post_aggregates::table
22 .filter(post_aggregates::post_id.eq(post_id))
30 aggregates::post_aggregates::PostAggregates,
31 establish_unpooled_connection,
37 use lemmy_db_schema::source::{
38 comment::{Comment, CommentForm},
39 community::{Community, CommunityForm},
40 post::{Post, PostForm, PostLike, PostLikeForm},
41 user::{UserForm, User_},
46 let conn = establish_unpooled_connection();
48 let new_user = UserForm {
49 name: "thommy_community_agg".into(),
50 preferred_username: None,
51 password_encrypted: "nope".into(),
61 theme: "browser".into(),
62 default_sort_type: SortType::Hot as i16,
63 default_listing_type: ListingType::Subscribed as i16,
64 lang: "browser".into(),
66 send_notifications_to_email: false,
72 last_refreshed_at: None,
75 let inserted_user = User_::create(&conn, &new_user).unwrap();
77 let another_user = UserForm {
78 name: "jerry_community_agg".into(),
79 preferred_username: None,
80 password_encrypted: "nope".into(),
90 theme: "browser".into(),
91 default_sort_type: SortType::Hot as i16,
92 default_listing_type: ListingType::Subscribed as i16,
93 lang: "browser".into(),
95 send_notifications_to_email: false,
101 last_refreshed_at: None,
104 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
106 let new_community = CommunityForm {
107 name: "TIL_community_agg".into(),
108 creator_id: inserted_user.id,
109 title: "nada".to_owned(),
120 last_refreshed_at: None,
126 let inserted_community = Community::create(&conn, &new_community).unwrap();
128 let new_post = PostForm {
129 name: "A test post".into(),
132 creator_id: inserted_user.id,
133 community_id: inserted_community.id,
141 embed_description: None,
149 let inserted_post = Post::create(&conn, &new_post).unwrap();
151 let comment_form = CommentForm {
152 content: "A test comment".into(),
153 creator_id: inserted_user.id,
154 post_id: inserted_post.id,
165 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
167 let child_comment_form = CommentForm {
168 content: "A test comment".into(),
169 creator_id: inserted_user.id,
170 post_id: inserted_post.id,
174 parent_id: Some(inserted_comment.id),
181 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
183 let post_like = PostLikeForm {
184 post_id: inserted_post.id,
185 user_id: inserted_user.id,
189 PostLike::like(&conn, &post_like).unwrap();
191 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
193 assert_eq!(2, post_aggs_before_delete.comments);
194 assert_eq!(1, post_aggs_before_delete.score);
195 assert_eq!(1, post_aggs_before_delete.upvotes);
196 assert_eq!(0, post_aggs_before_delete.downvotes);
198 // Add a post dislike from the other user
199 let post_dislike = PostLikeForm {
200 post_id: inserted_post.id,
201 user_id: another_inserted_user.id,
205 PostLike::like(&conn, &post_dislike).unwrap();
207 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
209 assert_eq!(2, post_aggs_after_dislike.comments);
210 assert_eq!(0, post_aggs_after_dislike.score);
211 assert_eq!(1, post_aggs_after_dislike.upvotes);
212 assert_eq!(1, post_aggs_after_dislike.downvotes);
214 // Remove the parent comment
215 Comment::delete(&conn, inserted_comment.id).unwrap();
216 let after_comment_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
217 assert_eq!(0, after_comment_delete.comments);
218 assert_eq!(0, after_comment_delete.score);
219 assert_eq!(1, after_comment_delete.upvotes);
220 assert_eq!(1, after_comment_delete.downvotes);
222 // Remove the first post like
223 PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
224 let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
225 assert_eq!(0, after_like_remove.comments);
226 assert_eq!(-1, after_like_remove.score);
227 assert_eq!(0, after_like_remove.upvotes);
228 assert_eq!(1, after_like_remove.downvotes);
230 // This should delete all the associated rows, and fire triggers
231 User_::delete(&conn, another_inserted_user.id).unwrap();
232 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
233 assert_eq!(1, user_num_deleted);
235 // Should be none found, since the creator was deleted
236 let after_delete = PostAggregates::read(&conn, inserted_post.id);
237 assert!(after_delete.is_err());