123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- # frozen_string_literal: true
- require 'test_helper'
- class ProductTest < Test::Unit::TestCase
- def setup
- super
- refresh_product
- end
- def test_add_metafields_to_product
- fake("products/632910392/metafields", method: :post, status: 201, body: load_fixture('metafield'))
- field = @product.add_metafield(
- ShopifyAPI::Metafield.new(namespace: "contact", key: "email", value: "123@example.com", value_type: "string")
- )
- assert_equal(
- ActiveSupport::JSON.decode(
- '{"metafield":{"namespace":"contact","key":"email","value":"123@example.com","value_type":"string"}}'
- ),
- ActiveSupport::JSON.decode(WebMock.last_request.body)
- )
- assert(!field.new_record?)
- assert_equal("contact", field.namespace)
- assert_equal("email", field.key)
- assert_equal("123@example.com", field.value)
- end
- def test_get_all_metafields_for_product
- fake("products/632910392/metafields", body: load_fixture('metafields'))
- metafields = @product.metafields
- assert_equal(3, metafields.length)
- assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
- end
- def test_get_2_metafields_for_product
- body = ActiveSupport::JSON.decode(load_fixture('metafields'))
- body['metafields'].slice!(2, 1)
- fake('products/632910392/metafields.json?limit=2', body: body.to_json, extension: false)
- metafields = @product.metafields(limit: 2)
- assert_equal(2, metafields.length)
- assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
- end
- def test_update_loaded_variant
- fake("products/632910392/variants/808950810", method: :put, status: 200, body: load_fixture('variant'))
- variant = @product.variants.first
- variant.price = "0.50"
- variant.save
- end
- def test_price_range
- assert_equal('199.00', @product.price_range)
- end
- def test_price_range_when_has_different_price
- @product.variants[0].price = '100.00'
- assert_equal('100.00 - 199.00', @product.price_range)
- end
- 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
- assert_equal(10, variant.inventory_quantity)
- end
- def test_deprecated_inventory_fields_are_removed_in_2020_01
- ShopifyAPI::Base.api_version = '2020-01'
- refresh_product(api_version: ShopifyAPI::Base.api_version)
- refute(@product.as_json.include?('total_inventory'))
- end
- def test_setting_product_total_inventory_passes_in_api_before_2019_10
- ShopifyAPI::Base.api_version = '2019-07'
- refresh_product(api_version: ShopifyAPI::Base.api_version)
- @product.total_inventory = 8
- end
- def test_setting_product_total_inventory_fails_in_2019_10_api
- ShopifyAPI::Base.api_version = '2019-10'
- refresh_product(api_version: ShopifyAPI::Base.api_version)
- 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
- refresh_product(api_version: ShopifyAPI::Base.api_version)
- assert_raises(ShopifyAPI::ValidationException) do
- @product.total_inventory = 8
- end
- end
- private
- def refresh_product(api_version: nil)
- fake("products/632910392", body: load_fixture('product'), api_version: api_version)
- @product = ShopifyAPI::Product.find(632910392)
- end
- end
|