From 14a697c189977d69bac00c33c820daff14e6209c Mon Sep 17 00:00:00 2001 From: DJ Houghton Date: Fri, 20 Dec 2019 17:01:24 -0500 Subject: [PATCH] Add tests for deprecation of product and variant inventory-related fields --- lib/shopify_api/resources/product.rb | 9 ++++ lib/shopify_api/resources/variant.rb | 19 +++++++ test/product_test.rb | 22 ++++++++ test/test_helper.rb | 2 +- test/variant_test.rb | 75 ++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 1 deletion(-) diff --git a/lib/shopify_api/resources/product.rb b/lib/shopify_api/resources/product.rb index cf67fa6e4..57335b3f5 100644 --- a/lib/shopify_api/resources/product.rb +++ b/lib/shopify_api/resources/product.rb @@ -16,6 +16,15 @@ def price_range end end + def serializable_hash(options = {}) + super.except("total_inventory") + end + + def total_inventory=(new_value) + raise(ShopifyAPI::ValidationException, 'deprecated behaviour') unless Base.api_version < ApiVersion.find_version('2019-10') + super + end + def collections CustomCollection.find(:all, :params => {:product_id => self.id}) end diff --git a/lib/shopify_api/resources/variant.rb b/lib/shopify_api/resources/variant.rb index 76ab4da49..acd5ec788 100644 --- a/lib/shopify_api/resources/variant.rb +++ b/lib/shopify_api/resources/variant.rb @@ -4,5 +4,24 @@ class Variant < Base include DisablePrefixCheck conditional_prefix :product + + def serializable_hash(options = {}) + super.except("inventory_quantity_adjustment", "inventory_quantity", "old_inventory_quantity") + end + + def inventory_quantity_adjustment=(new_value) + raise(ShopifyAPI::ValidationException, 'deprecated behaviour') unless Base.api_version < ApiVersion.find_version('2019-10') + super + end + + def inventory_quantity=(new_value) + raise(ShopifyAPI::ValidationException, 'deprecated behaviour') unless Base.api_version < ApiVersion.find_version('2019-10') + super + end + + def old_inventory_quantity=(new_value) + raise(ShopifyAPI::ValidationException, 'deprecated behaviour') unless Base.api_version < ApiVersion.find_version('2019-10') + super + end end end diff --git a/test/product_test.rb b/test/product_test.rb index fe5bd6621..06e260a49 100644 --- a/test/product_test.rb +++ b/test/product_test.rb @@ -57,4 +57,26 @@ def test_price_range_when_has_different_price assert_equal('100.00 - 199.00', @product.price_range) end + + def test_setting_product_total_inventory_passes_in_api_before_2019_10 + ShopifyAPI::Base.api_version = '2019-07' + fake("products/632910392", {:body => load_fixture('product')}) + @product.total_inventory = 8 + end + + def test_setting_product_total_inventory_fails_in_2019_10_api + ShopifyAPI::Base.api_version = '2019-10' + fake("products/632910392", {:body => load_fixture('product')}) + assert_raises(ShopifyAPI::ValidationException) do + @product.total_inventory = 8 + end + end + + def test_setting_product_total_inventory_fails_in_the_unstable_api + ShopifyAPI::Base.api_version = :unstable + fake("products/632910392", {:body => load_fixture('product')}) + assert_raises(ShopifyAPI::ValidationException) do + @product.total_inventory = 8 + end + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 4a3017c61..7432f031c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -95,7 +95,7 @@ def fake(endpoint, options={}) body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint) format = options.delete(:format) || :json method = options.delete(:method) || :get - api_version = options.delete(:api_version) || ShopifyAPI::ApiVersion.find_version('2019-01') + api_version = options.delete(:api_version) || ShopifyAPI::Base.api_version extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false status = options.delete(:status) || 200 url = if options.has_key?(:url) diff --git a/test/variant_test.rb b/test/variant_test.rb index 9bac02386..b3f8be1b7 100644 --- a/test/variant_test.rb +++ b/test/variant_test.rb @@ -43,4 +43,79 @@ def test_delete_variant v = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) assert v.destroy end + + def test_setting_variant_inventory_quantity_adjustment_passes_in_api_before_2019_10 + ShopifyAPI::Base.api_version = '2019-07' + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + variant.inventory_quantity_adjustment = 8 + end + + def test_setting_variant_inventory_quantity_adjustment_fails_in_2019_10_api + ShopifyAPI::Base.api_version = '2019-10' + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + assert_raises(ShopifyAPI::ValidationException) do + variant.inventory_quantity_adjustment = 8 + end + end + + def test_setting_variant_inventory_quantity_adjustment_fails_in_the_unstable_api + ShopifyAPI::Base.api_version = :unstable + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + assert_raises(ShopifyAPI::ValidationException) do + variant.inventory_quantity_adjustment = 8 + end + end + + def test_setting_variant_inventory_quantity_passes_in_api_before_2019_10 + ShopifyAPI::Base.api_version = '2019-07' + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + variant.inventory_quantity = 8 + end + + def test_setting_variant_inventory_quantity_fails_in_2019_10_api + ShopifyAPI::Base.api_version = '2019-10' + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + 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 + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + assert_raises(ShopifyAPI::ValidationException) do + variant.inventory_quantity = 8 + end + end + + def test_setting_variant_old_inventory_quantity_passes_in_api_before_2019_10 + ShopifyAPI::Base.api_version = '2019-07' + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + variant.old_inventory_quantity = 8 + end + + def test_setting_variant_old_inventory_quantity_fails_in_2019_10_api + ShopifyAPI::Base.api_version = '2019-10' + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + 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 + fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant') + variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392}) + assert_raises(ShopifyAPI::ValidationException) do + variant.old_inventory_quantity = 8 + end + end end