]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/site_aggregates.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[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       ..PersonForm::default()
34     };
35
36     let inserted_person = Person::create(&conn, &new_person).unwrap();
37
38     let site_form = SiteForm {
39       name: "test_site".into(),
40       ..Default::default()
41     };
42
43     let inserted_site = Site::create(&conn, &site_form).unwrap();
44
45     let new_community = CommunityForm {
46       name: "TIL_site_agg".into(),
47       title: "nada".to_owned(),
48       ..CommunityForm::default()
49     };
50
51     let inserted_community = Community::create(&conn, &new_community).unwrap();
52
53     let new_post = PostForm {
54       name: "A test post".into(),
55       creator_id: inserted_person.id,
56       community_id: inserted_community.id,
57       ..PostForm::default()
58     };
59
60     // Insert two of those posts
61     let inserted_post = Post::create(&conn, &new_post).unwrap();
62     let _inserted_post_again = Post::create(&conn, &new_post).unwrap();
63
64     let comment_form = CommentForm {
65       content: "A test comment".into(),
66       creator_id: inserted_person.id,
67       post_id: inserted_post.id,
68       ..CommentForm::default()
69     };
70
71     // Insert two of those comments
72     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
73
74     let child_comment_form = CommentForm {
75       content: "A test comment".into(),
76       creator_id: inserted_person.id,
77       post_id: inserted_post.id,
78       parent_id: Some(inserted_comment.id),
79       ..CommentForm::default()
80     };
81
82     let _inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
83
84     let site_aggregates_before_delete = SiteAggregates::read(&conn).unwrap();
85
86     assert_eq!(1, site_aggregates_before_delete.users);
87     assert_eq!(1, site_aggregates_before_delete.communities);
88     assert_eq!(2, site_aggregates_before_delete.posts);
89     assert_eq!(2, site_aggregates_before_delete.comments);
90
91     // Try a post delete
92     Post::delete(&conn, inserted_post.id).unwrap();
93     let site_aggregates_after_post_delete = SiteAggregates::read(&conn).unwrap();
94     assert_eq!(1, site_aggregates_after_post_delete.posts);
95     assert_eq!(0, site_aggregates_after_post_delete.comments);
96
97     // This shouuld delete all the associated rows, and fire triggers
98     let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
99     assert_eq!(1, person_num_deleted);
100
101     // Delete the community
102     let community_num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
103     assert_eq!(1, community_num_deleted);
104
105     // Site should still exist, it can without a site creator.
106     let after_delete_creator = SiteAggregates::read(&conn);
107     assert!(after_delete_creator.is_ok());
108
109     Site::delete(&conn, inserted_site.id).unwrap();
110     let after_delete_site = SiteAggregates::read(&conn);
111     assert!(after_delete_site.is_err());
112   }
113 }