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_},
42 use serial_test::serial;
47 let conn = establish_unpooled_connection();
49 let new_user = UserForm {
50 name: "thommy_site_agg".into(),
51 preferred_username: None,
52 password_encrypted: "nope".into(),
62 theme: "browser".into(),
63 default_sort_type: SortType::Hot as i16,
64 default_listing_type: ListingType::Subscribed as i16,
65 lang: "browser".into(),
67 send_notifications_to_email: false,
73 last_refreshed_at: None,
75 shared_inbox_url: None,
78 let inserted_user = User_::create(&conn, &new_user).unwrap();
80 let site_form = SiteForm {
81 name: "test_site".into(),
85 creator_id: inserted_user.id,
86 enable_downvotes: true,
87 open_registration: true,
92 Site::create(&conn, &site_form).unwrap();
94 let new_community = CommunityForm {
95 name: "TIL_site_agg".into(),
96 creator_id: inserted_user.id,
97 title: "nada".to_owned(),
107 last_refreshed_at: None,
113 shared_inbox_url: None,
116 let inserted_community = Community::create(&conn, &new_community).unwrap();
118 let new_post = PostForm {
119 name: "A test post".into(),
122 creator_id: inserted_user.id,
123 community_id: inserted_community.id,
131 embed_description: None,
139 // Insert two of those posts
140 let inserted_post = Post::create(&conn, &new_post).unwrap();
141 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
143 let comment_form = CommentForm {
144 content: "A test comment".into(),
145 creator_id: inserted_user.id,
146 post_id: inserted_post.id,
157 // Insert two of those comments
158 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
160 let child_comment_form = CommentForm {
161 content: "A test comment".into(),
162 creator_id: inserted_user.id,
163 post_id: inserted_post.id,
167 parent_id: Some(inserted_comment.id),
174 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
176 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
178 assert_eq!(1, site_aggregates_before_delete.users);
179 assert_eq!(1, site_aggregates_before_delete.communities);
180 assert_eq!(2, site_aggregates_before_delete.posts);
181 assert_eq!(2, site_aggregates_before_delete.comments);
184 Post::delete(&conn, inserted_post.id).unwrap();
185 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
186 assert_eq!(1, site_aggregates_after_post_delete.posts);
187 assert_eq!(0, site_aggregates_after_post_delete.comments);
189 // This shouuld delete all the associated rows, and fire triggers
190 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
191 assert_eq!(1, user_num_deleted);
193 let after_delete = SiteAggregates::read(&conn);
194 assert!(after_delete.is_err());