1 use crate::{newtypes::CommunityId, schema::community_aggregates};
2 use diesel::{result::Error, *};
3 use serde::{Deserialize, Serialize};
6 Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
8 #[table_name = "community_aggregates"]
9 pub struct CommunityAggregates {
11 pub community_id: CommunityId,
15 pub published: chrono::NaiveDateTime,
16 pub users_active_day: i64,
17 pub users_active_week: i64,
18 pub users_active_month: i64,
19 pub users_active_half_year: i64,
22 impl CommunityAggregates {
23 pub fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
24 community_aggregates::table
25 .filter(community_aggregates::community_id.eq(community_id))
33 aggregates::community_aggregates::CommunityAggregates,
34 establish_unpooled_connection,
36 comment::{Comment, CommentForm},
37 community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
38 person::{Person, PersonForm},
39 post::{Post, PostForm},
41 traits::{Crud, Followable},
43 use serial_test::serial;
48 let conn = establish_unpooled_connection();
50 let new_person = PersonForm {
51 name: "thommy_community_agg".into(),
52 ..PersonForm::default()
55 let inserted_person = Person::create(&conn, &new_person).unwrap();
57 let another_person = PersonForm {
58 name: "jerry_community_agg".into(),
59 ..PersonForm::default()
62 let another_inserted_person = Person::create(&conn, &another_person).unwrap();
64 let new_community = CommunityForm {
65 name: "TIL_community_agg".into(),
66 title: "nada".to_owned(),
67 ..CommunityForm::default()
70 let inserted_community = Community::create(&conn, &new_community).unwrap();
72 let another_community = CommunityForm {
73 name: "TIL_community_agg_2".into(),
74 title: "nada".to_owned(),
75 ..CommunityForm::default()
78 let another_inserted_community = Community::create(&conn, &another_community).unwrap();
80 let first_person_follow = CommunityFollowerForm {
81 community_id: inserted_community.id,
82 person_id: inserted_person.id,
86 CommunityFollower::follow(&conn, &first_person_follow).unwrap();
88 let second_person_follow = CommunityFollowerForm {
89 community_id: inserted_community.id,
90 person_id: another_inserted_person.id,
94 CommunityFollower::follow(&conn, &second_person_follow).unwrap();
96 let another_community_follow = CommunityFollowerForm {
97 community_id: another_inserted_community.id,
98 person_id: inserted_person.id,
102 CommunityFollower::follow(&conn, &another_community_follow).unwrap();
104 let new_post = PostForm {
105 name: "A test post".into(),
106 creator_id: inserted_person.id,
107 community_id: inserted_community.id,
108 ..PostForm::default()
111 let inserted_post = Post::create(&conn, &new_post).unwrap();
113 let comment_form = CommentForm {
114 content: "A test comment".into(),
115 creator_id: inserted_person.id,
116 post_id: inserted_post.id,
117 ..CommentForm::default()
120 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
122 let child_comment_form = CommentForm {
123 content: "A test comment".into(),
124 creator_id: inserted_person.id,
125 post_id: inserted_post.id,
126 parent_id: Some(inserted_comment.id),
127 ..CommentForm::default()
130 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
132 let community_aggregates_before_delete =
133 CommunityAggregates::read(&conn, inserted_community.id).unwrap();
135 assert_eq!(2, community_aggregates_before_delete.subscribers);
136 assert_eq!(1, community_aggregates_before_delete.posts);
137 assert_eq!(2, community_aggregates_before_delete.comments);
139 // Test the other community
140 let another_community_aggs =
141 CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
142 assert_eq!(1, another_community_aggs.subscribers);
143 assert_eq!(0, another_community_aggs.posts);
144 assert_eq!(0, another_community_aggs.comments);
147 CommunityFollower::unfollow(&conn, &second_person_follow).unwrap();
148 let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
149 assert_eq!(1, after_unfollow.subscribers);
151 // Follow again just for the later tests
152 CommunityFollower::follow(&conn, &second_person_follow).unwrap();
153 let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
154 assert_eq!(2, after_follow_again.subscribers);
156 // Remove a parent comment (the comment count should also be 0)
157 Post::delete(&conn, inserted_post.id).unwrap();
158 let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
159 assert_eq!(0, after_parent_post_delete.comments);
160 assert_eq!(0, after_parent_post_delete.posts);
162 // Remove the 2nd person
163 Person::delete(&conn, another_inserted_person.id).unwrap();
164 let after_person_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
165 assert_eq!(1, after_person_delete.subscribers);
167 // This should delete all the associated rows, and fire triggers
168 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
169 assert_eq!(1, person_num_deleted);
171 // Delete the community
172 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
173 assert_eq!(1, community_num_deleted);
175 let another_community_num_deleted =
176 Community::delete(&conn, another_inserted_community.id).unwrap();
177 assert_eq!(1, another_community_num_deleted);
179 // Should be none found, since the creator was deleted
180 let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
181 assert!(after_delete.is_err());