variant_test.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_read_only_inventory_quantity
  30. ShopifyAPI::Base.api_version = '2020-01'
  31. refresh_variant(api_version: ShopifyAPI::Base.api_version)
  32. assert_equal(10, @variant.inventory_quantity)
  33. end
  34. def test_setting_variant_inventory_quantity_adjustment_fails_in_the_unstable_api
  35. ShopifyAPI::Base.api_version = :unstable
  36. assert_raises(ShopifyAPI::ValidationException) do
  37. @variant.inventory_quantity_adjustment = 8
  38. end
  39. end
  40. def test_setting_variant_inventory_quantity_fails_in_the_unstable_api
  41. ShopifyAPI::Base.api_version = :unstable
  42. assert_raises(ShopifyAPI::ValidationException) do
  43. @variant.inventory_quantity = 8
  44. end
  45. end
  46. def test_setting_variant_old_inventory_quantity_fails_in_the_unstable_api
  47. ShopifyAPI::Base.api_version = :unstable
  48. assert_raises(ShopifyAPI::ValidationException) do
  49. @variant.old_inventory_quantity = 8
  50. end
  51. end
  52. private
  53. def refresh_variant(api_version: nil)
  54. fake(
  55. "products/632910392/variants/808950810", method: :get, body: load_fixture('variant'), api_version: api_version
  56. )
  57. @variant = ShopifyAPI::Variant.find(808950810, params: { product_id: 632910392 })
  58. end
  59. end