]> Untitled Git - lemmy.git/blob - lemmy_db/src/aggregates/site_aggregates.rs
Adding site aggregates unit test.
[lemmy.git] / lemmy_db / src / aggregates / site_aggregates.rs
1 use crate::schema::site_aggregates;
2 use diesel::{result::Error, *};
3 use serde::Serialize;
4
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
6 #[table_name = "site_aggregates"]
7 pub struct SiteAggregates {
8   pub id: i32,
9   pub users: i64,
10   pub posts: i64,
11   pub comments: i64,
12   pub communities: i64,
13 }
14
15 impl SiteAggregates {
16   pub fn read(conn: &PgConnection) -> Result<Self, Error> {
17     site_aggregates::table.first::<Self>(conn)
18   }
19 }
20
21 #[cfg(test)]
22 mod tests {
23   use crate::{
24     aggregates::site_aggregates::SiteAggregates,
25     comment::{Comment, CommentForm},
26     community::{Community, CommunityForm},
27     post::{Post, PostForm},
28     tests::establish_unpooled_connection,
29     user::{UserForm, User_},
30     Crud,
31     ListingType,
32     SortType,
33   };
34
35   #[test]
36   fn test_crud() {
37     let conn = establish_unpooled_connection();
38
39     let new_user = UserForm {
40       name: "thommy_site_agg".into(),
41       preferred_username: None,
42       password_encrypted: "nope".into(),
43       email: None,
44       matrix_user_id: None,
45       avatar: None,
46       banner: None,
47       admin: false,
48       banned: Some(false),
49       published: None,
50       updated: None,
51       show_nsfw: false,
52       theme: "browser".into(),
53       default_sort_type: SortType::Hot as i16,
54       default_listing_type: ListingType::Subscribed as i16,
55       lang: "browser".into(),
56       show_avatars: true,
57       send_notifications_to_email: false,
58       actor_id: None,
59       bio: None,
60       local: true,
61       private_key: None,
62       public_key: None,
63       last_refreshed_at: None,
64     };
65
66     let inserted_user = User_::create(&conn, &new_user).unwrap();
67
68     let new_community = CommunityForm {
69       name: "TIL_site_agg".into(),
70       creator_id: inserted_user.id,
71       title: "nada".to_owned(),
72       description: None,
73       category_id: 1,
74       nsfw: false,
75       removed: None,
76       deleted: None,
77       updated: None,
78       actor_id: None,
79       local: true,
80       private_key: None,
81       public_key: None,
82       last_refreshed_at: None,
83       published: None,
84       icon: None,
85       banner: None,
86     };
87
88     let inserted_community = Community::create(&conn, &new_community).unwrap();
89
90     let new_post = PostForm {
91       name: "A test post".into(),
92       url: None,
93       body: None,
94       creator_id: inserted_user.id,
95       community_id: inserted_community.id,
96       removed: None,
97       deleted: None,
98       locked: None,
99       stickied: None,
100       nsfw: false,
101       updated: None,
102       embed_title: None,
103       embed_description: None,
104       embed_html: None,
105       thumbnail_url: None,
106       ap_id: None,
107       local: true,
108       published: None,
109     };
110
111     let inserted_post = Post::create(&conn, &new_post).unwrap();
112
113     let comment_form = CommentForm {
114       content: "A test comment".into(),
115       creator_id: inserted_user.id,
116       post_id: inserted_post.id,
117       removed: None,
118       deleted: None,
119       read: None,
120       parent_id: None,
121       published: None,
122       updated: None,
123       ap_id: None,
124       local: true,
125     };
126
127     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
128
129     let child_comment_form = CommentForm {
130       content: "A test comment".into(),
131       creator_id: inserted_user.id,
132       post_id: inserted_post.id,
133       removed: None,
134       deleted: None,
135       read: None,
136       parent_id: Some(inserted_comment.id),
137       published: None,
138       updated: None,
139       ap_id: None,
140       local: true,
141     };
142
143     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
144
145     let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
146
147     assert_eq!(1, site_aggregates_before_delete.users);
148     assert_eq!(1, site_aggregates_before_delete.communities);
149     assert_eq!(1, site_aggregates_before_delete.posts);
150     assert_eq!(2, site_aggregates_before_delete.comments);
151
152     // This shouuld delete all the associated rows, and fire triggers
153     let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
154     assert_eq!(1, user_num_deleted);
155
156     let site_aggregates_after_delete = SiteAggregates::read(&conn).unwrap();
157     assert_eq!(0, site_aggregates_after_delete.users);
158     assert_eq!(0, site_aggregates_after_delete.communities);
159     assert_eq!(0, site_aggregates_after_delete.posts);
160     assert_eq!(0, site_aggregates_after_delete.comments);
161   }
162 }