]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/person_aggregates.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_schema / src / aggregates / person_aggregates.rs
1 use crate::{aggregates::structs::PersonAggregates, newtypes::PersonId, schema::person_aggregates};
2 use diesel::{result::Error, *};
3
4 impl PersonAggregates {
5   pub fn read(conn: &mut PgConnection, person_id: PersonId) -> Result<Self, Error> {
6     person_aggregates::table
7       .filter(person_aggregates::person_id.eq(person_id))
8       .first::<Self>(conn)
9   }
10 }
11
12 #[cfg(test)]
13 mod tests {
14   use crate::{
15     aggregates::person_aggregates::PersonAggregates,
16     source::{
17       comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
18       community::{Community, CommunityForm},
19       person::{Person, PersonForm},
20       post::{Post, PostForm, PostLike, PostLikeForm},
21     },
22     traits::{Crud, Likeable},
23     utils::establish_unpooled_connection,
24   };
25   use serial_test::serial;
26
27   #[test]
28   #[serial]
29   fn test_crud() {
30     let conn = &mut establish_unpooled_connection();
31
32     let new_person = PersonForm {
33       name: "thommy_user_agg".into(),
34       public_key: Some("pubkey".to_string()),
35       ..PersonForm::default()
36     };
37
38     let inserted_person = Person::create(conn, &new_person).unwrap();
39
40     let another_person = PersonForm {
41       name: "jerry_user_agg".into(),
42       public_key: Some("pubkey".to_string()),
43       ..PersonForm::default()
44     };
45
46     let another_inserted_person = Person::create(conn, &another_person).unwrap();
47
48     let new_community = CommunityForm {
49       name: "TIL_site_agg".into(),
50       title: "nada".to_owned(),
51       public_key: Some("pubkey".to_string()),
52       ..CommunityForm::default()
53     };
54
55     let inserted_community = Community::create(conn, &new_community).unwrap();
56
57     let new_post = PostForm {
58       name: "A test post".into(),
59       creator_id: inserted_person.id,
60       community_id: inserted_community.id,
61       ..PostForm::default()
62     };
63
64     let inserted_post = Post::create(conn, &new_post).unwrap();
65
66     let post_like = PostLikeForm {
67       post_id: inserted_post.id,
68       person_id: inserted_person.id,
69       score: 1,
70     };
71
72     let _inserted_post_like = PostLike::like(conn, &post_like).unwrap();
73
74     let comment_form = CommentForm {
75       content: "A test comment".into(),
76       creator_id: inserted_person.id,
77       post_id: inserted_post.id,
78       ..CommentForm::default()
79     };
80
81     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
82
83     let mut comment_like = CommentLikeForm {
84       comment_id: inserted_comment.id,
85       person_id: inserted_person.id,
86       post_id: inserted_post.id,
87       score: 1,
88     };
89
90     let _inserted_comment_like = CommentLike::like(conn, &comment_like).unwrap();
91
92     let child_comment_form = CommentForm {
93       content: "A test comment".into(),
94       creator_id: inserted_person.id,
95       post_id: inserted_post.id,
96       ..CommentForm::default()
97     };
98
99     let inserted_child_comment =
100       Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
101
102     let child_comment_like = CommentLikeForm {
103       comment_id: inserted_child_comment.id,
104       person_id: another_inserted_person.id,
105       post_id: inserted_post.id,
106       score: 1,
107     };
108
109     let _inserted_child_comment_like = CommentLike::like(conn, &child_comment_like).unwrap();
110
111     let person_aggregates_before_delete = PersonAggregates::read(conn, inserted_person.id).unwrap();
112
113     assert_eq!(1, person_aggregates_before_delete.post_count);
114     assert_eq!(1, person_aggregates_before_delete.post_score);
115     assert_eq!(2, person_aggregates_before_delete.comment_count);
116     assert_eq!(2, person_aggregates_before_delete.comment_score);
117
118     // Remove a post like
119     PostLike::remove(conn, inserted_person.id, inserted_post.id).unwrap();
120     let after_post_like_remove = PersonAggregates::read(conn, inserted_person.id).unwrap();
121     assert_eq!(0, after_post_like_remove.post_score);
122
123     // Remove a parent comment (the scores should also be removed)
124     Comment::delete(conn, inserted_comment.id).unwrap();
125     Comment::delete(conn, inserted_child_comment.id).unwrap();
126     let after_parent_comment_delete = PersonAggregates::read(conn, inserted_person.id).unwrap();
127     assert_eq!(0, after_parent_comment_delete.comment_count);
128     assert_eq!(0, after_parent_comment_delete.comment_score);
129
130     // Add in the two comments again, then delete the post.
131     let new_parent_comment = Comment::create(conn, &comment_form, None).unwrap();
132     let _new_child_comment =
133       Comment::create(conn, &child_comment_form, Some(&new_parent_comment.path)).unwrap();
134     comment_like.comment_id = new_parent_comment.id;
135     CommentLike::like(conn, &comment_like).unwrap();
136     let after_comment_add = PersonAggregates::read(conn, inserted_person.id).unwrap();
137     assert_eq!(2, after_comment_add.comment_count);
138     assert_eq!(1, after_comment_add.comment_score);
139
140     Post::delete(conn, inserted_post.id).unwrap();
141     let after_post_delete = PersonAggregates::read(conn, inserted_person.id).unwrap();
142     assert_eq!(0, after_post_delete.comment_score);
143     assert_eq!(0, after_post_delete.comment_count);
144     assert_eq!(0, after_post_delete.post_score);
145     assert_eq!(0, after_post_delete.post_count);
146
147     // This should delete all the associated rows, and fire triggers
148     let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
149     assert_eq!(1, person_num_deleted);
150     Person::delete(conn, another_inserted_person.id).unwrap();
151
152     // Delete the community
153     let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
154     assert_eq!(1, community_num_deleted);
155
156     // Should be none found
157     let after_delete = PersonAggregates::read(conn, inserted_person.id);
158     assert!(after_delete.is_err());
159   }
160 }