1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::site_aggregates;
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "site_aggregates"]
7 pub struct SiteAggregates {
14 pub users_active_day: i64,
15 pub users_active_week: i64,
16 pub users_active_month: i64,
17 pub users_active_half_year: i64,
21 pub fn read(conn: &PgConnection) -> Result<Self, Error> {
22 site_aggregates::table.first::<Self>(conn)
29 aggregates::site_aggregates::SiteAggregates,
30 establish_unpooled_connection,
35 use lemmy_db_schema::source::{
36 comment::{Comment, CommentForm},
37 community::{Community, CommunityForm},
38 post::{Post, PostForm},
39 site::{Site, SiteForm},
40 user::{UserForm, User_},
45 let conn = establish_unpooled_connection();
47 let new_user = UserForm {
48 name: "thommy_site_agg".into(),
49 preferred_username: None,
50 password_encrypted: "nope".into(),
60 theme: "browser".into(),
61 default_sort_type: SortType::Hot as i16,
62 default_listing_type: ListingType::Subscribed as i16,
63 lang: "browser".into(),
65 send_notifications_to_email: false,
71 last_refreshed_at: None,
73 shared_inbox_url: None,
76 let inserted_user = User_::create(&conn, &new_user).unwrap();
78 let site_form = SiteForm {
79 name: "test_site".into(),
83 creator_id: inserted_user.id,
84 enable_downvotes: true,
85 open_registration: true,
90 Site::create(&conn, &site_form).unwrap();
92 let new_community = CommunityForm {
93 name: "TIL_site_agg".into(),
94 creator_id: inserted_user.id,
95 title: "nada".to_owned(),
105 last_refreshed_at: None,
111 shared_inbox_url: None,
114 let inserted_community = Community::create(&conn, &new_community).unwrap();
116 let new_post = PostForm {
117 name: "A test post".into(),
120 creator_id: inserted_user.id,
121 community_id: inserted_community.id,
129 embed_description: None,
137 // Insert two of those posts
138 let inserted_post = Post::create(&conn, &new_post).unwrap();
139 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
141 let comment_form = CommentForm {
142 content: "A test comment".into(),
143 creator_id: inserted_user.id,
144 post_id: inserted_post.id,
155 // Insert two of those comments
156 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
158 let child_comment_form = CommentForm {
159 content: "A test comment".into(),
160 creator_id: inserted_user.id,
161 post_id: inserted_post.id,
165 parent_id: Some(inserted_comment.id),
172 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
174 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
176 assert_eq!(1, site_aggregates_before_delete.users);
177 assert_eq!(1, site_aggregates_before_delete.communities);
178 assert_eq!(2, site_aggregates_before_delete.posts);
179 assert_eq!(2, site_aggregates_before_delete.comments);
182 Post::delete(&conn, inserted_post.id).unwrap();
183 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
184 assert_eq!(1, site_aggregates_after_post_delete.posts);
185 assert_eq!(0, site_aggregates_after_post_delete.comments);
187 // This shouuld delete all the associated rows, and fire triggers
188 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
189 assert_eq!(1, user_num_deleted);
191 let after_delete = SiteAggregates::read(&conn);
192 assert!(after_delete.is_err());