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(),
58 creator_id: inserted_person.id,
63 enable_downvotes: None,
64 open_registration: None,
67 community_creation_admin_only: Some(false),
68 require_email_verification: None,
69 require_application: None,
70 application_question: None,
71 private_instance: None,
74 Site::create(&conn, &site_form).unwrap();
76 let new_community = CommunityForm {
77 name: "TIL_site_agg".into(),
78 title: "nada".to_owned(),
79 ..CommunityForm::default()
82 let inserted_community = Community::create(&conn, &new_community).unwrap();
84 let new_post = PostForm {
85 name: "A test post".into(),
86 creator_id: inserted_person.id,
87 community_id: inserted_community.id,
91 // Insert two of those posts
92 let inserted_post = Post::create(&conn, &new_post).unwrap();
93 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
95 let comment_form = CommentForm {
96 content: "A test comment".into(),
97 creator_id: inserted_person.id,
98 post_id: inserted_post.id,
99 ..CommentForm::default()
102 // Insert two of those comments
103 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
105 let child_comment_form = CommentForm {
106 content: "A test comment".into(),
107 creator_id: inserted_person.id,
108 post_id: inserted_post.id,
109 parent_id: Some(inserted_comment.id),
110 ..CommentForm::default()
113 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
115 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
117 assert_eq!(1, site_aggregates_before_delete.users);
118 assert_eq!(1, site_aggregates_before_delete.communities);
119 assert_eq!(2, site_aggregates_before_delete.posts);
120 assert_eq!(2, site_aggregates_before_delete.comments);
123 Post::delete(&conn, inserted_post.id).unwrap();
124 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
125 assert_eq!(1, site_aggregates_after_post_delete.posts);
126 assert_eq!(0, site_aggregates_after_post_delete.comments);
128 // This shouuld delete all the associated rows, and fire triggers
129 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
130 assert_eq!(1, person_num_deleted);
132 // Delete the community
133 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
134 assert_eq!(1, community_num_deleted);
136 let after_delete = SiteAggregates::read(&conn);
137 assert!(after_delete.is_err());