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),
70 Site::create(&conn, &site_form).unwrap();
72 let new_community = CommunityForm {
73 name: "TIL_site_agg".into(),
74 title: "nada".to_owned(),
75 ..CommunityForm::default()
78 let inserted_community = Community::create(&conn, &new_community).unwrap();
80 let new_post = PostForm {
81 name: "A test post".into(),
82 creator_id: inserted_person.id,
83 community_id: inserted_community.id,
87 // Insert two of those posts
88 let inserted_post = Post::create(&conn, &new_post).unwrap();
89 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
91 let comment_form = CommentForm {
92 content: "A test comment".into(),
93 creator_id: inserted_person.id,
94 post_id: inserted_post.id,
95 ..CommentForm::default()
98 // Insert two of those comments
99 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
101 let child_comment_form = CommentForm {
102 content: "A test comment".into(),
103 creator_id: inserted_person.id,
104 post_id: inserted_post.id,
105 parent_id: Some(inserted_comment.id),
106 ..CommentForm::default()
109 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
111 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
113 assert_eq!(1, site_aggregates_before_delete.users);
114 assert_eq!(1, site_aggregates_before_delete.communities);
115 assert_eq!(2, site_aggregates_before_delete.posts);
116 assert_eq!(2, site_aggregates_before_delete.comments);
119 Post::delete(&conn, inserted_post.id).unwrap();
120 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
121 assert_eq!(1, site_aggregates_after_post_delete.posts);
122 assert_eq!(0, site_aggregates_after_post_delete.comments);
124 // This shouuld delete all the associated rows, and fire triggers
125 let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
126 assert_eq!(1, person_num_deleted);
128 // Delete the community
129 let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
130 assert_eq!(1, community_num_deleted);
132 let after_delete = SiteAggregates::read(&conn);
133 assert!(after_delete.is_err());