Skip to content

Commit

Permalink
Retrieve /tags endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer committed Dec 24, 2024
1 parent 9b61e62 commit 27ecabe
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ 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.TagListParams
import uniffi.wp_api.wpAuthenticationFromUsernameAndPassword
import kotlin.test.assertNotNull
import kotlin.test.assertNull

private const val TAG_ID_100: Long = 100

class TagsEndpointTest {
private val testCredentials = TestCredentials.INSTANCE
Expand All @@ -20,4 +25,41 @@ class TagsEndpointTest {
}.assertSuccessAndRetrieveData().data
assert(tagList.isNotEmpty())
}

@Test
fun testFilterTagListRequest() = runTest {
val tagList = client.request { requestBuilder ->
requestBuilder.tags().filterListWithEditContext(
params = TagListParams(),
fields = listOf(
SparseTagFieldWithEditContext.NAME,
SparseTagFieldWithEditContext.SLUG
)
)
}.assertSuccessAndRetrieveData().data
assert(tagList.isNotEmpty())
assertNull(tagList.first().description)
}

@Test
fun testRetrieveMediaRequest() = runTest {
val tag = client.request { requestBuilder ->
requestBuilder.tags().retrieveWithEditContext(TAG_ID_100)
}.assertSuccessAndRetrieveData().data
assertNotNull(tag)
}

@Test
fun testFilterRetrieveTagRequest() = runTest {
val tag = client.request { requestBuilder ->
requestBuilder.tags().filterRetrieveWithEditContext(
TAG_ID_100,
fields = listOf(
SparseTagFieldWithEditContext.NAME,
SparseTagFieldWithEditContext.SLUG
)
)
}.assertSuccessAndRetrieveData().data
assertNull(tag.description)
}
}
4 changes: 3 additions & 1 deletion wp_api/src/request/endpoint/tags_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{AsNamespace, DerivedRequest, WpNamespace};
use crate::{
tags::{
SparseTagFieldWithEditContext, SparseTagFieldWithEmbedContext,
SparseTagFieldWithViewContext, TagListParams,
SparseTagFieldWithViewContext, TagId, TagListParams,
},
SparseField,
};
Expand All @@ -12,6 +12,8 @@ use wp_derive_request_builder::WpDerivedRequest;
enum TagsRequest {
#[contextual_paged(url = "/tags", params = &TagListParams, output = Vec<crate::tags::SparseTag>, filter_by = crate::tags::SparseTagField)]
List,
#[contextual_get(url = "/tags/<tag_id>", output = crate::tags::SparseTag, filter_by = crate::tags::SparseTagField)]
Retrieve,
}

impl DerivedRequest for TagsRequest {
Expand Down
6 changes: 6 additions & 0 deletions wp_api/src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ impl FromStr for TagId {
}
}

impl std::fmt::Display for TagId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
pub enum WpApiParamTagsOrderBy {
Id,
Expand Down
78 changes: 78 additions & 0 deletions wp_api_integration_tests/tests/test_tags_immut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ async fn list_with_view_context(#[case] params: TagListParams) {
.assert_response();
}

#[tokio::test]
#[parallel]
async fn retrieve_with_edit_context() {
api_client()
.tags()
.retrieve_with_edit_context(&TAG_ID_100)
.await
.assert_response();
}

#[tokio::test]
#[parallel]
async fn retrieve_with_embed_context() {
api_client()
.tags()
.retrieve_with_embed_context(&TAG_ID_100)
.await
.assert_response();
}

#[tokio::test]
#[parallel]
async fn retrieve_with_view_context() {
api_client()
.tags()
.retrieve_with_view_context(&TAG_ID_100)
.await
.assert_response();
}

#[template]
#[rstest]
#[case::default(TagListParams::default())]
Expand Down Expand Up @@ -90,6 +120,22 @@ mod filter {
});
}

#[apply(sparse_tag_field_with_edit_context_test_cases)]
#[case(&[SparseTagFieldWithEditContext::Name, SparseTagFieldWithEditContext::Slug])]
#[tokio::test]
#[parallel]
async fn filter_retrieve_posts_with_edit_context(
#[case] fields: &[SparseTagFieldWithEditContext],
) {
let tag = api_client()
.tags()
.filter_retrieve_with_edit_context(&TAG_ID_100, fields)
.await
.assert_response()
.data;
tag.assert_that_instance_fields_nullability_match_provided_fields(fields)
}

#[apply(sparse_tag_field_with_embed_context_test_cases)]
#[case(&[SparseTagFieldWithEmbedContext::Name, SparseTagFieldWithEmbedContext::Slug])]
#[tokio::test]
Expand All @@ -115,6 +161,22 @@ mod filter {
});
}

#[apply(sparse_tag_field_with_embed_context_test_cases)]
#[case(&[SparseTagFieldWithEmbedContext::Name, SparseTagFieldWithEmbedContext::Slug])]
#[tokio::test]
#[parallel]
async fn filter_retrieve_posts_with_embed_context(
#[case] fields: &[SparseTagFieldWithEmbedContext],
) {
let tag = api_client()
.tags()
.filter_retrieve_with_embed_context(&TAG_ID_100, fields)
.await
.assert_response()
.data;
tag.assert_that_instance_fields_nullability_match_provided_fields(fields)
}

#[apply(sparse_tag_field_with_view_context_test_cases)]
#[case(&[SparseTagFieldWithViewContext::Name, SparseTagFieldWithViewContext::Slug])]
#[tokio::test]
Expand All @@ -139,4 +201,20 @@ mod filter {
tag.assert_that_instance_fields_nullability_match_provided_fields(fields)
});
}

#[apply(sparse_tag_field_with_view_context_test_cases)]
#[case(&[SparseTagFieldWithViewContext::Name, SparseTagFieldWithViewContext::Slug])]
#[tokio::test]
#[parallel]
async fn filter_retrieve_posts_with_view_context(
#[case] fields: &[SparseTagFieldWithViewContext],
) {
let tag = api_client()
.tags()
.filter_retrieve_with_view_context(&TAG_ID_100, fields)
.await
.assert_response()
.data;
tag.assert_that_instance_fields_nullability_match_provided_fields(fields)
}
}

0 comments on commit 27ecabe

Please sign in to comment.