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: &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;
41 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
45 let new_person = PersonInsertForm::builder()
46 .name("thommy_user_agg".into())
47 .public_key("pubkey".to_string())
48 .instance_id(inserted_instance.id)
51 let inserted_person = Person::create(pool, &new_person).await.unwrap();
53 let another_person = PersonInsertForm::builder()
54 .name("jerry_user_agg".into())
55 .public_key("pubkey".to_string())
56 .instance_id(inserted_instance.id)
59 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
61 let new_community = CommunityInsertForm::builder()
62 .name("TIL_site_agg".into())
63 .title("nada".to_owned())
64 .public_key("pubkey".to_string())
65 .instance_id(inserted_instance.id)
68 let inserted_community = Community::create(pool, &new_community).await.unwrap();
70 let new_post = PostInsertForm::builder()
71 .name("A test post".into())
72 .creator_id(inserted_person.id)
73 .community_id(inserted_community.id)
76 let inserted_post = Post::create(pool, &new_post).await.unwrap();
78 let post_like = PostLikeForm {
79 post_id: inserted_post.id,
80 person_id: inserted_person.id,
84 let _inserted_post_like = PostLike::like(pool, &post_like).await.unwrap();
86 let comment_form = CommentInsertForm::builder()
87 .content("A test comment".into())
88 .creator_id(inserted_person.id)
89 .post_id(inserted_post.id)
92 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
94 let mut comment_like = CommentLikeForm {
95 comment_id: inserted_comment.id,
96 person_id: inserted_person.id,
97 post_id: inserted_post.id,
101 let _inserted_comment_like = CommentLike::like(pool, &comment_like).await.unwrap();
103 let child_comment_form = CommentInsertForm::builder()
104 .content("A test comment".into())
105 .creator_id(inserted_person.id)
106 .post_id(inserted_post.id)
109 let inserted_child_comment =
110 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
114 let child_comment_like = CommentLikeForm {
115 comment_id: inserted_child_comment.id,
116 person_id: another_inserted_person.id,
117 post_id: inserted_post.id,
121 let _inserted_child_comment_like = CommentLike::like(pool, &child_comment_like).await.unwrap();
123 let person_aggregates_before_delete = PersonAggregates::read(pool, inserted_person.id)
127 assert_eq!(1, person_aggregates_before_delete.post_count);
128 assert_eq!(1, person_aggregates_before_delete.post_score);
129 assert_eq!(2, person_aggregates_before_delete.comment_count);
130 assert_eq!(2, person_aggregates_before_delete.comment_score);
132 // Remove a post like
133 PostLike::remove(pool, inserted_person.id, inserted_post.id)
136 let after_post_like_remove = PersonAggregates::read(pool, inserted_person.id)
139 assert_eq!(0, after_post_like_remove.post_score);
144 &CommentUpdateForm::builder().removed(Some(true)).build(),
150 inserted_child_comment.id,
151 &CommentUpdateForm::builder().removed(Some(true)).build(),
156 let after_parent_comment_removed = PersonAggregates::read(pool, inserted_person.id)
159 assert_eq!(0, after_parent_comment_removed.comment_count);
160 assert_eq!(0, after_parent_comment_removed.comment_score);
162 // Remove a parent comment (the scores should also be removed)
163 Comment::delete(pool, inserted_comment.id).await.unwrap();
164 Comment::delete(pool, inserted_child_comment.id)
167 let after_parent_comment_delete = PersonAggregates::read(pool, inserted_person.id)
170 assert_eq!(0, after_parent_comment_delete.comment_count);
171 assert_eq!(0, after_parent_comment_delete.comment_score);
173 // Add in the two comments again, then delete the post.
174 let new_parent_comment = Comment::create(pool, &comment_form, None).await.unwrap();
175 let _new_child_comment =
176 Comment::create(pool, &child_comment_form, Some(&new_parent_comment.path))
179 comment_like.comment_id = new_parent_comment.id;
180 CommentLike::like(pool, &comment_like).await.unwrap();
181 let after_comment_add = PersonAggregates::read(pool, inserted_person.id)
184 assert_eq!(2, after_comment_add.comment_count);
185 assert_eq!(1, after_comment_add.comment_score);
187 Post::delete(pool, inserted_post.id).await.unwrap();
188 let after_post_delete = PersonAggregates::read(pool, inserted_person.id)
191 assert_eq!(0, after_post_delete.comment_score);
192 assert_eq!(0, after_post_delete.comment_count);
193 assert_eq!(0, after_post_delete.post_score);
194 assert_eq!(0, after_post_delete.post_count);
196 // This should delete all the associated rows, and fire triggers
197 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
198 assert_eq!(1, person_num_deleted);
199 Person::delete(pool, another_inserted_person.id)
203 // Delete the community
204 let community_num_deleted = Community::delete(pool, inserted_community.id)
207 assert_eq!(1, community_num_deleted);
209 // Should be none found
210 let after_delete = PersonAggregates::read(pool, inserted_person.id).await;
211 assert!(after_delete.is_err());
213 Instance::delete(pool, inserted_instance.id).await.unwrap();