]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/post_aggregates.rs
Diesel 2.0.0 upgrade (#2452)
[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: &mut 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 = &mut establish_unpooled_connection();
31
32     let new_person = PersonForm {
33       name: "thommy_community_agg".into(),
34       public_key: Some("pubkey".to_string()),
35       ..PersonForm::default()
36     };
37
38     let inserted_person = Person::create(conn, &new_person).unwrap();
39
40     let another_person = PersonForm {
41       name: "jerry_community_agg".into(),
42       public_key: Some("pubkey".to_string()),
43       ..PersonForm::default()
44     };
45
46     let another_inserted_person = Person::create(conn, &another_person).unwrap();
47
48     let new_community = CommunityForm {
49       name: "TIL_community_agg".into(),
50       title: "nada".to_owned(),
51       public_key: Some("pubkey".to_string()),
52       ..CommunityForm::default()
53     };
54
55     let inserted_community = Community::create(conn, &new_community).unwrap();
56
57     let new_post = PostForm {
58       name: "A test post".into(),
59       creator_id: inserted_person.id,
60       community_id: inserted_community.id,
61       ..PostForm::default()
62     };
63
64     let inserted_post = Post::create(conn, &new_post).unwrap();
65
66     let comment_form = CommentForm {
67       content: "A test comment".into(),
68       creator_id: inserted_person.id,
69       post_id: inserted_post.id,
70       ..CommentForm::default()
71     };
72
73     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
74
75     let child_comment_form = CommentForm {
76       content: "A test comment".into(),
77       creator_id: inserted_person.id,
78       post_id: inserted_post.id,
79       ..CommentForm::default()
80     };
81
82     let inserted_child_comment =
83       Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
84
85     let post_like = PostLikeForm {
86       post_id: inserted_post.id,
87       person_id: inserted_person.id,
88       score: 1,
89     };
90
91     PostLike::like(conn, &post_like).unwrap();
92
93     let post_aggs_before_delete = PostAggregates::read(conn, inserted_post.id).unwrap();
94
95     assert_eq!(2, post_aggs_before_delete.comments);
96     assert_eq!(1, post_aggs_before_delete.score);
97     assert_eq!(1, post_aggs_before_delete.upvotes);
98     assert_eq!(0, post_aggs_before_delete.downvotes);
99
100     // Add a post dislike from the other person
101     let post_dislike = PostLikeForm {
102       post_id: inserted_post.id,
103       person_id: another_inserted_person.id,
104       score: -1,
105     };
106
107     PostLike::like(conn, &post_dislike).unwrap();
108
109     let post_aggs_after_dislike = PostAggregates::read(conn, inserted_post.id).unwrap();
110
111     assert_eq!(2, post_aggs_after_dislike.comments);
112     assert_eq!(0, post_aggs_after_dislike.score);
113     assert_eq!(1, post_aggs_after_dislike.upvotes);
114     assert_eq!(1, post_aggs_after_dislike.downvotes);
115
116     // Remove the comments
117     Comment::delete(conn, inserted_comment.id).unwrap();
118     Comment::delete(conn, inserted_child_comment.id).unwrap();
119     let after_comment_delete = PostAggregates::read(conn, inserted_post.id).unwrap();
120     assert_eq!(0, after_comment_delete.comments);
121     assert_eq!(0, after_comment_delete.score);
122     assert_eq!(1, after_comment_delete.upvotes);
123     assert_eq!(1, after_comment_delete.downvotes);
124
125     // Remove the first post like
126     PostLike::remove(conn, inserted_person.id, inserted_post.id).unwrap();
127     let after_like_remove = PostAggregates::read(conn, inserted_post.id).unwrap();
128     assert_eq!(0, after_like_remove.comments);
129     assert_eq!(-1, after_like_remove.score);
130     assert_eq!(0, after_like_remove.upvotes);
131     assert_eq!(1, after_like_remove.downvotes);
132
133     // This should delete all the associated rows, and fire triggers
134     Person::delete(conn, another_inserted_person.id).unwrap();
135     let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
136     assert_eq!(1, person_num_deleted);
137
138     // Delete the community
139     let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
140     assert_eq!(1, community_num_deleted);
141
142     // Should be none found, since the creator was deleted
143     let after_delete = PostAggregates::read(conn, inserted_post.id);
144     assert!(after_delete.is_err());
145   }
146 }