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))
23 aggregates::person_aggregates::PersonAggregates,
25 comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
26 community::{Community, CommunityInsertForm},
28 person::{Person, PersonInsertForm},
29 post::{Post, PostInsertForm, PostLike, PostLikeForm},
31 traits::{Crud, Likeable},
32 utils::build_db_pool_for_tests,
34 use serial_test::serial;
38 async fn test_crud() {
39 let pool = &build_db_pool_for_tests().await;
40 let pool = &mut pool.into();
42 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
46 let new_person = PersonInsertForm::builder()
47 .name("thommy_user_agg".into())
48 .public_key("pubkey".to_string())
49 .instance_id(inserted_instance.id)
52 let inserted_person = Person::create(pool, &new_person).await.unwrap();
54 let another_person = PersonInsertForm::builder()
55 .name("jerry_user_agg".into())
56 .public_key("pubkey".to_string())
57 .instance_id(inserted_instance.id)
60 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
62 let new_community = CommunityInsertForm::builder()
63 .name("TIL_site_agg".into())
64 .title("nada".to_owned())
65 .public_key("pubkey".to_string())
66 .instance_id(inserted_instance.id)
69 let inserted_community = Community::create(pool, &new_community).await.unwrap();
71 let new_post = PostInsertForm::builder()
72 .name("A test post".into())
73 .creator_id(inserted_person.id)
74 .community_id(inserted_community.id)
77 let inserted_post = Post::create(pool, &new_post).await.unwrap();
79 let post_like = PostLikeForm {
80 post_id: inserted_post.id,
81 person_id: inserted_person.id,
85 let _inserted_post_like = PostLike::like(pool, &post_like).await.unwrap();
87 let comment_form = CommentInsertForm::builder()
88 .content("A test comment".into())
89 .creator_id(inserted_person.id)
90 .post_id(inserted_post.id)
93 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
95 let mut comment_like = CommentLikeForm {
96 comment_id: inserted_comment.id,
97 person_id: inserted_person.id,
98 post_id: inserted_post.id,
102 let _inserted_comment_like = CommentLike::like(pool, &comment_like).await.unwrap();
104 let child_comment_form = CommentInsertForm::builder()
105 .content("A test comment".into())
106 .creator_id(inserted_person.id)
107 .post_id(inserted_post.id)
110 let inserted_child_comment =
111 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
115 let child_comment_like = CommentLikeForm {
116 comment_id: inserted_child_comment.id,
117 person_id: another_inserted_person.id,
118 post_id: inserted_post.id,
122 let _inserted_child_comment_like = CommentLike::like(pool, &child_comment_like).await.unwrap();
124 let person_aggregates_before_delete = PersonAggregates::read(pool, inserted_person.id)
128 assert_eq!(1, person_aggregates_before_delete.post_count);
129 assert_eq!(1, person_aggregates_before_delete.post_score);
130 assert_eq!(2, person_aggregates_before_delete.comment_count);
131 assert_eq!(2, person_aggregates_before_delete.comment_score);
133 // Remove a post like
134 PostLike::remove(pool, inserted_person.id, inserted_post.id)
137 let after_post_like_remove = PersonAggregates::read(pool, inserted_person.id)
140 assert_eq!(0, after_post_like_remove.post_score);
145 &CommentUpdateForm::builder().removed(Some(true)).build(),
151 inserted_child_comment.id,
152 &CommentUpdateForm::builder().removed(Some(true)).build(),
157 let after_parent_comment_removed = PersonAggregates::read(pool, inserted_person.id)
160 assert_eq!(0, after_parent_comment_removed.comment_count);
161 assert_eq!(0, after_parent_comment_removed.comment_score);
163 // Remove a parent comment (the scores should also be removed)
164 Comment::delete(pool, inserted_comment.id).await.unwrap();
165 Comment::delete(pool, inserted_child_comment.id)
168 let after_parent_comment_delete = PersonAggregates::read(pool, inserted_person.id)
171 assert_eq!(0, after_parent_comment_delete.comment_count);
172 assert_eq!(0, after_parent_comment_delete.comment_score);
174 // Add in the two comments again, then delete the post.
175 let new_parent_comment = Comment::create(pool, &comment_form, None).await.unwrap();
176 let _new_child_comment =
177 Comment::create(pool, &child_comment_form, Some(&new_parent_comment.path))
180 comment_like.comment_id = new_parent_comment.id;
181 CommentLike::like(pool, &comment_like).await.unwrap();
182 let after_comment_add = PersonAggregates::read(pool, inserted_person.id)
185 assert_eq!(2, after_comment_add.comment_count);
186 assert_eq!(1, after_comment_add.comment_score);
188 Post::delete(pool, inserted_post.id).await.unwrap();
189 let after_post_delete = PersonAggregates::read(pool, inserted_person.id)
192 assert_eq!(0, after_post_delete.comment_score);
193 assert_eq!(0, after_post_delete.comment_count);
194 assert_eq!(0, after_post_delete.post_score);
195 assert_eq!(0, after_post_delete.post_count);
197 // This should delete all the associated rows, and fire triggers
198 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
199 assert_eq!(1, person_num_deleted);
200 Person::delete(pool, another_inserted_person.id)
204 // Delete the community
205 let community_num_deleted = Community::delete(pool, inserted_community.id)
208 assert_eq!(1, community_num_deleted);
210 // Should be none found
211 let after_delete = PersonAggregates::read(pool, inserted_person.id).await;
212 assert!(after_delete.is_err());
214 Instance::delete(pool, inserted_instance.id).await.unwrap();