Skip to content

Commit

Permalink
Create /tags endpoint (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer authored Jan 16, 2025
1 parent f5b7232 commit 1ae53c9
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package rs.wordpress.api.kotlin
import kotlinx.coroutines.test.runTest
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.wpAuthenticationFromUsernameAndPassword
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

Expand Down Expand Up @@ -63,6 +65,17 @@ class TagsEndpointTest {
assertNull(tag.description)
}

@Test
fun createTagRequest() = runTest {
val createdTag = client.request { requestBuilder ->
requestBuilder.tags()
.create(TagCreateParams(name = "foo", description = "bar"))
}.assertSuccessAndRetrieveData().data
assertEquals(createdTag.name, "foo")
assertEquals(createdTag.description, "bar")
restoreTestServer()
}

@Test
fun deleteTagRequest() = runTest {
val deletedTag = client.request { requestBuilder ->
Expand Down
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 @@ -14,6 +14,8 @@ enum TagsRequest {
List,
#[contextual_get(url = "/tags/<tag_id>", output = crate::tags::SparseTag, filter_by = crate::tags::SparseTagField)]
Retrieve,
#[post(url = "/tags", params = &crate::tags::TagCreateParams, output = crate::tags::TagWithEditContext)]
Create,
#[delete(url = "/tags/<tag_id>", output = crate::tags::TagDeleteResponse)]
Delete,
}
Expand Down
15 changes: 15 additions & 0 deletions wp_api/src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ pub struct TagDeleteResponse {
pub previous: TagWithEditContext,
}

#[derive(Debug, Serialize, uniffi::Record)]
pub struct TagCreateParams {
/// HTML title for the term.
pub name: 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
92 changes: 91 additions & 1 deletion wp_api_integration_tests/tests/test_tags_mut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,81 @@
use serial_test::serial;
use wp_api::tags::{TagCreateParams, TagWithEditContext};
use wp_api_integration_tests::backend::{Backend, RestoreServer};
use wp_api_integration_tests::{api_client, TAG_ID_100};
use wp_api_integration_tests::{api_client, AssertResponse, TAG_ID_100};
use wp_cli::WpCliTag;

#[tokio::test]
#[serial]
async fn create_tag_with_just_name() {
test_create_tag(
&TagCreateParams {
name: "foo".to_string(),
description: None,
slug: None,
},
|created_tag, tag_from_wp_cli| {
assert_eq!(created_tag.name, "foo");
assert_eq!(tag_from_wp_cli.name, "foo");
},
)
.await;
}

#[tokio::test]
#[serial]
async fn create_tag_with_name_and_description() {
test_create_tag(
&TagCreateParams {
name: "foo".to_string(),
description: Some("bar".to_string()),
slug: None,
},
|created_tag, tag_from_wp_cli| {
assert_eq!(created_tag.name, "foo");
assert_eq!(created_tag.description, "bar");
assert_eq!(tag_from_wp_cli.description, "bar");
},
)
.await;
}

#[tokio::test]
#[serial]
async fn create_tag_with_name_and_slug() {
test_create_tag(
&TagCreateParams {
name: "foo".to_string(),
description: None,
slug: Some("bar".to_string()),
},
|created_tag, tag_from_wp_cli| {
assert_eq!(created_tag.name, "foo");
assert_eq!(created_tag.slug, "bar");
assert_eq!(tag_from_wp_cli.slug, "bar");
},
)
.await;
}

#[tokio::test]
#[serial]
async fn create_tag_with_name_description_and_slug() {
test_create_tag(
&TagCreateParams {
name: "foo".to_string(),
description: Some("bar".to_string()),
slug: Some("quox".to_string()),
},
|created_tag, tag_from_wp_cli| {
assert_eq!(created_tag.name, "foo");
assert_eq!(created_tag.description, "bar");
assert_eq!(tag_from_wp_cli.description, "bar");
assert_eq!(created_tag.slug, "quox");
assert_eq!(tag_from_wp_cli.slug, "quox");
},
)
.await;
}

#[tokio::test]
#[serial]
Expand All @@ -21,3 +96,18 @@ async fn delete_tag() {

RestoreServer::db().await;
}

async fn test_create_tag<F>(params: &TagCreateParams, assert: F)
where
F: Fn(TagWithEditContext, WpCliTag),
{
let created_tag = api_client()
.tags()
.create(params)
.await
.assert_response()
.data;
let created_tag_from_wp_cli = Backend::tag(&created_tag.id).await;
assert(created_tag, created_tag_from_wp_cli);
RestoreServer::db().await;
}

0 comments on commit 1ae53c9

Please sign in to comment.