diff --git a/Cargo.lock b/Cargo.lock index 165353be0..d6558f4d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2665,6 +2665,7 @@ dependencies = [ "serde_json", "serde_with", "serial_test", + "test-context", "tokio", "tracing", "ts-rs", diff --git a/crates/db_views/Cargo.toml b/crates/db_views/Cargo.toml index b2d2b2f8d..8b0669ff9 100644 --- a/crates/db_views/Cargo.toml +++ b/crates/db_views/Cargo.toml @@ -47,3 +47,4 @@ serial_test = { workspace = true } tokio = { workspace = true } pretty_assertions = { workspace = true } url = { workspace = true } +test-context = "0.3.0" diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index ed62cabe4..a7fe71034 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -690,7 +690,14 @@ mod tests { site::Site, }, traits::{Bannable, Blockable, Crud, Followable, Joinable, Likeable, Saveable}, - utils::{build_db_pool, build_db_pool_for_tests, get_conn, uplete, DbPool, RANK_DEFAULT}, + utils::{ + build_db_pool, + get_conn, + uplete, + ActualDbPool, + DbPool, + RANK_DEFAULT, + }, CommunityVisibility, PostSortType, SubscribedType, @@ -699,6 +706,7 @@ mod tests { use pretty_assertions::assert_eq; use serial_test::serial; use std::time::{Duration, Instant}; + use test_context::{test_context, AsyncTestContext}; use url::Url; const POST_WITH_ANOTHER_TITLE: &str = "Another title"; @@ -712,6 +720,7 @@ mod tests { } struct Data { + pool: ActualDbPool, inserted_instance: Instance, local_user_view: LocalUserView, blocked_local_user_view: LocalUserView, @@ -726,6 +735,12 @@ mod tests { } impl Data { + fn pool(&self) -> ActualDbPool { + self.pool.clone() + } + pub fn pool2(&self) -> DbPool<'_> { + DbPool::Pool(&self.pool) + } fn default_post_query(&self) -> PostQuery<'_> { PostQuery { sort: Some(PostSortType::New), @@ -733,202 +748,222 @@ mod tests { ..Default::default() } } + + async fn setup() -> LemmyResult { + let actual_pool = build_db_pool()?; + let pool = &mut (&actual_pool).into(); + let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?; + + let new_person = PersonInsertForm::test_form(inserted_instance.id, "tegan"); + + let inserted_person = Person::create(pool, &new_person).await?; + + let local_user_form = LocalUserInsertForm { + admin: Some(true), + ..LocalUserInsertForm::test_form(inserted_person.id) + }; + let inserted_local_user = LocalUser::create(pool, &local_user_form, vec![]).await?; + + let new_bot = PersonInsertForm { + bot_account: Some(true), + ..PersonInsertForm::test_form(inserted_instance.id, "mybot") + }; + + let inserted_bot = Person::create(pool, &new_bot).await?; + + let new_community = CommunityInsertForm::new( + inserted_instance.id, + "test_community_3".to_string(), + "nada".to_owned(), + "pubkey".to_string(), + ); + let inserted_community = Community::create(pool, &new_community).await?; + + // Test a person block, make sure the post query doesn't include their post + let blocked_person = PersonInsertForm::test_form(inserted_instance.id, "john"); + + let inserted_blocked_person = Person::create(pool, &blocked_person).await?; + + let inserted_blocked_local_user = LocalUser::create( + pool, + &LocalUserInsertForm::test_form(inserted_blocked_person.id), + vec![], + ) + .await?; + + let post_from_blocked_person = PostInsertForm { + language_id: Some(LanguageId(1)), + ..PostInsertForm::new( + POST_BY_BLOCKED_PERSON.to_string(), + inserted_blocked_person.id, + inserted_community.id, + ) + }; + Post::create(pool, &post_from_blocked_person).await?; + + // block that person + let person_block = PersonBlockForm { + person_id: inserted_person.id, + target_id: inserted_blocked_person.id, + }; + + PersonBlock::block(pool, &person_block).await?; + + // Two community post tags + let tag_1 = Tag::create( + pool, + &TagInsertForm { + ap_id: Url::parse(&format!("{}/tags/test_tag1", inserted_community.actor_id))?.into(), + name: "Test Tag 1".into(), + published: None, + updated: None, + deleted: None, + }, + ) + .await?; + CommunityPostTag::create( + pool, + &CommunityPostTagInsertForm { + community_id: inserted_community.id, + tag_id: tag_1.id, + published: None, + }, + ) + .await?; + let tag_2 = Tag::create( + pool, + &TagInsertForm { + ap_id: Url::parse(&format!("{}/tags/test_tag2", inserted_community.actor_id))?.into(), + name: "Test Tag 2".into(), + published: None, + updated: None, + deleted: None, + }, + ) + .await?; + CommunityPostTag::create( + pool, + &CommunityPostTagInsertForm { + community_id: inserted_community.id, + tag_id: tag_2.id, + published: None, + }, + ) + .await?; + + // A sample post + let new_post = PostInsertForm { + language_id: Some(LanguageId(47)), + ..PostInsertForm::new(POST.to_string(), inserted_person.id, inserted_community.id) + }; + + let inserted_post = Post::create(pool, &new_post).await?; + + let new_bot_post = PostInsertForm::new( + POST_BY_BOT.to_string(), + inserted_bot.id, + inserted_community.id, + ); + let inserted_bot_post = Post::create(pool, &new_bot_post).await?; + + // A sample post with tags + let new_post = PostInsertForm { + language_id: Some(LanguageId(47)), + ..PostInsertForm::new( + POST_WITH_TAGS.to_string(), + inserted_person.id, + inserted_community.id, + ) + }; + + let inserted_post_with_tags = Post::create(pool, &new_post).await?; + let inserted_tags = vec![ + PostTagInsertForm { + post_id: inserted_post_with_tags.id, + tag_id: tag_1.id, + }, + PostTagInsertForm { + post_id: inserted_post_with_tags.id, + tag_id: tag_2.id, + }, + ]; + PostTagInsertForm::insert_tag_associations(pool, &inserted_tags).await?; + + let local_user_view = LocalUserView { + local_user: inserted_local_user, + local_user_vote_display_mode: LocalUserVoteDisplayMode::default(), + person: inserted_person, + counts: Default::default(), + }; + let blocked_local_user_view = LocalUserView { + local_user: inserted_blocked_local_user, + local_user_vote_display_mode: LocalUserVoteDisplayMode::default(), + person: inserted_blocked_person, + counts: Default::default(), + }; + + let site = Site { + id: Default::default(), + name: String::new(), + sidebar: None, + published: Default::default(), + updated: None, + icon: None, + banner: None, + description: None, + actor_id: Url::parse("http://example.com")?.into(), + last_refreshed_at: Default::default(), + inbox_url: Url::parse("http://example.com")?.into(), + private_key: None, + public_key: String::new(), + instance_id: Default::default(), + content_warning: None, + }; + + Ok(Data { + pool: actual_pool, + inserted_instance, + local_user_view, + blocked_local_user_view, + inserted_bot, + inserted_community, + inserted_post, + inserted_bot_post, + inserted_post_with_tags, + tag_1, + tag_2, + site, + }) + } + async fn teardown(data: Data) -> LemmyResult<()> { + let pool = &mut data.pool2(); + // let pool = &mut (&pool).into(); + let num_deleted = Post::delete(pool, data.inserted_post.id).await?; + Community::delete(pool, data.inserted_community.id).await?; + Person::delete(pool, data.local_user_view.person.id).await?; + Person::delete(pool, data.inserted_bot.id).await?; + Person::delete(pool, data.blocked_local_user_view.person.id).await?; + Instance::delete(pool, data.inserted_instance.id).await?; + assert_eq!(1, num_deleted); + + Ok(()) + } + } + impl AsyncTestContext for Data { + async fn setup() -> Self { + Data::setup().await.expect("setup failed") + } + async fn teardown(self) { + Data::teardown(self).await.expect("teardown failed") + } } - async fn init_data(pool: &mut DbPool<'_>) -> LemmyResult { - let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?; - - let new_person = PersonInsertForm::test_form(inserted_instance.id, "tegan"); - - let inserted_person = Person::create(pool, &new_person).await?; - - let local_user_form = LocalUserInsertForm { - admin: Some(true), - ..LocalUserInsertForm::test_form(inserted_person.id) - }; - let inserted_local_user = LocalUser::create(pool, &local_user_form, vec![]).await?; - - let new_bot = PersonInsertForm { - bot_account: Some(true), - ..PersonInsertForm::test_form(inserted_instance.id, "mybot") - }; - - let inserted_bot = Person::create(pool, &new_bot).await?; - - let new_community = CommunityInsertForm::new( - inserted_instance.id, - "test_community_3".to_string(), - "nada".to_owned(), - "pubkey".to_string(), - ); - let inserted_community = Community::create(pool, &new_community).await?; - - // Test a person block, make sure the post query doesn't include their post - let blocked_person = PersonInsertForm::test_form(inserted_instance.id, "john"); - - let inserted_blocked_person = Person::create(pool, &blocked_person).await?; - - let inserted_blocked_local_user = LocalUser::create( - pool, - &LocalUserInsertForm::test_form(inserted_blocked_person.id), - vec![], - ) - .await?; - - let post_from_blocked_person = PostInsertForm { - language_id: Some(LanguageId(1)), - ..PostInsertForm::new( - POST_BY_BLOCKED_PERSON.to_string(), - inserted_blocked_person.id, - inserted_community.id, - ) - }; - Post::create(pool, &post_from_blocked_person).await?; - - // block that person - let person_block = PersonBlockForm { - person_id: inserted_person.id, - target_id: inserted_blocked_person.id, - }; - - PersonBlock::block(pool, &person_block).await?; - - // Two community post tags - let tag_1 = Tag::create( - pool, - &TagInsertForm { - ap_id: Url::parse(&format!("{}/tags/test_tag1", inserted_community.actor_id)) - .expect("valid") - .into(), - name: "Test Tag 1".into(), - published: None, - updated: None, - deleted: None, - }, - ) - .await?; - CommunityPostTag::create( - pool, - &CommunityPostTagInsertForm { - community_id: inserted_community.id, - tag_id: tag_1.id, - published: None, - }, - ) - .await?; - let tag_2 = Tag::create( - pool, - &TagInsertForm { - ap_id: Url::parse(&format!("{}/tags/test_tag2", inserted_community.actor_id)) - .expect("valid") - .into(), - name: "Test Tag 2".into(), - published: None, - updated: None, - deleted: None, - }, - ) - .await?; - CommunityPostTag::create( - pool, - &CommunityPostTagInsertForm { - community_id: inserted_community.id, - tag_id: tag_2.id, - published: None, - }, - ) - .await?; - - // A sample post - let new_post = PostInsertForm { - language_id: Some(LanguageId(47)), - ..PostInsertForm::new(POST.to_string(), inserted_person.id, inserted_community.id) - }; - - let inserted_post = Post::create(pool, &new_post).await?; - - let new_bot_post = PostInsertForm::new( - POST_BY_BOT.to_string(), - inserted_bot.id, - inserted_community.id, - ); - let inserted_bot_post = Post::create(pool, &new_bot_post).await?; - - // A sample post with tags - let new_post = PostInsertForm { - language_id: Some(LanguageId(47)), - ..PostInsertForm::new( - POST_WITH_TAGS.to_string(), - inserted_person.id, - inserted_community.id, - ) - }; - - let inserted_post_with_tags = Post::create(pool, &new_post).await?; - let inserted_tags = vec![ - PostTagInsertForm { - post_id: inserted_post_with_tags.id, - tag_id: tag_1.id, - }, - PostTagInsertForm { - post_id: inserted_post_with_tags.id, - tag_id: tag_2.id, - }, - ]; - PostTagInsertForm::insert_tag_associations(pool, &inserted_tags).await?; - - let local_user_view = LocalUserView { - local_user: inserted_local_user, - local_user_vote_display_mode: LocalUserVoteDisplayMode::default(), - person: inserted_person, - counts: Default::default(), - }; - let blocked_local_user_view = LocalUserView { - local_user: inserted_blocked_local_user, - local_user_vote_display_mode: LocalUserVoteDisplayMode::default(), - person: inserted_blocked_person, - counts: Default::default(), - }; - - let site = Site { - id: Default::default(), - name: String::new(), - sidebar: None, - published: Default::default(), - updated: None, - icon: None, - banner: None, - description: None, - actor_id: Url::parse("http://example.com")?.into(), - last_refreshed_at: Default::default(), - inbox_url: Url::parse("http://example.com")?.into(), - private_key: None, - public_key: String::new(), - instance_id: Default::default(), - content_warning: None, - }; - - Ok(Data { - inserted_instance, - local_user_view, - blocked_local_user_view, - inserted_bot, - inserted_community, - inserted_post, - inserted_bot_post, - inserted_post_with_tags, - tag_1, - tag_2, - site, - }) - } - + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_with_person() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_with_person(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let mut data = init_data(pool).await?; let local_user_form = LocalUserUpdateForm { show_bot_accounts: Some(false), @@ -954,7 +989,7 @@ mod tests { ) .await?; - let expected_post_listing_with_user = expected_post_view(&data, pool).await?; + let expected_post_listing_with_user = expected_post_view(data, pool).await?; // Should be only one person, IE the bot post, and blocked should be missing assert_eq!( @@ -984,16 +1019,15 @@ mod tests { vec![POST_WITH_TAGS, POST_BY_BOT, POST], names(&post_listings_with_bots) ); - - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_no_person() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_no_person(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; let read_post_listing_multiple_no_person = PostQuery { community_id: Some(data.inserted_community.id), @@ -1006,7 +1040,7 @@ mod tests { let read_post_listing_single_no_person = PostView::read(pool, data.inserted_post.id, None, false).await?; - let expected_post_listing_no_person = expected_post_view(&data, pool).await?; + let expected_post_listing_no_person = expected_post_view(data, pool).await?; // Should be 2 posts, with the bot post, and the blocked assert_eq!( @@ -1022,16 +1056,15 @@ mod tests { expected_post_listing_no_person, read_post_listing_single_no_person ); - - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_title_only() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_title_only(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // A post which contains the search them 'Post' not in the title (but in the body) let new_post = PostInsertForm { @@ -1069,6 +1102,7 @@ mod tests { assert_eq!( vec![ POST_WITH_ANOTHER_TITLE, + POST_WITH_TAGS, POST_BY_BOT, POST, POST_BY_BLOCKED_PERSON @@ -1078,19 +1112,19 @@ mod tests { // Should be 3 posts when we search for title only assert_eq!( - vec![POST_BY_BOT, POST, POST_BY_BLOCKED_PERSON], + vec![POST_WITH_TAGS, POST_BY_BOT, POST, POST_BY_BLOCKED_PERSON], names(&read_post_listing_by_title_only) ); Post::delete(pool, inserted_post.id).await?; - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_block_community() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_block_community(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; let community_block = CommunityBlockForm { person_id: data.local_user_view.person.id, @@ -1108,15 +1142,15 @@ mod tests { assert_eq!(read_post_listings_with_person_after_block, vec![]); CommunityBlock::unblock(pool, &community_block).await?; - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_like() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_like(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let mut data = init_data(pool).await?; let post_like_form = PostLikeForm::new(data.inserted_post.id, data.local_user_view.person.id, 1); @@ -1139,7 +1173,7 @@ mod tests { ) .await?; - let mut expected_post_with_upvote = expected_post_view(&data, pool).await?; + let mut expected_post_with_upvote = expected_post_view(data, pool).await?; expected_post_with_upvote.my_vote = Some(1); expected_post_with_upvote.counts.score = 1; expected_post_with_upvote.counts.upvotes = 1; @@ -1164,15 +1198,15 @@ mod tests { let like_removed = PostLike::remove(pool, data.local_user_view.person.id, data.inserted_post.id).await?; assert_eq!(uplete::Count::only_deleted(1), like_removed); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_liked_only() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_liked_only(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Like both the bot post, and your own // The liked_only should not show your own post @@ -1207,15 +1241,15 @@ mod tests { // Should be no posts assert_eq!(read_disliked_post_listing, vec![]); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_saved_only() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_saved_only(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Save only the bot post // The saved_only should only show the bot post @@ -1235,15 +1269,15 @@ mod tests { // This should only include the bot post, not the one you created assert_eq!(vec![POST_BY_BOT], names(&read_saved_post_listing)); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn creator_info() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn creator_info(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Make one of the inserted persons a moderator let person_id = data.local_user_view.person.id; @@ -1272,17 +1306,17 @@ mod tests { assert_eq!(expected_post_listing, post_listing); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_person_language() -> LemmyResult<()> { + async fn post_listing_person_language(data: &mut Data) -> LemmyResult<()> { const EL_POSTO: &str = "el posto"; - let pool = &build_db_pool()?; + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; let spanish_id = Language::read_id_from_code(pool, "es").await?; @@ -1342,15 +1376,15 @@ mod tests { // french post and undetermined language post should be returned assert_eq!(expected_post_listings_french_und, post_listings_french_und); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listings_removed() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listings_removed(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let mut data = init_data(pool).await?; // Remove the post Post::update( @@ -1377,15 +1411,15 @@ mod tests { .await?; assert_eq!(vec![POST_BY_BOT], names(&post_listings_is_admin)); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listings_deleted() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listings_deleted(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Delete the post Post::update( @@ -1416,15 +1450,15 @@ mod tests { assert_eq!(expect_contains_deleted, contains_deleted); } - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listings_hidden_community() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listings_hidden_community(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; Community::update( pool, @@ -1452,17 +1486,17 @@ mod tests { let posts = data.default_post_query().list(&data.site, pool).await?; assert!(!posts.is_empty()); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_instance_block() -> LemmyResult<()> { + async fn post_listing_instance_block(data: &mut Data) -> LemmyResult<()> { const POST_FROM_BLOCKED_INSTANCE: &str = "post on blocked instance"; - let pool = &build_db_pool()?; + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; let blocked_instance = Instance::read_or_create(pool, "another_domain.tld".to_string()).await?; @@ -1527,15 +1561,15 @@ mod tests { ); Instance::delete(pool, blocked_instance.id).await?; - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn pagination_includes_each_post_once() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn pagination_includes_each_post_once(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; let community_form = CommunityInsertForm::new( data.inserted_instance.id, @@ -1637,15 +1671,15 @@ mod tests { assert_eq!(inserted_post_ids, listed_post_ids); Community::delete(pool, inserted_community.id).await?; - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listings_hide_read() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listings_hide_read(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let mut data = init_data(pool).await?; // Make sure local user hides read posts let local_user_form = LocalUserUpdateForm { @@ -1686,15 +1720,15 @@ mod tests { vec![POST_WITH_TAGS, POST], names(&post_listings_show_read_false) ); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listings_hide_hidden() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listings_hide_hidden(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Mark a post as hidden PostHide::hide( @@ -1728,15 +1762,15 @@ mod tests { // Make sure that hidden field is true. assert!(&post_listings_show_hidden.get(1).is_some_and(|p| p.hidden)); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listings_hide_nsfw() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listings_hide_nsfw(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Mark a post as nsfw let update_form = PostUpdateForm { @@ -1744,11 +1778,11 @@ mod tests { ..Default::default() }; - Post::update(pool, data.inserted_bot_post.id, &update_form).await?; + Post::update(pool, data.inserted_post_with_tags.id, &update_form).await?; // Make sure you don't see the nsfw post in the regular results let post_listings_hide_nsfw = data.default_post_query().list(&data.site, pool).await?; - assert_eq!(vec![POST_WITH_TAGS, POST], names(&post_listings_hide_nsfw)); + assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_hide_nsfw)); // Make sure it does come back with the show_nsfw option let post_listings_show_nsfw = PostQuery { @@ -1773,18 +1807,6 @@ mod tests { .nsfw ); - cleanup(data, pool).await - } - - async fn cleanup(data: Data, pool: &mut DbPool<'_>) -> LemmyResult<()> { - let num_deleted = Post::delete(pool, data.inserted_post.id).await?; - Community::delete(pool, data.inserted_community.id).await?; - Person::delete(pool, data.local_user_view.person.id).await?; - Person::delete(pool, data.inserted_bot.id).await?; - Person::delete(pool, data.blocked_local_user_view.person.id).await?; - Instance::delete(pool, data.inserted_instance.id).await?; - assert_eq!(1, num_deleted); - Ok(()) } @@ -1909,12 +1931,12 @@ mod tests { }) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn local_only_instance() -> LemmyResult<()> { - let pool = &build_db_pool_for_tests(); + async fn local_only_instance(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; Community::update( pool, @@ -1953,16 +1975,15 @@ mod tests { .await; assert!(authenticated_post.is_ok()); - cleanup(data, pool).await?; Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_local_user_banned_from_community() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_local_user_banned_from_community(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Test that post view shows if local user is blocked from community let banned_from_comm_person = PersonInsertForm::test_form(data.inserted_instance.id, "jill"); @@ -1997,15 +2018,15 @@ mod tests { assert!(post_view.banned_from_community); Person::delete(pool, inserted_banned_from_comm_person.id).await?; - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_local_user_not_banned_from_community() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_local_user_not_banned_from_community(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; let post_view = PostView::read( pool, @@ -2017,15 +2038,15 @@ mod tests { assert!(!post_view.banned_from_community); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn speed_check() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn speed_check(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Make sure the post_view query is less than this time let duration_max = Duration::from_millis(80); @@ -2073,15 +2094,15 @@ mod tests { duration_max ); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listings_no_comments_only() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listings_no_comments_only(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; // Create a comment for a post let comment_form = CommentInsertForm::new( @@ -2101,17 +2122,20 @@ mod tests { .list(&data.site, pool) .await?; - assert_eq!(vec![POST_BY_BOT], names(&post_listings_no_comments)); + assert_eq!( + vec![POST_WITH_TAGS, POST_BY_BOT], + names(&post_listings_no_comments) + ); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_listing_private_community() -> LemmyResult<()> { - let pool = &build_db_pool()?; + async fn post_listing_private_community(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let mut data = init_data(pool).await?; // Mark community as private Community::update( @@ -2163,7 +2187,7 @@ mod tests { } .list(&data.site, pool) .await?; - assert_eq!(2, read_post_listing.len()); + assert_eq!(3, read_post_listing.len()); let post_view = PostView::read( pool, data.inserted_post.id, @@ -2190,7 +2214,7 @@ mod tests { } .list(&data.site, pool) .await?; - assert_eq!(2, read_post_listing.len()); + assert_eq!(3, read_post_listing.len()); let post_view = PostView::read( pool, data.inserted_post.id, @@ -2200,15 +2224,15 @@ mod tests { .await; assert!(post_view.is_ok()); - cleanup(data, pool).await + Ok(()) } + #[test_context(Data)] #[tokio::test] #[serial] - async fn post_tags_present() -> LemmyResult<()> { - let pool = &build_db_pool_for_tests(); + async fn post_tags_present(data: &mut Data) -> LemmyResult<()> { + let pool = &data.pool(); let pool = &mut pool.into(); - let data = init_data(pool).await?; let post_view = PostView::read( pool,