]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/community_aggregates.rs
fc8cf0b063c40ebd936b4580102af10f937cd273
[lemmy.git] / crates / db_schema / src / aggregates / community_aggregates.rs
1 use crate::{
2   aggregates::structs::CommunityAggregates,
3   newtypes::CommunityId,
4   schema::community_aggregates,
5 };
6 use diesel::{result::Error, *};
7
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))
12       .first::<Self>(conn)
13   }
14 }
15
16 #[cfg(test)]
17 mod tests {
18   use crate::{
19     aggregates::community_aggregates::CommunityAggregates,
20     source::{
21       comment::{Comment, CommentForm},
22       community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
23       person::{Person, PersonForm},
24       post::{Post, PostForm},
25     },
26     traits::{Crud, Followable},
27     utils::establish_unpooled_connection,
28   };
29   use serial_test::serial;
30
31   #[test]
32   #[serial]
33   fn test_crud() {
34     let conn = establish_unpooled_connection();
35
36     let new_person = PersonForm {
37       name: "thommy_community_agg".into(),
38       ..PersonForm::default()
39     };
40
41     let inserted_person = Person::create(&conn, &new_person).unwrap();
42
43     let another_person = PersonForm {
44       name: "jerry_community_agg".into(),
45       ..PersonForm::default()
46     };
47
48     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
49
50     let new_community = CommunityForm {
51       name: "TIL_community_agg".into(),
52       title: "nada".to_owned(),
53       ..CommunityForm::default()
54     };
55
56     let inserted_community = Community::create(&conn, &new_community).unwrap();
57
58     let another_community = CommunityForm {
59       name: "TIL_community_agg_2".into(),
60       title: "nada".to_owned(),
61       ..CommunityForm::default()
62     };
63
64     let another_inserted_community = Community::create(&conn, &another_community).unwrap();
65
66     let first_person_follow = CommunityFollowerForm {
67       community_id: inserted_community.id,
68       person_id: inserted_person.id,
69       pending: Some(false),
70     };
71
72     CommunityFollower::follow(&conn, &first_person_follow).unwrap();
73
74     let second_person_follow = CommunityFollowerForm {
75       community_id: inserted_community.id,
76       person_id: another_inserted_person.id,
77       pending: Some(false),
78     };
79
80     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
81
82     let another_community_follow = CommunityFollowerForm {
83       community_id: another_inserted_community.id,
84       person_id: inserted_person.id,
85       pending: Some(false),
86     };
87
88     CommunityFollower::follow(&conn, &another_community_follow).unwrap();
89
90     let new_post = PostForm {
91       name: "A test post".into(),
92       creator_id: inserted_person.id,
93       community_id: inserted_community.id,
94       ..PostForm::default()
95     };
96
97     let inserted_post = Post::create(&conn, &new_post).unwrap();
98
99     let comment_form = CommentForm {
100       content: "A test comment".into(),
101       creator_id: inserted_person.id,
102       post_id: inserted_post.id,
103       ..CommentForm::default()
104     };
105
106     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
107
108     let child_comment_form = CommentForm {
109       content: "A test comment".into(),
110       creator_id: inserted_person.id,
111       post_id: inserted_post.id,
112       parent_id: Some(inserted_comment.id),
113       ..CommentForm::default()
114     };
115
116     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
117
118     let community_aggregates_before_delete =
119       CommunityAggregates::read(&conn, inserted_community.id).unwrap();
120
121     assert_eq!(2, community_aggregates_before_delete.subscribers);
122     assert_eq!(1, community_aggregates_before_delete.posts);
123     assert_eq!(2, community_aggregates_before_delete.comments);
124
125     // Test the other community
126     let another_community_aggs =
127       CommunityAggregates::read(&conn, another_inserted_community.id).unwrap();
128     assert_eq!(1, another_community_aggs.subscribers);
129     assert_eq!(0, another_community_aggs.posts);
130     assert_eq!(0, another_community_aggs.comments);
131
132     // Unfollow test
133     CommunityFollower::unfollow(&conn, &second_person_follow).unwrap();
134     let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
135     assert_eq!(1, after_unfollow.subscribers);
136
137     // Follow again just for the later tests
138     CommunityFollower::follow(&conn, &second_person_follow).unwrap();
139     let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
140     assert_eq!(2, after_follow_again.subscribers);
141
142     // Remove a parent comment (the comment count should also be 0)
143     Post::delete(&conn, inserted_post.id).unwrap();
144     let after_parent_post_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
145     assert_eq!(0, after_parent_post_delete.comments);
146     assert_eq!(0, after_parent_post_delete.posts);
147
148     // Remove the 2nd person
149     Person::delete(&conn, another_inserted_person.id).unwrap();
150     let after_person_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
151     assert_eq!(1, after_person_delete.subscribers);
152
153     // This should delete all the associated rows, and fire triggers
154     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
155     assert_eq!(1, person_num_deleted);
156
157     // Delete the community
158     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
159     assert_eq!(1, community_num_deleted);
160
161     let another_community_num_deleted =
162       Community::delete(&conn, another_inserted_community.id).unwrap();
163     assert_eq!(1, another_community_num_deleted);
164
165     // Should be none found, since the creator was deleted
166     let after_delete = CommunityAggregates::read(&conn, inserted_community.id);
167     assert!(after_delete.is_err());
168   }
169 }