1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::site_aggregates;
3 use serde::{Deserialize, Serialize};
6 Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
8 #[table_name = "site_aggregates"]
9 pub struct SiteAggregates {
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,
23 pub fn read(conn: &PgConnection) -> Result<Self, Error> {
24 site_aggregates::table.first::<Self>(conn)
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},
38 use serial_test::serial;
43 let conn = establish_unpooled_connection();
45 let new_person = PersonForm {
46 name: "thommy_site_agg".into(),
47 ..PersonForm::default()
50 let inserted_person = Person::create(&conn, &new_person).unwrap();
52 let site_form = SiteForm {
53 name: "test_site".into(),
54 creator_id: inserted_person.id,
59 enable_downvotes: None,
60 open_registration: None,
63 community_creation_admin_only: Some(false),
66 Site::create(&conn, &site_form).unwrap();
68 let new_community = CommunityForm {
69 name: "TIL_site_agg".into(),
70 title: "nada".to_owned(),
71 ..CommunityForm::default()
74 let inserted_community = Community::create(&conn, &new_community).unwrap();
76 let new_post = PostForm {
77 name: "A test post".into(),
78 creator_id: inserted_person.id,
79 community_id: inserted_community.id,
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();
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()
94 // Insert two of those comments
95 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
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()
105 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
107 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
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);
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);
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);
124 // Delete the community
125 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
126 assert_eq!(1, community_num_deleted);
128 let after_delete = SiteAggregates::read(&conn);
129 assert!(after_delete.is_err());