Skip to content

Commit 31f01b1

Browse files
authored
Merge pull request #655 from Shopify/inventory_param_deprecation
Add validation and tests for deprecation of product and variant inventory-related fields
2 parents ed26a0e + 4235a6f commit 31f01b1

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-1
lines changed

lib/shopify_api/resources/product.rb

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def price_range
1616
end
1717
end
1818

19+
def total_inventory=(new_value)
20+
raise_deprecated_inventory_call('total_inventory') unless allow_inventory_params?
21+
super
22+
end
23+
24+
def serializable_hash(options = {})
25+
allow_inventory_params? ? super(options) : super(options).except('total_inventory')
26+
end
27+
1928
def collections
2029
CustomCollection.find(:all, :params => {:product_id => self.id})
2130
end
@@ -31,5 +40,17 @@ def add_to_collection(collection)
3140
def remove_from_collection(collection)
3241
collection.remove_product(self)
3342
end
43+
44+
private
45+
46+
def raise_deprecated_inventory_call(parameter)
47+
raise(ShopifyAPI::ValidationException,
48+
"'#{parameter}' is deprecated - see https://help.shopify.com/en/api/guides/inventory-migration-guide",
49+
)
50+
end
51+
52+
def allow_inventory_params?
53+
Base.api_version < ApiVersion.find_version('2019-10')
54+
end
3455
end
3556
end

lib/shopify_api/resources/variant.rb

+31
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,36 @@ class Variant < Base
44
include DisablePrefixCheck
55

66
conditional_prefix :product
7+
8+
def inventory_quantity_adjustment=(new_value)
9+
raise_deprecated_inventory_call('inventory_quantity_adjustment') unless allow_inventory_params?
10+
super
11+
end
12+
13+
def inventory_quantity=(new_value)
14+
raise_deprecated_inventory_call('inventory_quantity') unless allow_inventory_params?
15+
super
16+
end
17+
18+
def old_inventory_quantity=(new_value)
19+
raise_deprecated_inventory_call('old_inventory_quantity') unless allow_inventory_params?
20+
super
21+
end
22+
23+
def serializable_hash(options = {})
24+
allow_inventory_params? ? super(options) : super(options).except('inventory_quantity', 'old_inventory_quantity')
25+
end
26+
27+
private
28+
29+
def raise_deprecated_inventory_call(parameter)
30+
raise(ShopifyAPI::ValidationException,
31+
"'#{parameter}' is deprecated - see https://help.shopify.com/en/api/guides/inventory-migration-guide",
32+
)
33+
end
34+
35+
def allow_inventory_params?
36+
Base.api_version < ApiVersion.find_version('2019-10')
37+
end
738
end
839
end

test/product_test.rb

+39
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,43 @@ def test_price_range_when_has_different_price
5757

5858
assert_equal('100.00 - 199.00', @product.price_range)
5959
end
60+
61+
def test_deprecated_variant_inventory_fields_are_included_in_2019_07
62+
ShopifyAPI::Base.api_version = '2019-07'
63+
variant = @product.variants.first
64+
assert variant.as_json.include?('inventory_quantity')
65+
end
66+
67+
def test_deprecated_variant_inventory_fields_are_removed_in_2020_01
68+
ShopifyAPI::Base.api_version = '2020-01'
69+
variant = @product.variants.first
70+
refute variant.as_json.include?('inventory_quantity')
71+
end
72+
73+
def test_deprecated_inventory_fields_are_removed_in_2020_01
74+
ShopifyAPI::Base.api_version = '2020-01'
75+
refute @product.as_json.include?('total_inventory')
76+
end
77+
78+
def test_setting_product_total_inventory_passes_in_api_before_2019_10
79+
ShopifyAPI::Base.api_version = '2019-07'
80+
fake("products/632910392", {:body => load_fixture('product')})
81+
@product.total_inventory = 8
82+
end
83+
84+
def test_setting_product_total_inventory_fails_in_2019_10_api
85+
ShopifyAPI::Base.api_version = '2019-10'
86+
fake("products/632910392", {:body => load_fixture('product')})
87+
assert_raises(ShopifyAPI::ValidationException) do
88+
@product.total_inventory = 8
89+
end
90+
end
91+
92+
def test_setting_product_total_inventory_fails_in_the_unstable_api
93+
ShopifyAPI::Base.api_version = :unstable
94+
fake("products/632910392", {:body => load_fixture('product')})
95+
assert_raises(ShopifyAPI::ValidationException) do
96+
@product.total_inventory = 8
97+
end
98+
end
6099
end

