1 use crate::schema::site_aggregates;
2 use diesel::{result::Error, *};
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)
31 aggregates::site_aggregates::SiteAggregates,
32 establish_unpooled_connection,
34 comment::{Comment, CommentForm},
35 community::{Community, CommunityForm},
36 person::{Person, PersonForm},
37 post::{Post, PostForm},
38 site::{Site, SiteForm},
42 use serial_test::serial;
47 let conn = establish_unpooled_connection();
49 let new_person = PersonForm {
50 name: "thommy_site_agg".into(),
51 ..PersonForm::default()
54 let inserted_person = Person::create(&conn, &new_person).unwrap();
56 let site_form = SiteForm {
57 name: "test_site".into(),
62 enable_downvotes: None,
63 open_registration: None,
66 community_creation_admin_only: Some(false),
67 require_email_verification: None,
68 require_application: None,
69 application_question: None,
70 private_instance: None,
73 Site::create(&conn, &site_form).unwrap();
75 let new_community = CommunityForm {
76 name: "TIL_site_agg".into(),
77 title: "nada".to_owned(),
78 ..CommunityForm::default()
81 let inserted_community = Community::create(&conn, &new_community).unwrap();
83 let new_post = PostForm {
84 name: "A test post".into(),
85 creator_id: inserted_person.id,
86 community_id: inserted_community.id,
90 // Insert two of those posts
91 let inserted_post = Post::create(&conn, &new_post).unwrap();
92 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
94 let comment_form = CommentForm {
95 content: "A test comment".into(),
96 creator_id: inserted_person.id,
97 post_id: inserted_post.id,
98 ..CommentForm::default()
101 // Insert two of those comments
102 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
104 let child_comment_form = CommentForm {
105 content: "A test comment".into(),
106 creator_id: inserted_person.id,
107 post_id: inserted_post.id,
108 parent_id: Some(inserted_comment.id),
109 ..CommentForm::default()
112 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
114 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
116 assert_eq!(1, site_aggregates_before_delete.users);
117 assert_eq!(1, site_aggregates_before_delete.communities);
118 assert_eq!(2, site_aggregates_before_delete.posts);
119 assert_eq!(2, site_aggregates_before_delete.comments);
122 Post::delete(&conn, inserted_post.id).unwrap();
123 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
124 assert_eq!(1, site_aggregates_after_post_delete.posts);
125 assert_eq!(0, site_aggregates_after_post_delete.comments);
127 // This shouuld delete all the associated rows, and fire triggers
128 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
129 assert_eq!(1, person_num_deleted);
131 // Delete the community
132 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
133 assert_eq!(1, community_num_deleted);
135 // Site should still exist, it can without a site creator.
136 let after_delete_creator = SiteAggregates::read(&conn);
137 assert!(after_delete_creator.is_ok());
139 Site::delete(&conn, 1).unwrap();
140 let after_delete_site = SiteAggregates::read(&conn);
141 assert!(after_delete_site.is_err());