1 use crate::{newtypes::PersonId, schema::person_aggregates};
2 use diesel::{result::Error, *};
3 use serde::{Deserialize, Serialize};
6 Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone, Default,
8 #[table_name = "person_aggregates"]
9 pub struct PersonAggregates {
11 pub person_id: PersonId,
14 pub comment_count: i64,
15 pub comment_score: i64,
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))
29 aggregates::person_aggregates::PersonAggregates,
30 establish_unpooled_connection,
32 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
33 community::{Community, CommunityForm},
34 person::{Person, PersonForm},
35 post::{Post, PostForm, PostLike, PostLikeForm},
37 traits::{Crud, Likeable},
39 use serial_test::serial;
44 let conn = establish_unpooled_connection();
46 let new_person = PersonForm {
47 name: "thommy_user_agg".into(),
48 ..PersonForm::default()
51 let inserted_person = Person::create(&conn, &new_person).unwrap();
53 let another_person = PersonForm {
54 name: "jerry_user_agg".into(),
55 ..PersonForm::default()
58 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
60 let new_community = CommunityForm {
61 name: "TIL_site_agg".into(),
62 title: "nada".to_owned(),
63 ..CommunityForm::default()
66 let inserted_community = Community::create(&conn, &new_community).unwrap();
68 let new_post = PostForm {
69 name: "A test post".into(),
70 creator_id: inserted_person.id,
71 community_id: inserted_community.id,
75 let inserted_post = Post::create(&conn, &new_post).unwrap();
77 let post_like = PostLikeForm {
78 post_id: inserted_post.id,
79 person_id: inserted_person.id,
83 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
85 let comment_form = CommentForm {
86 content: "A test comment".into(),
87 creator_id: inserted_person.id,
88 post_id: inserted_post.id,
89 ..CommentForm::default()
92 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
94 let mut comment_like = CommentLikeForm {
95 comment_id: inserted_comment.id,
96 person_id: inserted_person.id,
97 post_id: inserted_post.id,
101 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
103 let mut child_comment_form = CommentForm {
104 content: "A test comment".into(),
105 creator_id: inserted_person.id,
106 post_id: inserted_post.id,
107 parent_id: Some(inserted_comment.id),
108 ..CommentForm::default()
111 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
113 let child_comment_like = CommentLikeForm {
114 comment_id: inserted_child_comment.id,
115 person_id: another_inserted_person.id,
116 post_id: inserted_post.id,
120 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
122 let person_aggregates_before_delete =
123 PersonAggregates::read(&conn, inserted_person.id).unwrap();
125 assert_eq!(1, person_aggregates_before_delete.post_count);
126 assert_eq!(1, person_aggregates_before_delete.post_score);
127 assert_eq!(2, person_aggregates_before_delete.comment_count);
128 assert_eq!(2, person_aggregates_before_delete.comment_score);
130 // Remove a post like
131 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
132 let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
133 assert_eq!(0, after_post_like_remove.post_score);
135 // Remove a parent comment (the scores should also be removed)
136 Comment::delete(&conn, inserted_comment.id).unwrap();
137 let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
138 assert_eq!(0, after_parent_comment_delete.comment_count);
139 assert_eq!(0, after_parent_comment_delete.comment_score);
141 // Add in the two comments again, then delete the post.
142 let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
143 child_comment_form.parent_id = Some(new_parent_comment.id);
144 Comment::create(&conn, &child_comment_form).unwrap();
145 comment_like.comment_id = new_parent_comment.id;
146 CommentLike::like(&conn, &comment_like).unwrap();
147 let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
148 assert_eq!(2, after_comment_add.comment_count);
149 assert_eq!(1, after_comment_add.comment_score);
151 Post::delete(&conn, inserted_post.id).unwrap();
152 let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
153 assert_eq!(0, after_post_delete.comment_score);
154 assert_eq!(0, after_post_delete.comment_count);
155 assert_eq!(0, after_post_delete.post_score);
156 assert_eq!(0, after_post_delete.post_count);
158 // This should delete all the associated rows, and fire triggers
159 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
160 assert_eq!(1, person_num_deleted);
161 Person::delete(&conn, another_inserted_person.id).unwrap();
163 // Delete the community
164 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
165 assert_eq!(1, community_num_deleted);
167 // Should be none found
168 let after_delete = PersonAggregates::read(&conn, inserted_person.id);
169 assert!(after_delete.is_err());