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)
28 use crate::{aggregates::site_aggregates::SiteAggregates, establish_unpooled_connection, Crud};
29 use lemmy_db_schema::source::{
30 comment::{Comment, CommentForm},
31 community::{Community, CommunityForm},
32 person::{Person, PersonForm},
33 post::{Post, PostForm},
34 site::{Site, SiteForm},
36 use serial_test::serial;
41 let conn = establish_unpooled_connection();
43 let new_person = PersonForm {
44 name: "thommy_site_agg".into(),
45 ..PersonForm::default()
48 let inserted_person = Person::create(&conn, &new_person).unwrap();
50 let site_form = SiteForm {
51 name: "test_site".into(),
52 creator_id: inserted_person.id,
57 enable_downvotes: None,
58 open_registration: None,
61 community_creation_admin_only: Some(false),
64 Site::create(&conn, &site_form).unwrap();
66 let new_community = CommunityForm {
67 name: "TIL_site_agg".into(),
68 title: "nada".to_owned(),
69 ..CommunityForm::default()
72 let inserted_community = Community::create(&conn, &new_community).unwrap();
74 let new_post = PostForm {
75 name: "A test post".into(),
76 creator_id: inserted_person.id,
77 community_id: inserted_community.id,
81 // Insert two of those posts
82 let inserted_post = Post::create(&conn, &new_post).unwrap();
83 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
85 let comment_form = CommentForm {
86 content: "A test comment".into(),
87 creator_id: inserted_person.id,
88 post_id: inserted_post.id,
89 ..CommentForm::default()
92 // Insert two of those comments
93 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
95 let child_comment_form = CommentForm {
96 content: "A test comment".into(),
97 creator_id: inserted_person.id,
98 post_id: inserted_post.id,
99 parent_id: Some(inserted_comment.id),
100 ..CommentForm::default()
103 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
105 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
107 assert_eq!(1, site_aggregates_before_delete.users);
108 assert_eq!(1, site_aggregates_before_delete.communities);
109 assert_eq!(2, site_aggregates_before_delete.posts);
110 assert_eq!(2, site_aggregates_before_delete.comments);
113 Post::delete(&conn, inserted_post.id).unwrap();
114 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
115 assert_eq!(1, site_aggregates_after_post_delete.posts);
116 assert_eq!(0, site_aggregates_after_post_delete.comments);
118 // This shouuld delete all the associated rows, and fire triggers
119 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
120 assert_eq!(1, person_num_deleted);
122 // Delete the community
123 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
124 assert_eq!(1, community_num_deleted);
126 let after_delete = SiteAggregates::read(&conn);
127 assert!(after_delete.is_err());