Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow variant inventory quantity #802

Merged
merged 10 commits into from
Dec 8, 2020
19 changes: 9 additions & 10 deletions lib/shopify_api/resources/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!('inventory_quantity_adjustment', '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', 'old_inventory_quantity')
super
end

Expand All @@ -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
12 changes: 2 additions & 10 deletions test/product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(10, variant.inventory_quantity)
end

def test_deprecated_inventory_fields_are_removed_in_2020_01
Expand Down
48 changes: 2 additions & 46 deletions test/variant_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(10, @variant.inventory_quantity)
mllemango marked this conversation as resolved.
Show resolved Hide resolved
end

def test_setting_variant_inventory_quantity_adjustment_fails_in_the_unstable_api
Expand All @@ -68,37 +48,13 @@ 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
@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'
@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
Expand Down