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