]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/person_aggregates.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[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: &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 = establish_unpooled_connection();
31
32     let new_person = PersonForm {
33       name: "thommy_user_agg".into(),
34       ..PersonForm::default()
35     };
36
37     let inserted_person = Person::create(&conn, &new_person).unwrap();
38
39     let another_person = PersonForm {
40       name: "jerry_user_agg".into(),
41       ..PersonForm::default()
42     };
43
44     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
45
46     let new_community = CommunityForm {
47       name: "TIL_site_agg".into(),
48       title: "nada".to_owned(),
49       ..CommunityForm::default()
50     };
51
52     let inserted_community = Community::create(&conn, &new_community).unwrap();
53
54     let new_post = PostForm {
55       name: "A test post".into(),
56       creator_id: inserted_person.id,
57       community_id: inserted_community.id,
58       ..PostForm::default()
59     };
60
61     let inserted_post = Post::create(&conn, &new_post).unwrap();
62
63     let post_like = PostLikeForm {
64       post_id: inserted_post.id,
65       person_id: inserted_person.id,
66       score: 1,
67     };
68
69     let _inserted_post_like = PostLike::like(&conn, &post_like).unwrap();
70
71     let comment_form = CommentForm {
72       content: "A test comment".into(),
73       creator_id: inserted_person.id,
74       post_id: inserted_post.id,
75       ..CommentForm::default()
76     };
77
78     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
79
80     let mut comment_like = CommentLikeForm {
81       comment_id: inserted_comment.id,
82       person_id: inserted_person.id,
83       post_id: inserted_post.id,
84       score: 1,
85     };
86
87     let _inserted_comment_like = CommentLike::like(&conn, &comment_like).unwrap();
88
89     let mut child_comment_form = CommentForm {
90       content: "A test comment".into(),
91       creator_id: inserted_person.id,
92       post_id: inserted_post.id,
93       parent_id: Some(inserted_comment.id),
94       ..CommentForm::default()
95     };
96
97     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
98
99     let child_comment_like = CommentLikeForm {
100       comment_id: inserted_child_comment.id,
101       person_id: another_inserted_person.id,
102       post_id: inserted_post.id,
103       score: 1,
104     };
105
106     let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
107
108     let person_aggregates_before_delete =
109       PersonAggregates::read(&conn, inserted_person.id).unwrap();
110
111     assert_eq!(1, person_aggregates_before_delete.post_count);
112     assert_eq!(1, person_aggregates_before_delete.post_score);
113     assert_eq!(2, person_aggregates_before_delete.comment_count);
114     assert_eq!(2, person_aggregates_before_delete.comment_score);
115
116     // Remove a post like
117     PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
118     let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
119     assert_eq!(0, after_post_like_remove.post_score);
120
121     // Remove a parent comment (the scores should also be removed)
122     Comment::delete(&conn, inserted_comment.id).unwrap();
123     let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
124     assert_eq!(0, after_parent_comment_delete.comment_count);
125     assert_eq!(0, after_parent_comment_delete.comment_score);
126
127     // Add in the two comments again, then delete the post.
128     let new_parent_comment = Comment::create(&conn, &comment_form).unwrap();
129     child_comment_form.parent_id = Some(new_parent_comment.id);
130     Comment::create(&conn, &child_comment_form).unwrap();
131     comment_like.comment_id = new_parent_comment.id;
132     CommentLike::like(&conn, &comment_like).unwrap();
133     let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
134     assert_eq!(2, after_comment_add.comment_count);
135     assert_eq!(1, after_comment_add.comment_score);
136
137     Post::delete(&conn, inserted_post.id).unwrap();
138     let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
139     assert_eq!(0, after_post_delete.comment_score);
140     assert_eq!(0, after_post_delete.comment_count);
141     assert_eq!(0, after_post_delete.post_score);
142     assert_eq!(0, after_post_delete.post_count);
143
144     // This should delete all the associated rows, and fire triggers
145     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
146     assert_eq!(1, person_num_deleted);
147     Person::delete(&conn, another_inserted_person.id).unwrap();
148
149     // Delete the community
150     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
151     assert_eq!(1, community_num_deleted);
152
153     // Should be none found
154     let after_delete = PersonAggregates::read(&conn, inserted_person.id);
155     assert!(after_delete.is_err());
156   }
157 }