]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/person_aggregates.rs
Moving settings to Database. (#2492)
[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, CommentInsertForm, CommentLike, CommentLikeForm},
18       community::{Community, CommunityInsertForm},
19       instance::Instance,
20       person::{Person, PersonInsertForm},
21       post::{Post, PostInsertForm, PostLike, PostLikeForm},
22     },
23     traits::{Crud, Likeable},
24     utils::establish_unpooled_connection,
25   };
26   use serial_test::serial;
27
28   #[test]
29   #[serial]
30   fn test_crud() {
31     let conn = &mut establish_unpooled_connection();
32
33     let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
34
35     let new_person = PersonInsertForm::builder()
36       .name("thommy_user_agg".into())
37       .public_key("pubkey".to_string())
38       .instance_id(inserted_instance.id)
39       .build();
40
41     let inserted_person = Person::create(conn, &new_person).unwrap();
42
43     let another_person = PersonInsertForm::builder()
44       .name("jerry_user_agg".into())
45       .public_key("pubkey".to_string())
46       .instance_id(inserted_instance.id)
47       .build();
48
49     let another_inserted_person = Person::create(conn, &another_person).unwrap();
50
51     let new_community = CommunityInsertForm::builder()
52       .name("TIL_site_agg".into())
53       .title("nada".to_owned())
54       .public_key("pubkey".to_string())
55       .instance_id(inserted_instance.id)
56       .build();
57
58     let inserted_community = Community::create(conn, &new_community).unwrap();
59
60     let new_post = PostInsertForm::builder()
61       .name("A test post".into())
62       .creator_id(inserted_person.id)
63       .community_id(inserted_community.id)
64       .build();
65
66     let inserted_post = Post::create(conn, &new_post).unwrap();
67
68     let post_like = PostLikeForm {
69       post_id: inserted_post.id,
70       person_id: inserted_person.id,
71       score: 1,
72     };
73
74     let _inserted_post_like = PostLike::like(conn, &post_like).unwrap();
75
76     let comment_form = CommentInsertForm::builder()
77       .content("A test comment".into())
78       .creator_id(inserted_person.id)
79       .post_id(inserted_post.id)
80       .build();
81
82     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
83
84     let mut comment_like = CommentLikeForm {
85       comment_id: inserted_comment.id,
86       person_id: inserted_person.id,
87       post_id: inserted_post.id,
88       score: 1,
89     };
90
91     let _inserted_comment_like = CommentLike::like(conn, &comment_like).unwrap();
92
93     let child_comment_form = CommentInsertForm::builder()
94       .content("A test comment".into())
95       .creator_id(inserted_person.id)
96       .post_id(inserted_post.id)
97       .build();
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     Instance::delete(conn, inserted_instance.id).unwrap();
161   }
162 }