variant_test.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class VariantTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. refresh_variant
  7. end
  8. def test_get_variants
  9. fake("products/632910392/variants", method: :get, body: load_fixture('variants'))
  10. variants = ShopifyAPI::Variant.find(:all, params: { product_id: 632910392 })
  11. assert_equal(variants.map(&:id).sort, [39072856, 49148385, 457924702, 808950810])
  12. end
  13. def test_get_variant_namespaced
  14. assert_equal(632910392, @variant.product_id)
  15. end
  16. def test_get_variant
  17. assert_equal(632910392, @variant.product_id)
  18. end
  19. def test_product_id_should_be_accessible_if_via_product_endpoint
  20. assert_equal(632910392, @variant.product_id)
  21. end
  22. def test_product_id_should_be_accessible_if_via_variant_endpoint
  23. assert_equal(632910392, @variant.product_id)
  24. end
  25. def test_delete_variant
  26. fake("products/632910392/variants/808950810", method: :delete, body: 'destroyed')
  27. assert(@variant.destroy)
  28. end
  29. def test_deprecated_inventory_fields_are_included_in_2019_07
  30. ShopifyAPI::Base.api_version = '2019-07'
  31. refresh_variant(api_version: ShopifyAPI::Base.api_version)
  32. assert(@variant.as_json.include?('inventory_quantity'))
  33. end
  34. def test_deprecated_inventory_fields_are_removed_in_2020_01
  35. ShopifyAPI::Base.api_version = '2020-01'
  36. refresh_variant(api_version: ShopifyAPI::Base.api_version)
  37. refute(@variant.as_json.include?('inventory_quantity'))
  38. end
  39. def test_setting_variant_inventory_quantity_adjustment_passes_in_api_before_2019_10
  40. ShopifyAPI::Base.api_version = '2019-07'
  41. refresh_variant(api_version: ShopifyAPI::Base.api_version)
  42. @variant.inventory_quantity_adjustment = 8
  43. end
  44. def test_setting_variant_inventory_quantity_adjustment_fails_in_2019_10_api
  45. ShopifyAPI::Base.api_version = '2019-10'
  46. refresh_variant(api_version: ShopifyAPI::Base.api_version)
  47. assert_raises(ShopifyAPI::ValidationException) do
  48. @variant.inventory_quantity_adjustment = 8
  49. end
  50. end
  51. def test_setting_variant_inventory_quantity_adjustment_fails_in_the_unstable_api
  52. ShopifyAPI::Base.api_version = :unstable
  53. assert_raises(ShopifyAPI::ValidationException) do
  54. @variant.inventory_quantity_adjustment = 8
  55. end
  56. end
  57. def test_setting_variant_inventory_quantity_passes_in_api_before_2019_10
  58. ShopifyAPI::Base.api_version = '2019-07'
  59. @variant.inventory_quantity = 8
  60. end
  61. def test_setting_variant_inventory_quantity_fails_in_2019_10_api
  62. ShopifyAPI::Base.api_version = '2019-10'
  63. assert_raises(ShopifyAPI::ValidationException) do
  64. @variant.inventory_quantity = 8
  65. end
  66. end
  67. def test_setting_variant_inventory_quantity_fails_in_the_unstable_api
  68. ShopifyAPI::Base.api_version = :unstable
  69. assert_raises(ShopifyAPI::ValidationException) do
  70. @variant.inventory_quantity = 8
  71. end
  72. end
  73. def test_setting_variant_old_inventory_quantity_passes_in_api_before_2019_10
  74. ShopifyAPI::Base.api_version = '2019-07'
  75. @variant.old_inventory_quantity = 8
  76. end
  77. def test_setting_variant_old_inventory_quantity_fails_in_2019_10_api
  78. ShopifyAPI::Base.api_version = '2019-10'
  79. assert_raises(ShopifyAPI::ValidationException) do
  80. @variant.old_inventory_quantity = 8
  81. end
  82. end
  83. def test_setting_variant_old_inventory_quantity_fails_in_the_unstable_api
  84. ShopifyAPI::Base.api_version = :unstable
  85. assert_raises(ShopifyAPI::ValidationException) do
  86. @variant.old_inventory_quantity = 8
  87. end
  88. end
  89. private
  90. def refresh_variant(api_version: nil)
  91. fake(
  92. "products/632910392/variants/808950810", method: :get, body: load_fixture('variant'), api_version: api_version
  93. )
  94. @variant = ShopifyAPI::Variant.find(808950810, params: { product_id: 632910392 })
  95. end
  96. end