]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/comment_aggregates.rs
Add both (De)Serialize to all models (#1851)
[lemmy.git] / crates / db_queries / src / aggregates / comment_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::{schema::comment_aggregates, CommentId};
3 use serde::{Deserialize, Serialize};
4
5 #[derive(
6   Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
7 )]
8 #[table_name = "comment_aggregates"]
9 pub struct CommentAggregates {
10   pub id: i32,
11   pub comment_id: CommentId,
12   pub score: i64,
13   pub upvotes: i64,
14   pub downvotes: i64,
15   pub published: chrono::NaiveDateTime,
16 }
17
18 impl CommentAggregates {
19   pub fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
20     comment_aggregates::table
21       .filter(comment_aggregates::comment_id.eq(comment_id))
22       .first::<Self>(conn)
23   }
24 }
25
26 #[cfg(test)]
27 mod tests {
28   use crate::{
29     aggregates::comment_aggregates::CommentAggregates,
30     establish_unpooled_connection,
31     Crud,
32     Likeable,
33   };
34   use lemmy_db_schema::source::{
35     comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
36     community::{Community, CommunityForm},
37     person::{Person, PersonForm},
38     post::{Post, PostForm},
39   };
40   use serial_test::serial;
41
42   #[test]
43   #[serial]
44   fn test_crud() {
45     let conn = establish_unpooled_connection();
46
47     let new_person = PersonForm {
48       name: "thommy_comment_agg".into(),
49       ..PersonForm::default()
50     };
51
52     let inserted_person = Person::create(&conn, &new_person).unwrap();
53
54     let another_person = PersonForm {
55       name: "jerry_comment_agg".into(),
56       ..PersonForm::default()
57     };
58
59     let another_inserted_person = Person::create(&conn, &another_person).unwrap();
60
61     let new_community = CommunityForm {
62       name: "TIL_comment_agg".into(),
63       title: "nada".to_owned(),
64       ..CommunityForm::default()
65     };
66
67     let inserted_community = Community::create(&conn, &new_community).unwrap();
68
69     let new_post = PostForm {
70       name: "A test post".into(),
71       creator_id: inserted_person.id,
72       community_id: inserted_community.id,
73       ..PostForm::default()
74     };
75
76     let inserted_post = Post::create(&conn, &new_post).unwrap();
77
78     let comment_form = CommentForm {
79       content: "A test comment".into(),
80       creator_id: inserted_person.id,
81       post_id: inserted_post.id,
82       ..CommentForm::default()
83     };
84
85     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
86
87     let child_comment_form = CommentForm {
88       content: "A test comment".into(),
89       creator_id: inserted_person.id,
90       post_id: inserted_post.id,
91       parent_id: Some(inserted_comment.id),
92       ..CommentForm::default()
93     };
94
95     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
96
97     let comment_like = CommentLikeForm {
98       comment_id: inserted_comment.id,
99       post_id: inserted_post.id,
100       person_id: inserted_person.id,
101       score: 1,
102     };
103
104     CommentLike::like(&conn, &comment_like).unwrap();
105
106     let comment_aggs_before_delete = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
107
108     assert_eq!(1, comment_aggs_before_delete.score);
109     assert_eq!(1, comment_aggs_before_delete.upvotes);
110     assert_eq!(0, comment_aggs_before_delete.downvotes);
111
112     // Add a post dislike from the other person
113     let comment_dislike = CommentLikeForm {
114       comment_id: inserted_comment.id,
115       post_id: inserted_post.id,
116       person_id: another_inserted_person.id,
117       score: -1,
118     };
119
120     CommentLike::like(&conn, &comment_dislike).unwrap();
121
122     let comment_aggs_after_dislike = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
123
124     assert_eq!(0, comment_aggs_after_dislike.score);
125     assert_eq!(1, comment_aggs_after_dislike.upvotes);
126     assert_eq!(1, comment_aggs_after_dislike.downvotes);
127
128     // Remove the first comment like
129     CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
130     let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
131     assert_eq!(-1, after_like_remove.score);
132     assert_eq!(0, after_like_remove.upvotes);
133     assert_eq!(1, after_like_remove.downvotes);
134
135     // Remove the parent post
136     Post::delete(&conn, inserted_post.id).unwrap();
137
138     // Should be none found, since the post was deleted
139     let after_delete = CommentAggregates::read(&conn, inserted_comment.id);
140     assert!(after_delete.is_err());
141
142     // This should delete all the associated rows, and fire triggers
143     Person::delete(&conn, another_inserted_person.id).unwrap();
144     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
145     assert_eq!(1, person_num_deleted);
146
147     // Delete the community
148     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
149     assert_eq!(1, community_num_deleted);
150   }
151 }