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,
25 comment::{Comment, CommentForm},
26 community::{Community, CommunityForm},
27 post::{Post, PostForm},
28 tests::establish_unpooled_connection,
29 user::{UserForm, User_},
37 let conn = establish_unpooled_connection();
39 let new_user = UserForm {
40 name: "thommy_site_agg".into(),
41 preferred_username: None,
42 password_encrypted: "nope".into(),
52 theme: "browser".into(),
53 default_sort_type: SortType::Hot as i16,
54 default_listing_type: ListingType::Subscribed as i16,
55 lang: "browser".into(),
57 send_notifications_to_email: false,
63 last_refreshed_at: None,
66 let inserted_user = User_::create(&conn, &new_user).unwrap();
68 let new_community = CommunityForm {
69 name: "TIL_site_agg".into(),
70 creator_id: inserted_user.id,
71 title: "nada".to_owned(),
82 last_refreshed_at: None,
88 let inserted_community = Community::create(&conn, &new_community).unwrap();
90 let new_post = PostForm {
91 name: "A test post".into(),
94 creator_id: inserted_user.id,
95 community_id: inserted_community.id,
103 embed_description: None,
111 let inserted_post = Post::create(&conn, &new_post).unwrap();
113 let comment_form = CommentForm {
114 content: "A test comment".into(),
115 creator_id: inserted_user.id,
116 post_id: inserted_post.id,
127 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
129 let child_comment_form = CommentForm {
130 content: "A test comment".into(),
131 creator_id: inserted_user.id,
132 post_id: inserted_post.id,
136 parent_id: Some(inserted_comment.id),
143 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
145 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
147 assert_eq!(1, site_aggregates_before_delete.users);
148 assert_eq!(1, site_aggregates_before_delete.communities);
149 assert_eq!(1, site_aggregates_before_delete.posts);
150 assert_eq!(2, site_aggregates_before_delete.comments);
152 // This shouuld delete all the associated rows, and fire triggers
153 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
154 assert_eq!(1, user_num_deleted);
156 let site_aggregates_after_delete = SiteAggregates::read(&conn).unwrap();
157 assert_eq!(0, site_aggregates_after_delete.users);
158 assert_eq!(0, site_aggregates_after_delete.communities);
159 assert_eq!(0, site_aggregates_after_delete.posts);
160 assert_eq!(0, site_aggregates_after_delete.comments);