2 aggregates::structs::CommunityAggregates,
4 schema::community_aggregates,
6 use diesel::{result::Error, *};
8 impl CommunityAggregates {
9 pub fn read(conn: &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, CommentForm},
22 community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
23 person::{Person, PersonForm},
24 post::{Post, PostForm},
26 traits::{Crud, Followable},
27 utils::establish_unpooled_connection,
29 use serial_test::serial;
34 let conn = establish_unpooled_connection();
36 let new_person = PersonForm {
37 name: "thommy_community_agg".into(),
38 public_key: Some("pubkey".to_string()),
39 ..PersonForm::default()
42 let inserted_person = Person::create(&conn, &new_person).unwrap();
44 let another_person = PersonForm {
45 name: "jerry_community_agg".into(),
46 public_key: Some("pubkey".to_string()),
47 ..PersonForm::default()
50 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
52 let new_community = CommunityForm {
53 name: "TIL_community_agg".into(),
54 title: "nada".to_owned(),
55 public_key: Some("pubkey".to_string()),
56 ..CommunityForm::default()
59 let inserted_community = Community::create(&conn, &new_community).unwrap();
61 let another_community = CommunityForm {
62 name: "TIL_community_agg_2".into(),
63 title: "nada".to_owned(),
64 public_key: Some("pubkey".to_string()),
65 ..CommunityForm::default()
68 let another_inserted_community = Community::create(&conn, &another_community).unwrap();
70 let first_person_follow = CommunityFollowerForm {
71 community_id: inserted_community.id,
72 person_id: inserted_person.id,
76 CommunityFollower::follow(&conn, &first_person_follow).unwrap();
78 let second_person_follow = CommunityFollowerForm {
79 community_id: inserted_community.id,
80 person_id: another_inserted_person.id,
84 CommunityFollower::follow(&conn, &second_person_follow).unwrap();
86 let another_community_follow = CommunityFollowerForm {
87 community_id: another_inserted_community.id,
88 person_id: inserted_person.id,
92 CommunityFollower::follow(&conn, &another_community_follow).unwrap();
94 let new_post = PostForm {
95 name: "A test post".into(),
96 creator_id: inserted_person.id,
97 community_id: inserted_community.id,
101 let inserted_post = Post::create(&conn, &new_post).unwrap();
103 let comment_form = CommentForm {
104 content: "A test comment".into(),
105 creator_id: inserted_person.id,
106 post_id: inserted_post.id,
107 ..CommentForm::default()
110 let inserted_comment = Comment::create(&conn, &comment_form, None).unwrap();
112 let child_comment_form = CommentForm {
113 content: "A test comment".into(),
114 creator_id: inserted_person.id,
115 post_id: inserted_post.id,
116 ..CommentForm::default()
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());