]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/community_aggregates.rs
1a2c4d2477f9ab75f4b960e665e4baeb16c4112b
[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, CommentInsertForm},
22       community::{Community, CommunityFollower, CommunityFollowerForm, CommunityInsertForm},
23       instance::Instance,
24       person::{Person, PersonInsertForm},
25       post::{Post, PostInsertForm},
26     },
27     traits::{Crud, Followable},
28     utils::establish_unpooled_connection,
29   };
30   use serial_test::serial;
31
32   #[test]
33   #[serial]
34   fn test_crud() {
35     let conn = &mut establish_unpooled_connection();
36
37     let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
38
39     let new_person = PersonInsertForm::builder()
40       .name("thommy_community_agg".into())
41       .public_key("pubkey".to_string())
42       .instance_id(inserted_instance.id)
43       .build();
44
45     let inserted_person = Person::create(conn, &new_person).unwrap();
46
47     let another_person = PersonInsertForm::builder()
48       .name("jerry_community_agg".into())
49       .public_key("pubkey".to_string())
50       .instance_id(inserted_instance.id)
51       .build();
52
53     let another_inserted_person = Person::create(conn, &another_person).unwrap();
54
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)
60       .build();
61
62     let inserted_community = Community::create(conn, &new_community).unwrap();
63
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)
69       .build();
70
71     let another_inserted_community = Community::create(conn, &another_community).unwrap();
72
73     let first_person_follow = CommunityFollowerForm {
74       community_id: inserted_community.id,
75       person_id: inserted_person.id,
76       pending: false,
77     };
78
79     CommunityFollower::follow(conn, &first_person_follow).unwrap();
80
81     let second_person_follow = CommunityFollowerForm {
82       community_id: inserted_community.id,
83       person_id: another_inserted_person.id,
84       pending: false,
85     };
86
87     CommunityFollower::follow(conn, &second_person_follow).unwrap();
88
89     let another_community_follow = CommunityFollowerForm {
90       community_id: another_inserted_community.id,
91       person_id: inserted_person.id,
92       pending: false,
93     };
94
95     CommunityFollower::follow(conn, &another_community_follow).unwrap();
96
97     let new_post = PostInsertForm::builder()
98       .name("A test post".into())
99       .creator_id(inserted_person.id)
100       .community_id(inserted_community.id)
101       .build();
102
103     let inserted_post = Post::create(conn, &new_post).unwrap();
104
105     let comment_form = CommentInsertForm::builder()
106       .content("A test comment".into())
107       .creator_id(inserted_person.id)
108       .post_id(inserted_post.id)
109       .build();
110
111     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
112
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)
117       .build();
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 }