product_test.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_deprecated_variant_inventory_fields_are_included_in_2019_07
  52. ShopifyAPI::Base.api_version = '2019-07'
  53. refresh_product(api_version: ShopifyAPI::Base.api_version)
  54. variant = @product.variants.first
  55. assert(variant.as_json.include?('inventory_quantity'))
  56. end
  57. def test_deprecated_variant_inventory_fields_are_removed_in_2020_01
  58. ShopifyAPI::Base.api_version = '2020-01'
  59. refresh_product(api_version: ShopifyAPI::Base.api_version)
  60. variant = @product.variants.first
  61. refute(variant.as_json.include?('inventory_quantity'))
  62. end
  63. def test_deprecated_inventory_fields_are_removed_in_2020_01
  64. ShopifyAPI::Base.api_version = '2020-01'
  65. refresh_product(api_version: ShopifyAPI::Base.api_version)
  66. refute(@product.as_json.include?('total_inventory'))
  67. end
  68. def test_setting_product_total_inventory_passes_in_api_before_2019_10
  69. ShopifyAPI::Base.api_version = '2019-07'
  70. refresh_product(api_version: ShopifyAPI::Base.api_version)
  71. @product.total_inventory = 8
  72. end
  73. def test_setting_product_total_inventory_fails_in_2019_10_api
  74. ShopifyAPI::Base.api_version = '2019-10'
  75. refresh_product(api_version: ShopifyAPI::Base.api_version)
  76. assert_raises(ShopifyAPI::ValidationException) do
  77. @product.total_inventory = 8
  78. end
  79. end
  80. def test_setting_product_total_inventory_fails_in_the_unstable_api
  81. ShopifyAPI::Base.api_version = :unstable
  82. refresh_product(api_version: ShopifyAPI::Base.api_version)
  83. assert_raises(ShopifyAPI::ValidationException) do
  84. @product.total_inventory = 8
  85. end
  86. end
  87. private
  88. def refresh_product(api_version: nil)
  89. fake("products/632910392", body: load_fixture('product'), api_version: api_version)
  90. @product = ShopifyAPI::Product.find(632910392)
  91. end
  92. end