X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fdb_schema%2Fsrc%2Faggregates%2Fsite_aggregates.rs;h=cfc14df431e23185ea28c240a647f56ce7a6e7d1;hb=9c2490d4f24671cc2770ba68feaf6d7562b1db6a;hp=719beb8f7b918d0b42fddbb35b6957084fdacab9;hpb=651f2747ee704b149ca59bb5dce6ec2ff05cc5b7;p=lemmy.git diff --git a/crates/db_schema/src/aggregates/site_aggregates.rs b/crates/db_schema/src/aggregates/site_aggregates.rs index 719beb8f..cfc14df4 100644 --- a/crates/db_schema/src/aggregates/site_aggregates.rs +++ b/crates/db_schema/src/aggregates/site_aggregates.rs @@ -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(); + } }