2 aggregates::structs::CommunityAggregates,
4 schema::community_aggregates,
5 utils::{get_conn, DbPool},
7 use diesel::{result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::RunQueryDsl;
10 impl CommunityAggregates {
11 pub async fn read(pool: &mut 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))
23 aggregates::community_aggregates::CommunityAggregates,
25 comment::{Comment, CommentInsertForm},
26 community::{Community, CommunityFollower, CommunityFollowerForm, CommunityInsertForm},
28 person::{Person, PersonInsertForm},
29 post::{Post, PostInsertForm},
31 traits::{Crud, Followable},
32 utils::build_db_pool_for_tests,
34 use serial_test::serial;
38 async fn test_crud() {
39 let pool = &build_db_pool_for_tests().await;
40 let pool = &mut pool.into();
42 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
46 let new_person = PersonInsertForm::builder()
47 .name("thommy_community_agg".into())
48 .public_key("pubkey".to_string())
49 .instance_id(inserted_instance.id)
52 let inserted_person = Person::create(pool, &new_person).await.unwrap();
54 let another_person = PersonInsertForm::builder()
55 .name("jerry_community_agg".into())
56 .public_key("pubkey".to_string())
57 .instance_id(inserted_instance.id)
60 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
62 let new_community = CommunityInsertForm::builder()
63 .name("TIL_community_agg".into())
64 .title("nada".to_owned())
65 .public_key("pubkey".to_string())
66 .instance_id(inserted_instance.id)
69 let inserted_community = Community::create(pool, &new_community).await.unwrap();
71 let another_community = CommunityInsertForm::builder()
72 .name("TIL_community_agg_2".into())
73 .title("nada".to_owned())
74 .public_key("pubkey".to_string())
75 .instance_id(inserted_instance.id)
78 let another_inserted_community = Community::create(pool, &another_community).await.unwrap();
80 let first_person_follow = CommunityFollowerForm {
81 community_id: inserted_community.id,
82 person_id: inserted_person.id,
86 CommunityFollower::follow(pool, &first_person_follow)
90 let second_person_follow = CommunityFollowerForm {
91 community_id: inserted_community.id,
92 person_id: another_inserted_person.id,
96 CommunityFollower::follow(pool, &second_person_follow)
100 let another_community_follow = CommunityFollowerForm {
101 community_id: another_inserted_community.id,
102 person_id: inserted_person.id,
106 CommunityFollower::follow(pool, &another_community_follow)
110 let new_post = PostInsertForm::builder()
111 .name("A test post".into())
112 .creator_id(inserted_person.id)
113 .community_id(inserted_community.id)
116 let inserted_post = Post::create(pool, &new_post).await.unwrap();
118 let comment_form = CommentInsertForm::builder()
119 .content("A test comment".into())
120 .creator_id(inserted_person.id)
121 .post_id(inserted_post.id)
124 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
126 let child_comment_form = CommentInsertForm::builder()
127 .content("A test comment".into())
128 .creator_id(inserted_person.id)
129 .post_id(inserted_post.id)
132 let _inserted_child_comment =
133 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
137 let community_aggregates_before_delete = CommunityAggregates::read(pool, inserted_community.id)
141 assert_eq!(2, community_aggregates_before_delete.subscribers);
142 assert_eq!(1, community_aggregates_before_delete.posts);
143 assert_eq!(2, community_aggregates_before_delete.comments);
145 // Test the other community
146 let another_community_aggs = CommunityAggregates::read(pool, another_inserted_community.id)
149 assert_eq!(1, another_community_aggs.subscribers);
150 assert_eq!(0, another_community_aggs.posts);
151 assert_eq!(0, another_community_aggs.comments);
154 CommunityFollower::unfollow(pool, &second_person_follow)
157 let after_unfollow = CommunityAggregates::read(pool, inserted_community.id)
160 assert_eq!(1, after_unfollow.subscribers);
162 // Follow again just for the later tests
163 CommunityFollower::follow(pool, &second_person_follow)
166 let after_follow_again = CommunityAggregates::read(pool, inserted_community.id)
169 assert_eq!(2, after_follow_again.subscribers);
171 // Remove a parent post (the comment count should also be 0)
172 Post::delete(pool, inserted_post.id).await.unwrap();
173 let after_parent_post_delete = CommunityAggregates::read(pool, inserted_community.id)
176 assert_eq!(0, after_parent_post_delete.comments);
177 assert_eq!(0, after_parent_post_delete.posts);
179 // Remove the 2nd person
180 Person::delete(pool, another_inserted_person.id)
183 let after_person_delete = CommunityAggregates::read(pool, inserted_community.id)
186 assert_eq!(1, after_person_delete.subscribers);
188 // This should delete all the associated rows, and fire triggers
189 let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
190 assert_eq!(1, person_num_deleted);
192 // Delete the community
193 let community_num_deleted = Community::delete(pool, inserted_community.id)
196 assert_eq!(1, community_num_deleted);
198 let another_community_num_deleted = Community::delete(pool, another_inserted_community.id)
201 assert_eq!(1, another_community_num_deleted);
203 // Should be none found, since the creator was deleted
204 let after_delete = CommunityAggregates::read(pool, inserted_community.id).await;
205 assert!(after_delete.is_err());