]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/aggregates/person_aggregates.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / db_schema / src / aggregates / person_aggregates.rs
index b8e1887a1225b0a359a32d93b2aefd368dc59f1c..e03497da076263a551bc5c7a55db09e8098b1f88 100644 (file)
@@ -8,7 +8,7 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl};
 use diesel_async::RunQueryDsl;
 
 impl PersonAggregates {
-  pub async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
+  pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
     let conn = &mut get_conn(pool).await?;
     person_aggregates::table
       .filter(person_aggregates::person_id.eq(person_id))
@@ -22,7 +22,7 @@ mod tests {
   use crate::{
     aggregates::person_aggregates::PersonAggregates,
     source::{
-      comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
+      comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm},
       community::{Community, CommunityInsertForm},
       instance::Instance,
       person::{Person, PersonInsertForm},
@@ -37,8 +37,11 @@ mod tests {
   #[serial]
   async fn test_crud() {
     let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
 
-    let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
+    let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+      .await
+      .unwrap();
 
     let new_person = PersonInsertForm::builder()
       .name("thommy_user_agg".into())
@@ -136,6 +139,27 @@ mod tests {
       .unwrap();
     assert_eq!(0, after_post_like_remove.post_score);
 
+    Comment::update(
+      pool,
+      inserted_comment.id,
+      &CommentUpdateForm::builder().removed(Some(true)).build(),
+    )
+    .await
+    .unwrap();
+    Comment::update(
+      pool,
+      inserted_child_comment.id,
+      &CommentUpdateForm::builder().removed(Some(true)).build(),
+    )
+    .await
+    .unwrap();
+
+    let after_parent_comment_removed = PersonAggregates::read(pool, inserted_person.id)
+      .await
+      .unwrap();
+    assert_eq!(0, after_parent_comment_removed.comment_count);
+    assert_eq!(0, after_parent_comment_removed.comment_score);
+
     // Remove a parent comment (the scores should also be removed)
     Comment::delete(pool, inserted_comment.id).await.unwrap();
     Comment::delete(pool, inserted_child_comment.id)