Skip to content

Commit

Permalink
Merge pull request #391 from Ecwid/ECWID-136994
Browse files Browse the repository at this point in the history
ECWID-136994 Add search parameter to the customer group endpoint
  • Loading branch information
zenon-ecwid authored Apr 8, 2024
2 parents 03e102a + fa1ed65 commit cecdc51
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.ecwid.apiclient.v3.responsefields.ResponseFields
data class CustomerGroupsSearchRequest(
val offset: Int = 0,
val limit: Int = 100,
val keyword: String? = null,
val customerGroupIds: List<Int>? = null,
val responseFields: ResponseFields = ResponseFields.All,
) : ApiRequest {
override fun toRequestInfo() = RequestInfo.createGetRequest(
Expand All @@ -22,6 +24,12 @@ data class CustomerGroupsSearchRequest(
return mutableMapOf<String, String>().apply {
put("offset", request.offset.toString())
put("limit", request.limit.toString())
if (!request.keyword.isNullOrBlank()) {
put("keyword", request.keyword)
}
if (!request.customerGroupIds.isNullOrEmpty()) {
put("customerGroupIds", request.customerGroupIds.joinToString(","))
}
}.toMap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

const val TEST_SEARCH_PHRASE = "Test"
const val TEST_CUSTOMER_GROUP = "Customer group"

class CustomerGroupsTest : BaseEntityTest() {

@BeforeEach
Expand Down Expand Up @@ -61,10 +64,11 @@ class CustomerGroupsTest : BaseEntityTest() {

@Test
fun testSearchPaging() {
// Create three customer groups additionally to always existing “General” group
repeat(3) {
// Create 6 customer groups to test paging
val customerGroups = generateSearchTestCustomerGroups()
repeat(customerGroups.size) {
val customerGroupCreateRequest = CustomerGroupCreateRequest(
newCustomerGroup = generateTestCustomerGroup()
newCustomerGroup = customerGroups[it]
)
val customerGroupCreateResult = apiClient.createCustomerGroup(customerGroupCreateRequest)
assertTrue(customerGroupCreateResult.id > 0)
Expand All @@ -73,19 +77,53 @@ class CustomerGroupsTest : BaseEntityTest() {
// Trying to request first page
val customerGroupsSearchRequest1 = CustomerGroupsSearchRequest(offset = 0, limit = 2)
val customerGroupsSearchResult1 = apiClient.searchCustomerGroups(customerGroupsSearchRequest1)
assertEquals(2 + 1, customerGroupsSearchResult1.count) // “General” group exists is on every page
assertEquals(3, customerGroupsSearchResult1.total)
assertEquals(2, customerGroupsSearchResult1.count) // “General” group exists only of first page
assertEquals(7, customerGroupsSearchResult1.total)

// Trying to request second and the last page
val customerGroupsSearchRequest2 = CustomerGroupsSearchRequest(offset = 2, limit = 2)
val customerGroupsSearchRequest2 = CustomerGroupsSearchRequest(offset = 6, limit = 2)
val customerGroupsSearchResult2 = apiClient.searchCustomerGroups(customerGroupsSearchRequest2)
assertEquals(1 + 1, customerGroupsSearchResult2.count) // “General” group exists is on every page
assertEquals(3, customerGroupsSearchResult2.total)
assertEquals(1, customerGroupsSearchResult2.count) // “General” group exists only of first page
assertEquals(7, customerGroupsSearchResult2.total)

// test by keyword "Customer group"
val customerGroupsSearchRequest3 = CustomerGroupsSearchRequest(
keyword = TEST_CUSTOMER_GROUP,
)
val customerGroupsSearchResult3 = apiClient.searchCustomerGroups(customerGroupsSearchRequest3)
assertEquals(true, customerGroupsSearchResult3.items.all { it.name.contains(TEST_CUSTOMER_GROUP) })

// test by keyword "Test"
val customerGroupsSearchRequest4 = CustomerGroupsSearchRequest(
keyword = TEST_SEARCH_PHRASE,
)
val customerGroupsSearchResult4 = apiClient.searchCustomerGroups(customerGroupsSearchRequest4)
assertEquals(true, customerGroupsSearchResult4.items.all { it.name.contains(TEST_SEARCH_PHRASE) })

val testGroupIds = customerGroupsSearchResult4.items.map { it.id }

// test by customerGroupIds
val customerGroupsSearchRequest5 = CustomerGroupsSearchRequest(
customerGroupIds = testGroupIds,
)
val customerGroupsSearchResult5 = apiClient.searchCustomerGroups(customerGroupsSearchRequest5)
assertEquals(testGroupIds.size, customerGroupsSearchResult5.total)
assertEquals(testGroupIds, customerGroupsSearchResult5.items.map { it.id })

}
}

private fun generateTestCustomerGroup(): UpdatedCustomerGroup {
return UpdatedCustomerGroup(
name = "Customer group " + randomAlphanumeric(8)
name = "$TEST_CUSTOMER_GROUP " + randomAlphanumeric(8)
)
}

private fun generateSearchTestCustomerGroups(): List<UpdatedCustomerGroup> {
val result = mutableListOf<UpdatedCustomerGroup>()
repeat(3) {
result.add(UpdatedCustomerGroup("$TEST_CUSTOMER_GROUP $it"))
result.add(UpdatedCustomerGroup("$TEST_SEARCH_PHRASE $it"))
}
return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ecwid.apiclient.v3.dto.batch.result.GetEscapedBatchResult
import com.ecwid.apiclient.v3.dto.batch.result.GetTypedBatchResult
import com.ecwid.apiclient.v3.dto.cart.result.CartUpdateResult
import com.ecwid.apiclient.v3.dto.cart.result.ConvertCartToOrderResult
import com.ecwid.apiclient.v3.dto.customergroup.request.CustomerGroupsSearchRequest
import com.ecwid.apiclient.v3.dto.instantsite.redirects.request.InstantSiteRedirectsGetForExactPathRequest
import com.ecwid.apiclient.v3.dto.instantsite.redirects.request.InstantSiteRedirectsSearchRequest
import com.ecwid.apiclient.v3.dto.order.result.DeletedOrder
Expand Down Expand Up @@ -132,6 +133,9 @@ val otherNullablePropertyRules: List<NullablePropertyRule<*, *>> = listOf(
AllowNullable(InstantSiteRedirectsGetForExactPathRequest::exactPath),

AllowNullable(UpdatedProductReviewStatus::status),

AllowNullable(CustomerGroupsSearchRequest::keyword),
AllowNullable(CustomerGroupsSearchRequest::customerGroupIds),
)

val nullablePropertyRules: List<NullablePropertyRule<*, *>> = listOf(
Expand Down

0 comments on commit cecdc51

Please sign in to comment.