]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/site_aggregates.rs
Fix problem where actors can have empty public key (fixes #2347) (#2348)
[lemmy.git] / crates / db_schema / src / aggregates / site_aggregates.rs
1 use crate::{aggregates::structs::SiteAggregates, schema::site_aggregates};
2 use diesel::{result::Error, *};
3
4 impl SiteAggregates {
5   pub fn read(conn: &PgConnection) -> Result<Self, Error> {
6     site_aggregates::table.first::<Self>(conn)
7   }
8 }
9
10 #[cfg(test)]
11 mod tests {
12   use crate::{
13     aggregates::site_aggregates::SiteAggregates,
14     source::{
15       comment::{Comment, CommentForm},
16       community::{Community, CommunityForm},
17       person::{Person, PersonForm},
18       post::{Post, PostForm},
19       site::{Site, SiteForm},
20     },
21     traits::Crud,
22     utils::establish_unpooled_connection,
23   };
24   use serial_test::serial;
25
26   #[test]
27   #[serial]
28   fn test_crud() {
29     let conn = establish_unpooled_connection();
30
31     let new_person = PersonForm {
32       name: "thommy_site_agg".into(),
33       public_key: Some("pubkey".to_string()),
34       ..PersonForm::default()
35     };
36
37     let inserted_person = Person::create(&conn, &new_person).unwrap();
38
39     let site_form = SiteForm {
40       name: "test_site".into(),
41       public_key: Some("pubkey".to_string()),
42       ..Default::default()
43     };
44
45     let inserted_site = Site::create(&conn, &site_form).unwrap();
46
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()
52     };
53
54     let inserted_community = Community::create(&conn, &new_community).unwrap();
55
56     let new_post = PostForm {
57       name: "A test post".into(),
58       creator_id: inserted_person.id,
59       community_id: inserted_community.id,
60       ..PostForm::default()
61     };
62
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();
66
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()
72     };
73
74     // Insert two of those comments
75     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
76
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       parent_id: Some(inserted_comment.id),
82       ..CommentForm::default()
83     };
84
85     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
86
87     let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
88
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);
93
94     // Try a post delete
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);
99
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);
103
104     // Delete the community
105     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
106     assert_eq!(1, community_num_deleted);
107
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());
111
112     Site::delete(&conn, inserted_site.id).unwrap();
113     let after_delete_site = SiteAggregates::read(&conn);
114     assert!(after_delete_site.is_err());
115   }
116 }