]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/site_aggregates.rs
First pass at invite-only migration. (#1949)
[lemmy.git] / crates / db_schema / src / aggregates / site_aggregates.rs
1 use crate::schema::site_aggregates;
2 use diesel::{result::Error, *};
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::{
31     aggregates::site_aggregates::SiteAggregates,
32     establish_unpooled_connection,
33     source::{
34       comment::{Comment, CommentForm},
35       community::{Community, CommunityForm},
36       person::{Person, PersonForm},
37       post::{Post, PostForm},
38       site::{Site, SiteForm},
39     },
40     traits::Crud,
41   };
42   use serial_test::serial;
43
44   #[test]
45   #[serial]
46   fn test_crud() {
47     let conn = establish_unpooled_connection();
48
49     let new_person = PersonForm {
50       name: "thommy_site_agg".into(),
51       ..PersonForm::default()
52     };
53
54     let inserted_person = Person::create(&conn, &new_person).unwrap();
55
56     let site_form = SiteForm {
57       name: "test_site".into(),
58       creator_id: inserted_person.id,
59       sidebar: None,
60       description: None,
61       icon: None,
62       banner: None,
63       enable_downvotes: None,
64       open_registration: None,
65       enable_nsfw: None,
66       updated: None,
67       community_creation_admin_only: Some(false),
68       require_email_verification: None,
69       require_application: None,
70       application_question: None,
71       private_instance: None,
72     };
73
74     Site::create(&conn, &site_form).unwrap();
75
76     let new_community = CommunityForm {
77       name: "TIL_site_agg".into(),
78       title: "nada".to_owned(),
79       ..CommunityForm::default()
80     };
81
82     let inserted_community = Community::create(&conn, &new_community).unwrap();
83
84     let new_post = PostForm {
85       name: "A test post".into(),
86       creator_id: inserted_person.id,
87       community_id: inserted_community.id,
88       ..PostForm::default()
89     };
90
91     // Insert two of those posts
92     let inserted_post = Post::create(&conn, &new_post).unwrap();
93     let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
94
95     let comment_form = CommentForm {
96       content: "A test comment".into(),
97       creator_id: inserted_person.id,
98       post_id: inserted_post.id,
99       ..CommentForm::default()
100     };
101
102     // Insert two of those comments
103     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
104
105     let child_comment_form = CommentForm {
106       content: "A test comment".into(),
107       creator_id: inserted_person.id,
108       post_id: inserted_post.id,
109       parent_id: Some(inserted_comment.id),
110       ..CommentForm::default()
111     };
112
113     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
114
115     let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
116
117     assert_eq!(1, site_aggregates_before_delete.users);
118     assert_eq!(1, site_aggregates_before_delete.communities);
119     assert_eq!(2, site_aggregates_before_delete.posts);
120     assert_eq!(2, site_aggregates_before_delete.comments);
121
122     // Try a post delete
123     Post::delete(&conn, inserted_post.id).unwrap();
124     let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
125     assert_eq!(1, site_aggregates_after_post_delete.posts);
126     assert_eq!(0, site_aggregates_after_post_delete.comments);
127
128     // This shouuld delete all the associated rows, and fire triggers
129     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
130     assert_eq!(1, person_num_deleted);
131
132     // Delete the community
133     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
134     assert_eq!(1, community_num_deleted);
135
136     let after_delete = SiteAggregates::read(&conn);
137     assert!(after_delete.is_err());
138   }
139 }