Skip to content

Commit

Permalink
Merge pull request #2883 from wordpress-mobile/issue/9541-bundle-quan…
Browse files Browse the repository at this point in the history
…tity-rules-support

Bundle quantity rules support
  • Loading branch information
atorresveiga authored Nov 14, 2023
2 parents a11114f + d5ad09c commit 8b108e7
Show file tree
Hide file tree
Showing 8 changed files with 837 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.wordpress.android.fluxc.model

import com.google.gson.Gson
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.wordpress.android.fluxc.JsonLoaderUtils.jsonFileAs
import org.wordpress.android.fluxc.model.WCMetaData.BundleMetadataKeys
import org.wordpress.android.fluxc.model.addons.RemoteAddonDto.RemotePriceType.FlatFee
import org.wordpress.android.fluxc.model.addons.RemoteAddonDto.RemoteRestrictionsType.AnyText
import org.wordpress.android.fluxc.model.addons.RemoteAddonDto.RemoteType.Checkbox
Expand All @@ -13,7 +15,7 @@ class WCProductModelTest {
@Test
fun `Product addons should be serialized correctly`() {
val productModelUnderTest =
"wc/product-with-addons.json"
"wc/product-with-addons.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

Expand All @@ -25,9 +27,9 @@ class WCProductModelTest {
@Test
fun `Product addons should be serialized with enum values correctly`() {
val productModelUnderTest =
"wc/product-with-addons.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()
"wc/product-with-addons.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

assertThat(productModelUnderTest).isNotNull
assertThat(productModelUnderTest?.addons).isNotEmpty
Expand All @@ -42,12 +44,12 @@ class WCProductModelTest {
@Test
fun `Product addons should contain Addon options serialized correctly`() {
val addonOptions = "wc/product-with-addons.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()
?.addons
?.takeIf { it.isNotEmpty() }
?.first()
?.options
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()
?.addons
?.takeIf { it.isNotEmpty() }
?.first()
?.options

assertThat(addonOptions).isNotNull
assertThat(addonOptions).isNotEmpty
Expand All @@ -56,9 +58,9 @@ class WCProductModelTest {
@Test
fun `Product metadata is serialized correctly`() {
val productModelUnderTest =
"wc/product-with-addons.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()
"wc/product-with-addons.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

assertThat(productModelUnderTest).isNotNull
assertThat(productModelUnderTest?.metadata).isNotNull
Expand All @@ -67,12 +69,66 @@ class WCProductModelTest {
@Test
fun `Product addons with incorrect key should be null`() {
val productModelUnderTest =
"wc/product-with-incorrect-addons-key.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()
"wc/product-with-incorrect-addons-key.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

assertThat(productModelUnderTest).isNotNull
assertThat(productModelUnderTest?.metadata).isNotNull
assertThat(productModelUnderTest?.addons).isNull()
}

@Test
fun `Bundled product with max size is serialized correctly`() {
val product = "wc/product-bundle-with-max-quantity.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

assertThat(product?.metadata).isNotNull
val map = Gson()
.fromJson(product?.metadata, Array<WCMetaData>::class.java).associateBy { it.key }

assertThat(map).containsKey(BundleMetadataKeys.BUNDLE_MAX_SIZE)
assertThat(map).doesNotContainKey(BundleMetadataKeys.BUNDLE_MIN_SIZE)
assertThat(map.getValue(BundleMetadataKeys.BUNDLE_MAX_SIZE).value).isEqualTo("5")
}
@Test
fun `Bundled product with min size is serialized correctly`() {
val product = "wc/product-bundle-with-min-quantity.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

assertThat(product?.metadata).isNotNull
val map = Gson()
.fromJson(product?.metadata, Array<WCMetaData>::class.java).associateBy { it.key }

assertThat(map).containsKey(BundleMetadataKeys.BUNDLE_MIN_SIZE)
assertThat(map).doesNotContainKey(BundleMetadataKeys.BUNDLE_MAX_SIZE)
assertThat(map.getValue(BundleMetadataKeys.BUNDLE_MIN_SIZE).value).isEqualTo("5")
}

@Test
fun `Bundled product with quantity rules is serialized correctly`() {
val product = "wc/product-bundle-with-quantity-rules.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

assertThat(product?.metadata).isNotNull
val map = Gson()
.fromJson(product?.metadata, Array<WCMetaData>::class.java).associateBy { it.key }

assertThat(map).containsKey(BundleMetadataKeys.BUNDLE_MIN_SIZE)
assertThat(map).containsKey(BundleMetadataKeys.BUNDLE_MAX_SIZE)
assertThat(map.getValue(BundleMetadataKeys.BUNDLE_MAX_SIZE).value).isEqualTo("5")
assertThat(map.getValue(BundleMetadataKeys.BUNDLE_MIN_SIZE).value).isEqualTo("5")
}

@Test
fun `Bundled product with special stock status is serialized correctly`() {
val product = "wc/product-bundle-with-max-quantity.json"
.jsonFileAs(ProductApiResponse::class.java)
?.asProductModel()

assertThat(product?.specialStockStatus).isEqualTo("insufficientstock")
}
}
230 changes: 230 additions & 0 deletions example/src/test/resources/wc/product-bundle-with-max-quantity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"id": 533,
"name": "Pizza",
"slug": "pizza",
"permalink": "https:\/\/mystagingwebsite.com\/product\/pizza\/",
"date_created": "2021-07-07T13:19:31",
"date_created_gmt": "2021-07-07T13:19:31",
"date_modified": "2021-07-22T01:57:18",
"date_modified_gmt": "2021-07-22T01:57:18",
"type": "bundle",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "",
"short_description": "",
"sku": "",
"price": "",
"regular_price": "",
"sale_price": "",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"on_sale": false,
"purchasable": false,
"total_sales": 0,
"virtual": false,
"downloadable": false,
"downloads": [
],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "none",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"low_stock_amount": null,
"sold_individually": true,
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"shipping_required": true,
"shipping_taxable": false,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": true,
"average_rating": "0",
"rating_count": 0,
"upsell_ids": [
],
"cross_sell_ids": [
],
"parent_id": 0,
"purchase_note": "",
"categories": [
{
"id": 15,
"name": "Uncategorized",
"slug": "uncategorized"
}
],
"tags": [
],
"images": [
],
"attributes": [
{
"id": 1,
"name": "Color",
"position": 0,
"visible": true,
"variation": true,
"options": [
"Black",
"Blue",
"Green"
]
}
],
"default_attributes": [
],
"variations": [
],
"grouped_products": [
],
"menu_order": 0,
"price_html": "",
"related_ids": [
],
"bundle_stock_status": "insufficientstock",
"bundle_max_size": 5,
"meta_data": [
{
"id": 11116,
"key": "_last_editor_used_jetpack",
"value": "classic-editor"
},
{
"id": 11118,
"key": "_product_addons",
"value": [
{
"name": "Topping",
"title_format": "label",
"description_enable": 1,
"description": "Pizza topping",
"type": "checkbox",
"display": "select",
"position": 0,
"required": 0,
"restrictions": 0,
"restrictions_type": "any_text",
"adjust_price": 0,
"price_type": "flat_fee",
"price": "",
"min": 0,
"max": 0,
"options": [
{
"label": "Peperoni",
"price": "3",
"image": "",
"price_type": "flat_fee"
},
{
"label": "Extra cheese",
"price": "4",
"image": "",
"price_type": "flat_fee"
},
{
"label": "Salami",
"price": "3",
"image": "",
"price_type": "flat_fee"
},
{
"label": "Ham",
"price": "3",
"image": "",
"price_type": "flat_fee"
}
]
},
{
"name": "Soda",
"title_format": "label",
"description_enable": 0,
"description": "",
"type": "input_multiplier",
"display": "select",
"position": 1,
"required": 0,
"restrictions": 1,
"restrictions_type": "any_text",
"adjust_price": 1,
"price_type": "flat_fee",
"price": "2",
"min": 0,
"max": 0,
"options": [
{
"label": "",
"price": "",
"image": "",
"price_type": "flat_fee"
}
]
},
{
"name": "Delivery",
"title_format": "label",
"description_enable": 0,
"description": "",
"type": "multiple_choice",
"display": "radiobutton",
"position": 2,
"required": 1,
"restrictions": 0,
"restrictions_type": "any_text",
"adjust_price": 0,
"price_type": "flat_fee",
"price": "",
"min": 0,
"max": 0,
"options": [
{
"label": "Yes",
"price": "5",
"image": "",
"price_type": "flat_fee"
},
{
"label": "No",
"price": "",
"image": "",
"price_type": "flat_fee"
}
]
}
]
},
{
"id": 11119,
"key": "_product_addons_exclude_global",
"value": "0"
}
],
"stock_status": "outofstock",
"_links": {
"self": [
{
"href": "https:\/\/mystagingwebsite.com\/wp-json\/wc\/v3\/products\/533"
}
],
"collection": [
{
"href": "https:\/\/mystagingwebsite.com\/wp-json\/wc\/v3\/products"
}
]
}
}
Loading

0 comments on commit 8b108e7

Please sign in to comment.