]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/post_aggregates.rs
Add diesel_async, get rid of blocking function (#2510)
[lemmy.git] / crates / db_schema / src / aggregates / post_aggregates.rs
1 use crate::{
2   aggregates::structs::PostAggregates,
3   newtypes::PostId,
4   schema::post_aggregates,
5   utils::{get_conn, DbPool},
6 };
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
9
10 impl PostAggregates {
11   pub async fn read(pool: &DbPool, post_id: PostId) -> Result<Self, Error> {
12     let conn = &mut get_conn(pool).await?;
13     post_aggregates::table
14       .filter(post_aggregates::post_id.eq(post_id))
15       .first::<Self>(conn)
16       .await
17   }
18 }
19
20 #[cfg(test)]
21 mod tests {
22   use crate::{
23     aggregates::post_aggregates::PostAggregates,
24     source::{
25       comment::{Comment, CommentInsertForm},
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_community_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_community_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_community_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 comment_form = CommentInsertForm::builder()
77       .content("A test comment".into())
78       .creator_id(inserted_person.id)
79       .post_id(inserted_post.id)
80       .build();
81
82     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
83
84     let child_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_child_comment =
91       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
92         .await
93         .unwrap();
94
95     let post_like = PostLikeForm {
96       post_id: inserted_post.id,
97       person_id: inserted_person.id,
98       score: 1,
99     };
100
101     PostLike::like(pool, &post_like).await.unwrap();
102
103     let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
104
105     assert_eq!(2, post_aggs_before_delete.comments);
106     assert_eq!(1, post_aggs_before_delete.score);
107     assert_eq!(1, post_aggs_before_delete.upvotes);
108     assert_eq!(0, post_aggs_before_delete.downvotes);
109
110     // Add a post dislike from the other person
111     let post_dislike = PostLikeForm {
112       post_id: inserted_post.id,
113       person_id: another_inserted_person.id,
114       score: -1,
115     };
116
117     PostLike::like(pool, &post_dislike).await.unwrap();
118
119     let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
120
121     assert_eq!(2, post_aggs_after_dislike.comments);
122     assert_eq!(0, post_aggs_after_dislike.score);
123     assert_eq!(1, post_aggs_after_dislike.upvotes);
124     assert_eq!(1, post_aggs_after_dislike.downvotes);
125
126     // Remove the comments
127     Comment::delete(pool, inserted_comment.id).await.unwrap();
128     Comment::delete(pool, inserted_child_comment.id)
129       .await
130       .unwrap();
131     let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
132     assert_eq!(0, after_comment_delete.comments);
133     assert_eq!(0, after_comment_delete.score);
134     assert_eq!(1, after_comment_delete.upvotes);
135     assert_eq!(1, after_comment_delete.downvotes);
136
137     // Remove the first post like
138     PostLike::remove(pool, inserted_person.id, inserted_post.id)
139       .await
140       .unwrap();
141     let after_like_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
142     assert_eq!(0, after_like_remove.comments);
143     assert_eq!(-1, after_like_remove.score);
144     assert_eq!(0, after_like_remove.upvotes);
145     assert_eq!(1, after_like_remove.downvotes);
146
147     // This should delete all the associated rows, and fire triggers
148     Person::delete(pool, another_inserted_person.id)
149       .await
150       .unwrap();
151     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
152     assert_eq!(1, person_num_deleted);
153
154     // Delete the community
155     let community_num_deleted = Community::delete(pool, inserted_community.id)
156       .await
157       .unwrap();
158     assert_eq!(1, community_num_deleted);
159
160     // Should be none found, since the creator was deleted
161     let after_delete = PostAggregates::read(pool, inserted_post.id).await;
162     assert!(after_delete.is_err());
163
164     Instance::delete(pool, inserted_instance.id).await.unwrap();
165   }
166 }