]> Untitled Git - lemmy.git/blob - crates/db_schema/src/aggregates/site_aggregates.rs
719beb8f7b918d0b42fddbb35b6957084fdacab9
[lemmy.git] / crates / db_schema / src / aggregates / site_aggregates.rs
1 use crate::{
2   aggregates::structs::SiteAggregates,
3   schema::site_aggregates,
4   utils::{get_conn, DbPool},
5 };
6 use diesel::result::Error;
7 use diesel_async::RunQueryDsl;
8
9 impl SiteAggregates {
10   pub async fn read(pool: &DbPool) -> Result<Self, Error> {
11     let conn = &mut get_conn(pool).await?;
12     site_aggregates::table.first::<Self>(conn).await
13   }
14 }
15
16 #[cfg(test)]
17 mod tests {
18   use crate::{
19     aggregates::site_aggregates::SiteAggregates,
20     source::{
21       comment::{Comment, CommentInsertForm},
22       community::{Community, CommunityInsertForm},
23       instance::Instance,
24       person::{Person, PersonInsertForm},
25       post::{Post, PostInsertForm},
26       site::{Site, SiteInsertForm},
27     },
28     traits::Crud,
29     utils::build_db_pool_for_tests,
30   };
31   use serial_test::serial;
32
33   #[tokio::test]
34   #[serial]
35   async fn test_crud() {
36     let pool = &build_db_pool_for_tests().await;
37
38     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
39       .await
40       .unwrap();
41
42     let new_person = PersonInsertForm::builder()
43       .name("thommy_site_agg".into())
44       .public_key("pubkey".to_string())
45       .instance_id(inserted_instance.id)
46       .build();
47
48     let inserted_person = Person::create(pool, &new_person).await.unwrap();
49
50     let site_form = SiteInsertForm::builder()
51       .name("test_site".into())
52       .instance_id(inserted_instance.id)
53       .build();
54
55     let inserted_site = Site::create(pool, &site_form).await.unwrap();
56
57     let new_community = CommunityInsertForm::builder()
58       .name("TIL_site_agg".into())
59       .title("nada".to_owned())
60       .public_key("pubkey".to_string())
61       .instance_id(inserted_instance.id)
62       .build();
63
64     let inserted_community = Community::create(pool, &new_community).await.unwrap();
65
66     let new_post = PostInsertForm::builder()
67       .name("A test post".into())
68       .creator_id(inserted_person.id)
69       .community_id(inserted_community.id)
70       .build();
71
72     // Insert two of those posts
73     let inserted_post = Post::create(pool, &new_post).await.unwrap();
74     let _inserted_post_again = Post::create(pool, &new_post).await.unwrap();
75
76     let comment_form = CommentInsertForm::builder()
77       .content("A test comment".into())
78       .creator_id(inserted_person.id)
79       .post_id(inserted_post.id)
80       .build();
81
82     // Insert two of those comments
83     let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
84
85     let child_comment_form = CommentInsertForm::builder()
86       .content("A test comment".into())
87       .creator_id(inserted_person.id)
88       .post_id(inserted_post.id)
89       .build();
90
91     let _inserted_child_comment =
92       Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
93         .await
94         .unwrap();
95
96     let site_aggregates_before_delete = SiteAggregates::read(pool).await.unwrap();
97
98     // TODO: this is unstable, sometimes it returns 0 users, sometimes 1
99     //assert_eq!(0, site_aggregates_before_delete.users);
100     assert_eq!(1, site_aggregates_before_delete.communities);
101     assert_eq!(2, site_aggregates_before_delete.posts);
102     assert_eq!(2, site_aggregates_before_delete.comments);
103
104     // Try a post delete
105     Post::delete(pool, inserted_post.id).await.unwrap();
106     let site_aggregates_after_post_delete = SiteAggregates::read(pool).await.unwrap();
107     assert_eq!(1, site_aggregates_after_post_delete.posts);
108     assert_eq!(0, site_aggregates_after_post_delete.comments);
109
110     // This shouuld delete all the associated rows, and fire triggers
111     let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
112     assert_eq!(1, person_num_deleted);
113
114     // Delete the community
115     let community_num_deleted = Community::delete(pool, inserted_community.id)
116       .await
117       .unwrap();
118     assert_eq!(1, community_num_deleted);
119
120     // Site should still exist, it can without a site creator.
121     let after_delete_creator = SiteAggregates::read(pool).await;
122     assert!(after_delete_creator.is_ok());
123
124     Site::delete(pool, inserted_site.id).await.unwrap();
125     let after_delete_site = SiteAggregates::read(pool).await;
126     assert!(after_delete_site.is_err());
127
128     Instance::delete(pool, inserted_instance.id).await.unwrap();
129   }
130 }