1 use crate::{aggregates::structs::PersonAggregates, newtypes::PersonId, schema::person_aggregates};
2 use diesel::{result::Error, *};
4 impl PersonAggregates {
5 pub fn read(conn: &mut PgConnection, person_id: PersonId) -> Result<Self, Error> {
6 person_aggregates::table
7 .filter(person_aggregates::person_id.eq(person_id))
15 aggregates::person_aggregates::PersonAggregates,
17 comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
18 community::{Community, CommunityForm},
19 person::{Person, PersonForm},
20 post::{Post, PostForm, PostLike, PostLikeForm},
22 traits::{Crud, Likeable},
23 utils::establish_unpooled_connection,
25 use serial_test::serial;
30 let conn = &mut establish_unpooled_connection();
32 let new_person = PersonForm {
33 name: "thommy_user_agg".into(),
34 public_key: Some("pubkey".to_string()),
35 ..PersonForm::default()
38 let inserted_person = Person::create(conn, &new_person).unwrap();
40 let another_person = PersonForm {
41 name: "jerry_user_agg".into(),
42 public_key: Some("pubkey".to_string()),
43 ..PersonForm::default()
46 let another_inserted_person = Person::create(conn, &another_person).unwrap();
48 let new_community = CommunityForm {
49 name: "TIL_site_agg".into(),
50 title: "nada".to_owned(),
51 public_key: Some("pubkey".to_string()),
52 ..CommunityForm::default()
55 let inserted_community = Community::create(conn, &new_community).unwrap();
57 let new_post = PostForm {
58 name: "A test post".into(),
59 creator_id: inserted_person.id,
60 community_id: inserted_community.id,
64 let inserted_post = Post::create(conn, &new_post).unwrap();
66 let post_like = PostLikeForm {
67 post_id: inserted_post.id,
68 person_id: inserted_person.id,
72 let _inserted_post_like = PostLike::like(conn, &post_like).unwrap();
74 let comment_form = CommentForm {
75 content: "A test comment".into(),
76 creator_id: inserted_person.id,
77 post_id: inserted_post.id,
78 ..CommentForm::default()
81 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
83 let mut comment_like = CommentLikeForm {
84 comment_id: inserted_comment.id,
85 person_id: inserted_person.id,
86 post_id: inserted_post.id,
90 let _inserted_comment_like = CommentLike::like(conn, &comment_like).unwrap();
92 let child_comment_form = CommentForm {
93 content: "A test comment".into(),
94 creator_id: inserted_person.id,
95 post_id: inserted_post.id,
96 ..CommentForm::default()
99 let inserted_child_comment =
100 Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
102 let child_comment_like = CommentLikeForm {
103 comment_id: inserted_child_comment.id,
104 person_id: another_inserted_person.id,
105 post_id: inserted_post.id,
109 let _inserted_child_comment_like = CommentLike::like(conn, &child_comment_like).unwrap();
111 let person_aggregates_before_delete = PersonAggregates::read(conn, inserted_person.id).unwrap();
113 assert_eq!(1, person_aggregates_before_delete.post_count);
114 assert_eq!(1, person_aggregates_before_delete.post_score);
115 assert_eq!(2, person_aggregates_before_delete.comment_count);
116 assert_eq!(2, person_aggregates_before_delete.comment_score);
118 // Remove a post like
119 PostLike::remove(conn, inserted_person.id, inserted_post.id).unwrap();
120 let after_post_like_remove = PersonAggregates::read(conn, inserted_person.id).unwrap();
121 assert_eq!(0, after_post_like_remove.post_score);
123 // Remove a parent comment (the scores should also be removed)
124 Comment::delete(conn, inserted_comment.id).unwrap();
125 Comment::delete(conn, inserted_child_comment.id).unwrap();
126 let after_parent_comment_delete = PersonAggregates::read(conn, inserted_person.id).unwrap();
127 assert_eq!(0, after_parent_comment_delete.comment_count);
128 assert_eq!(0, after_parent_comment_delete.comment_score);
130 // Add in the two comments again, then delete the post.
131 let new_parent_comment = Comment::create(conn, &comment_form, None).unwrap();
132 let _new_child_comment =
133 Comment::create(conn, &child_comment_form, Some(&new_parent_comment.path)).unwrap();
134 comment_like.comment_id = new_parent_comment.id;
135 CommentLike::like(conn, &comment_like).unwrap();
136 let after_comment_add = PersonAggregates::read(conn, inserted_person.id).unwrap();
137 assert_eq!(2, after_comment_add.comment_count);
138 assert_eq!(1, after_comment_add.comment_score);
140 Post::delete(conn, inserted_post.id).unwrap();
141 let after_post_delete = PersonAggregates::read(conn, inserted_person.id).unwrap();
142 assert_eq!(0, after_post_delete.comment_score);
143 assert_eq!(0, after_post_delete.comment_count);
144 assert_eq!(0, after_post_delete.post_score);
145 assert_eq!(0, after_post_delete.post_count);
147 // This should delete all the associated rows, and fire triggers
148 let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
149 assert_eq!(1, person_num_deleted);
150 Person::delete(conn, another_inserted_person.id).unwrap();
152 // Delete the community
153 let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
154 assert_eq!(1, community_num_deleted);
156 // Should be none found
157 let after_delete = PersonAggregates::read(conn, inserted_person.id);
158 assert!(after_delete.is_err());