2 aggregates::structs::PersonAggregates,
4 schema::person_aggregates,
5 utils::{get_conn, DbPool},
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
10 impl PersonAggregates {
11 pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
12 let conn = &mut get_conn(pool).await?;
13 person_aggregates::table
14 .filter(person_aggregates::person_id.eq(person_id))
22 #![allow(clippy::unwrap_used)]
23 #![allow(clippy::indexing_slicing)]
26 aggregates::person_aggregates::PersonAggregates,
28 comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
29 community::{Community, CommunityInsertForm},
31 person::{Person, PersonInsertForm},
32 post::{Post, PostInsertForm, PostLike, PostLikeForm},
34 traits::{Crud, Likeable},
35 utils::build_db_pool_for_tests,
37 use serial_test::serial;
41 async fn test_crud() {
42 let pool = &build_db_pool_for_tests().await;
43 let pool = &mut pool.into();
45 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
49 let new_person = PersonInsertForm::builder()
50 .name("thommy_user_agg".into())
51 .public_key("pubkey".to_string())
52 .instance_id(inserted_instance.id)
55 let inserted_person = Person::create(pool, &new_person).await.unwrap();
57 let another_person = PersonInsertForm::builder()
58 .name("jerry_user_agg".into())
59 .public_key("pubkey".to_string())
60 .instance_id(inserted_instance.id)
63 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
65 let new_community = CommunityInsertForm::builder()
66 .name("TIL_site_agg".into())
67 .title("nada".to_owned())
68 .public_key("pubkey".to_string())
69 .instance_id(inserted_instance.id)
72 let inserted_community = Community::create(pool, &new_community).await.unwrap();
74 let new_post = PostInsertForm::builder()
75 .name("A test post".into())
76 .creator_id(inserted_person.id)
77 .community_id(inserted_community.id)
80 let inserted_post = Post::create(pool, &new_post).await.unwrap();
82 let post_like = PostLikeForm {
83 post_id: inserted_post.id,
84 person_id: inserted_person.id,
88 let _inserted_post_like = PostLike::like(pool, &post_like).await.unwrap();
90 let comment_form = CommentInsertForm::builder()
91 .content("A test comment".into())
92 .creator_id(inserted_person.id)
93 .post_id(inserted_post.id)
96 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
98 let mut comment_like = CommentLikeForm {
99 comment_id: inserted_comment.id,
100 person_id: inserted_person.id,
101 post_id: inserted_post.id,
105 let _inserted_comment_like = CommentLike::like(pool, &comment_like).await.unwrap();
107 let child_comment_form = CommentInsertForm::builder()
108 .content("A test comment".into())
109 .creator_id(inserted_person.id)
110 .post_id(inserted_post.id)
113 let inserted_child_comment =
114 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
118 let child_comment_like = CommentLikeForm {
119 comment_id: inserted_child_comment.id,
120 person_id: another_inserted_person.id,
121 post_id: inserted_post.id,
125 let _inserted_child_comment_like = CommentLike::like(pool, &child_comment_like).await.unwrap();
127 let person_aggregates_before_delete = PersonAggregates::read(pool, inserted_person.id)
131 assert_eq!(1, person_aggregates_before_delete.post_count);
132 assert_eq!(1, person_aggregates_before_delete.post_score);
133 assert_eq!(2, person_aggregates_before_delete.comment_count);
134 assert_eq!(2, person_aggregates_before_delete.comment_score);
136 // Remove a post like
137 PostLike::remove(pool, inserted_person.id, inserted_post.id)
140 let after_post_like_remove = PersonAggregates::read(pool, inserted_person.id)
143 assert_eq!(0, after_post_like_remove.post_score);
148 &CommentUpdateForm::builder().removed(Some(true)).build(),
154 inserted_child_comment.id,
155 &CommentUpdateForm::builder().removed(Some(true)).build(),
160 let after_parent_comment_removed = PersonAggregates::read(pool, inserted_person.id)
163 assert_eq!(0, after_parent_comment_removed.comment_count);
164 // TODO: fix person aggregate comment score calculation
165 // assert_eq!(0, after_parent_comment_removed.comment_score);
167 // Remove a parent comment (the scores should also be removed)
168 Comment::delete(pool, inserted_comment.id).await.unwrap();
169 Comment::delete(pool, inserted_child_comment.id)
172 let after_parent_comment_delete = PersonAggregates::read(pool, inserted_person.id)
175 assert_eq!(0, after_parent_comment_delete.comment_count);
176 // TODO: fix person aggregate comment score calculation
177 // assert_eq!(0, after_parent_comment_delete.comment_score);
179 // Add in the two comments again, then delete the post.
180 let new_parent_comment = Comment::create(pool, &comment_form, None).await.unwrap();
181 let _new_child_comment =
182 Comment::create(pool, &child_comment_form, Some(&new_parent_comment.path))
185 comment_like.comment_id = new_parent_comment.id;
186 CommentLike::like(pool, &comment_like).await.unwrap();
187 let after_comment_add = PersonAggregates::read(pool, inserted_person.id)
190 assert_eq!(2, after_comment_add.comment_count);
191 // TODO: fix person aggregate comment score calculation
192 // assert_eq!(1, after_comment_add.comment_score);
194 Post::delete(pool, inserted_post.id).await.unwrap();
195 let after_post_delete = PersonAggregates::read(pool, inserted_person.id)
198 // TODO: fix person aggregate comment score calculation
199 // assert_eq!(0, after_post_delete.comment_score);
200 assert_eq!(0, after_post_delete.comment_count);
201 assert_eq!(0, after_post_delete.post_score);
202 assert_eq!(0, after_post_delete.post_count);
204 // This should delete all the associated rows, and fire triggers
205 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
206 assert_eq!(1, person_num_deleted);
207 Person::delete(pool, another_inserted_person.id)
211 // Delete the community
212 let community_num_deleted = Community::delete(pool, inserted_community.id)
215 assert_eq!(1, community_num_deleted);
217 // Should be none found
218 let after_delete = PersonAggregates::read(pool, inserted_person.id).await;
219 assert!(after_delete.is_err());
221 Instance::delete(pool, inserted_instance.id).await.unwrap();