Require count on hand in non backorderable StockItem to be positive or zero

Fix setting of count on hand in line item specs
This commit is contained in:
Kristina Lim
2020-05-12 15:28:26 +08:00
committed by Luis Ramos
parent e53913756c
commit 4694f1b21a
3 changed files with 34 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ module Spree
# rubocop:disable Rails/UniqueValidationWithoutIndex
validates :variant_id, uniqueness: { scope: :stock_location_id }
# rubocop:enable Rails/UniqueValidationWithoutIndex
validates :count_on_hand, numericality: { greater_than_or_equal_to: 0, unless: :backorderable? }
attr_accessible :count_on_hand, :variant, :stock_location, :backorderable, :variant_id

View File

@@ -97,7 +97,7 @@ module Spree
end
it "caps at zero when stock is negative" do
v.update! on_hand: -2
v.__send__(:stock_item).update_column(:count_on_hand, -2)
li.cap_quantity_at_stock!
expect(li.reload.quantity).to eq 0
end
@@ -123,7 +123,7 @@ module Spree
before { vo.update(count_on_hand: -3) }
it "caps at zero" do
v.update(on_hand: -2)
v.__send__(:stock_item).update_column(:count_on_hand, -2)
li.cap_quantity_at_stock!
expect(li.reload.quantity).to eq 0
end

View File

@@ -7,6 +7,33 @@ RSpec.describe Spree::StockItem do
subject { stock_location.stock_items.order(:id).first }
describe "validation" do
let(:stock_item) { stock_location.stock_items.first }
it "requires count_on_hand to be positive if not backorderable" do
stock_item.backorderable = false
stock_item.__send__(:count_on_hand=, 1)
expect(stock_item.valid?).to eq(true)
stock_item.__send__(:count_on_hand=, 0)
expect(stock_item.valid?).to eq(true)
stock_item.__send__(:count_on_hand=, -1)
expect(stock_item.valid?).to eq(false)
end
it "allows count_on_hand to be negative if backorderable" do
stock_item.backorderable = true
stock_item.__send__(:count_on_hand=, 1)
expect(stock_item.valid?).to eq(true)
stock_item.__send__(:count_on_hand=, -1)
expect(stock_item.valid?).to eq(true)
end
end
it 'maintains the count on hand for a variant' do
expect(subject.count_on_hand).to eq 15
end
@@ -53,7 +80,10 @@ RSpec.describe Spree::StockItem do
let(:inventory_unit) { double('InventoryUnit') }
let(:inventory_unit_2) { double('InventoryUnit2') }
before { subject.adjust_count_on_hand(- (current_on_hand + 2)) }
before do
allow(subject).to receive(:backorderable?).and_return(true)
subject.adjust_count_on_hand(- (current_on_hand + 2))
end
it "doesn't process backorders" do
expect(subject).not_to receive(:backordered_inventory_units)