Skip to content

Commit

Permalink
Update /tags endpoint (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer authored Jan 16, 2025
1 parent 1ae53c9 commit 6ef8d83
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test
import uniffi.wp_api.SparseTagFieldWithEditContext
import uniffi.wp_api.TagCreateParams
import uniffi.wp_api.TagListParams
import uniffi.wp_api.TagUpdateParams
import uniffi.wp_api.wpAuthenticationFromUsernameAndPassword
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
Expand Down Expand Up @@ -71,8 +72,8 @@ class TagsEndpointTest {
requestBuilder.tags()
.create(TagCreateParams(name = "foo", description = "bar"))
}.assertSuccessAndRetrieveData().data
assertEquals(createdTag.name, "foo")
assertEquals(createdTag.description, "bar")
assertEquals("foo", createdTag.name)
assertEquals("bar", createdTag.description)
restoreTestServer()
}

Expand All @@ -84,4 +85,23 @@ class TagsEndpointTest {
assert(deletedTag.deleted)
restoreTestServer()
}

@Test
fun updateTagRequest() = runTest {
val updatedTag = client.request { requestBuilder ->
requestBuilder.tags()
.update(
tagId = TAG_ID_100,
TagUpdateParams(
name = "new_name",
description = "new_description",
slug = "new_slug"
)
)
}.assertSuccessAndRetrieveData().data
assertEquals("new_name", updatedTag.name)
assertEquals("new_description", updatedTag.description)
assertEquals("new_slug", updatedTag.slug)
restoreTestServer()
}
}
2 changes: 2 additions & 0 deletions wp_api/src/request/endpoint/tags_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ enum TagsRequest {
Create,
#[delete(url = "/tags/<tag_id>", output = crate::tags::TagDeleteResponse)]
Delete,
#[post(url = "/tags/<tag_id>", params = &crate::tags::TagUpdateParams, output = crate::tags::TagWithEditContext)]
Update,
}

impl DerivedRequest for TagsRequest {
Expand Down
17 changes: 17 additions & 0 deletions wp_api/src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ pub struct TagCreateParams {
// meta field is omitted for now: https://github.com/Automattic/wordpress-rs/issues/463
}

#[derive(Debug, Default, Serialize, uniffi::Record)]
pub struct TagUpdateParams {
/// HTML title for the term.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// HTML description of the term.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// An alphanumeric identifier for the term unique to its type.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub slug: Option<String>,
// meta field is omitted for now: https://github.com/Automattic/wordpress-rs/issues/463
}

#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
pub struct SparseTag {
#[WpContext(edit, embed, view)]
Expand Down
70 changes: 69 additions & 1 deletion wp_api_integration_tests/tests/test_tags_mut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use macro_helper::generate_update_test;
use serial_test::serial;
use wp_api::tags::{TagCreateParams, TagWithEditContext};
use wp_api::tags::{TagCreateParams, TagUpdateParams, TagWithEditContext};
use wp_api_integration_tests::backend::{Backend, RestoreServer};
use wp_api_integration_tests::{api_client, AssertResponse, TAG_ID_100};
use wp_cli::WpCliTag;
Expand Down Expand Up @@ -97,6 +98,36 @@ async fn delete_tag() {
RestoreServer::db().await;
}

generate_update_test!(
update_description,
description,
"new_description".to_string(),
|updated_tag, updated_tag_from_wp_cli| {
assert_eq!(updated_tag.description, "new_description");
assert_eq!(updated_tag_from_wp_cli.description, "new_description");
}
);

generate_update_test!(
update_name,
name,
"new_name".to_string(),
|updated_tag, updated_tag_from_wp_cli| {
assert_eq!(updated_tag.name, "new_name");
assert_eq!(updated_tag_from_wp_cli.name, "new_name");
}
);

generate_update_test!(
update_slug,
slug,
"new_slug".to_string(),
|updated_tag, updated_tag_from_wp_cli| {
assert_eq!(updated_tag.slug, "new_slug");
assert_eq!(updated_tag_from_wp_cli.slug, "new_slug");
}
);

async fn test_create_tag<F>(params: &TagCreateParams, assert: F)
where
F: Fn(TagWithEditContext, WpCliTag),
Expand All @@ -111,3 +142,40 @@ where
assert(created_tag, created_tag_from_wp_cli);
RestoreServer::db().await;
}

async fn test_update_tag<F>(params: &TagUpdateParams, assert: F)
where
F: Fn(TagWithEditContext, WpCliTag),
{
let updated_tag = api_client()
.tags()
.update(&TAG_ID_100, params)
.await
.assert_response()
.data;
let updated_tag_from_wp_cli = Backend::tag(&TAG_ID_100).await;
assert(updated_tag, updated_tag_from_wp_cli);
RestoreServer::db().await;
}

mod macro_helper {
macro_rules! generate_update_test {
($ident:ident, $field:ident, $new_value:expr, $assertion:expr) => {
paste::paste! {
#[tokio::test]
#[serial]
async fn $ident() {
let updated_value = $new_value;
test_update_tag(
&TagUpdateParams {
$field: Some(updated_value),
..Default::default()
}, $assertion)
.await;
}
}
};
}

pub(super) use generate_update_test;
}

0 comments on commit 6ef8d83

Please sign in to comment.