]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/comment_aggregates.rs
Moving settings to Database. (#2492)
[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: &mut 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, CommentInsertForm, CommentLike, CommentLikeForm},
22       community::{Community, CommunityInsertForm},
23       instance::Instance,
24       person::{Person, PersonInsertForm},
25       post::{Post, PostInsertForm},
26     },
27     traits::{Crud, Likeable},
28     utils::establish_unpooled_connection,
29   };
30   use serial_test::serial;
31
32   #[test]
33   #[serial]
34   fn test_crud() {
35     let conn = &mut establish_unpooled_connection();
36
37     let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
38
39     let new_person = PersonInsertForm::builder()
40       .name("thommy_comment_agg".into())
41       .public_key("pubkey".into())
42       .instance_id(inserted_instance.id)
43       .build();
44
45     let inserted_person = Person::create(conn, &new_person).unwrap();
46
47     let another_person = PersonInsertForm::builder()
48       .name("jerry_comment_agg".into())
49       .public_key("pubkey".into())
50       .instance_id(inserted_instance.id)
51       .build();
52
53     let another_inserted_person = Person::create(conn, &another_person).unwrap();
54
55     let new_community = CommunityInsertForm::builder()
56       .name("TIL_comment_agg".into())
57       .title("nada".to_owned())
58       .public_key("pubkey".to_string())
59       .instance_id(inserted_instance.id)
60       .build();
61
62     let inserted_community = Community::create(conn, &new_community).unwrap();
63
64     let new_post = PostInsertForm::builder()
65       .name("A test post".into())
66       .creator_id(inserted_person.id)
67       .community_id(inserted_community.id)
68       .build();
69
70     let inserted_post = Post::create(conn, &new_post).unwrap();
71
72     let comment_form = CommentInsertForm::builder()
73       .content("A test comment".into())
74       .creator_id(inserted_person.id)
75       .post_id(inserted_post.id)
76       .build();
77
78     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
79
80     let child_comment_form = CommentInsertForm::builder()
81       .content("A test comment".into())
82       .creator_id(inserted_person.id)
83       .post_id(inserted_post.id)
84       .build();
85
86     let _inserted_child_comment =
87       Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
88
89     let comment_like = CommentLikeForm {
90       comment_id: inserted_comment.id,
91       post_id: inserted_post.id,
92       person_id: inserted_person.id,
93       score: 1,
94     };
95
96     CommentLike::like(conn, &comment_like).unwrap();
97
98     let comment_aggs_before_delete = CommentAggregates::read(conn, inserted_comment.id).unwrap();
99
100     assert_eq!(1, comment_aggs_before_delete.score);
101     assert_eq!(1, comment_aggs_before_delete.upvotes);
102     assert_eq!(0, comment_aggs_before_delete.downvotes);
103
104     // Add a post dislike from the other person
105     let comment_dislike = CommentLikeForm {
106       comment_id: inserted_comment.id,
107       post_id: inserted_post.id,
108       person_id: another_inserted_person.id,
109       score: -1,
110     };
111
112     CommentLike::like(conn, &comment_dislike).unwrap();
113
114     let comment_aggs_after_dislike = CommentAggregates::read(conn, inserted_comment.id).unwrap();
115
116     assert_eq!(0, comment_aggs_after_dislike.score);
117     assert_eq!(1, comment_aggs_after_dislike.upvotes);
118     assert_eq!(1, comment_aggs_after_dislike.downvotes);
119
120     // Remove the first comment like
121     CommentLike::remove(conn, inserted_person.id, inserted_comment.id).unwrap();
122     let after_like_remove = CommentAggregates::read(conn, inserted_comment.id).unwrap();
123     assert_eq!(-1, after_like_remove.score);
124     assert_eq!(0, after_like_remove.upvotes);
125     assert_eq!(1, after_like_remove.downvotes);
126
127     // Remove the parent post
128     Post::delete(conn, inserted_post.id).unwrap();
129
130     // Should be none found, since the post was deleted
131     let after_delete = CommentAggregates::read(conn, inserted_comment.id);
132     assert!(after_delete.is_err());
133
134     // This should delete all the associated rows, and fire triggers
135     Person::delete(conn, another_inserted_person.id).unwrap();
136     let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
137     assert_eq!(1, person_num_deleted);
138
139     // Delete the community
140     let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
141     assert_eq!(1, community_num_deleted);
142
143     Instance::delete(conn, inserted_instance.id).unwrap();
144   }
145 }