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 tests::establish_unpooled_connection,
31 use lemmy_db_schema::source::{
32 comment::{Comment, CommentForm},
33 community::{Community, CommunityForm},
34 post::{Post, PostForm},
35 user::{UserForm, User_},
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 site_form = SiteForm {
72 name: "test_site".into(),
76 creator_id: inserted_user.id,
77 enable_downvotes: true,
78 open_registration: true,
83 Site::create(&conn, &site_form).unwrap();
85 let new_community = CommunityForm {
86 name: "TIL_site_agg".into(),
87 creator_id: inserted_user.id,
88 title: "nada".to_owned(),
99 last_refreshed_at: None,
105 let inserted_community = Community::create(&conn, &new_community).unwrap();
107 let new_post = PostForm {
108 name: "A test post".into(),
111 creator_id: inserted_user.id,
112 community_id: inserted_community.id,
120 embed_description: None,
128 // Insert two of those posts
129 let inserted_post = Post::create(&conn, &new_post).unwrap();
130 let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
132 let comment_form = CommentForm {
133 content: "A test comment".into(),
134 creator_id: inserted_user.id,
135 post_id: inserted_post.id,
146 // Insert two of those comments
147 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
149 let child_comment_form = CommentForm {
150 content: "A test comment".into(),
151 creator_id: inserted_user.id,
152 post_id: inserted_post.id,
156 parent_id: Some(inserted_comment.id),
163 let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
165 let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
167 assert_eq!(1, site_aggregates_before_delete.users);
168 assert_eq!(1, site_aggregates_before_delete.communities);
169 assert_eq!(2, site_aggregates_before_delete.posts);
170 assert_eq!(2, site_aggregates_before_delete.comments);
173 Post::delete(&conn, inserted_post.id).unwrap();
174 let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
175 assert_eq!(1, site_aggregates_after_post_delete.posts);
176 assert_eq!(0, site_aggregates_after_post_delete.comments);
178 // This shouuld delete all the associated rows, and fire triggers
179 let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
180 assert_eq!(1, user_num_deleted);
182 let after_delete = SiteAggregates::read(&conn);
183 assert!(after_delete.is_err());