1 use crate::{aggregates::structs::SiteAggregates, schema::site_aggregates};
2 use diesel::{result::Error, *};
5 pub fn read(conn: &mut PgConnection) -> Result<Self, Error> {
6 site_aggregates::table.first::<Self>(conn)
13 aggregates::site_aggregates::SiteAggregates,
15 comment::{Comment, CommentForm},
16 community::{Community, CommunityForm},
17 person::{Person, PersonForm},
18 post::{Post, PostForm},
19 site::{Site, SiteForm},
22 utils::establish_unpooled_connection,
24 use serial_test::serial;
29 let conn = &mut establish_unpooled_connection();
31 let new_person = PersonForm {
32 name: "thommy_site_agg".into(),
33 public_key: Some("pubkey".to_string()),
34 ..PersonForm::default()
37 let inserted_person = Person::create(conn, &new_person).unwrap();
39 let site_form = SiteForm {
40 name: "test_site".into(),
41 public_key: Some("pubkey".to_string()),
45 let inserted_site = Site::create(conn, &site_form).unwrap();
47 let new_community = CommunityForm {
48 name: "TIL_site_agg".into(),
49 title: "nada".to_owned(),
50 public_key: Some("pubkey".to_string()),
51 ..CommunityForm::default()
54 let inserted_community = Community::create(conn, &new_community).unwrap();
56 let new_post = PostForm {
57 name: "A test post".into(),
58 creator_id: inserted_person.id,
59 community_id: inserted_community.id,
63 // Insert two of those posts
64 let inserted_post = Post::create(conn, &new_post).unwrap();
65 let _inserted_post_again = Post::create(conn, &new_post).unwrap();
67 let comment_form = CommentForm {
68 content: "A test comment".into(),
69 creator_id: inserted_person.id,
70 post_id: inserted_post.id,
71 ..CommentForm::default()
74 // Insert two of those comments
75 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
77 let child_comment_form = CommentForm {
78 content: "A test comment".into(),
79 creator_id: inserted_person.id,
80 post_id: inserted_post.id,
81 ..CommentForm::default()
84 let _inserted_child_comment =
85 Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
87 let site_aggregates_before_delete = SiteAggregates::read(conn).unwrap();
89 assert_eq!(1, site_aggregates_before_delete.users);
90 assert_eq!(1, site_aggregates_before_delete.communities);
91 assert_eq!(2, site_aggregates_before_delete.posts);
92 assert_eq!(2, site_aggregates_before_delete.comments);
95 Post::delete(conn, inserted_post.id).unwrap();
96 let site_aggregates_after_post_delete = SiteAggregates::read(conn).unwrap();
97 assert_eq!(1, site_aggregates_after_post_delete.posts);
98 assert_eq!(0, site_aggregates_after_post_delete.comments);
100 // This shouuld delete all the associated rows, and fire triggers
101 let person_num_deleted = Person::delete(conn, inserted_person.id).unwrap();
102 assert_eq!(1, person_num_deleted);
104 // Delete the community
105 let community_num_deleted = Community::delete(conn, inserted_community.id).unwrap();
106 assert_eq!(1, community_num_deleted);
108 // Site should still exist, it can without a site creator.
109 let after_delete_creator = SiteAggregates::read(conn);
110 assert!(after_delete_creator.is_ok());
112 Site::delete(conn, inserted_site.id).unwrap();
113 let after_delete_site = SiteAggregates::read(conn);
114 assert!(after_delete_site.is_err());