1 use crate::{aggregates::structs::PersonAggregates, newtypes::PersonId, schema::person_aggregates};
2 use diesel::{result::Error, *};
4 impl PersonAggregates {
5 pub fn read(conn: &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 = establish_unpooled_connection();
32 let new_person = PersonForm {
33 name: "thommy_user_agg".into(),
34 ..PersonForm::default()
37 let inserted_person = Person::create(&conn, &new_person).unwrap();
39 let another_person = PersonForm {
40 name: "jerry_user_agg".into(),
41 ..PersonForm::default()
44 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
46 let new_community = CommunityForm {
47 name: "TIL_site_agg".into(),
48 title: "nada".to_owned(),
49 ..CommunityForm::default()
52 let inserted_community = Community::create(&conn, &new_community).unwrap();
54 let new_post = PostForm {
55 name: "A test post".into(),
56 creator_id: inserted_person.id,
57 community_id: inserted_community.id,
61 let inserted_post = Post::create(&conn, &new_post).unwrap();
63 let post_like = PostLikeForm {
64 post_id: inserted_post.id,
65 person_id: inserted_person.id,
69 let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
71 let comment_form = CommentForm {
72 content: "A test comment".into(),
73 creator_id: inserted_person.id,
74 post_id: inserted_post.id,
75 ..CommentForm::default()
78 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
80 let mut comment_like = CommentLikeForm {
81 comment_id: inserted_comment.id,
82 person_id: inserted_person.id,
83 post_id: inserted_post.id,
87 let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
89 let mut child_comment_form = CommentForm {
90 content: "A test comment".into(),
91 creator_id: inserted_person.id,
92 post_id: inserted_post.id,
93 parent_id: Some(inserted_comment.id),
94 ..CommentForm::default()
97 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
99 let child_comment_like = CommentLikeForm {
100 comment_id: inserted_child_comment.id,
101 person_id: another_inserted_person.id,
102 post_id: inserted_post.id,
106 let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
108 let person_aggregates_before_delete =
109 PersonAggregates::read(&conn, inserted_person.id).unwrap();
111 assert_eq!(1, person_aggregates_before_delete.post_count);
112 assert_eq!(1, person_aggregates_before_delete.post_score);
113 assert_eq!(2, person_aggregates_before_delete.comment_count);
114 assert_eq!(2, person_aggregates_before_delete.comment_score);
116 // Remove a post like
117 PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
118 let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
119 assert_eq!(0, after_post_like_remove.post_score);
121 // Remove a parent comment (the scores should also be removed)
122 Comment::delete(&conn, inserted_comment.id).unwrap();
123 let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
124 assert_eq!(0, after_parent_comment_delete.comment_count);
125 assert_eq!(0, after_parent_comment_delete.comment_score);
127 // Add in the two comments again, then delete the post.
128 let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
129 child_comment_form.parent_id = Some(new_parent_comment.id);
130 Comment::create(&conn, &child_comment_form).unwrap();
131 comment_like.comment_id = new_parent_comment.id;
132 CommentLike::like(&conn, &comment_like).unwrap();
133 let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
134 assert_eq!(2, after_comment_add.comment_count);
135 assert_eq!(1, after_comment_add.comment_score);
137 Post::delete(&conn, inserted_post.id).unwrap();
138 let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
139 assert_eq!(0, after_post_delete.comment_score);
140 assert_eq!(0, after_post_delete.comment_count);
141 assert_eq!(0, after_post_delete.post_score);
142 assert_eq!(0, after_post_delete.post_count);
144 // This should delete all the associated rows, and fire triggers
145 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
146 assert_eq!(1, person_num_deleted);
147 Person::delete(&conn, another_inserted_person.id).unwrap();
149 // Delete the community
150 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
151 assert_eq!(1, community_num_deleted);
153 // Should be none found
154 let after_delete = PersonAggregates::read(&conn, inserted_person.id);
155 assert!(after_delete.is_err());