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,
74 let inserted_user = User_::create(&conn, &new_user).unwrap();
76 let site_form = SiteForm {
77 name: "test_site".into(),
81 creator_id: inserted_user.id,
82 enable_downvotes: true,
83 open_registration: true,
88 Site::create(&conn, &site_form).unwrap();
90 let new_community = CommunityForm {
91 name: "TIL_site_agg".into(),
92 creator_id: inserted_user.id,
93 title: "nada".to_owned(),
104 last_refreshed_at: None,
110 let inserted_community = Community::create(&conn, &new_community).unwrap();
112 let new_post = PostForm {
113 name: "A test post".into(),
116 creator_id: inserted_user.id,
117 community_id: inserted_community.id,
125 embed_description: None,
133 // Insert two of those posts
134 let inserted_post = Post::create(&conn, &new_post).unwrap();
135 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
137 let comment_form = CommentForm {
138 content: "A test comment".into(),
139 creator_id: inserted_user.id,
140 post_id: inserted_post.id,
151 // Insert two of those comments
152 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
154 let child_comment_form = CommentForm {
155 content: "A test comment".into(),
156 creator_id: inserted_user.id,
157 post_id: inserted_post.id,
161 parent_id: Some(inserted_comment.id),
168 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
170 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
172 assert_eq!(1, site_aggregates_before_delete.users);
173 assert_eq!(1, site_aggregates_before_delete.communities);
174 assert_eq!(2, site_aggregates_before_delete.posts);
175 assert_eq!(2, site_aggregates_before_delete.comments);
178 Post::delete(&conn, inserted_post.id).unwrap();
179 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
180 assert_eq!(1, site_aggregates_after_post_delete.posts);
181 assert_eq!(0, site_aggregates_after_post_delete.comments);
183 // This shouuld delete all the associated rows, and fire triggers
184 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
185 assert_eq!(1, user_num_deleted);
187 let after_delete = SiteAggregates::read(&conn);
188 assert!(after_delete.is_err());