From 4694f1b21a67467738e83859aa2ed537b4850dac Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Tue, 12 May 2020 15:28:26 +0800 Subject: [PATCH] Require count on hand in non backorderable StockItem to be positive or zero Fix setting of count on hand in line item specs --- app/models/spree/stock_item.rb | 1 + spec/models/spree/line_item_spec.rb | 4 ++-- spec/models/spree/stock_item_spec.rb | 32 +++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/models/spree/stock_item.rb b/app/models/spree/stock_item.rb index b3e14969eb..f71df2b942 100644 --- a/app/models/spree/stock_item.rb +++ b/app/models/spree/stock_item.rb @@ -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 diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index ca4a5353d9..ff00240aaf 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -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 diff --git a/spec/models/spree/stock_item_spec.rb b/spec/models/spree/stock_item_spec.rb index 679af05f90..9c9b233986 100644 --- a/spec/models/spree/stock_item_spec.rb +++ b/spec/models/spree/stock_item_spec.rb @@ -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)