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, CommentInsertForm, CommentLike, CommentLikeForm},
18 community::{Community, CommunityInsertForm},
20 person::{Person, PersonInsertForm},
21 post::{Post, PostInsertForm, PostLike, PostLikeForm},
23 traits::{Crud, Likeable},
24 utils::establish_unpooled_connection,
26 use serial_test::serial;
31 let conn = &mut establish_unpooled_connection();
33 let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
35 let new_person = PersonInsertForm::builder()
36 .name("thommy_user_agg".into())
37 .public_key("pubkey".to_string())
38 .instance_id(inserted_instance.id)
41 let inserted_person = Person::create(conn, &new_person).unwrap();
43 let another_person = PersonInsertForm::builder()
44 .name("jerry_user_agg".into())
45 .public_key("pubkey".to_string())
46 .instance_id(inserted_instance.id)
49 let another_inserted_person = Person::create(conn, &another_person).unwrap();
51 let new_community = CommunityInsertForm::builder()
52 .name("TIL_site_agg".into())
53 .title("nada".to_owned())
54 .public_key("pubkey".to_string())
55 .instance_id(inserted_instance.id)
58 let inserted_community = Community::create(conn, &new_community).unwrap();
60 let new_post = PostInsertForm::builder()
61 .name("A test post".into())
62 .creator_id(inserted_person.id)
63 .community_id(inserted_community.id)
66 let inserted_post = Post::create(conn, &new_post).unwrap();
68 let post_like = PostLikeForm {
69 post_id: inserted_post.id,
70 person_id: inserted_person.id,
74 let _inserted_post_like = PostLike::like(conn, &post_like).unwrap();
76 let comment_form = CommentInsertForm::builder()
77 .content("A test comment".into())
78 .creator_id(inserted_person.id)
79 .post_id(inserted_post.id)
82 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
84 let mut comment_like = CommentLikeForm {
85 comment_id: inserted_comment.id,
86 person_id: inserted_person.id,
87 post_id: inserted_post.id,
91 let _inserted_comment_like = CommentLike::like(conn, &comment_like).unwrap();
93 let child_comment_form = CommentInsertForm::builder()
94 .content("A test comment".into())
95 .creator_id(inserted_person.id)
96 .post_id(inserted_post.id)
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());
160 Instance::delete(conn, inserted_instance.id).unwrap();