From 38e64825e6f38d36787eeeb91c8256dbc950c60b Mon Sep 17 00:00:00 2001 From: Nutomic Date: Thu, 4 Jan 2024 17:51:55 +0100 Subject: [PATCH] Add macro assert_length!() for tests (#4348) * Add macro assert_length!() for tests * fix * number --- crates/db_schema/src/lib.rs | 9 +++++++ crates/db_views/src/comment_view.rs | 19 ++++++------- crates/db_views/src/post_report_view.rs | 3 ++- crates/db_views/src/post_view.rs | 27 ++++++++++--------- .../src/private_message_report_view.rs | 5 ++-- crates/db_views/src/private_message_view.rs | 11 ++++---- crates/db_views_actor/src/person_view.rs | 7 ++--- 7 files changed, 48 insertions(+), 33 deletions(-) diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index a53a1733c..de1069f9f 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -216,3 +216,12 @@ pub enum PostFeatureType { /// Features to the top of the community. Community, } + +/// Wrapper for assert_eq! macro. Checks that vec matches the given length, and prints the +/// vec on failure. +#[macro_export] +macro_rules! assert_length { + ($len:expr, $vec:expr) => {{ + assert_eq!($len, $vec.len(), "Vec has wrong length: {:?}", $vec) + }}; +} diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index eb57a5e48..62f6634c1 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -399,6 +399,7 @@ mod tests { }; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, + assert_length, impls::actor_language::UNDETERMINED_ID, newtypes::LanguageId, source::{ @@ -637,7 +638,7 @@ mod tests { ); // Make sure its 1, not showing the blocked comment - assert_eq!(5, read_comment_views_with_person.len()); + assert_length!(5, read_comment_views_with_person); let read_comment_from_blocked_person = CommentView::read( pool, @@ -664,7 +665,7 @@ mod tests { read_liked_comment_views[0] ); - assert_eq!(1, read_liked_comment_views.len()); + assert_length!(1, read_liked_comment_views); let read_disliked_comment_views: Vec = CommentQuery { local_user: (Some(&data.timmy_local_user_view)), @@ -708,8 +709,8 @@ mod tests { .unwrap(); // Make sure the comment parent-limited fetch is correct - assert_eq!(6, read_comment_views_top_path.len()); - assert_eq!(4, read_comment_views_child_path.len()); + assert_length!(6, read_comment_views_top_path); + assert_length!(4, read_comment_views_child_path); // Make sure it contains the parent, but not the comment from the other tree let child_comments = read_comment_views_child_path @@ -733,7 +734,7 @@ mod tests { expected_comment_view(&data, pool).await, read_comment_views_top_max_depth[0] ); - assert_eq!(1, read_comment_views_top_max_depth.len()); + assert_length!(1, read_comment_views_top_max_depth); let child_path = data.inserted_comment_1.path.clone(); let read_comment_views_parent_max_depth = CommentQuery { @@ -752,7 +753,7 @@ mod tests { .comment .content .eq("Comment 3")); - assert_eq!(3, read_comment_views_parent_max_depth.len()); + assert_length!(3, read_comment_views_parent_max_depth); cleanup(data, pool).await; } @@ -773,7 +774,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(5, all_languages.len()); + assert_length!(5, all_languages); // change user lang to finnish, should only show one post in finnish and one undetermined let finnish_id = Language::read_id_from_code(pool, Some("fi")) @@ -794,7 +795,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(2, finnish_comments.len()); + assert_length!(2, finnish_comments); let finnish_comment = finnish_comments .iter() .find(|c| c.comment.language_id == finnish_id); @@ -819,7 +820,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(1, undetermined_comment.len()); + assert_length!(1, undetermined_comment); cleanup(data, pool).await; } diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 00918970d..c503ae81a 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -196,6 +196,7 @@ mod tests { structs::LocalUserView, }; use lemmy_db_schema::{ + assert_length, source::{ community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm}, instance::Instance, @@ -383,7 +384,7 @@ mod tests { .list(pool, &timmy_view) .await .unwrap(); - assert_eq!(reports_after_resolve.len(), 1); + assert_length!(1, reports_after_resolve); assert_eq!(reports_after_resolve[0].creator.id, inserted_sara.id); // Make sure the counts are correct diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index ab1d87b67..f5acc3804 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -729,6 +729,7 @@ mod tests { use chrono::Utc; use lemmy_db_schema::{ aggregates::structs::PostAggregates, + assert_length, impls::actor_language::UNDETERMINED_ID, newtypes::LanguageId, source::{ @@ -915,7 +916,7 @@ mod tests { let mut 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!(1, read_post_listing.len()); + assert_length!(1, read_post_listing); assert_eq!(expected_post_listing_with_user, read_post_listing[0]); expected_post_listing_with_user.my_vote = None; @@ -944,7 +945,7 @@ mod tests { .await .unwrap(); // should include bot post which has "undetermined" language - assert_eq!(2, post_listings_with_bots.len()); + assert_length!(2, post_listings_with_bots); cleanup(data, pool).await; } @@ -973,7 +974,7 @@ mod tests { 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!(3, read_post_listing_multiple_no_person.len()); + assert_length!(3, read_post_listing_multiple_no_person); assert_eq!( expected_post_listing_no_person, @@ -1010,7 +1011,7 @@ mod tests { .await .unwrap(); // Should be 0 posts after the community block - assert_eq!(0, read_post_listings_with_person_after_block.len()); + assert_length!(0, read_post_listings_with_person_after_block); CommunityBlock::unblock(pool, &community_block) .await @@ -1075,7 +1076,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(1, read_post_listing.len()); + assert_length!(1, read_post_listing); assert_eq!(expected_post_with_upvote, read_post_listing[0]); @@ -1200,7 +1201,7 @@ mod tests { .unwrap(); // no language filters specified, all posts should be returned - assert_eq!(3, post_listings_all.len()); + assert_length!(3, post_listings_all); let french_id = Language::read_id_from_code(pool, Some("fr")) .await @@ -1220,7 +1221,7 @@ mod tests { .unwrap(); // only one post in french and one undetermined should be returned - assert_eq!(2, post_listing_french.len()); + assert_length!(2, post_listing_french); assert!(post_listing_french .iter() .any(|p| p.post.language_id == french_id)); @@ -1242,7 +1243,7 @@ mod tests { .unwrap(); // french post and undetermined language post should be returned - assert_eq!(2, post_listings_french_und.len()); + assert_length!(2, post_listings_french_und); assert_eq!( UNDETERMINED_ID, post_listings_french_und[0].post.language_id @@ -1280,7 +1281,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(1, post_listings_no_admin.len()); + assert_length!(1, post_listings_no_admin); // Removed bot post is shown to admins on its profile page data.local_user_view.local_user.admin = true; @@ -1379,7 +1380,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(post_listings_all.len(), 3); + assert_length!(3, post_listings_all); // block the instance let block_form = InstanceBlockForm { @@ -1396,7 +1397,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(post_listings_blocked.len(), 2); + assert_length!(2, post_listings_blocked); assert_ne!( post_listings_blocked[0].post.id, post_from_blocked_instance.id @@ -1415,7 +1416,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(post_listings_blocked.len(), 3); + assert_length!(3, post_listings_blocked); Instance::delete(pool, blocked_instance.id).await.unwrap(); cleanup(data, pool).await; @@ -1535,7 +1536,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(1, post_listings_hide_read.len()); + assert_length!(1, post_listings_hide_read); cleanup(data, pool).await; } diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 712e53eb9..2c6919542 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -112,6 +112,7 @@ mod tests { use crate::private_message_report_view::PrivateMessageReportQuery; use lemmy_db_schema::{ + assert_length, source::{ instance::Instance, person::{Person, PersonInsertForm}, @@ -171,7 +172,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(1, reports.len()); + assert_length!(1, reports); assert!(!reports[0].private_message_report.resolved); assert_eq!(inserted_timmy.name, reports[0].private_message_creator.name); assert_eq!(inserted_jessica.name, reports[0].creator.name); @@ -197,7 +198,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(1, reports.len()); + assert_length!(1, reports); assert!(reports[0].private_message_report.resolved); assert!(reports[0].resolver.is_some()); assert_eq!( diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 6aa87f670..d04ff7b49 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -159,6 +159,7 @@ mod tests { use crate::{private_message_view::PrivateMessageQuery, structs::PrivateMessageView}; use lemmy_db_schema::{ + assert_length, source::{ instance::Instance, person::{Person, PersonInsertForm}, @@ -251,7 +252,7 @@ mod tests { .await .unwrap(); - assert_eq!(timmy_messages.len(), 3); + assert_length!(3, &timmy_messages); assert_eq!(timmy_messages[0].creator.id, jess.id); assert_eq!(timmy_messages[0].recipient.id, timmy.id); assert_eq!(timmy_messages[1].creator.id, timmy.id); @@ -268,7 +269,7 @@ mod tests { .await .unwrap(); - assert_eq!(timmy_unread_messages.len(), 2); + assert_length!(2, &timmy_unread_messages); assert_eq!(timmy_unread_messages[0].creator.id, jess.id); assert_eq!(timmy_unread_messages[0].recipient.id, timmy.id); assert_eq!(timmy_unread_messages[1].creator.id, sara.id); @@ -283,7 +284,7 @@ mod tests { .await .unwrap(); - assert_eq!(timmy_sara_messages.len(), 2); + assert_length!(2, &timmy_sara_messages); assert_eq!(timmy_sara_messages[0].creator.id, timmy.id); assert_eq!(timmy_sara_messages[0].recipient.id, sara.id); assert_eq!(timmy_sara_messages[1].creator.id, sara.id); @@ -298,7 +299,7 @@ mod tests { .await .unwrap(); - assert_eq!(timmy_sara_unread_messages.len(), 1); + assert_length!(1, &timmy_sara_unread_messages); assert_eq!(timmy_sara_unread_messages[0].creator.id, sara.id); assert_eq!(timmy_sara_unread_messages[0].recipient.id, timmy.id); @@ -328,7 +329,7 @@ mod tests { .await .unwrap(); - assert_eq!(timmy_messages.len(), 1); + assert_length!(1, &timmy_messages); let timmy_unread_messages = PrivateMessageView::get_unread_messages(pool, timmy.id) .await diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 2fff7cc76..08ad2880f 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -159,6 +159,7 @@ mod tests { use super::*; use diesel::NotFound; use lemmy_db_schema::{ + assert_length, source::{ instance::Instance, local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm}, @@ -257,7 +258,7 @@ mod tests { .list(pool) .await .unwrap(); - assert_eq!(list.len(), 1); + assert_length!(1, list); assert_eq!(list[0].person.id, data.bob.id); cleanup(data, pool).await; @@ -282,7 +283,7 @@ mod tests { .unwrap(); let list = PersonView::banned(pool).await.unwrap(); - assert_eq!(list.len(), 1); + assert_length!(1, list); assert_eq!(list[0].person.id, data.alice.id); cleanup(data, pool).await; @@ -307,7 +308,7 @@ mod tests { .unwrap(); let list = PersonView::admins(pool).await.unwrap(); - assert_eq!(list.len(), 1); + assert_length!(1, list); assert_eq!(list[0].person.id, data.alice.id); let is_admin = PersonView::read(pool, data.alice.id)