]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/comment_aggregates.rs
Still continuing on....
[lemmy.git] / crates / db_queries / src / aggregates / comment_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::comment_aggregates;
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "comment_aggregates"]
7 pub struct CommentAggregates {
8   pub id: i32,
9   pub comment_id: i32,
10   pub score: i64,
11   pub upvotes: i64,
12   pub downvotes: i64,
13   pub published: chrono::NaiveDateTime,
14 }
15
16 impl CommentAggregates {
17   pub fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
18     comment_aggregates::table
19       .filter(comment_aggregates::comment_id.eq(comment_id))
20       .first::<Self>(conn)
21   }
22 }
23
24 #[cfg(test)]
25 mod tests {
26   use crate::{
27     aggregates::comment_aggregates::CommentAggregates,
28     establish_unpooled_connection,
29     Crud,
30     Likeable,
31     ListingType,
32     SortType,
33   };
34   use lemmy_db_schema::source::{
35     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36     community::{Community, CommunityForm},
37     post::{Post, PostForm},
38     person::{PersonForm, Person},
39   };
40
41   #[test]
42   fn test_crud() {
43     let conn = establish_unpooled_connection();
44
45     let new_person = PersonForm {
46       name: "thommy_comment_agg".into(),
47       preferred_username: None,
48       avatar: None,
49       banner: None,
50       banned: None,
51       deleted: None,
52       published: None,
53       updated: None,
54       actor_id: None,
55       bio: None,
56       local: None,
57       private_key: None,
58       public_key: None,
59       last_refreshed_at: None,
60       inbox_url: None,
61       shared_inbox_url: None,
62     };
63
64     let inserted_person = Person::create(&conn, &new_person).unwrap();
65
66     let another_person = PersonForm {
67       name: "jerry_comment_agg".into(),
68       preferred_username: None,
69       avatar: None,
70       banner: None,
71       banned: None,
72       deleted: None,
73       published: None,
74       updated: None,
75       actor_id: None,
76       bio: None,
77       local: None,
78       private_key: None,
79       public_key: None,
80       last_refreshed_at: None,
81       inbox_url: None,
82       shared_inbox_url: None,
83     };
84
85     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
86
87     let new_community = CommunityForm {
88       name: "TIL_comment_agg".into(),
89       creator_id: inserted_person.id,
90       title: "nada".to_owned(),
91       description: None,
92       nsfw: false,
93       removed: None,
94       deleted: None,
95       updated: None,
96       actor_id: None,
97       local: true,
98       private_key: None,
99       public_key: None,
100       last_refreshed_at: None,
101       published: None,
102       icon: None,
103       banner: None,
104       followers_url: None,
105       inbox_url: None,
106       shared_inbox_url: None,
107     };
108
109     let inserted_community = Community::create(&conn, &new_community).unwrap();
110
111     let new_post = PostForm {
112       name: "A test post".into(),
113       url: None,
114       body: None,
115       creator_id: inserted_person.id,
116       community_id: inserted_community.id,
117       removed: None,
118       deleted: None,
119       locked: None,
120       stickied: None,
121       nsfw: false,
122       updated: None,
123       embed_title: None,
124       embed_description: None,
125       embed_html: None,
126       thumbnail_url: None,
127       ap_id: None,
128       local: true,
129       published: None,
130     };
131
132     let inserted_post = Post::create(&conn, &new_post).unwrap();
133
134     let comment_form = CommentForm {
135       content: "A test comment".into(),
136       creator_id: inserted_person.id,
137       post_id: inserted_post.id,
138       removed: None,
139       deleted: None,
140       read: None,
141       parent_id: None,
142       published: None,
143       updated: None,
144       ap_id: None,
145       local: true,
146     };
147
148     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
149
150     let child_comment_form = CommentForm {
151       content: "A test comment".into(),
152       creator_id: inserted_person.id,
153       post_id: inserted_post.id,
154       removed: None,
155       deleted: None,
156       read: None,
157       parent_id: Some(inserted_comment.id),
158       published: None,
159       updated: None,
160       ap_id: None,
161       local: true,
162     };
163
164     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
165
166     let comment_like = CommentLikeForm {
167       comment_id: inserted_comment.id,
168       post_id: inserted_post.id,
169       person_id: inserted_person.id,
170       score: 1,
171     };
172
173     CommentLike::like(&conn, &comment_like).unwrap();
174
175     let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
176
177     assert_eq!(1, comment_aggs_before_delete.score);
178     assert_eq!(1, comment_aggs_before_delete.upvotes);
179     assert_eq!(0, comment_aggs_before_delete.downvotes);
180
181     // Add a post dislike from the other person
182     let comment_dislike = CommentLikeForm {
183       comment_id: inserted_comment.id,
184       post_id: inserted_post.id,
185       person_id: another_inserted_person.id,
186       score: -1,
187     };
188
189     CommentLike::like(&conn, &comment_dislike).unwrap();
190
191     let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
192
193     assert_eq!(0, comment_aggs_after_dislike.score);
194     assert_eq!(1, comment_aggs_after_dislike.upvotes);
195     assert_eq!(1, comment_aggs_after_dislike.downvotes);
196
197     // Remove the first comment like
198     CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
199     let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
200     assert_eq!(-1, after_like_remove.score);
201     assert_eq!(0, after_like_remove.upvotes);
202     assert_eq!(1, after_like_remove.downvotes);
203
204     // Remove the parent post
205     Post::delete(&conn, inserted_post.id).unwrap();
206
207     // Should be none found, since the post was deleted
208     let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
209     assert!(after_delete.is_err());
210
211     // This should delete all the associated rows, and fire triggers
212     Person::delete(&conn, another_inserted_person.id).unwrap();
213     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
214     assert_eq!(1, person_num_deleted);
215   }
216 }