From 17783280583018a4ccbb6656b6ef3ec349b0d6a5 Mon Sep 17 00:00:00 2001 From: Sean Date: Mon, 11 Feb 2019 15:40:46 -0600 Subject: [PATCH] Fixing inventory unit finalizer --- .../spree/stock/inventory_units_finalizer.rb | 2 +- .../stock/inventory_units_finalizer_spec.rb | 60 +++++++++++++------ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/core/app/models/spree/stock/inventory_units_finalizer.rb b/core/app/models/spree/stock/inventory_units_finalizer.rb index afff1627a91..bca3a7871f5 100644 --- a/core/app/models/spree/stock/inventory_units_finalizer.rb +++ b/core/app/models/spree/stock/inventory_units_finalizer.rb @@ -38,7 +38,7 @@ def unstock_inventory_units inventory_units_for_shipment.group_by(&:line_item_id).each_value do |units| shipment = units.first.shipment line_item = units.first.line_item - shipment.stock_location.unstock line_item.variant, inventory_units.count, shipment + shipment.stock_location.unstock line_item.variant, units.count, shipment end end end diff --git a/core/spec/models/spree/stock/inventory_units_finalizer_spec.rb b/core/spec/models/spree/stock/inventory_units_finalizer_spec.rb index ca0a8311da3..f9048e70a81 100644 --- a/core/spec/models/spree/stock/inventory_units_finalizer_spec.rb +++ b/core/spec/models/spree/stock/inventory_units_finalizer_spec.rb @@ -5,29 +5,53 @@ module Spree module Stock RSpec.describe InventoryUnitsFinalizer, type: :model do - let(:order) { build(:order_with_line_items) } - let(:inventory_unit) { build(:inventory_unit, order: order, variant: order.line_items.first.variant, shipment: order.shipments.first) } - let(:stock_item) { inventory_unit.variant.stock_items.first } - - before do - stock_item.set_count_on_hand(10) - stock_item.update_attributes!(backorderable: false) - inventory_unit.update_attributes!(pending: true) - end + context "when finalizing an order with one line_item" do + let(:order) { build(:order_with_line_items) } + let(:inventory_unit) { build(:inventory_unit, order: order, variant: order.line_items.first.variant, shipment: order.shipments.first) } + let(:stock_item) { inventory_unit.variant.stock_items.first } - subject { described_class.new([inventory_unit]).run! } + before do + stock_item.set_count_on_hand(10) + stock_item.update_attributes!(backorderable: false) + inventory_unit.update_attributes!(pending: true) + end - it "updates the associated inventory units" do - inventory_unit.update_columns(updated_at: 1.hour.ago) - expect { subject }.to change { inventory_unit.reload.updated_at } - end + subject { described_class.new([inventory_unit]).run! } + + it "updates the associated inventory units" do + inventory_unit.update_columns(updated_at: 1.hour.ago) + expect { subject }.to change { inventory_unit.reload.updated_at } + end + + it "updates the inventory units to not be pending" do + expect { subject }.to change { inventory_unit.reload.pending }.to(false) + end - it "updates the inventory units to not be pending" do - expect { subject }.to change { inventory_unit.reload.pending }.to(false) + it "unstocks the variant" do + expect { subject }.to change { stock_item.reload.count_on_hand }.from(10).to(9) + end end - it "unstocks the variant" do - expect { subject }.to change { stock_item.reload.count_on_hand }.from(10).to(9) + context "when finalizing an order with multiple line_items" do + let(:order) { build(:order_with_line_items, line_items_count: 2) } + let(:inventory_unit) { build(:inventory_unit, order: order, variant: order.line_items.first.variant, shipment: order.shipments.first) } + let(:inventory_unit_2) { build(:inventory_unit, order: order, variant: order.line_items.second.variant, shipment: order.shipments.first) } + let(:stock_item) { inventory_unit.variant.stock_items.first } + let(:stock_item_2) { inventory_unit.variant.stock_items.first } + + before do + stock_item.set_count_on_hand(10) + stock_item_2.set_count_on_hand(10) + inventory_unit.update_attributes!(pending: true) + inventory_unit_2.update_attributes!(pending: true) + end + + subject { described_class.new([inventory_unit, inventory_unit_2]).run! } + + it "unstocks the variant with the correct quantity" do + expect { subject }.to change { stock_item.reload.count_on_hand }.from(10).to(9) + .and change { stock_item_2.reload.count_on_hand }.from(10).to(9) + end end end end