1 use crate::schema::site_aggregates;
2 use diesel::{result::Error, *};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
6 #[table_name = "site_aggregates"]
7 pub struct SiteAggregates {
17 pub fn read(conn: &PgConnection) -> Result<Self, Error> {
18 site_aggregates::table.first::<Self>(conn)
25 aggregates::site_aggregates::SiteAggregates,
27 comment::{Comment, CommentForm},
28 community::{Community, CommunityForm},
29 post::{Post, PostForm},
30 user::{UserForm, User_},
32 tests::establish_unpooled_connection,
40 let conn = establish_unpooled_connection();
42 let new_user = UserForm {
43 name: "thommy_site_agg".into(),
44 preferred_username: None,
45 password_encrypted: "nope".into(),
55 theme: "browser".into(),
56 default_sort_type: SortType::Hot as i16,
57 default_listing_type: ListingType::Subscribed as i16,
58 lang: "browser".into(),
60 send_notifications_to_email: false,
66 last_refreshed_at: None,
69 let inserted_user = User_::create(&conn, &new_user).unwrap();
71 let new_community = CommunityForm {
72 name: "TIL_site_agg".into(),
73 creator_id: inserted_user.id,
74 title: "nada".to_owned(),
85 last_refreshed_at: None,
91 let inserted_community = Community::create(&conn, &new_community).unwrap();
93 let new_post = PostForm {
94 name: "A test post".into(),
97 creator_id: inserted_user.id,
98 community_id: inserted_community.id,
106 embed_description: None,
114 // Insert two of those posts
115 let inserted_post = Post::create(&conn, &new_post).unwrap();
116 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
118 let comment_form = CommentForm {
119 content: "A test comment".into(),
120 creator_id: inserted_user.id,
121 post_id: inserted_post.id,
132 // Insert two of those comments
133 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
135 let child_comment_form = CommentForm {
136 content: "A test comment".into(),
137 creator_id: inserted_user.id,
138 post_id: inserted_post.id,
142 parent_id: Some(inserted_comment.id),
149 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
151 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
153 assert_eq!(1, site_aggregates_before_delete.users);
154 assert_eq!(1, site_aggregates_before_delete.communities);
155 assert_eq!(2, site_aggregates_before_delete.posts);
156 assert_eq!(2, site_aggregates_before_delete.comments);
159 Post::delete(&conn, inserted_post.id).unwrap();
160 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
161 assert_eq!(1, site_aggregates_after_post_delete.posts);
162 assert_eq!(0, site_aggregates_after_post_delete.comments);
164 // This shouuld delete all the associated rows, and fire triggers
165 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
166 assert_eq!(1, user_num_deleted);
168 let site_aggregates_after_delete = SiteAggregates::read(&conn).unwrap();
169 assert_eq!(0, site_aggregates_after_delete.users);
170 assert_eq!(0, site_aggregates_after_delete.communities);
171 assert_eq!(0, site_aggregates_after_delete.posts);
172 assert_eq!(0, site_aggregates_after_delete.comments);