]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/aggregates/site_aggregates.rs
Fix #3501 - Fix aggregation counts for elements removed and deleted (#3543)
[lemmy.git] / crates / db_schema / src / aggregates / site_aggregates.rs
index 719beb8f7b918d0b42fddbb35b6957084fdacab9..cfc14df431e23185ea28c240a647f56ce7a6e7d1 100644 (file)
@@ -19,22 +19,18 @@ mod tests {
     aggregates::site_aggregates::SiteAggregates,
     source::{
       comment::{Comment, CommentInsertForm},
-      community::{Community, CommunityInsertForm},
+      community::{Community, CommunityInsertForm, CommunityUpdateForm},
       instance::Instance,
       person::{Person, PersonInsertForm},
       post::{Post, PostInsertForm},
       site::{Site, SiteInsertForm},
     },
     traits::Crud,
-    utils::build_db_pool_for_tests,
+    utils::{build_db_pool_for_tests, DbPool},
   };
   use serial_test::serial;
 
-  #[tokio::test]
-  #[serial]
-  async fn test_crud() {
-    let pool = &build_db_pool_for_tests().await;
-
+  async fn prepare_site_with_community(pool: &DbPool) -> (Instance, Person, Site, Community) {
     let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
       .await
       .unwrap();
@@ -62,6 +58,21 @@ mod tests {
       .build();
 
     let inserted_community = Community::create(pool, &new_community).await.unwrap();
+    (
+      inserted_instance,
+      inserted_person,
+      inserted_site,
+      inserted_community,
+    )
+  }
+
+  #[tokio::test]
+  #[serial]
+  async fn test_crud() {
+    let pool = &build_db_pool_for_tests().await;
+
+    let (inserted_instance, inserted_person, inserted_site, inserted_community) =
+      prepare_site_with_community(pool).await;
 
     let new_post = PostInsertForm::builder()
       .name("A test post".into())
@@ -127,4 +138,64 @@ mod tests {
 
     Instance::delete(pool, inserted_instance.id).await.unwrap();
   }
+
+  #[tokio::test]
+  #[serial]
+  async fn test_soft_delete() {
+    let pool = &build_db_pool_for_tests().await;
+
+    let (inserted_instance, inserted_person, inserted_site, inserted_community) =
+      prepare_site_with_community(pool).await;
+
+    let site_aggregates_before = SiteAggregates::read(pool).await.unwrap();
+    assert_eq!(1, site_aggregates_before.communities);
+
+    Community::update(
+      pool,
+      inserted_community.id,
+      &CommunityUpdateForm::builder().deleted(Some(true)).build(),
+    )
+    .await
+    .unwrap();
+
+    let site_aggregates_after_delete = SiteAggregates::read(pool).await.unwrap();
+    assert_eq!(0, site_aggregates_after_delete.communities);
+
+    Community::update(
+      pool,
+      inserted_community.id,
+      &CommunityUpdateForm::builder().deleted(Some(false)).build(),
+    )
+    .await
+    .unwrap();
+
+    Community::update(
+      pool,
+      inserted_community.id,
+      &CommunityUpdateForm::builder().removed(Some(true)).build(),
+    )
+    .await
+    .unwrap();
+
+    let site_aggregates_after_remove = SiteAggregates::read(pool).await.unwrap();
+    assert_eq!(0, site_aggregates_after_remove.communities);
+
+    Community::update(
+      pool,
+      inserted_community.id,
+      &CommunityUpdateForm::builder().deleted(Some(true)).build(),
+    )
+    .await
+    .unwrap();
+
+    let site_aggregates_after_remove_delete = SiteAggregates::read(pool).await.unwrap();
+    assert_eq!(0, site_aggregates_after_remove_delete.communities);
+
+    Community::delete(pool, inserted_community.id)
+      .await
+      .unwrap();
+    Site::delete(pool, inserted_site.id).await.unwrap();
+    Person::delete(pool, inserted_person.id).await.unwrap();
+    Instance::delete(pool, inserted_instance.id).await.unwrap();
+  }
 }