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: &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;
41 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
45 let new_person = PersonInsertForm::builder()
46 .name("thommy_community_agg".into())
47 .public_key("pubkey".to_string())
48 .instance_id(inserted_instance.id)
51 let inserted_person = Person::create(pool, &new_person).await.unwrap();
53 let another_person = PersonInsertForm::builder()
54 .name("jerry_community_agg".into())
55 .public_key("pubkey".to_string())
56 .instance_id(inserted_instance.id)
59 let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
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)
68 let inserted_community = Community::create(pool, &new_community).await.unwrap();
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)
77 let another_inserted_community = Community::create(pool, &another_community).await.unwrap();
79 let first_person_follow = CommunityFollowerForm {
80 community_id: inserted_community.id,
81 person_id: inserted_person.id,
85 CommunityFollower::follow(pool, &first_person_follow)
89 let second_person_follow = CommunityFollowerForm {
90 community_id: inserted_community.id,
91 person_id: another_inserted_person.id,
95 CommunityFollower::follow(pool, &second_person_follow)
99 let another_community_follow = CommunityFollowerForm {
100 community_id: another_inserted_community.id,
101 person_id: inserted_person.id,
105 CommunityFollower::follow(pool, &another_community_follow)
109 let new_post = PostInsertForm::builder()
110 .name("A test post".into())
111 .creator_id(inserted_person.id)
112 .community_id(inserted_community.id)
115 let inserted_post = Post::create(pool, &new_post).await.unwrap();
117 let comment_form = CommentInsertForm::builder()
118 .content("A test comment".into())
119 .creator_id(inserted_person.id)
120 .post_id(inserted_post.id)
123 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
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)
131 let _inserted_child_comment =
132 Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
136 let community_aggregates_before_delete = CommunityAggregates::read(pool, inserted_community.id)
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);
144 // Test the other community
145 let another_community_aggs = CommunityAggregates::read(pool, another_inserted_community.id)
148 assert_eq!(1, another_community_aggs.subscribers);
149 assert_eq!(0, another_community_aggs.posts);
150 assert_eq!(0, another_community_aggs.comments);
153 CommunityFollower::unfollow(pool, &second_person_follow)
156 let after_unfollow = CommunityAggregates::read(pool, inserted_community.id)
159 assert_eq!(1, after_unfollow.subscribers);
161 // Follow again just for the later tests
162 CommunityFollower::follow(pool, &second_person_follow)
165 let after_follow_again = CommunityAggregates::read(pool, inserted_community.id)
168 assert_eq!(2, after_follow_again.subscribers);
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)
175 assert_eq!(0, after_parent_post_delete.comments);
176 assert_eq!(0, after_parent_post_delete.posts);
178 // Remove the 2nd person
179 Person::delete(pool, another_inserted_person.id)
182 let after_person_delete = CommunityAggregates::read(pool, inserted_community.id)
185 assert_eq!(1, after_person_delete.subscribers);
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);
191 // Delete the community
192 let community_num_deleted = Community::delete(pool, inserted_community.id)
195 assert_eq!(1, community_num_deleted);
197 let another_community_num_deleted = Community::delete(pool, another_inserted_community.id)
200 assert_eq!(1, another_community_num_deleted);
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());