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