]> Untitled Git - lemmy.git/blob - crates/db_queries/src/aggregates/site_aggregates.rs
Add both (De)Serialize to all models (#1851)
[lemmy.git] / crates / db_queries / src / aggregates / site_aggregates.rs
1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::site_aggregates;
3 use serde::{Deserialize, Serialize};
4
5 #[derive(
6   Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
7 )]
8 #[table_name = "site_aggregates"]
9 pub struct SiteAggregates {
10   pub id: i32,
11   pub site_id: i32,
12   pub users: i64,
13   pub posts: i64,
14   pub comments: i64,
15   pub communities: i64,
16   pub users_active_day: i64,
17   pub users_active_week: i64,
18   pub users_active_month: i64,
19   pub users_active_half_year: i64,
20 }
21
22 impl SiteAggregates {
23   pub fn read(conn: &PgConnection) -> Result<Self, Error> {
24     site_aggregates::table.first::<Self>(conn)
25   }
26 }
27
28 #[cfg(test)]
29 mod tests {
30   use crate::{aggregates::site_aggregates::SiteAggregates, establish_unpooled_connection, Crud};
31   use lemmy_db_schema::source::{
32     comment::{Comment, CommentForm},
33     community::{Community, CommunityForm},
34     person::{Person, PersonForm},
35     post::{Post, PostForm},
36     site::{Site, SiteForm},
37   };
38   use serial_test::serial;
39
40   #[test]
41   #[serial]
42   fn test_crud() {
43     let conn = establish_unpooled_connection();
44
45     let new_person = PersonForm {
46       name: "thommy_site_agg".into(),
47       ..PersonForm::default()
48     };
49
50     let inserted_person = Person::create(&conn, &new_person).unwrap();
51
52     let site_form = SiteForm {
53       name: "test_site".into(),
54       creator_id: inserted_person.id,
55       sidebar: None,
56       description: None,
57       icon: None,
58       banner: None,
59       enable_downvotes: None,
60       open_registration: None,
61       enable_nsfw: None,
62       updated: None,
63       community_creation_admin_only: Some(false),
64     };
65
66     Site::create(&conn, &site_form).unwrap();
67
68     let new_community = CommunityForm {
69       name: "TIL_site_agg".into(),
70       title: "nada".to_owned(),
71       ..CommunityForm::default()
72     };
73
74     let inserted_community = Community::create(&conn, &new_community).unwrap();
75
76     let new_post = PostForm {
77       name: "A test post".into(),
78       creator_id: inserted_person.id,
79       community_id: inserted_community.id,
80       ..PostForm::default()
81     };
82
83     // Insert two of those posts
84     let inserted_post = Post::create(&conn, &new_post).unwrap();
85     let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
86
87     let comment_form = CommentForm {
88       content: "A test comment".into(),
89       creator_id: inserted_person.id,
90       post_id: inserted_post.id,
91       ..CommentForm::default()
92     };
93
94     // Insert two of those comments
95     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
96
97     let child_comment_form = CommentForm {
98       content: "A test comment".into(),
99       creator_id: inserted_person.id,
100       post_id: inserted_post.id,
101       parent_id: Some(inserted_comment.id),
102       ..CommentForm::default()
103     };
104
105     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
106
107     let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
108
109     assert_eq!(1, site_aggregates_before_delete.users);
110     assert_eq!(1, site_aggregates_before_delete.communities);
111     assert_eq!(2, site_aggregates_before_delete.posts);
112     assert_eq!(2, site_aggregates_before_delete.comments);
113
114     // Try a post delete
115     Post::delete(&conn, inserted_post.id).unwrap();
116     let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
117     assert_eq!(1, site_aggregates_after_post_delete.posts);
118     assert_eq!(0, site_aggregates_after_post_delete.comments);
119
120     // This shouuld delete all the associated rows, and fire triggers
121     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
122     assert_eq!(1, person_num_deleted);
123
124     // Delete the community
125     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
126     assert_eq!(1, community_num_deleted);
127
128     let after_delete = SiteAggregates::read(&conn);
129     assert!(after_delete.is_err());
130   }
131 }