]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/person_aggregates.rs
6f804590845016db79490b97d44e6c8932ecb309
[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: &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},
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
41     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
42       .await
43       .unwrap();
44
45     let new_person = PersonInsertForm::builder()
46       .name("thommy_user_agg".into())
47       .public_key("pubkey".to_string())
48       .instance_id(inserted_instance.id)
49       .build();
50
51     let inserted_person = Person::create(pool, &new_person).await.unwrap();
52
53     let another_person = PersonInsertForm::builder()
54       .name("jerry_user_agg".into())
55       .public_key("pubkey".to_string())
56       .instance_id(inserted_instance.id)
57       .build();
58
59     let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
60
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)
66       .build();
67
68     let inserted_community = Community::create(pool, &new_community).await.unwrap();
69
70     let new_post = PostInsertForm::builder()
71       .name("A test post".into())
72       .creator_id(inserted_person.id)
73       .community_id(inserted_community.id)
74       .build();
75
76     let inserted_post = Post::create(pool, &new_post).await.unwrap();
77
78     let post_like = PostLikeForm {
79       post_id: inserted_post.id,
80       person_id: inserted_person.id,
81       score: 1,
82     };
83
84     let _inserted_post_like = PostLike::like(pool, &post_like).await.unwrap();
85
86     let comment_form = CommentInsertForm::builder()
87       .content("A test comment".into())
88       .creator_id(inserted_person.id)
89       .post_id(inserted_post.id)
90       .build();
91
92     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
93
94     let mut comment_like = CommentLikeForm {
95       comment_id: inserted_comment.id,
96       person_id: inserted_person.id,
97       post_id: inserted_post.id,
98       score: 1,
99     };
100
101     let _inserted_comment_like = CommentLike::like(pool, &comment_like).await.unwrap();
102
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)
107       .build();
108
109     let inserted_child_comment =
110       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
111         .await
112         .unwrap();
113
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,
118       score: 1,
119     };
120
121     let _inserted_child_comment_like = CommentLike::like(pool, &child_comment_like).await.unwrap();
122
123     let person_aggregates_before_delete = PersonAggregates::read(pool, inserted_person.id)
124       .await
125       .unwrap();
126
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);
131
132     // Remove a post like
133     PostLike::remove(pool, inserted_person.id, inserted_post.id)
134       .await
135       .unwrap();
136     let after_post_like_remove = PersonAggregates::read(pool, inserted_person.id)
137       .await
138       .unwrap();
139     assert_eq!(0, after_post_like_remove.post_score);
140
141     // Remove a parent comment (the scores should also be removed)
142     Comment::delete(pool, inserted_comment.id).await.unwrap();
143     Comment::delete(pool, inserted_child_comment.id)
144       .await
145       .unwrap();
146     let after_parent_comment_delete = PersonAggregates::read(pool, inserted_person.id)
147       .await
148       .unwrap();
149     assert_eq!(0, after_parent_comment_delete.comment_count);
150     assert_eq!(0, after_parent_comment_delete.comment_score);
151
152     // Add in the two comments again, then delete the post.
153     let new_parent_comment = Comment::create(pool, &comment_form, None).await.unwrap();
154     let _new_child_comment =
155       Comment::create(pool, &child_comment_form, Some(&new_parent_comment.path))
156         .await
157         .unwrap();
158     comment_like.comment_id = new_parent_comment.id;
159     CommentLike::like(pool, &comment_like).await.unwrap();
160     let after_comment_add = PersonAggregates::read(pool, inserted_person.id)
161       .await
162       .unwrap();
163     assert_eq!(2, after_comment_add.comment_count);
164     assert_eq!(1, after_comment_add.comment_score);
165
166     Post::delete(pool, inserted_post.id).await.unwrap();
167     let after_post_delete = PersonAggregates::read(pool, inserted_person.id)
168       .await
169       .unwrap();
170     assert_eq!(0, after_post_delete.comment_score);
171     assert_eq!(0, after_post_delete.comment_count);
172     assert_eq!(0, after_post_delete.post_score);
173     assert_eq!(0, after_post_delete.post_count);
174
175     // This should delete all the associated rows, and fire triggers
176     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
177     assert_eq!(1, person_num_deleted);
178     Person::delete(pool, another_inserted_person.id)
179       .await
180       .unwrap();
181
182     // Delete the community
183     let community_num_deleted = Community::delete(pool, inserted_community.id)
184       .await
185       .unwrap();
186     assert_eq!(1, community_num_deleted);
187
188     // Should be none found
189     let after_delete = PersonAggregates::read(pool, inserted_person.id).await;
190     assert!(after_delete.is_err());
191
192     Instance::delete(pool, inserted_instance.id).await.unwrap();
193   }
194 }