test/test_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def fake(endpoint, options={})
9696
body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
9797
format = options.delete(:format) || :json
9898
method = options.delete(:method) || :get
99-
api_version = options.delete(:api_version) || ShopifyAPI::ApiVersion.find_version('2019-01')
99+
api_version = options.delete(:api_version) || ShopifyAPI::Base.api_version
100100
extension = ".#{options.delete(:extension)||'json'}" unless options[:extension]==false
101101
status = options.delete(:status) || 200
102102
url = if options.has_key?(:url)

test/variant_test.rb

+89
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,93 @@ def test_delete_variant
4343
v = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
4444
assert v.destroy
4545
end
46+
47+
def test_deprecated_inventory_fields_are_included_in_2019_07
48+
ShopifyAPI::Base.api_version = '2019-07'
49+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
50+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
51+
assert variant.as_json.include?('inventory_quantity')
52+
end
53+
54+
def test_deprecated_inventory_fields_are_removed_in_2020_01
55+
ShopifyAPI::Base.api_version = '2020-01'
56+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
57+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
58+
refute variant.as_json.include?('inventory_quantity')
59+
end
60+
61+
def test_setting_variant_inventory_quantity_adjustment_passes_in_api_before_2019_10
62+
ShopifyAPI::Base.api_version = '2019-07'
63+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
64+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
65+
variant.inventory_quantity_adjustment = 8
66+
end
67+
68+
def test_setting_variant_inventory_quantity_adjustment_fails_in_2019_10_api
69+
ShopifyAPI::Base.api_version = '2019-10'
70+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
71+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
72+
assert_raises(ShopifyAPI::ValidationException) do
73+
variant.inventory_quantity_adjustment = 8
74+
end
75+
end
76+
77+
def test_setting_variant_inventory_quantity_adjustment_fails_in_the_unstable_api
78+
ShopifyAPI::Base.api_version = :unstable
79+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
80+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
81+
assert_raises(ShopifyAPI::ValidationException) do
82+
variant.inventory_quantity_adjustment = 8
83+
end
84+
end
85+
86+
def test_setting_variant_inventory_quantity_passes_in_api_before_2019_10
87+
ShopifyAPI::Base.api_version = '2019-07'
88+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
89+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
90+
variant.inventory_quantity = 8
91+
end
92+
93+
def test_setting_variant_inventory_quantity_fails_in_2019_10_api
94+
ShopifyAPI::Base.api_version = '2019-10'
95+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
96+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
97+
assert_raises(ShopifyAPI::ValidationException) do
98+
variant.inventory_quantity = 8
99+
end
100+
end
101+
102+
def test_setting_variant_inventory_quantity_fails_in_the_unstable_api
103+
ShopifyAPI::Base.api_version = :unstable
104+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
105+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
106+
assert_raises(ShopifyAPI::ValidationException) do
107+
variant.inventory_quantity = 8
108+
end
109+
end
110+
111+
def test_setting_variant_old_inventory_quantity_passes_in_api_before_2019_10
112+
ShopifyAPI::Base.api_version = '2019-07'
113+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
114+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
115+
variant.old_inventory_quantity = 8
116+
end
117+
118+
def test_setting_variant_old_inventory_quantity_fails_in_2019_10_api
119+
ShopifyAPI::Base.api_version = '2019-10'
120+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
121+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
122+
assert_raises(ShopifyAPI::ValidationException) do
123+
variant.old_inventory_quantity = 8
124+
end
125+
end
126+
127+
def test_setting_variant_old_inventory_quantity_fails_in_the_unstable_api
128+
ShopifyAPI::Base.api_version = :unstable
129+
fake "products/632910392/variants/808950810", :method => :get, :body => load_fixture('variant')
130+
variant = ShopifyAPI::Variant.find(808950810, :params => {:product_id => 632910392})
131+
assert_raises(ShopifyAPI::ValidationException) do
132+
variant.old_inventory_quantity = 8
133+
end
134+
end
46135
end

0 commit comments

Comments
 (0)