]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/community_aggregates.rs
310178f145e0e6f4a692210a0d60af5bb1ce6b95
[lemmy.git] / crates / db_schema / src / aggregates / community_aggregates.rs
1 use crate::{
2   aggregates::structs::CommunityAggregates,
3   newtypes::CommunityId,
4   schema::community_aggregates,
5   utils::{get_conn, DbPool},
6 };
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
9
10 impl CommunityAggregates {
11   pub async fn read(pool: &DbPool, community_id: CommunityId) -> Result<Self, Error> {
12     let conn = &mut get_conn(pool).await?;
13     community_aggregates::table
14       .filter(community_aggregates::community_id.eq(community_id))
15       .first::<Self>(conn)
16       .await
17   }
18 }
19
20 #[cfg(test)]
21 mod tests {
22   use crate::{
23     aggregates::community_aggregates::CommunityAggregates,
24     source::{
25       comment::{Comment, CommentInsertForm},
26       community::{Community, CommunityFollower, CommunityFollowerForm, CommunityInsertForm},
27       instance::Instance,
28       person::{Person, PersonInsertForm},
29       post::{Post, PostInsertForm},
30     },
31     traits::{Crud, Followable},
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_community_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_community_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_community_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 another_community = CommunityInsertForm::builder()
71       .name("TIL_community_agg_2".into())
72       .title("nada".to_owned())
73       .public_key("pubkey".to_string())
74       .instance_id(inserted_instance.id)
75       .build();
76
77     let another_inserted_community = Community::create(pool, &another_community).await.unwrap();
78
79     let first_person_follow = CommunityFollowerForm {
80       community_id: inserted_community.id,
81       person_id: inserted_person.id,
82       pending: false,
83     };
84
85     CommunityFollower::follow(pool, &first_person_follow)
86       .await
87       .unwrap();
88
89     let second_person_follow = CommunityFollowerForm {
90       community_id: inserted_community.id,
91       person_id: another_inserted_person.id,
92       pending: false,
93     };
94
95     CommunityFollower::follow(pool, &second_person_follow)
96       .await
97       .unwrap();
98
99     let another_community_follow = CommunityFollowerForm {
100       community_id: another_inserted_community.id,
101       person_id: inserted_person.id,
102       pending: false,
103     };
104
105     CommunityFollower::follow(pool, &another_community_follow)
106       .await
107       .unwrap();
108
109     let new_post = PostInsertForm::builder()
110       .name("A test post".into())
111       .creator_id(inserted_person.id)
112       .community_id(inserted_community.id)
113       .build();
114
115     let inserted_post = Post::create(pool, &new_post).await.unwrap();
116
117     let comment_form = CommentInsertForm::builder()
118       .content("A test comment".into())
119       .creator_id(inserted_person.id)
120       .post_id(inserted_post.id)
121       .build();
122
123     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
124
125     let child_comment_form = CommentInsertForm::builder()
126       .content("A test comment".into())
127       .creator_id(inserted_person.id)
128       .post_id(inserted_post.id)
129       .build();
130
131     let _inserted_child_comment =
132       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
133         .await
134         .unwrap();
135
136     let community_aggregates_before_delete = CommunityAggregates::read(pool, inserted_community.id)
137       .await
138       .unwrap();
139
140     assert_eq!(2, community_aggregates_before_delete.subscribers);
141     assert_eq!(1, community_aggregates_before_delete.posts);
142     assert_eq!(2, community_aggregates_before_delete.comments);
143
144     // Test the other community
145     let another_community_aggs = CommunityAggregates::read(pool, another_inserted_community.id)
146       .await
147       .unwrap();
148     assert_eq!(1, another_community_aggs.subscribers);
149     assert_eq!(0, another_community_aggs.posts);
150     assert_eq!(0, another_community_aggs.comments);
151
152     // Unfollow test
153     CommunityFollower::unfollow(pool, &second_person_follow)
154       .await
155       .unwrap();
156     let after_unfollow = CommunityAggregates::read(pool, inserted_community.id)
157       .await
158       .unwrap();
159     assert_eq!(1, after_unfollow.subscribers);
160
161     // Follow again just for the later tests
162     CommunityFollower::follow(pool, &second_person_follow)
163       .await
164       .unwrap();
165     let after_follow_again = CommunityAggregates::read(pool, inserted_community.id)
166       .await
167       .unwrap();
168     assert_eq!(2, after_follow_again.subscribers);
169
170     // Remove a parent post (the comment count should also be 0)
171     Post::delete(pool, inserted_post.id).await.unwrap();
172     let after_parent_post_delete = CommunityAggregates::read(pool, inserted_community.id)
173       .await
174       .unwrap();
175     assert_eq!(0, after_parent_post_delete.comments);
176     assert_eq!(0, after_parent_post_delete.posts);
177
178     // Remove the 2nd person
179     Person::delete(pool, another_inserted_person.id)
180       .await
181       .unwrap();
182     let after_person_delete = CommunityAggregates::read(pool, inserted_community.id)
183       .await
184       .unwrap();
185     assert_eq!(1, after_person_delete.subscribers);
186
187     // This should delete all the associated rows, and fire triggers
188     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
189     assert_eq!(1, person_num_deleted);
190
191     // Delete the community
192     let community_num_deleted = Community::delete(pool, inserted_community.id)
193       .await
194       .unwrap();
195     assert_eq!(1, community_num_deleted);
196
197     let another_community_num_deleted = Community::delete(pool, another_inserted_community.id)
198       .await
199       .unwrap();
200     assert_eq!(1, another_community_num_deleted);
201
202     // Should be none found, since the creator was deleted
203     let after_delete = CommunityAggregates::read(pool, inserted_community.id).await;
204     assert!(after_delete.is_err());
205   }
206 }