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, CommentInsertForm},
16 community::{Community, CommunityInsertForm},
18 person::{Person, PersonInsertForm},
19 post::{Post, PostInsertForm},
20 site::{Site, SiteInsertForm},
23 utils::establish_unpooled_connection,
25 use serial_test::serial;
30 let conn = &mut establish_unpooled_connection();
32 let inserted_instance = Instance::create(conn, "my_domain.tld").unwrap();
34 let new_person = PersonInsertForm::builder()
35 .name("thommy_site_agg".into())
36 .public_key("pubkey".to_string())
37 .instance_id(inserted_instance.id)
40 let inserted_person = Person::create(conn, &new_person).unwrap();
42 let site_form = SiteInsertForm::builder()
43 .name("test_site".into())
44 .instance_id(inserted_instance.id)
47 let inserted_site = Site::create(conn, &site_form).unwrap();
49 let new_community = CommunityInsertForm::builder()
50 .name("TIL_site_agg".into())
51 .title("nada".to_owned())
52 .public_key("pubkey".to_string())
53 .instance_id(inserted_instance.id)
56 let inserted_community = Community::create(conn, &new_community).unwrap();
58 let new_post = PostInsertForm::builder()
59 .name("A test post".into())
60 .creator_id(inserted_person.id)
61 .community_id(inserted_community.id)
64 // Insert two of those posts
65 let inserted_post = Post::create(conn, &new_post).unwrap();
66 let _inserted_post_again = Post::create(conn, &new_post).unwrap();
68 let comment_form = CommentInsertForm::builder()
69 .content("A test comment".into())
70 .creator_id(inserted_person.id)
71 .post_id(inserted_post.id)
74 // Insert two of those comments
75 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
77 let child_comment_form = CommentInsertForm::builder()
78 .content("A test comment".into())
79 .creator_id(inserted_person.id)
80 .post_id(inserted_post.id)
83 let _inserted_child_comment =
84 Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
86 let site_aggregates_before_delete = SiteAggregates::read(conn).unwrap();
88 // TODO: this is unstable, sometimes it returns 0 users, sometimes 1
89 //assert_eq!(0, 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());
116 Instance::delete(conn, inserted_instance.id).unwrap();