product_test.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class ProductTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. refresh_product
  7. end
  8. def test_add_metafields_to_product
  9. fake("products/632910392/metafields", method: :post, status: 201, body: load_fixture('metafield'))
  10. field = @product.add_metafield(
  11. ShopifyAPI::Metafield.new(namespace: "contact", key: "email", value: "123@example.com", value_type: "string")
  12. )
  13. assert_equal(
  14. ActiveSupport::JSON.decode(
  15. '{"metafield":{"namespace":"contact","key":"email","value":"123@example.com","value_type":"string"}}'
  16. ),
  17. ActiveSupport::JSON.decode(WebMock.last_request.body)
  18. )
  19. assert(!field.new_record?)
  20. assert_equal("contact", field.namespace)
  21. assert_equal("email", field.key)
  22. assert_equal("123@example.com", field.value)
  23. end
  24. def test_get_all_metafields_for_product
  25. fake("products/632910392/metafields", body: load_fixture('metafields'))
  26. metafields = @product.metafields
  27. assert_equal(3, metafields.length)
  28. assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
  29. end
  30. def test_get_2_metafields_for_product
  31. body = ActiveSupport::JSON.decode(load_fixture('metafields'))
  32. body['metafields'].slice!(2, 1)
  33. fake('products/632910392/metafields.json?limit=2', body: body.to_json, extension: false)
  34. metafields = @product.metafields(limit: 2)
  35. assert_equal(2, metafields.length)
  36. assert(metafields.all? { |m| m.is_a?(ShopifyAPI::Metafield) })
  37. end
  38. def test_update_loaded_variant
  39. fake("products/632910392/variants/808950810", method: :put, status: 200, body: load_fixture('variant'))
  40. variant = @product.variants.first
  41. variant.price = "0.50"
  42. variant.save
  43. end
  44. def test_price_range
  45. assert_equal('199.00', @product.price_range)
  46. end
  47. def test_price_range_when_has_different_price
  48. @product.variants[0].price = '100.00'
  49. assert_equal('100.00 - 199.00', @product.price_range)
  50. end
  51. def test_read_only_variant_inventory_fields_not_removed_in_2020_01
  52. ShopifyAPI::Base.api_version = '2020-01'
  53. refresh_product(api_version: ShopifyAPI::Base.api_version)
  54. variant = @product.variants.first
  55. assert_equal(10, variant.inventory_quantity)
  56. end
  57. def test_deprecated_inventory_fields_are_removed_in_2020_01
  58. ShopifyAPI::Base.api_version = '2020-01'
  59. refresh_product(api_version: ShopifyAPI::Base.api_version)
  60. refute(@product.as_json.include?('total_inventory'))
  61. end
  62. def test_setting_product_total_inventory_passes_in_api_before_2019_10
  63. ShopifyAPI::Base.api_version = '2019-07'
  64. refresh_product(api_version: ShopifyAPI::Base.api_version)
  65. @product.total_inventory = 8
  66. end
  67. def test_setting_product_total_inventory_fails_in_2019_10_api
  68. ShopifyAPI::Base.api_version = '2019-10'
  69. refresh_product(api_version: ShopifyAPI::Base.api_version)
  70. assert_raises(ShopifyAPI::ValidationException) do
  71. @product.total_inventory = 8
  72. end
  73. end
  74. def test_setting_product_total_inventory_fails_in_the_unstable_api
  75. ShopifyAPI::Base.api_version = :unstable
  76. refresh_product(api_version: ShopifyAPI::Base.api_version)
  77. assert_raises(ShopifyAPI::ValidationException) do
  78. @product.total_inventory = 8
  79. end
  80. end
  81. private
  82. def refresh_product(api_version: nil)
  83. fake("products/632910392", body: load_fixture('product'), api_version: api_version)
  84. @product = ShopifyAPI::Product.find(632910392)
  85. end
  86. end