From fe4145d9d561aaffbf089b3bbdb5377212f0886f Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 2 Mar 2018 11:40:20 +1100 Subject: [PATCH 1/2] Use expect syntax --- spec/models/spree/line_item_spec.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 61c4ca0f23..2d32674d0f 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -52,28 +52,28 @@ module Spree it "caps quantity" do li.cap_quantity_at_stock! - li.reload.quantity.should == 5 + expect(li.reload.quantity).to eq 5 end it "does not cap max_quantity" do li.cap_quantity_at_stock! - li.reload.max_quantity.should == 10 + expect(li.reload.max_quantity).to eq 10 end it "works for products without max_quantity" do li.update_column :max_quantity, nil li.cap_quantity_at_stock! li.reload - li.quantity.should == 5 - li.max_quantity.should be_nil + expect(li.quantity).to eq 5 + expect(li.max_quantity).to be nil end it "does nothing for on_demand items" do v.update_attributes! on_demand: true li.cap_quantity_at_stock! li.reload - li.quantity.should == 10 - li.max_quantity.should == 10 + expect(li.quantity).to eq 10 + expect(li.max_quantity).to eq 10 end context "when a variant override is in place" do @@ -90,7 +90,7 @@ module Spree it "caps quantity to override stock level" do li.cap_quantity_at_stock! - li.quantity.should == 2 + expect(li.quantity).to eq 2 end end end From 28491ee94882c315d7bd0fca8bdf315f5c8256e0 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 2 Mar 2018 12:03:12 +1100 Subject: [PATCH 2/2] Skip stock check when requested quantity is zero This prevents an error being raised when available stock is negative --- app/models/spree/line_item_decorator.rb | 2 ++ spec/models/spree/line_item_spec.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 6250530c51..11c9d36850 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -131,7 +131,9 @@ Spree::LineItem.class_eval do # MONKEYPATCH of Spree method # Enables scoping of variant to hub/shop, so we check stock against relevant overrides if they exist + # Also skips stock check if requested quantity is zero def sufficient_stock? + return true if quantity == 0 # This line added scoper.scope(variant) # This line added return true if Spree::Config[:allow_backorders] if new_record? || !order.completed? diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 2d32674d0f..cb1755f60c 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -76,6 +76,12 @@ module Spree expect(li.max_quantity).to eq 10 end + it "caps at zero when stock is negative" do + v.update_attributes(on_hand: -2) + li.cap_quantity_at_stock! + expect(li.reload.quantity).to eq 0 + end + context "when a variant override is in place" do let!(:hub) { create(:distributor_enterprise) } let!(:vo) { create(:variant_override, hub: hub, variant: v, count_on_hand: 2) } @@ -92,6 +98,16 @@ module Spree li.cap_quantity_at_stock! expect(li.quantity).to eq 2 end + + context "when count on hand is negative" do + before { vo.update_attributes(count_on_hand: -3) } + + it "caps at zero" do + v.update_attributes(on_hand: -2) + li.cap_quantity_at_stock! + expect(li.reload.quantity).to eq 0 + end + end end end