mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-10-01 01:36:12 -04:00
Add LemmyResult to post_aggregates tests
This commit is contained in:
parent
8bfbdca660
commit
9178c51fa6
@ -49,8 +49,6 @@ impl PostAggregates {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[expect(clippy::unwrap_used)]
|
||||
|
||||
mod tests {
|
||||
|
||||
use crate::{
|
||||
@ -65,26 +63,25 @@ mod tests {
|
||||
traits::{Crud, Likeable},
|
||||
utils::build_db_pool_for_tests,
|
||||
};
|
||||
use lemmy_utils::error::LemmyResult;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serial_test::serial;
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_crud() {
|
||||
async fn test_crud() -> LemmyResult<()> {
|
||||
let pool = &build_db_pool_for_tests().await;
|
||||
let pool = &mut pool.into();
|
||||
|
||||
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
||||
|
||||
let new_person = PersonInsertForm::test_form(inserted_instance.id, "thommy_community_agg");
|
||||
|
||||
let inserted_person = Person::create(pool, &new_person).await.unwrap();
|
||||
let inserted_person = Person::create(pool, &new_person).await?;
|
||||
|
||||
let another_person = PersonInsertForm::test_form(inserted_instance.id, "jerry_community_agg");
|
||||
|
||||
let another_inserted_person = Person::create(pool, &another_person).await.unwrap();
|
||||
let another_inserted_person = Person::create(pool, &another_person).await?;
|
||||
|
||||
let new_community = CommunityInsertForm::new(
|
||||
inserted_instance.id,
|
||||
@ -92,21 +89,21 @@ mod tests {
|
||||
"nada".to_owned(),
|
||||
"pubkey".to_string(),
|
||||
);
|
||||
let inserted_community = Community::create(pool, &new_community).await.unwrap();
|
||||
let inserted_community = Community::create(pool, &new_community).await?;
|
||||
|
||||
let new_post = PostInsertForm::new(
|
||||
"A test post".into(),
|
||||
inserted_person.id,
|
||||
inserted_community.id,
|
||||
);
|
||||
let inserted_post = Post::create(pool, &new_post).await.unwrap();
|
||||
let inserted_post = Post::create(pool, &new_post).await?;
|
||||
|
||||
let comment_form = CommentInsertForm::new(
|
||||
inserted_person.id,
|
||||
inserted_post.id,
|
||||
"A test comment".into(),
|
||||
);
|
||||
let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
|
||||
let inserted_comment = Comment::create(pool, &comment_form, None).await?;
|
||||
|
||||
let child_comment_form = CommentInsertForm::new(
|
||||
inserted_person.id,
|
||||
@ -114,9 +111,7 @@ mod tests {
|
||||
"A test comment".into(),
|
||||
);
|
||||
let inserted_child_comment =
|
||||
Comment::create(pool, &child_comment_form, Some(&inserted_comment.path))
|
||||
.await
|
||||
.unwrap();
|
||||
Comment::create(pool, &child_comment_form, Some(&inserted_comment.path)).await?;
|
||||
|
||||
let post_like = PostLikeForm {
|
||||
post_id: inserted_post.id,
|
||||
@ -124,9 +119,9 @@ mod tests {
|
||||
score: 1,
|
||||
};
|
||||
|
||||
PostLike::like(pool, &post_like).await.unwrap();
|
||||
PostLike::like(pool, &post_like).await?;
|
||||
|
||||
let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
|
||||
assert_eq!(2, post_aggs_before_delete.comments);
|
||||
assert_eq!(1, post_aggs_before_delete.score);
|
||||
@ -140,9 +135,9 @@ mod tests {
|
||||
score: -1,
|
||||
};
|
||||
|
||||
PostLike::like(pool, &post_dislike).await.unwrap();
|
||||
PostLike::like(pool, &post_dislike).await?;
|
||||
|
||||
let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
|
||||
assert_eq!(2, post_aggs_after_dislike.comments);
|
||||
assert_eq!(0, post_aggs_after_dislike.score);
|
||||
@ -150,59 +145,51 @@ mod tests {
|
||||
assert_eq!(1, post_aggs_after_dislike.downvotes);
|
||||
|
||||
// Remove the comments
|
||||
Comment::delete(pool, inserted_comment.id).await.unwrap();
|
||||
Comment::delete(pool, inserted_child_comment.id)
|
||||
.await
|
||||
.unwrap();
|
||||
let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
Comment::delete(pool, inserted_comment.id).await?;
|
||||
Comment::delete(pool, inserted_child_comment.id).await?;
|
||||
let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
assert_eq!(0, after_comment_delete.comments);
|
||||
assert_eq!(0, after_comment_delete.score);
|
||||
assert_eq!(1, after_comment_delete.upvotes);
|
||||
assert_eq!(1, after_comment_delete.downvotes);
|
||||
|
||||
// Remove the first post like
|
||||
PostLike::remove(pool, inserted_person.id, inserted_post.id)
|
||||
.await
|
||||
.unwrap();
|
||||
let after_like_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
PostLike::remove(pool, inserted_person.id, inserted_post.id).await?;
|
||||
let after_like_remove = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
assert_eq!(0, after_like_remove.comments);
|
||||
assert_eq!(-1, after_like_remove.score);
|
||||
assert_eq!(0, after_like_remove.upvotes);
|
||||
assert_eq!(1, after_like_remove.downvotes);
|
||||
|
||||
// This should delete all the associated rows, and fire triggers
|
||||
Person::delete(pool, another_inserted_person.id)
|
||||
.await
|
||||
.unwrap();
|
||||
let person_num_deleted = Person::delete(pool, inserted_person.id).await.unwrap();
|
||||
Person::delete(pool, another_inserted_person.id).await?;
|
||||
let person_num_deleted = Person::delete(pool, inserted_person.id).await?;
|
||||
assert_eq!(1, person_num_deleted);
|
||||
|
||||
// Delete the community
|
||||
let community_num_deleted = Community::delete(pool, inserted_community.id)
|
||||
.await
|
||||
.unwrap();
|
||||
let community_num_deleted = Community::delete(pool, inserted_community.id).await?;
|
||||
assert_eq!(1, community_num_deleted);
|
||||
|
||||
// Should be none found, since the creator was deleted
|
||||
let after_delete = PostAggregates::read(pool, inserted_post.id).await;
|
||||
assert!(after_delete.is_err());
|
||||
|
||||
Instance::delete(pool, inserted_instance.id).await.unwrap();
|
||||
Instance::delete(pool, inserted_instance.id).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_soft_delete() {
|
||||
async fn test_soft_delete() -> LemmyResult<()> {
|
||||
let pool = &build_db_pool_for_tests().await;
|
||||
let pool = &mut pool.into();
|
||||
|
||||
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
||||
|
||||
let new_person = PersonInsertForm::test_form(inserted_instance.id, "thommy_community_agg");
|
||||
|
||||
let inserted_person = Person::create(pool, &new_person).await.unwrap();
|
||||
let inserted_person = Person::create(pool, &new_person).await?;
|
||||
|
||||
let new_community = CommunityInsertForm::new(
|
||||
inserted_instance.id,
|
||||
@ -210,14 +197,14 @@ mod tests {
|
||||
"nada".to_owned(),
|
||||
"pubkey".to_string(),
|
||||
);
|
||||
let inserted_community = Community::create(pool, &new_community).await.unwrap();
|
||||
let inserted_community = Community::create(pool, &new_community).await?;
|
||||
|
||||
let new_post = PostInsertForm::new(
|
||||
"A test post".into(),
|
||||
inserted_person.id,
|
||||
inserted_community.id,
|
||||
);
|
||||
let inserted_post = Post::create(pool, &new_post).await.unwrap();
|
||||
let inserted_post = Post::create(pool, &new_post).await?;
|
||||
|
||||
let comment_form = CommentInsertForm::new(
|
||||
inserted_person.id,
|
||||
@ -225,9 +212,9 @@ mod tests {
|
||||
"A test comment".into(),
|
||||
);
|
||||
|
||||
let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
|
||||
let inserted_comment = Comment::create(pool, &comment_form, None).await?;
|
||||
|
||||
let post_aggregates_before = PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
let post_aggregates_before = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
assert_eq!(1, post_aggregates_before.comments);
|
||||
|
||||
Comment::update(
|
||||
@ -238,10 +225,9 @@ mod tests {
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
let post_aggregates_after_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
let post_aggregates_after_remove = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
assert_eq!(0, post_aggregates_after_remove.comments);
|
||||
|
||||
Comment::update(
|
||||
@ -252,8 +238,7 @@ mod tests {
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
Comment::update(
|
||||
pool,
|
||||
@ -263,10 +248,9 @@ mod tests {
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
let post_aggregates_after_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
let post_aggregates_after_delete = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
assert_eq!(0, post_aggregates_after_delete.comments);
|
||||
|
||||
Comment::update(
|
||||
@ -277,19 +261,17 @@ mod tests {
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
let post_aggregates_after_delete_remove =
|
||||
PostAggregates::read(pool, inserted_post.id).await.unwrap();
|
||||
let post_aggregates_after_delete_remove = PostAggregates::read(pool, inserted_post.id).await?;
|
||||
assert_eq!(0, post_aggregates_after_delete_remove.comments);
|
||||
|
||||
Comment::delete(pool, inserted_comment.id).await.unwrap();
|
||||
Post::delete(pool, inserted_post.id).await.unwrap();
|
||||
Person::delete(pool, inserted_person.id).await.unwrap();
|
||||
Community::delete(pool, inserted_community.id)
|
||||
.await
|
||||
.unwrap();
|
||||
Instance::delete(pool, inserted_instance.id).await.unwrap();
|
||||
Comment::delete(pool, inserted_comment.id).await?;
|
||||
Post::delete(pool, inserted_post.id).await?;
|
||||
Person::delete(pool, inserted_person.id).await?;
|
||||
Community::delete(pool, inserted_community.id).await?;
|
||||
Instance::delete(pool, inserted_instance.id).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user