From ba47575839cc40ac5332455a535558ec73b041a6 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Thu, 2 May 2024 13:23:48 -0400 Subject: [PATCH 1/4] Implement test_user_list_params_default_is_empty --- wp_api/src/users.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/wp_api/src/users.rs b/wp_api/src/users.rs index 876cc07fd..363c34202 100644 --- a/wp_api/src/users.rs +++ b/wp_api/src/users.rs @@ -128,7 +128,6 @@ impl Default for WPApiParamUsersWho { #[derive(Default, uniffi::Record)] pub struct UserListParams { - // TODO: Implement the `_filter` /// Current page of the collection. /// Default: `1` pub page: Option, @@ -443,3 +442,18 @@ impl SparseUserField { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_user_list_params_default_is_empty() { + assert!(UserListParams::default() + .query_pairs() + .into_iter() + .peekable() + .peek() + .is_none()); + } +} From 2e6a126bc31ff353f8cabcce03efef26fd9f0095 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Thu, 2 May 2024 15:44:02 -0400 Subject: [PATCH 2/4] Implement unit tests for UserListParams --- wp_api/src/endpoint/users_endpoint.rs | 4 +- wp_api/src/users.rs | 63 ++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/wp_api/src/endpoint/users_endpoint.rs b/wp_api/src/endpoint/users_endpoint.rs index e47b4f751..b3419d900 100644 --- a/wp_api/src/endpoint/users_endpoint.rs +++ b/wp_api/src/endpoint/users_endpoint.rs @@ -164,7 +164,7 @@ mod tests { }; validate_endpoint( users_endpoint.list(WPContext::Edit, Some(¶ms)), - "/users?context=edit&page=2&per_page=60&search=foo&slug=bar%2Cbaz&has_published_post=true", + "/users?context=edit&page=2&per_page=60&search=foo&slug=bar%2Cbaz&has_published_posts=true", &api_base_url, ); } @@ -188,7 +188,7 @@ mod tests { }; validate_endpoint( users_endpoint.filter_list(WPContext::Edit, Some(¶ms), &vec![SparseUserField::Name, SparseUserField::Email]), - "/users?context=edit&page=2&per_page=60&search=foo&slug=bar%2Cbaz&has_published_post=true&_fields=name%2Cemail", + "/users?context=edit&page=2&per_page=60&search=foo&slug=bar%2Cbaz&has_published_posts=true&_fields=name%2Cemail", &api_base_url, ); } diff --git a/wp_api/src/users.rs b/wp_api/src/users.rs index 363c34202..d4f975719 100644 --- a/wp_api/src/users.rs +++ b/wp_api/src/users.rs @@ -126,7 +126,7 @@ impl Default for WPApiParamUsersWho { } } -#[derive(Default, uniffi::Record)] +#[derive(Default, Debug, uniffi::Record)] pub struct UserListParams { /// Current page of the collection. /// Default: `1` @@ -209,7 +209,7 @@ impl UserListParams { self.who.and_then(|x| x.as_str().map(|s| s.to_string())), ), ( - "has_published_post", + "has_published_posts", self.has_published_posts.map(|x| x.to_string()), ), ] @@ -446,14 +446,57 @@ impl SparseUserField { #[cfg(test)] mod tests { use super::*; + use rstest::*; - #[test] - fn test_user_list_params_default_is_empty() { - assert!(UserListParams::default() - .query_pairs() - .into_iter() - .peekable() - .peek() - .is_none()); + macro_rules! create_params { + ($t: ident) => { + $t::default() + }; + ($t: ident $(, ($f:ident, $v:expr))*) => {{ + let mut params = $t::default(); + $(params.$f = $v;)* + params + }}; + } + + #[rstest] + #[case(create_params!(UserListParams), &[])] + #[case(create_params!(UserListParams, (page, Some(1))), &[("page", "1")])] + #[case(create_params!(UserListParams, (page, Some(2)), (per_page, Some(5))), &[("page", "2"), ("per_page", "5")])] + #[case(create_params!(UserListParams, (search, Some("foo".to_string()))), &[("search", "foo")])] + #[case(create_params!(UserListParams, (exclude, vec![UserId(1), UserId(2)])), &[("exclude", "1,2")])] + #[case(create_params!(UserListParams, (include, vec![UserId(1)])), &[("include", "1")])] + #[case(create_params!(UserListParams, (per_page, Some(100)), (offset, Some(20))), &[("per_page", "100"), ("offset", "20")])] + #[case(create_params!(UserListParams, (order, Some(WPApiParamOrder::Asc))), &[("order", "asc")])] + #[case(create_params!(UserListParams, (orderby, Some(WPApiParamUsersOrderBy::Id))), &[("orderby", "id")])] + #[case(create_params!(UserListParams, (order, Some(WPApiParamOrder::Desc)), (orderby, Some(WPApiParamUsersOrderBy::Email))), &[("order", "desc"), ("orderby", "email")])] + #[case(create_params!(UserListParams, (slug, vec!["foo".to_string(), "bar".to_string()])), &[("slug", "foo,bar")])] + #[case(create_params!(UserListParams, (roles, vec!["author".to_string(), "editor".to_string()])), &[("roles", "author,editor")])] + #[case(create_params!(UserListParams, (slug, vec!["foo".to_string(), "bar".to_string()]), (roles, vec!["author".to_string(), "editor".to_string()])), &[("slug", "foo,bar"), ("roles", "author,editor")])] + #[case(create_params!(UserListParams, (capabilities, vec!["edit_themes".to_string(), "delete_pages".to_string()])), &[("capabilities", "edit_themes,delete_pages")])] + #[case::who_all_param_should_be_empty(create_params!(UserListParams, (who, Some(WPApiParamUsersWho::All))), &[])] + #[case(create_params!(UserListParams, (who, Some(WPApiParamUsersWho::Authors))), &[("who", "authors")])] + #[case(create_params!(UserListParams, (has_published_posts, Some(true))), &[("has_published_posts", "true")])] + #[trace] + fn test_user_list_params( + #[case] params: UserListParams, + #[case] expected_pairs: &[(&str, &str)], + ) { + assert_expected_query_pairs(params.query_pairs(), expected_pairs); + } + + fn assert_expected_query_pairs<'a>( + query_pairs: impl IntoIterator, + expected_pairs: &[(&'a str, &str)], + ) { + let mut query_pairs = query_pairs.into_iter().collect::>(); + let mut expected_pairs: Vec<(&str, String)> = expected_pairs + .iter() + .map(|(k, v)| (*k, v.to_string())) + .collect(); + // The order of query pairs doesn't matter + query_pairs.sort(); + expected_pairs.sort(); + assert_eq!(query_pairs, expected_pairs); } } From 0f67b4a4bfd41747e38cf51cb8509d627c1e73e9 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Thu, 2 May 2024 15:59:30 -0400 Subject: [PATCH 3/4] Implement test_user_delete_params --- wp_api/src/users.rs | 59 +++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/wp_api/src/users.rs b/wp_api/src/users.rs index d4f975719..85270562f 100644 --- a/wp_api/src/users.rs +++ b/wp_api/src/users.rs @@ -330,13 +330,17 @@ pub struct UserUpdateParams { pub meta: Option, } -#[derive(uniffi::Record)] +#[derive(Debug, uniffi::Record)] pub struct UserDeleteParams { /// Reassign the deleted user's posts and links to this user ID. pub reassign: UserId, } impl UserDeleteParams { + pub fn new(reassign: UserId) -> Self { + Self { reassign } + } + pub fn query_pairs(&self) -> impl IntoIterator { [ ("reassign", self.reassign.to_string()), @@ -448,35 +452,35 @@ mod tests { use super::*; use rstest::*; - macro_rules! create_params { - ($t: ident) => { - $t::default() + macro_rules! user_list_params { + () => { + UserListParams::default() }; - ($t: ident $(, ($f:ident, $v:expr))*) => {{ - let mut params = $t::default(); + ($(($f:ident, $v:expr)), *) => {{ + let mut params = UserListParams::default(); $(params.$f = $v;)* params }}; } #[rstest] - #[case(create_params!(UserListParams), &[])] - #[case(create_params!(UserListParams, (page, Some(1))), &[("page", "1")])] - #[case(create_params!(UserListParams, (page, Some(2)), (per_page, Some(5))), &[("page", "2"), ("per_page", "5")])] - #[case(create_params!(UserListParams, (search, Some("foo".to_string()))), &[("search", "foo")])] - #[case(create_params!(UserListParams, (exclude, vec![UserId(1), UserId(2)])), &[("exclude", "1,2")])] - #[case(create_params!(UserListParams, (include, vec![UserId(1)])), &[("include", "1")])] - #[case(create_params!(UserListParams, (per_page, Some(100)), (offset, Some(20))), &[("per_page", "100"), ("offset", "20")])] - #[case(create_params!(UserListParams, (order, Some(WPApiParamOrder::Asc))), &[("order", "asc")])] - #[case(create_params!(UserListParams, (orderby, Some(WPApiParamUsersOrderBy::Id))), &[("orderby", "id")])] - #[case(create_params!(UserListParams, (order, Some(WPApiParamOrder::Desc)), (orderby, Some(WPApiParamUsersOrderBy::Email))), &[("order", "desc"), ("orderby", "email")])] - #[case(create_params!(UserListParams, (slug, vec!["foo".to_string(), "bar".to_string()])), &[("slug", "foo,bar")])] - #[case(create_params!(UserListParams, (roles, vec!["author".to_string(), "editor".to_string()])), &[("roles", "author,editor")])] - #[case(create_params!(UserListParams, (slug, vec!["foo".to_string(), "bar".to_string()]), (roles, vec!["author".to_string(), "editor".to_string()])), &[("slug", "foo,bar"), ("roles", "author,editor")])] - #[case(create_params!(UserListParams, (capabilities, vec!["edit_themes".to_string(), "delete_pages".to_string()])), &[("capabilities", "edit_themes,delete_pages")])] - #[case::who_all_param_should_be_empty(create_params!(UserListParams, (who, Some(WPApiParamUsersWho::All))), &[])] - #[case(create_params!(UserListParams, (who, Some(WPApiParamUsersWho::Authors))), &[("who", "authors")])] - #[case(create_params!(UserListParams, (has_published_posts, Some(true))), &[("has_published_posts", "true")])] + #[case(user_list_params!(), &[])] + #[case(user_list_params!((page, Some(1))), &[("page", "1")])] + #[case(user_list_params!((page, Some(2)), (per_page, Some(5))), &[("page", "2"), ("per_page", "5")])] + #[case(user_list_params!((search, Some("foo".to_string()))), &[("search", "foo")])] + #[case(user_list_params!((exclude, vec![UserId(1), UserId(2)])), &[("exclude", "1,2")])] + #[case(user_list_params!((include, vec![UserId(1)])), &[("include", "1")])] + #[case(user_list_params!((per_page, Some(100)), (offset, Some(20))), &[("per_page", "100"), ("offset", "20")])] + #[case(user_list_params!((order, Some(WPApiParamOrder::Asc))), &[("order", "asc")])] + #[case(user_list_params!((orderby, Some(WPApiParamUsersOrderBy::Id))), &[("orderby", "id")])] + #[case(user_list_params!((order, Some(WPApiParamOrder::Desc)), (orderby, Some(WPApiParamUsersOrderBy::Email))), &[("order", "desc"), ("orderby", "email")])] + #[case(user_list_params!((slug, vec!["foo".to_string(), "bar".to_string()])), &[("slug", "foo,bar")])] + #[case(user_list_params!((roles, vec!["author".to_string(), "editor".to_string()])), &[("roles", "author,editor")])] + #[case(user_list_params!((slug, vec!["foo".to_string(), "bar".to_string()]), (roles, vec!["author".to_string(), "editor".to_string()])), &[("slug", "foo,bar"), ("roles", "author,editor")])] + #[case(user_list_params!((capabilities, vec!["edit_themes".to_string(), "delete_pages".to_string()])), &[("capabilities", "edit_themes,delete_pages")])] + #[case::who_all_param_should_be_empty(user_list_params!((who, Some(WPApiParamUsersWho::All))), &[])] + #[case(user_list_params!((who, Some(WPApiParamUsersWho::Authors))), &[("who", "authors")])] + #[case(user_list_params!((has_published_posts, Some(true))), &[("has_published_posts", "true")])] #[trace] fn test_user_list_params( #[case] params: UserListParams, @@ -485,6 +489,15 @@ mod tests { assert_expected_query_pairs(params.query_pairs(), expected_pairs); } + #[test] + fn test_user_delete_params() { + let params = UserDeleteParams::new(UserId(987)); + assert_expected_query_pairs( + params.query_pairs(), + &[("force", "true"), ("reassign", "987")], + ); + } + fn assert_expected_query_pairs<'a>( query_pairs: impl IntoIterator, expected_pairs: &[(&'a str, &str)], From e409d6a51ae58e12e268cf46050fde27fd6fbdb7 Mon Sep 17 00:00:00 2001 From: Oguz Kocer Date: Thu, 2 May 2024 16:08:47 -0400 Subject: [PATCH 4/4] Use --nocapture option for unit & doc tests --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 826c5b2ce..f987d14c3 100644 --- a/Makefile +++ b/Makefile @@ -208,10 +208,10 @@ test-android: bindings _test-android publish-android-local: bindings _publish-android-local test-rust-lib: - $(rust_docker_run) cargo test --lib + $(rust_docker_run) cargo test --lib -- --nocapture test-rust-doc: - $(rust_docker_run) cargo test --doc + $(rust_docker_run) cargo test --doc -- --nocapture test-server: stop-server rm -rf test_credentials && touch test_credentials && chmod 777 test_credentials