Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve /tags endpoint #465

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter retrieve test names refer to posts. I am fixing these in the /categories PR I am working on right now to avoid merge conflicts.

#[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)
}
}
Loading