Ver código fonte

Allow variant inventory quantity (#802)

* Allow inventory quantity

* Remove 2019 tests since its deprecated

* Correct the tests;

* Filter field out of serialization

* Rewrite save method instead of serialize method

* Update changelog

* Clean up

* Fix assert lines
Melanie Wang 4 anos atrás
pai
commit
8fd4f579a4
4 arquivos alterados com 15 adições e 66 exclusões
  1. 2 0
      CHANGELOG.md
  2. 9 10
      lib/shopify_api/resources/variant.rb
  3. 2 10
      test/product_test.rb
  4. 2 46
      test/variant_test.rb

+ 2 - 0
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

+ 9 - 10
lib/shopify_api/resources/variant.rb

@@ -8,23 +8,26 @@ module ShopifyAPI
 
     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 @@ module ShopifyAPI
         "'#{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

+ 2 - 10
test/product_test.rb

@@ -65,20 +65,12 @@ class ProductTest < Test::Unit::TestCase
     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

+ 2 - 46
test/variant_test.rb

@@ -35,30 +35,10 @@ class VariantTest < Test::Unit::TestCase
     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 @@ class VariantTest < Test::Unit::TestCase
     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 @@ class VariantTest < Test::Unit::TestCase
     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