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 preferred_username: None,
57 last_refreshed_at: None,
59 shared_inbox_url: None,
63 let inserted_person = Person::create(&conn, &new_person).unwrap();
65 let site_form = SiteForm {
66 name: "test_site".into(),
70 creator_id: inserted_person.id,
71 enable_downvotes: true,
72 open_registration: true,
77 Site::create(&conn, &site_form).unwrap();
79 let new_community = CommunityForm {
80 name: "TIL_site_agg".into(),
81 creator_id: inserted_person.id,
82 title: "nada".to_owned(),
92 last_refreshed_at: None,
98 shared_inbox_url: None,
101 let inserted_community = Community::create(&conn, &new_community).unwrap();
103 let new_post = PostForm {
104 name: "A test post".into(),
107 creator_id: inserted_person.id,
108 community_id: inserted_community.id,
116 embed_description: None,
124 // Insert two of those posts
125 let inserted_post = Post::create(&conn, &new_post).unwrap();
126 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
128 let comment_form = CommentForm {
129 content: "A test comment".into(),
130 creator_id: inserted_person.id,
131 post_id: inserted_post.id,
142 // Insert two of those comments
143 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
145 let child_comment_form = CommentForm {
146 content: "A test comment".into(),
147 creator_id: inserted_person.id,
148 post_id: inserted_post.id,
152 parent_id: Some(inserted_comment.id),
159 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
161 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
163 assert_eq!(1, site_aggregates_before_delete.users);
164 assert_eq!(1, site_aggregates_before_delete.communities);
165 assert_eq!(2, site_aggregates_before_delete.posts);
166 assert_eq!(2, site_aggregates_before_delete.comments);
169 Post::delete(&conn, inserted_post.id).unwrap();
170 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
171 assert_eq!(1, site_aggregates_after_post_delete.posts);
172 assert_eq!(0, site_aggregates_after_post_delete.comments);
174 // This shouuld delete all the associated rows, and fire triggers
175 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
176 assert_eq!(1, person_num_deleted);
178 let after_delete = SiteAggregates::read(&conn);
179 assert!(after_delete.is_err());