Skip to content

Commit

Permalink
Allow variant inventory quantity (#802)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mllemango authored Dec 8, 2020
1 parent 60bcc27 commit 8fd4f57
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 66 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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!('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

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_equal(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_equal(10, @variant.inventory_quantity)
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

0 comments on commit 8fd4f57

Please sign in to comment.