inventory_level_test.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class InventoryLevelTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. @inventory_level_response = ActiveSupport::JSON.decode(load_fixture('inventory_level'))
  7. @inventory_level = ShopifyAPI::InventoryLevel.new(@inventory_level_response['inventory_level'])
  8. end
  9. test ".find with inventory_item_ids and location_ids returns expected inventory levels" do
  10. params = { inventory_item_ids: [808950810, 39072856], location_ids: [905684977, 487838322] }
  11. fake(
  12. "inventory_levels.json?#{params.to_param}",
  13. extension: false,
  14. method: :get,
  15. status: 200,
  16. body: load_fixture('inventory_levels')
  17. )
  18. inventory_levels = ShopifyAPI::InventoryLevel.find(:all, params: params)
  19. assert(
  20. inventory_levels.all? do |item|
  21. params[:location_ids].include?(item.location_id) &&
  22. params[:inventory_item_ids].include?(item.inventory_item_id)
  23. end,
  24. message: 'Response contained inventory_items or locations not requested.'
  25. )
  26. end
  27. test '#adjust with adjustment value returns inventory_level with available increased by adjustment value' do
  28. adjustment = 5
  29. updated_available = @inventory_level.available + adjustment
  30. @inventory_level_response[:available] = updated_available
  31. fake('inventory_levels/adjust', method: :post, body: ActiveSupport::JSON.encode(@inventory_level_response))
  32. @inventory_level.adjust(adjustment)
  33. assert_equal(updated_available, @inventory_level.available)
  34. end
  35. test '#connect saves an inventory_level associated with inventory_item and location_id' do
  36. params = { inventory_item_id: 808950810, location_id: 99999999 }
  37. response = params.clone
  38. response[:available] = 0
  39. fake('inventory_levels/connect', method: :post, body: ActiveSupport::JSON.encode(response))
  40. inventory_level = ShopifyAPI::InventoryLevel.new(params)
  41. inventory_level.connect
  42. assert_equal(0, inventory_level.available, message: 'expected newly connected location to have 0 inventory')
  43. end
  44. test '#destroy removes inventory_level and returns nil' do
  45. params = { inventory_item_id: @inventory_level.inventory_item_id, location_id: @inventory_level.location_id }
  46. fake("inventory_levels.json?#{params.to_param}", extension: false, method: :delete, status: 204, body: nil)
  47. assert_nil(@inventory_level.destroy)
  48. end
  49. test '#set with available value returns inventory_level with available as the available value' do
  50. available = 13
  51. response = @inventory_level_response.clone
  52. response['inventory_level']['available'] = available
  53. fake('inventory_levels/set', method: :post, body: ActiveSupport::JSON.encode(response))
  54. @inventory_level.set(available)
  55. assert_equal(available, @inventory_level.available)
  56. end
  57. end