]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/post_aggregates.rs
Moving settings to Database. (#2492)
[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, CommentInsertForm},
18       community::{Community, CommunityInsertForm},
19       instance::Instance,
20       person::{Person, PersonInsertForm},
21       post::{Post, PostInsertForm, PostLike, PostLikeForm},
22     },
23     traits::{Crud, Likeable},
24     utils::establish_unpooled_connection,
25   };
26   use serial_test::serial;
27
28   #[test]
29   #[serial]
30   fn test_crud() {
31     let conn = &mut establish_unpooled_connection();
32
33     let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
34
35     let new_person = PersonInsertForm::builder()
36       .name("thommy_community_agg".into())
37       .public_key("pubkey".to_string())
38       .instance_id(inserted_instance.id)
39       .build();
40
41     let inserted_person = Person::create(conn, &new_person).unwrap();
42
43     let another_person = PersonInsertForm::builder()
44       .name("jerry_community_agg".into())
45       .public_key("pubkey".to_string())
46       .instance_id(inserted_instance.id)
47       .build();
48
49     let another_inserted_person = Person::create(conn, &another_person).unwrap();
50
51     let new_community = CommunityInsertForm::builder()
52       .name("TIL_community_agg".into())
53       .title("nada".to_owned())
54       .public_key("pubkey".to_string())
55       .instance_id(inserted_instance.id)
56       .build();
57
58     let inserted_community = Community::create(conn, &new_community).unwrap();
59
60     let new_post = PostInsertForm::builder()
61       .name("A test post".into())
62       .creator_id(inserted_person.id)
63       .community_id(inserted_community.id)
64       .build();
65
66     let inserted_post = Post::create(conn, &new_post).unwrap();
67
68     let comment_form = CommentInsertForm::builder()
69       .content("A test comment".into())
70       .creator_id(inserted_person.id)
71       .post_id(inserted_post.id)
72       .build();
73
74     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
75
76     let child_comment_form = CommentInsertForm::builder()
77       .content("A test comment".into())
78       .creator_id(inserted_person.id)
79       .post_id(inserted_post.id)
80       .build();
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     Instance::delete(conn, inserted_instance.id).unwrap();
147   }
148 }