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