1 use diesel::{result::Error, *};
2 use lemmy_db_schema::schema::site_aggregates;
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,
26 establish_unpooled_connection,
31 use lemmy_db_schema::source::{
32 comment::{Comment, CommentForm},
33 community::{Community, CommunityForm},
34 post::{Post, PostForm},
35 site::{Site, SiteForm},
36 user::{UserForm, User_},
41 let conn = establish_unpooled_connection();
43 let new_user = UserForm {
44 name: "thommy_site_agg".into(),
45 preferred_username: None,
46 password_encrypted: "nope".into(),
56 theme: "browser".into(),
57 default_sort_type: SortType::Hot as i16,
58 default_listing_type: ListingType::Subscribed as i16,
59 lang: "browser".into(),
61 send_notifications_to_email: false,
67 last_refreshed_at: None,
70 let inserted_user = User_::create(&conn, &new_user).unwrap();
72 let site_form = SiteForm {
73 name: "test_site".into(),
77 creator_id: inserted_user.id,
78 enable_downvotes: true,
79 open_registration: true,
84 Site::create(&conn, &site_form).unwrap();
86 let new_community = CommunityForm {
87 name: "TIL_site_agg".into(),
88 creator_id: inserted_user.id,
89 title: "nada".to_owned(),
100 last_refreshed_at: None,
106 let inserted_community = Community::create(&conn, &new_community).unwrap();
108 let new_post = PostForm {
109 name: "A test post".into(),
112 creator_id: inserted_user.id,
113 community_id: inserted_community.id,
121 embed_description: None,
129 // Insert two of those posts
130 let inserted_post = Post::create(&conn, &new_post).unwrap();
131 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
133 let comment_form = CommentForm {
134 content: "A test comment".into(),
135 creator_id: inserted_user.id,
136 post_id: inserted_post.id,
147 // Insert two of those comments
148 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
150 let child_comment_form = CommentForm {
151 content: "A test comment".into(),
152 creator_id: inserted_user.id,
153 post_id: inserted_post.id,
157 parent_id: Some(inserted_comment.id),
164 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
166 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
168 assert_eq!(1, site_aggregates_before_delete.users);
169 assert_eq!(1, site_aggregates_before_delete.communities);
170 assert_eq!(2, site_aggregates_before_delete.posts);
171 assert_eq!(2, site_aggregates_before_delete.comments);
174 Post::delete(&conn, inserted_post.id).unwrap();
175 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
176 assert_eq!(1, site_aggregates_after_post_delete.posts);
177 assert_eq!(0, site_aggregates_after_post_delete.comments);
179 // This shouuld delete all the associated rows, and fire triggers
180 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
181 assert_eq!(1, user_num_deleted);
183 let after_delete = SiteAggregates::read(&conn);
184 assert!(after_delete.is_err());