]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/person_aggregates.rs
e03497da076263a551bc5c7a55db09e8098b1f88
[lemmy.git] / crates / db_schema / src / aggregates / person_aggregates.rs
1 use crate::{
2   aggregates::structs::PersonAggregates,
3   newtypes::PersonId,
4   schema::person_aggregates,
5   utils::{get_conn, DbPool},
6 };
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
9
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))
15       .first::<Self>(conn)
16       .await
17   }
18 }
19
20 #[cfg(test)]
21 mod tests {
22   use crate::{
23     aggregates::person_aggregates::PersonAggregates,
24     source::{
25       comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
26       community::{Community, CommunityInsertForm},
27       instance::Instance,
28       person::{Person, PersonInsertForm},
29       post::{Post, PostInsertForm, PostLike, PostLikeForm},
30     },
31     traits::{Crud, Likeable},
32     utils::build_db_pool_for_tests,
33   };
34   use serial_test::serial;
35
36   #[tokio::test]
37   #[serial]
38   async fn test_crud() {
39     let pool = &build_db_pool_for_tests().await;
40     let pool = &mut pool.into();
41
42     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
43       .await
44       .unwrap();
45
46     let new_person = PersonInsertForm::builder()
47       .name("thommy_user_agg".into())
48       .public_key("pubkey".to_string())
49       .instance_id(inserted_instance.id)
50       .build();
51
52     let inserted_person = Person::create(pool, &new_person).await.unwrap();
53
54     let another_person = PersonInsertForm::builder()
55       .name("jerry_user_agg".into())
56       .public_key("pubkey".to_string())
57       .instance_id(inserted_instance.id)
58       .build();
59
60     let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
61
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)
67       .build();
68
69     let inserted_community = Community::create(pool, &new_community).await.unwrap();
70
71     let new_post = PostInsertForm::builder()
72       .name("A test post".into())
73       .creator_id(inserted_person.id)
74       .community_id(inserted_community.id)
75       .build();
76
77     let inserted_post = Post::create(pool, &new_post).await.unwrap();
78
79     let post_like = PostLikeForm {
80       post_id: inserted_post.id,
81       person_id: inserted_person.id,
82       score: 1,
83     };
84
85     let _inserted_post_like = PostLike::like(pool, &post_like).await.unwrap();
86
87     let comment_form = CommentInsertForm::builder()
88       .content("A test comment".into())
89       .creator_id(inserted_person.id)
90       .post_id(inserted_post.id)
91       .build();
92
93     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
94
95     let mut comment_like = CommentLikeForm {
96       comment_id: inserted_comment.id,
97       person_id: inserted_person.id,
98       post_id: inserted_post.id,
99       score: 1,
100     };
101
102     let _inserted_comment_like = CommentLike::like(pool, &comment_like).await.unwrap();
103
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)
108       .build();
109
110     let inserted_child_comment =
111       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
112         .await
113         .unwrap();
114
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,
119       score: 1,
120     };
121
122     let _inserted_child_comment_like = CommentLike::like(pool, &child_comment_like).await.unwrap();
123
124     let person_aggregates_before_delete = PersonAggregates::read(pool, inserted_person.id)
125       .await
126       .unwrap();
127
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);
132
133     // Remove a post like
134     PostLike::remove(pool, inserted_person.id, inserted_post.id)
135       .await
136       .unwrap();
137     let after_post_like_remove = PersonAggregates::read(pool, inserted_person.id)
138       .await
139       .unwrap();
140     assert_eq!(0, after_post_like_remove.post_score);
141
142     Comment::update(
143       pool,
144       inserted_comment.id,
145       &CommentUpdateForm::builder().removed(Some(true)).build(),
146     )
147     .await
148     .unwrap();
149     Comment::update(
150       pool,
151       inserted_child_comment.id,
152       &CommentUpdateForm::builder().removed(Some(true)).build(),
153     )
154     .await
155     .unwrap();
156
157     let after_parent_comment_removed = PersonAggregates::read(pool, inserted_person.id)
158       .await
159       .unwrap();
160     assert_eq!(0, after_parent_comment_removed.comment_count);
161     assert_eq!(0, after_parent_comment_removed.comment_score);
162
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)
166       .await
167       .unwrap();
168     let after_parent_comment_delete = PersonAggregates::read(pool, inserted_person.id)
169       .await
170       .unwrap();
171     assert_eq!(0, after_parent_comment_delete.comment_count);
172     assert_eq!(0, after_parent_comment_delete.comment_score);
173
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))
178         .await
179         .unwrap();
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)
183       .await
184       .unwrap();
185     assert_eq!(2, after_comment_add.comment_count);
186     assert_eq!(1, after_comment_add.comment_score);
187
188     Post::delete(pool, inserted_post.id).await.unwrap();
189     let after_post_delete = PersonAggregates::read(pool, inserted_person.id)
190       .await
191       .unwrap();
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);
196
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)
201       .await
202       .unwrap();
203
204     // Delete the community
205     let community_num_deleted = Community::delete(pool, inserted_community.id)
206       .await
207       .unwrap();
208     assert_eq!(1, community_num_deleted);
209
210     // Should be none found
211     let after_delete = PersonAggregates::read(pool, inserted_person.id).await;
212     assert!(after_delete.is_err());
213
214     Instance::delete(pool, inserted_instance.id).await.unwrap();
215   }
216 }