1 use crate::schema::post_aggregates;
2 use diesel::{result::Error, *};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "post_aggregates"]
7 pub struct PostAggregates {
14 pub newest_comment_time: chrono::NaiveDateTime,
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))
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_},
42 let conn = establish_unpooled_connection();
44 let new_user = UserForm {
45 name: "thommy_community_agg".into(),
46 preferred_username: None,
47 password_encrypted: "nope".into(),
57 theme: "browser".into(),
58 default_sort_type: SortType::Hot as i16,
59 default_listing_type: ListingType::Subscribed as i16,
60 lang: "browser".into(),
62 send_notifications_to_email: false,
68 last_refreshed_at: None,
71 let inserted_user = User_::create(&conn, &new_user).unwrap();
73 let another_user = UserForm {
74 name: "jerry_community_agg".into(),
75 preferred_username: None,
76 password_encrypted: "nope".into(),
86 theme: "browser".into(),
87 default_sort_type: SortType::Hot as i16,
88 default_listing_type: ListingType::Subscribed as i16,
89 lang: "browser".into(),
91 send_notifications_to_email: false,
97 last_refreshed_at: None,
100 let another_inserted_user = User_::create(&conn, &another_user).unwrap();
102 let new_community = CommunityForm {
103 name: "TIL_community_agg".into(),
104 creator_id: inserted_user.id,
105 title: "nada".to_owned(),
116 last_refreshed_at: None,
122 let inserted_community = Community::create(&conn, &new_community).unwrap();
124 let new_post = PostForm {
125 name: "A test post".into(),
128 creator_id: inserted_user.id,
129 community_id: inserted_community.id,
137 embed_description: None,
145 let inserted_post = Post::create(&conn, &new_post).unwrap();
147 let comment_form = CommentForm {
148 content: "A test comment".into(),
149 creator_id: inserted_user.id,
150 post_id: inserted_post.id,
161 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
163 let child_comment_form = CommentForm {
164 content: "A test comment".into(),
165 creator_id: inserted_user.id,
166 post_id: inserted_post.id,
170 parent_id: Some(inserted_comment.id),
177 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
179 let post_like = PostLikeForm {
180 post_id: inserted_post.id,
181 user_id: inserted_user.id,
185 PostLike::like(&conn, &post_like).unwrap();
187 let post_aggs_before_delete = PostAggregates::read(&conn, inserted_post.id).unwrap();
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);
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,
201 PostLike::like(&conn, &post_dislike).unwrap();
203 let post_aggs_after_dislike = PostAggregates::read(&conn, inserted_post.id).unwrap();
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);
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);
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);
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);
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());