1 use crate::schema::site_aggregates;
2 use diesel::{result::Error, *};
5 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)]
6 #[table_name = "site_aggregates"]
7 pub struct SiteAggregates {
16 pub fn read(conn: &PgConnection) -> Result<Self, Error> {
17 site_aggregates::table.first::<Self>(conn)
24 aggregates::site_aggregates::SiteAggregates,
26 comment::{Comment, CommentForm},
27 community::{Community, CommunityForm},
28 post::{Post, PostForm},
29 user::{UserForm, User_},
31 tests::establish_unpooled_connection,
39 let conn = establish_unpooled_connection();
41 let new_user = UserForm {
42 name: "thommy_site_agg".into(),
43 preferred_username: None,
44 password_encrypted: "nope".into(),
54 theme: "browser".into(),
55 default_sort_type: SortType::Hot as i16,
56 default_listing_type: ListingType::Subscribed as i16,
57 lang: "browser".into(),
59 send_notifications_to_email: false,
65 last_refreshed_at: None,
68 let inserted_user = User_::create(&conn, &new_user).unwrap();
70 let new_community = CommunityForm {
71 name: "TIL_site_agg".into(),
72 creator_id: inserted_user.id,
73 title: "nada".to_owned(),
84 last_refreshed_at: None,
90 let inserted_community = Community::create(&conn, &new_community).unwrap();
92 let new_post = PostForm {
93 name: "A test post".into(),
96 creator_id: inserted_user.id,
97 community_id: inserted_community.id,
105 embed_description: None,
113 // Insert two of those posts
114 let inserted_post = Post::create(&conn, &new_post).unwrap();
115 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
117 let comment_form = CommentForm {
118 content: "A test comment".into(),
119 creator_id: inserted_user.id,
120 post_id: inserted_post.id,
131 // Insert two of those comments
132 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
134 let child_comment_form = CommentForm {
135 content: "A test comment".into(),
136 creator_id: inserted_user.id,
137 post_id: inserted_post.id,
141 parent_id: Some(inserted_comment.id),
148 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
150 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
152 assert_eq!(1, site_aggregates_before_delete.users);
153 assert_eq!(1, site_aggregates_before_delete.communities);
154 assert_eq!(2, site_aggregates_before_delete.posts);
155 assert_eq!(2, site_aggregates_before_delete.comments);
158 Post::delete(&conn, inserted_post.id).unwrap();
159 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
160 assert_eq!(1, site_aggregates_after_post_delete.posts);
161 assert_eq!(0, site_aggregates_after_post_delete.comments);
163 // This shouuld delete all the associated rows, and fire triggers
164 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
165 assert_eq!(1, user_num_deleted);
167 let site_aggregates_after_delete = SiteAggregates::read(&conn).unwrap();
168 assert_eq!(0, site_aggregates_after_delete.users);
169 assert_eq!(0, site_aggregates_after_delete.communities);
170 assert_eq!(0, site_aggregates_after_delete.posts);
171 assert_eq!(0, site_aggregates_after_delete.comments);