From 5d82efa213086b1315f0f82c3bb52795ee2d0417 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 1 Feb 2019 16:16:04 +0000 Subject: [PATCH 1/2] Add unit test for VariantStock.can_supply? --- spec/models/concerns/variant_stock_spec.rb | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/spec/models/concerns/variant_stock_spec.rb b/spec/models/concerns/variant_stock_spec.rb index 7dc3080b45..50c89671e4 100644 --- a/spec/models/concerns/variant_stock_spec.rb +++ b/spec/models/concerns/variant_stock_spec.rb @@ -159,4 +159,44 @@ describe VariantStock do end end end + + describe '#can_supply?' do + context 'when variant on_demand' do + let(:variant) { create(:variant, on_demand: true) } + + it "returns true for zero" do + expect(variant.can_supply?(0)).to eq(true) + end + it "returns true for large quantity" do + expect(variant.can_supply?(100000)).to eq(true) + end + end + + context 'when variant not on_demand' do + context 'when variant in stock' do + it "returns true for zero" do + expect(variant.can_supply?(0)).to eq(true) + end + it "returns true for number equal to stock level" do + expect(variant.can_supply?(variant.total_on_hand)).to eq(true) + end + + it "returns false for number above stock level" do + expect(variant.can_supply?(variant.total_on_hand + 1)).to eq(false) + end + end + + context 'when variant out of stock' do + before { variant.count_on_hand = 0 } + + it "returns true for zero" do + expect(variant.can_supply?(0)).to eq(true) + end + + it "returns false for one" do + expect(variant.can_supply?(1)).to eq(false) + end + end + end + end end From e584fd21550b9e9d02e028d499fdd9c9b12eb38b Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 1 Feb 2019 16:18:09 +0000 Subject: [PATCH 2/2] Make in_stock? work for both variants and overrides by moving it from VariantStock to variant_decorator. Added tests for it in scope_hub_to_variant and improved VO shopping specs --- app/models/concerns/variant_stock.rb | 5 -- app/models/spree/variant_decorator.rb | 6 +++ .../shopping/variant_overrides_spec.rb | 8 ++- .../scope_variant_to_hub_spec.rb | 53 +++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/app/models/concerns/variant_stock.rb b/app/models/concerns/variant_stock.rb index 0a7ba06d28..d39967bb18 100644 --- a/app/models/concerns/variant_stock.rb +++ b/app/models/concerns/variant_stock.rb @@ -125,11 +125,6 @@ module VariantStock on_demand || total_on_hand >= quantity end - # We override Spree::Variant.in_stock? to avoid depending on the non-overidable method Spree::Stock::Quantifier.can_supply? - def in_stock?(quantity = 1) - can_supply?(quantity) - end - # Moving Spree::StockLocation.fill_status to the variant enables us to override this behaviour for variant overrides # We can have this responsibility here in the variant because there is only one stock item per variant # diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index b45be2bdfe..de8f151c2c 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -87,6 +87,12 @@ Spree::Variant.class_eval do ] end + # We override in_stock? to avoid depending on the non-overridable method Spree::Stock::Quantifier.can_supply? + # VariantStock implements can_supply? itself which depends on overridable methods + def in_stock?(quantity = 1) + can_supply?(quantity) + end + def price_with_fees(distributor, order_cycle) price + fees_for(distributor, order_cycle) end diff --git a/spec/features/consumer/shopping/variant_overrides_spec.rb b/spec/features/consumer/shopping/variant_overrides_spec.rb index 1349335132..8638616872 100644 --- a/spec/features/consumer/shopping/variant_overrides_spec.rb +++ b/spec/features/consumer/shopping/variant_overrides_spec.rb @@ -39,12 +39,10 @@ feature "shopping with variant overrides defined", js: true, retry: 3 do end describe "viewing products" do - it "shows the overridden price" do - page.should_not have_price with_currency(12.22) # product1_variant1.price ($11.11) + 10% fee + it "shows price and stock from the override" do page.should have_price with_currency(61.11) # product1_variant1_override.price ($55.55) + 10% fee - end + page.should_not have_price with_currency(12.22) # product1_variant1.price ($11.11) + 10% fee - it "looks up stock from the override" do # Product should appear but one of the variants is out of stock page.should_not have_content product1_variant2.options_text @@ -150,7 +148,7 @@ feature "shopping with variant overrides defined", js: true, retry: 3 do end it "does not show out of stock flags on order confirmation page" do - product1_variant3.update_attribute :count_on_hand, 0 + product1_variant3.count_on_hand = 0 fill_in "variants[#{product1_variant3.id}]", with: "2" click_checkout diff --git a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb index b2cdf48d04..11bf218a1d 100644 --- a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb +++ b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb @@ -1,3 +1,4 @@ +require 'spec_helper' require 'open_food_network/scope_variant_to_hub' module OpenFoodNetwork @@ -107,6 +108,58 @@ module OpenFoodNetwork end end + # in_stock? is indirectly overridden through can_supply? + # can_supply? is indirectly overridden by on_demand and total_on_hand + # these tests validate this chain is working correctly + describe "overriding in_stock?" do + before { v.on_demand = false } + + context "when an override exists" do + before { vo } + + context "when variant in stock" do + it "returns true if VO in stock" do + scoper.scope v + expect(v.in_stock?).to eq(true) + end + + it "returns false if VO out of stock" do + vo.update_attribute :count_on_hand, 0 + scoper.scope v + expect(v.in_stock?).to eq(false) + end + end + + context "when variant out of stock" do + before { v.count_on_hand = 0 } + + it "returns true if VO in stock" do + scoper.scope v + expect(v.in_stock?).to eq(true) + end + + it "returns false if VO out of stock" do + vo.update_attribute :count_on_hand, 0 + scoper.scope v + expect(v.in_stock?).to eq(false) + end + end + end + + context "when there's no override" do + it "returns true if variant in stock" do + scoper.scope v + expect(v.in_stock?).to eq(true) + end + + it "returns false if variant out of stock" do + v.count_on_hand = 0 + scoper.scope v + expect(v.in_stock?).to eq(false) + end + end + end + describe "overriding sku" do context "when an override exists" do before { vo }