Improve scope_variant_to_hub code and spec by testing an invalid data state

This commit is contained in:
luisramos0
2019-02-21 11:25:01 +00:00
parent ef786adcfc
commit 45e5fed609
3 changed files with 21 additions and 13 deletions

View File

@@ -62,6 +62,10 @@ class VariantOverride < ActiveRecord::Base
count_on_hand.present?
end
def use_producer_settings?
on_demand.nil?
end
def decrement_stock!(quantity)
if stock_overridden?
decrement! :count_on_hand, quantity

View File

@@ -29,15 +29,19 @@ module OpenFoodNetwork
end
def count_on_hand
return super unless @variant_override.andand.stock_overridden?
@variant_override.count_on_hand
if @variant_override.present? && @variant_override.stock_overridden?
@variant_override.count_on_hand
else
super
end
end
def on_demand
return super if @variant_override.andand.on_demand.nil?
@variant_override.andand.on_demand
if @variant_override.present? && !@variant_override.use_producer_settings?
@variant_override.on_demand
else
super
end
end
def decrement!(attribute, by = 1)

View File

@@ -82,13 +82,6 @@ module OpenFoodNetwork
context "without an on_demand set" do
before { vo.update_column(:on_demand, nil) }
context "when count_on_hand is set" do
it "returns variant's on_demand" do
scoper.scope v
expect(v.on_demand).to be true
end
end
context "when count_on_hand is not set" do
before { vo.update_column(:count_on_hand, nil) }
@@ -97,6 +90,13 @@ module OpenFoodNetwork
expect(v.on_demand).to be true
end
end
context "when count_on_hand is set" do
it "should return validation error on save" do
scoper.scope v
expect{ vo.save! }.to raise_error ActiveRecord::RecordInvalid
end
end
end
end