2 aggregates::structs::CommunityAggregates,
4 schema::community_aggregates,
6 use diesel::{result::Error, *};
8 impl CommunityAggregates {
9 pub fn read(conn: &mut PgConnection, community_id: CommunityId) -> Result<Self, Error> {
10 community_aggregates::table
11 .filter(community_aggregates::community_id.eq(community_id))
19 aggregates::community_aggregates::CommunityAggregates,
21 comment::{Comment, CommentInsertForm},
22 community::{Community, CommunityFollower, CommunityFollowerForm, CommunityInsertForm},
24 person::{Person, PersonInsertForm},
25 post::{Post, PostInsertForm},
27 traits::{Crud, Followable},
28 utils::establish_unpooled_connection,
30 use serial_test::serial;
35 let conn = &mut establish_unpooled_connection();
37 let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
39 let new_person = PersonInsertForm::builder()
40 .name("thommy_community_agg".into())
41 .public_key("pubkey".to_string())
42 .instance_id(inserted_instance.id)
45 let inserted_person = Person::create(conn, &new_person).unwrap();
47 let another_person = PersonInsertForm::builder()
48 .name("jerry_community_agg".into())
49 .public_key("pubkey".to_string())
50 .instance_id(inserted_instance.id)
53 let another_inserted_person = Person::create(conn, &another_person).unwrap();
55 let new_community = CommunityInsertForm::builder()
56 .name("TIL_community_agg".into())
57 .title("nada".to_owned())
58 .public_key("pubkey".to_string())
59 .instance_id(inserted_instance.id)
62 let inserted_community = Community::create(conn, &new_community).unwrap();
64 let another_community = CommunityInsertForm::builder()
65 .name("TIL_community_agg_2".into())
66 .title("nada".to_owned())
67 .public_key("pubkey".to_string())
68 .instance_id(inserted_instance.id)
71 let another_inserted_community = Community::create(conn, &another_community).unwrap();
73 let first_person_follow = CommunityFollowerForm {
74 community_id: inserted_community.id,
75 person_id: inserted_person.id,
79 CommunityFollower::follow(conn, &first_person_follow).unwrap();
81 let second_person_follow = CommunityFollowerForm {
82 community_id: inserted_community.id,
83 person_id: another_inserted_person.id,
87 CommunityFollower::follow(conn, &second_person_follow).unwrap();
89 let another_community_follow = CommunityFollowerForm {
90 community_id: another_inserted_community.id,
91 person_id: inserted_person.id,
95 CommunityFollower::follow(conn, &another_community_follow).unwrap();
97 let new_post = PostInsertForm::builder()
98 .name("A test post".into())
99 .creator_id(inserted_person.id)
100 .community_id(inserted_community.id)
103 let inserted_post = Post::create(conn, &new_post).unwrap();
105 let comment_form = CommentInsertForm::builder()
106 .content("A test comment".into())
107 .creator_id(inserted_person.id)
108 .post_id(inserted_post.id)
111 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
113 let child_comment_form = CommentInsertForm::builder()
114 .content("A test comment".into())
115 .creator_id(inserted_person.id)
116 .post_id(inserted_post.id)
119 let _inserted_child_comment =
120 Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
122 let community_aggregates_before_delete =
123 CommunityAggregates::read(conn, inserted_community.id).unwrap();
125 assert_eq!(2, community_aggregates_before_delete.subscribers);
126 assert_eq!(1, community_aggregates_before_delete.posts);
127 assert_eq!(2, community_aggregates_before_delete.comments);
129 // Test the other community
130 let another_community_aggs =
131 CommunityAggregates::read(conn, another_inserted_community.id).unwrap();
132 assert_eq!(1, another_community_aggs.subscribers);
133 assert_eq!(0, another_community_aggs.posts);
134 assert_eq!(0, another_community_aggs.comments);
137 CommunityFollower::unfollow(conn, &second_person_follow).unwrap();
138 let after_unfollow = CommunityAggregates::read(conn, inserted_community.id).unwrap();
139 assert_eq!(1, after_unfollow.subscribers);
141 // Follow again just for the later tests
142 CommunityFollower::follow(conn, &second_person_follow).unwrap();
143 let after_follow_again = CommunityAggregates::read(conn, inserted_community.id).unwrap();
144 assert_eq!(2, after_follow_again.subscribers);
146 // Remove a parent comment (the comment count should also be 0)
147 Post::delete(conn, inserted_post.id).unwrap();
148 let after_parent_post_delete = CommunityAggregates::read(conn, inserted_community.id).unwrap();
149 assert_eq!(0, after_parent_post_delete.comments);
150 assert_eq!(0, after_parent_post_delete.posts);
152 // Remove the 2nd person
153 Person::delete(conn, another_inserted_person.id).unwrap();
154 let after_person_delete = CommunityAggregates::read(conn, inserted_community.id).unwrap();
155 assert_eq!(1, after_person_delete.subscribers);
157 // This should delete all the associated rows, and fire triggers
158 let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
159 assert_eq!(1, person_num_deleted);
161 // Delete the community
162 let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
163 assert_eq!(1, community_num_deleted);
165 let another_community_num_deleted =
166 Community::delete(conn, another_inserted_community.id).unwrap();
167 assert_eq!(1, another_community_num_deleted);
169 // Should be none found, since the creator was deleted
170 let after_delete = CommunityAggregates::read(conn, inserted_community.id);
171 assert!(after_delete.is_err());