Simplify tests using default (#5026)

This commit is contained in:
Nutomic 2024-09-19 10:43:27 +02:00 committed by GitHub
parent 6b6457cc54
commit 026e23cf32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 259 additions and 610 deletions

View file

@ -203,7 +203,6 @@ mod tests {
use lemmy_api_common::site::CreateSite; use lemmy_api_common::site::CreateSite;
use lemmy_db_schema::{ use lemmy_db_schema::{
source::local_site::LocalSite, source::local_site::LocalSite,
CommentSortType,
ListingType, ListingType,
PostSortType, PostSortType,
RegistrationMode, RegistrationMode,
@ -216,177 +215,114 @@ mod tests {
( (
"CreateSite attempted on set up LocalSite", "CreateSite attempted on set up LocalSite",
LemmyErrorType::SiteAlreadyExists, LemmyErrorType::SiteAlreadyExists,
&generate_local_site( &LocalSite {
true, site_setup: true,
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &CreateSite {
&generate_create_site( name: String::from("site_name"),
String::from("site_name"), ..Default::default()
None::<String>, },
None::<String>,
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"CreateSite name matches LocalSite slur filter", "CreateSite name matches LocalSite slur filter",
LemmyErrorType::Slurs, LemmyErrorType::Slurs,
&generate_local_site( &LocalSite {
false, site_setup: false,
Some(String::from("(foo|bar)")), private_instance: true,
true, slur_filter_regex: Some(String::from("(foo|bar)")),
false, federation_enabled: false,
None::<String>, registration_mode: RegistrationMode::Open,
RegistrationMode::Open, ..Default::default()
), },
&generate_create_site( &CreateSite {
String::from("foo site_name"), name: String::from("foo site_name"),
None::<String>, ..Default::default()
None::<String>, },
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"CreateSite name matches new slur filter", "CreateSite name matches new slur filter",
LemmyErrorType::Slurs, LemmyErrorType::Slurs,
&generate_local_site( &LocalSite {
false, site_setup: false,
Some(String::from("(foo|bar)")), private_instance: true,
true, slur_filter_regex: Some(String::from("(foo|bar)")),
false, federation_enabled: false,
None::<String>, registration_mode: RegistrationMode::Open,
RegistrationMode::Open, ..Default::default()
), },
&generate_create_site( &CreateSite {
String::from("zeta site_name"), name: String::from("zeta site_name"),
None::<String>, slur_filter_regex: Some(String::from("(zeta|alpha)")),
None::<String>, ..Default::default()
None::<ListingType>, },
None::<PostSortType>,
None::<CommentSortType>,
Some(String::from("(zeta|alpha)")),
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"CreateSite listing type is Subscribed, which is invalid", "CreateSite listing type is Subscribed, which is invalid",
LemmyErrorType::InvalidDefaultPostListingType, LemmyErrorType::InvalidDefaultPostListingType,
&generate_local_site( &LocalSite {
false, site_setup: false,
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &CreateSite {
&generate_create_site( name: String::from("site_name"),
String::from("site_name"), default_post_listing_type: Some(ListingType::Subscribed),
None::<String>, ..Default::default()
None::<String>, },
Some(ListingType::Subscribed),
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"CreateSite is both private and federated", "CreateSite is both private and federated",
LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether, LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
&generate_local_site( &LocalSite {
false, site_setup: false,
None::<String>, private_instance: true,
true, federation_enabled: false,
false, ..Default::default()
None::<String>, },
RegistrationMode::Open, &CreateSite {
), name: String::from("site_name"),
&generate_create_site( private_instance: Some(true),
String::from("site_name"), federation_enabled: Some(true),
None::<String>, ..Default::default()
None::<String>, },
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
Some(true),
Some(true),
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"LocalSite is private, but CreateSite also makes it federated", "LocalSite is private, but CreateSite also makes it federated",
LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether, LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
&generate_local_site( &LocalSite {
false, site_setup: false,
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &CreateSite {
&generate_create_site( name: String::from("site_name"),
String::from("site_name"), federation_enabled: Some(true),
None::<String>, ..Default::default()
None::<String>, },
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
Some(true),
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"CreateSite requires application, but neither it nor LocalSite has an application question", "CreateSite requires application, but neither it nor LocalSite has an application question",
LemmyErrorType::ApplicationQuestionRequired, LemmyErrorType::ApplicationQuestionRequired,
&generate_local_site( &LocalSite {
false, site_setup: false,
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &CreateSite {
&generate_create_site( name: String::from("site_name"),
String::from("site_name"), registration_mode: Some(RegistrationMode::RequireApplication),
None::<String>, ..Default::default()
None::<String>, },
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
Some(RegistrationMode::RequireApplication),
),
), ),
]; ];
@ -425,99 +361,72 @@ mod tests {
let valid_payloads = [ let valid_payloads = [
( (
"No changes between LocalSite and CreateSite", "No changes between LocalSite and CreateSite",
&generate_local_site( &LocalSite {
false, site_setup: false,
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &CreateSite {
&generate_create_site( name: String::from("site_name"),
String::from("site_name"), ..Default::default()
None::<String>, },
None::<String>,
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"CreateSite allows clearing and changing values", "CreateSite allows clearing and changing values",
&generate_local_site( &LocalSite {
false, site_setup: false,
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &CreateSite {
&generate_create_site( name: String::from("site_name"),
String::from("site_name"), sidebar: Some(String::new()),
Some(String::new()), description: Some(String::new()),
Some(String::new()), application_question: Some(String::new()),
Some(ListingType::All), private_instance: Some(false),
Some(PostSortType::Active), default_post_listing_type: Some(ListingType::All),
Some(CommentSortType::Hot), default_post_sort_type: Some(PostSortType::Active),
Some(String::new()), slur_filter_regex: Some(String::new()),
Some(false), federation_enabled: Some(true),
Some(true), registration_mode: Some(RegistrationMode::Open),
Some(String::new()), ..Default::default()
Some(RegistrationMode::Open), },
),
), ),
( (
"CreateSite clears existing slur filter regex", "CreateSite clears existing slur filter regex",
&generate_local_site( &LocalSite {
false, site_setup: false,
Some(String::from("(foo|bar)")), private_instance: true,
true, slur_filter_regex: Some(String::from("(foo|bar)")),
false, federation_enabled: false,
None::<String>, registration_mode: RegistrationMode::Open,
RegistrationMode::Open, ..Default::default()
), },
&generate_create_site( &CreateSite {
String::from("foo site_name"), name: String::from("foo site_name"),
None::<String>, slur_filter_regex: Some(String::new()),
None::<String>, ..Default::default()
None::<ListingType>, },
None::<PostSortType>,
None::<CommentSortType>,
Some(String::new()),
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
),
), ),
( (
"LocalSite has application question and CreateSite now requires applications,", "LocalSite has application question and CreateSite now requires applications,",
&generate_local_site( &LocalSite {
false, site_setup: false,
None::<String>, application_question: Some(String::from("question")),
true, private_instance: true,
false, federation_enabled: false,
Some(String::from("question")), registration_mode: RegistrationMode::Open,
RegistrationMode::Open, ..Default::default()
), },
&generate_create_site( &CreateSite {
String::from("site_name"), name: String::from("site_name"),
None::<String>, registration_mode: Some(RegistrationMode::RequireApplication),
None::<String>, ..Default::default()
None::<ListingType>, },
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
Some(RegistrationMode::RequireApplication),
),
), ),
]; ];
@ -533,87 +442,4 @@ mod tests {
); );
}) })
} }
fn generate_local_site(
site_setup: bool,
site_slur_filter_regex: Option<String>,
site_is_private: bool,
site_is_federated: bool,
site_application_question: Option<String>,
site_registration_mode: RegistrationMode,
) -> LocalSite {
LocalSite {
site_setup,
application_question: site_application_question,
private_instance: site_is_private,
slur_filter_regex: site_slur_filter_regex,
federation_enabled: site_is_federated,
registration_mode: site_registration_mode,
..Default::default()
}
}
// Allow the test helper function to have too many arguments.
// It's either this or generate the entire struct each time for testing.
#[allow(clippy::too_many_arguments)]
fn generate_create_site(
site_name: String,
site_description: Option<String>,
site_sidebar: Option<String>,
site_listing_type: Option<ListingType>,
site_post_sort_type: Option<PostSortType>,
site_comment_sort_type: Option<CommentSortType>,
site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>,
site_is_federated: Option<bool>,
site_application_question: Option<String>,
site_registration_mode: Option<RegistrationMode>,
) -> CreateSite {
CreateSite {
name: site_name,
sidebar: site_sidebar,
description: site_description,
icon: None,
banner: None,
enable_downvotes: None,
enable_nsfw: None,
community_creation_admin_only: None,
require_email_verification: None,
application_question: site_application_question,
private_instance: site_is_private,
default_theme: None,
default_post_listing_type: site_listing_type,
default_post_sort_type: site_post_sort_type,
default_comment_sort_type: site_comment_sort_type,
legal_information: None,
application_email_admins: None,
hide_modlog_mod_names: None,
discussion_languages: None,
slur_filter_regex: site_slur_filter_regex,
actor_name_max_length: None,
rate_limit_message: None,
rate_limit_message_per_second: None,
rate_limit_post: None,
rate_limit_post_per_second: None,
rate_limit_register: None,
rate_limit_register_per_second: None,
rate_limit_image: None,
rate_limit_image_per_second: None,
rate_limit_comment: None,
rate_limit_comment_per_second: None,
rate_limit_search: None,
rate_limit_search_per_second: None,
federation_enabled: site_is_federated,
federation_debug: None,
captcha_enabled: None,
captcha_difficulty: None,
allowed_instances: None,
blocked_instances: None,
taglines: None,
registration_mode: site_registration_mode,
oauth_registration: None,
content_warning: None,
default_post_listing_mode: None,
}
}
} }

View file

@ -255,7 +255,6 @@ mod tests {
use lemmy_api_common::site::EditSite; use lemmy_api_common::site::EditSite;
use lemmy_db_schema::{ use lemmy_db_schema::{
source::local_site::LocalSite, source::local_site::LocalSite,
CommentSortType,
ListingType, ListingType,
PostSortType, PostSortType,
RegistrationMode, RegistrationMode,
@ -268,152 +267,94 @@ mod tests {
( (
"EditSite name matches LocalSite slur filter", "EditSite name matches LocalSite slur filter",
LemmyErrorType::Slurs, LemmyErrorType::Slurs,
&generate_local_site( &LocalSite {
Some(String::from("(foo|bar)")), private_instance: true,
true, slur_filter_regex: Some(String::from("(foo|bar)")),
false, federation_enabled: false,
None::<String>, registration_mode: RegistrationMode::Open,
RegistrationMode::Open, ..Default::default()
), },
&generate_edit_site( &EditSite {
Some(String::from("foo site_name")), name: Some(String::from("foo site_name")),
None::<String>, ..Default::default()
None::<String>, },
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
None::<bool>,
),
), ),
( (
"EditSite name matches new slur filter", "EditSite name matches new slur filter",
LemmyErrorType::Slurs, LemmyErrorType::Slurs,
&generate_local_site( &LocalSite {
Some(String::from("(foo|bar)")), private_instance: true,
true, slur_filter_regex: Some(String::from("(foo|bar)")),
false, federation_enabled: false,
None::<String>, registration_mode: RegistrationMode::Open,
RegistrationMode::Open, ..Default::default()
), },
&generate_edit_site( &EditSite {
Some(String::from("zeta site_name")), name: Some(String::from("zeta site_name")),
None::<String>, slur_filter_regex: Some(String::from("(zeta|alpha)")),
None::<String>, ..Default::default()
None::<ListingType>, },
None::<PostSortType>,
None::<CommentSortType>,
Some(String::from("(zeta|alpha)")),
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
None::<bool>,
),
), ),
( (
"EditSite listing type is Subscribed, which is invalid", "EditSite listing type is Subscribed, which is invalid",
LemmyErrorType::InvalidDefaultPostListingType, LemmyErrorType::InvalidDefaultPostListingType,
&generate_local_site( &LocalSite {
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &EditSite {
&generate_edit_site( name: Some(String::from("site_name")),
Some(String::from("site_name")), default_post_listing_type: Some(ListingType::Subscribed),
None::<String>, ..Default::default()
None::<String>, },
Some(ListingType::Subscribed),
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
None::<bool>,
),
), ),
( (
"EditSite is both private and federated", "EditSite is both private and federated",
LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether, LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
&generate_local_site( &LocalSite {
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &EditSite {
&generate_edit_site( name: Some(String::from("site_name")),
Some(String::from("site_name")), private_instance: Some(true),
None::<String>, federation_enabled: Some(true),
None::<String>, ..Default::default()
None::<ListingType>, },
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
Some(true),
Some(true),
None::<String>,
None::<RegistrationMode>,
None::<bool>,
),
), ),
( (
"LocalSite is private, but EditSite also makes it federated", "LocalSite is private, but EditSite also makes it federated",
LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether, LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether,
&generate_local_site( &LocalSite {
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &EditSite {
&generate_edit_site( name: Some(String::from("site_name")),
Some(String::from("site_name")), federation_enabled: Some(true),
None::<String>, ..Default::default()
None::<String>, },
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
Some(true),
None::<String>,
None::<RegistrationMode>,
None::<bool>,
),
), ),
( (
"EditSite requires application, but neither it nor LocalSite has an application question", "EditSite requires application, but neither it nor LocalSite has an application question",
LemmyErrorType::ApplicationQuestionRequired, LemmyErrorType::ApplicationQuestionRequired,
&generate_local_site( &LocalSite {
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &EditSite {
&generate_edit_site( name: Some(String::from("site_name")),
Some(String::from("site_name")), registration_mode: Some(RegistrationMode::RequireApplication),
None::<String>, ..Default::default()
None::<String>, },
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
Some(RegistrationMode::RequireApplication),
None::<bool>,
),
), ),
]; ];
@ -449,99 +390,65 @@ mod tests {
let valid_payloads = [ let valid_payloads = [
( (
"No changes between LocalSite and EditSite", "No changes between LocalSite and EditSite",
&generate_local_site( &LocalSite {
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &EditSite::default(),
&generate_edit_site(
None::<String>,
None::<String>,
None::<String>,
None::<ListingType>,
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
None::<bool>,
),
), ),
( (
"EditSite allows clearing and changing values", "EditSite allows clearing and changing values",
&generate_local_site( &LocalSite {
None::<String>, private_instance: true,
true, federation_enabled: false,
false, registration_mode: RegistrationMode::Open,
None::<String>, ..Default::default()
RegistrationMode::Open, },
), &EditSite {
&generate_edit_site( name: Some(String::from("site_name")),
Some(String::from("site_name")), sidebar: Some(String::new()),
Some(String::new()), description: Some(String::new()),
Some(String::new()), application_question: Some(String::new()),
Some(ListingType::All), private_instance: Some(false),
Some(PostSortType::Active), default_post_listing_type: Some(ListingType::All),
Some(CommentSortType::Hot), default_post_sort_type: Some(PostSortType::Active),
Some(String::new()), slur_filter_regex: Some(String::new()),
Some(false), registration_mode: Some(RegistrationMode::Open),
Some(true), federation_enabled: Some(true),
Some(String::new()), ..Default::default()
Some(RegistrationMode::Open), },
None::<bool>,
),
), ),
( (
"EditSite name passes slur filter regex", "EditSite name passes slur filter regex",
&generate_local_site( &LocalSite {
Some(String::from("(foo|bar)")), private_instance: true,
true, slur_filter_regex: Some(String::from("(foo|bar)")),
false, registration_mode: RegistrationMode::Open,
None::<String>, federation_enabled: false,
RegistrationMode::Open, ..Default::default()
), },
&generate_edit_site( &EditSite {
Some(String::from("foo site_name")), name: Some(String::from("foo site_name")),
None::<String>, slur_filter_regex: Some(String::new()),
None::<String>, ..Default::default()
None::<ListingType>, },
None::<PostSortType>,
None::<CommentSortType>,
Some(String::new()),
None::<bool>,
None::<bool>,
None::<String>,
None::<RegistrationMode>,
None::<bool>,
),
), ),
( (
"LocalSite has application question and EditSite now requires applications,", "LocalSite has application question and EditSite now requires applications,",
&generate_local_site( &LocalSite {
None::<String>, application_question: Some(String::from("question")),
true, private_instance: true,
false, federation_enabled: false,
Some(String::from("question")), registration_mode: RegistrationMode::Open,
RegistrationMode::Open, ..Default::default()
), },
&generate_edit_site( &EditSite {
Some(String::from("site_name")), name: Some(String::from("site_name")),
None::<String>, registration_mode: Some(RegistrationMode::RequireApplication),
None::<String>, ..Default::default()
None::<ListingType>, },
None::<PostSortType>,
None::<CommentSortType>,
None::<String>,
None::<bool>,
None::<bool>,
None::<String>,
Some(RegistrationMode::RequireApplication),
None::<bool>,
),
), ),
]; ];
@ -557,88 +464,4 @@ mod tests {
); );
}) })
} }
fn generate_local_site(
site_slur_filter_regex: Option<String>,
site_is_private: bool,
site_is_federated: bool,
site_application_question: Option<String>,
site_registration_mode: RegistrationMode,
) -> LocalSite {
LocalSite {
application_question: site_application_question,
private_instance: site_is_private,
slur_filter_regex: site_slur_filter_regex,
federation_enabled: site_is_federated,
registration_mode: site_registration_mode,
..Default::default()
}
}
// Allow the test helper function to have too many arguments.
// It's either this or generate the entire struct each time for testing.
#[allow(clippy::too_many_arguments)]
fn generate_edit_site(
site_name: Option<String>,
site_description: Option<String>,
site_sidebar: Option<String>,
site_listing_type: Option<ListingType>,
site_post_sort_type: Option<PostSortType>,
site_comment_sort_type: Option<CommentSortType>,
site_slur_filter_regex: Option<String>,
site_is_private: Option<bool>,
site_is_federated: Option<bool>,
site_application_question: Option<String>,
site_registration_mode: Option<RegistrationMode>,
site_oauth_registration: Option<bool>,
) -> EditSite {
EditSite {
name: site_name,
sidebar: site_sidebar,
description: site_description,
icon: None,
banner: None,
enable_downvotes: None,
enable_nsfw: None,
community_creation_admin_only: None,
require_email_verification: None,
application_question: site_application_question,
private_instance: site_is_private,
default_theme: None,
default_post_listing_type: site_listing_type,
default_post_sort_type: site_post_sort_type,
default_comment_sort_type: site_comment_sort_type,
legal_information: None,
application_email_admins: None,
hide_modlog_mod_names: None,
discussion_languages: None,
slur_filter_regex: site_slur_filter_regex,
actor_name_max_length: None,
rate_limit_message: None,
rate_limit_message_per_second: None,
rate_limit_post: None,
rate_limit_post_per_second: None,
rate_limit_register: None,
rate_limit_register_per_second: None,
rate_limit_image: None,
rate_limit_image_per_second: None,
rate_limit_comment: None,
rate_limit_comment_per_second: None,
rate_limit_search: None,
rate_limit_search_per_second: None,
federation_enabled: site_is_federated,
federation_debug: None,
captcha_enabled: None,
captcha_difficulty: None,
allowed_instances: None,
blocked_instances: None,
blocked_urls: None,
taglines: None,
registration_mode: site_registration_mode,
reports_email_admins: None,
content_warning: None,
default_post_listing_mode: None,
oauth_registration: site_oauth_registration,
}
}
} }