]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/community_aggregates.rs
Diesel 2.0.0 upgrade (#2452)
[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: &mut 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 = &mut establish_unpooled_connection();
35
36     let new_person = PersonForm {
37       name: "thommy_community_agg".into(),
38       public_key: Some("pubkey".to_string()),
39       ..PersonForm::default()
40     };
41
42     let inserted_person = Person::create(conn, &new_person).unwrap();
43
44     let another_person = PersonForm {
45       name: "jerry_community_agg".into(),
46       public_key: Some("pubkey".to_string()),
47       ..PersonForm::default()
48     };
49
50     let another_inserted_person = Person::create(conn, &another_person).unwrap();
51
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()
57     };
58
59     let inserted_community = Community::create(conn, &new_community).unwrap();
60
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()
66     };
67
68     let another_inserted_community = Community::create(conn, &another_community).unwrap();
69
70     let first_person_follow = CommunityFollowerForm {
71       community_id: inserted_community.id,
72       person_id: inserted_person.id,
73       pending: false,
74     };
75
76     CommunityFollower::follow(conn, &first_person_follow).unwrap();
77
78     let second_person_follow = CommunityFollowerForm {
79       community_id: inserted_community.id,
80       person_id: another_inserted_person.id,
81       pending: false,
82     };
83
84     CommunityFollower::follow(conn, &second_person_follow).unwrap();
85
86     let another_community_follow = CommunityFollowerForm {
87       community_id: another_inserted_community.id,
88       person_id: inserted_person.id,
89       pending: false,
90     };
91
92     CommunityFollower::follow(conn, &another_community_follow).unwrap();
93
94     let new_post = PostForm {
95       name: "A test post".into(),
96       creator_id: inserted_person.id,
97       community_id: inserted_community.id,
98       ..PostForm::default()
99     };
100
101     let inserted_post = Post::create(conn, &new_post).unwrap();
102
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()
108     };
109
110     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
111
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()
117     };
118
119     let _inserted_child_comment =
120       Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
121
122     let community_aggregates_before_delete =
123       CommunityAggregates::read(conn, inserted_community.id).unwrap();
124
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);
128
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);
135
136     // Unfollow test
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);
140
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);
145
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);
151
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);
156
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);
160
161     // Delete the community
162     let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
163     assert_eq!(1, community_num_deleted);
164
165     let another_community_num_deleted =
166       Community::delete(conn, another_inserted_community.id).unwrap();
167     assert_eq!(1, another_community_num_deleted);
168
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());
172   }
173 }