product_test.rb 3.6 KB

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