diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d5694c3c..936dd1a9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ * Freeze all string literals. This should have no impact unless your application is modifying ('monkeypatching') the internals of the library in an unusual way. +* [#802](https://github.com/Shopify/shopify_api/pull/802) Made `inventory_quantity` a read-only field in Variant + ## Version 9.2.0 * Removes the `shopify` binary which will be used by the Shopify CLI diff --git a/lib/shopify_api/resources/variant.rb b/lib/shopify_api/resources/variant.rb index 2863cc6a8..93f281a21 100644 --- a/lib/shopify_api/resources/variant.rb +++ b/lib/shopify_api/resources/variant.rb @@ -8,23 +8,26 @@ class Variant < Base def initialize(*) super - unless allow_inventory_params? - attributes.except!('inventory_quantity_adjustment', 'inventory_quantity', 'old_inventory_quantity') - end + attributes.except!('old_inventory_quantity') end def inventory_quantity_adjustment=(new_value) - raise_deprecated_inventory_call('inventory_quantity_adjustment') unless allow_inventory_params? + raise_deprecated_inventory_call('inventory_quantity_adjustment') super end def inventory_quantity=(new_value) - raise_deprecated_inventory_call('inventory_quantity') unless allow_inventory_params? + raise_deprecated_inventory_call('inventory_quantity') super end def old_inventory_quantity=(new_value) - raise_deprecated_inventory_call('old_inventory_quantity') unless allow_inventory_params? + raise_deprecated_inventory_call('old_inventory_quantity') + super + end + + def save + attributes.except!('inventory_quantity') super end @@ -36,9 +39,5 @@ def raise_deprecated_inventory_call(parameter) "'#{parameter}' is deprecated - see https://help.shopify.com/en/api/guides/inventory-migration-guide", ) end - - def allow_inventory_params? - Base.api_version < ApiVersion.find_version('2019-10') - end end end diff --git a/test/product_test.rb b/test/product_test.rb index 9a0711021..2cde9d71a 100644 --- a/test/product_test.rb +++ b/test/product_test.rb @@ -65,20 +65,12 @@ def test_price_range_when_has_different_price assert_equal('100.00 - 199.00', @product.price_range) end - def test_deprecated_variant_inventory_fields_are_included_in_2019_07 - ShopifyAPI::Base.api_version = '2019-07' - refresh_product(api_version: ShopifyAPI::Base.api_version) - - variant = @product.variants.first - assert(variant.as_json.include?('inventory_quantity')) - end - - def test_deprecated_variant_inventory_fields_are_removed_in_2020_01 + def test_read_only_variant_inventory_fields_not_removed_in_2020_01 ShopifyAPI::Base.api_version = '2020-01' refresh_product(api_version: ShopifyAPI::Base.api_version) variant = @product.variants.first - refute(variant.as_json.include?('inventory_quantity')) + assert_equal(10, variant.inventory_quantity) end def test_deprecated_inventory_fields_are_removed_in_2020_01 diff --git a/test/variant_test.rb b/test/variant_test.rb index e2aa4fa97..df73514a4 100644 --- a/test/variant_test.rb +++ b/test/variant_test.rb @@ -35,30 +35,10 @@ def test_delete_variant assert(@variant.destroy) end - def test_deprecated_inventory_fields_are_included_in_2019_07 - ShopifyAPI::Base.api_version = '2019-07' - refresh_variant(api_version: ShopifyAPI::Base.api_version) - assert(@variant.as_json.include?('inventory_quantity')) - end - - def test_deprecated_inventory_fields_are_removed_in_2020_01 + def test_read_only_inventory_quantity ShopifyAPI::Base.api_version = '2020-01' refresh_variant(api_version: ShopifyAPI::Base.api_version) - refute(@variant.as_json.include?('inventory_quantity')) - end - - def test_setting_variant_inventory_quantity_adjustment_passes_in_api_before_2019_10 - ShopifyAPI::Base.api_version = '2019-07' - refresh_variant(api_version: ShopifyAPI::Base.api_version) - @variant.inventory_quantity_adjustment = 8 - end - - def test_setting_variant_inventory_quantity_adjustment_fails_in_2019_10_api - ShopifyAPI::Base.api_version = '2019-10' - refresh_variant(api_version: ShopifyAPI::Base.api_version) - assert_raises(ShopifyAPI::ValidationException) do - @variant.inventory_quantity_adjustment = 8 - end + assert_equal(10, @variant.inventory_quantity) end def test_setting_variant_inventory_quantity_adjustment_fails_in_the_unstable_api @@ -68,18 +48,6 @@ def test_setting_variant_inventory_quantity_adjustment_fails_in_the_unstable_api end end - def test_setting_variant_inventory_quantity_passes_in_api_before_2019_10 - ShopifyAPI::Base.api_version = '2019-07' - @variant.inventory_quantity = 8 - end - - def test_setting_variant_inventory_quantity_fails_in_2019_10_api - ShopifyAPI::Base.api_version = '2019-10' - assert_raises(ShopifyAPI::ValidationException) do - @variant.inventory_quantity = 8 - end - end - def test_setting_variant_inventory_quantity_fails_in_the_unstable_api ShopifyAPI::Base.api_version = :unstable assert_raises(ShopifyAPI::ValidationException) do @@ -87,18 +55,6 @@ def test_setting_variant_inventory_quantity_fails_in_the_unstable_api end end - def test_setting_variant_old_inventory_quantity_passes_in_api_before_2019_10 - ShopifyAPI::Base.api_version = '2019-07' - @variant.old_inventory_quantity = 8 - end - - def test_setting_variant_old_inventory_quantity_fails_in_2019_10_api - ShopifyAPI::Base.api_version = '2019-10' - assert_raises(ShopifyAPI::ValidationException) do - @variant.old_inventory_quantity = 8 - end - end - def test_setting_variant_old_inventory_quantity_fails_in_the_unstable_api ShopifyAPI::Base.api_version = :unstable assert_raises(ShopifyAPI::ValidationException) do