mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-19 00:27:25 +00:00
Merge pull request #3147 from luisramos0/2-0-vos
[Spree Upgrade] Adapt variant overrides to spree 2
This commit is contained in:
@@ -9,7 +9,7 @@ describe VariantOverride do
|
||||
let(:hub2) { create(:distributor_enterprise) }
|
||||
let!(:vo1) { create(:variant_override, hub: hub1, variant: variant, import_date: Time.zone.now.yesterday) }
|
||||
let!(:vo2) { create(:variant_override, hub: hub2, variant: variant, import_date: Time.zone.now) }
|
||||
let!(:vo3) { create(:variant_override, hub: hub1, variant: variant, permission_revoked_at: Time.now) }
|
||||
let!(:vo3) { create(:variant_override, hub: hub1, variant: variant, permission_revoked_at: Time.zone.now) }
|
||||
|
||||
it "ignores variant_overrides with revoked_permissions by default" do
|
||||
expect(VariantOverride.all).to_not include vo3
|
||||
@@ -17,7 +17,7 @@ describe VariantOverride do
|
||||
end
|
||||
|
||||
it "finds variant overrides for a set of hubs" do
|
||||
VariantOverride.for_hubs([hub1, hub2]).should match_array [vo1, vo2]
|
||||
expect(VariantOverride.for_hubs([hub1, hub2])).to match_array [vo1, vo2]
|
||||
end
|
||||
|
||||
it "fetches import dates for hubs in descending order" do
|
||||
@@ -29,13 +29,12 @@ describe VariantOverride do
|
||||
|
||||
describe "fetching variant overrides indexed by variant" do
|
||||
it "gets indexed variant overrides for one hub" do
|
||||
VariantOverride.indexed(hub1).should == {variant => vo1}
|
||||
VariantOverride.indexed(hub2).should == {variant => vo2}
|
||||
expect(VariantOverride.indexed(hub1)).to eq( variant => vo1 )
|
||||
expect(VariantOverride.indexed(hub2)).to eq( variant => vo2 )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "callbacks" do
|
||||
let!(:vo) { create(:variant_override, hub: hub, variant: variant) }
|
||||
|
||||
@@ -51,87 +50,71 @@ describe VariantOverride do
|
||||
end
|
||||
end
|
||||
|
||||
describe "with price" do
|
||||
let(:variant_override) { create(:variant_override, variant: variant, hub: hub, price: 12.34) }
|
||||
|
||||
describe "looking up prices" do
|
||||
it "returns the numeric price when present" do
|
||||
VariantOverride.create!(variant: variant, hub: hub, price: 12.34)
|
||||
VariantOverride.price_for(hub, variant).should == 12.34
|
||||
end
|
||||
|
||||
it "returns nil otherwise" do
|
||||
VariantOverride.price_for(hub, variant).should be_nil
|
||||
it "returns the numeric price" do
|
||||
expect(variant_override.price).to eq(12.34)
|
||||
end
|
||||
end
|
||||
|
||||
describe "looking up count on hand" do
|
||||
it "returns the numeric stock level when present" do
|
||||
VariantOverride.create!(variant: variant, hub: hub, count_on_hand: 12)
|
||||
VariantOverride.count_on_hand_for(hub, variant).should == 12
|
||||
end
|
||||
describe "with nil count on hand" do
|
||||
let(:variant_override) { create(:variant_override, variant: variant, hub: hub, count_on_hand: nil) }
|
||||
|
||||
it "returns nil otherwise" do
|
||||
VariantOverride.count_on_hand_for(hub, variant).should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "checking if stock levels have been overriden" do
|
||||
it "returns true when stock level has been overridden" do
|
||||
create(:variant_override, variant: variant, hub: hub, count_on_hand: 12)
|
||||
VariantOverride.stock_overridden?(hub, variant).should be true
|
||||
end
|
||||
|
||||
it "returns false when the override has no stock level" do
|
||||
create(:variant_override, variant: variant, hub: hub, count_on_hand: nil)
|
||||
VariantOverride.stock_overridden?(hub, variant).should be false
|
||||
end
|
||||
|
||||
it "returns false when there is no override for the hub/variant" do
|
||||
VariantOverride.stock_overridden?(hub, variant).should be false
|
||||
end
|
||||
end
|
||||
|
||||
describe "decrementing stock" do
|
||||
it "decrements stock" do
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12)
|
||||
VariantOverride.decrement_stock! hub, variant, 2
|
||||
vo.reload.count_on_hand.should == 10
|
||||
end
|
||||
|
||||
it "silently logs an error if the variant override does not exist" do
|
||||
Bugsnag.should_receive(:notify)
|
||||
VariantOverride.decrement_stock! hub, variant, 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "incrementing stock" do
|
||||
let!(:vo) { create(:variant_override, variant: variant, hub: hub, count_on_hand: 8) }
|
||||
|
||||
context "when the vo overrides stock" do
|
||||
it "increments stock" do
|
||||
vo.increment_stock! 2
|
||||
vo.reload.count_on_hand.should == 10
|
||||
describe "stock_overridden?" do
|
||||
it "returns false" do
|
||||
expect(variant_override.stock_overridden?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context "when the vo doesn't override stock" do
|
||||
before { vo.update_attributes(count_on_hand: nil) }
|
||||
|
||||
describe "move_stock!" do
|
||||
it "silently logs an error" do
|
||||
Bugsnag.should_receive(:notify)
|
||||
vo.increment_stock! 2
|
||||
expect(Bugsnag).to receive(:notify)
|
||||
variant_override.move_stock!(5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "with count on hand" do
|
||||
let(:variant_override) { create(:variant_override, variant: variant, hub: hub, count_on_hand: 12) }
|
||||
|
||||
it "returns the numeric count on hand" do
|
||||
expect(variant_override.count_on_hand).to eq(12)
|
||||
end
|
||||
|
||||
describe "stock_overridden?" do
|
||||
it "returns true" do
|
||||
expect(variant_override.stock_overridden?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
describe "move_stock!" do
|
||||
it "does nothing for quantity zero" do
|
||||
variant_override.move_stock!(0)
|
||||
expect(variant_override.reload.count_on_hand).to eq(12)
|
||||
end
|
||||
|
||||
it "increments count_on_hand when quantity is negative" do
|
||||
variant_override.move_stock!(2)
|
||||
expect(variant_override.reload.count_on_hand).to eq(14)
|
||||
end
|
||||
|
||||
it "decrements count_on_hand when quantity is negative" do
|
||||
variant_override.move_stock!(-2)
|
||||
expect(variant_override.reload.count_on_hand).to eq(10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "checking default stock value is present" do
|
||||
it "returns true when a default stock level has been set" do
|
||||
it "returns true when a default stock level has been set" do
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20)
|
||||
vo.default_stock?.should be true
|
||||
expect(vo.default_stock?).to be true
|
||||
end
|
||||
|
||||
it "returns false when the override has no default stock level" do
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock:nil)
|
||||
vo.default_stock?.should be false
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: nil)
|
||||
expect(vo.default_stock?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -139,18 +122,18 @@ describe VariantOverride do
|
||||
it "resets the on hand level to the value in the default_stock field" do
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 20, resettable: true)
|
||||
vo.reset_stock!
|
||||
vo.reload.count_on_hand.should == 20
|
||||
expect(vo.reload.count_on_hand).to eq(20)
|
||||
end
|
||||
it "silently logs an error if the variant override doesn't have a default stock level" do
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock:nil, resettable: true)
|
||||
Bugsnag.should_receive(:notify)
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: nil, resettable: true)
|
||||
expect(Bugsnag).to receive(:notify)
|
||||
vo.reset_stock!
|
||||
vo.reload.count_on_hand.should == 12
|
||||
expect(vo.reload.count_on_hand).to eq(12)
|
||||
end
|
||||
it "doesn't reset the level if the behaviour is disabled" do
|
||||
vo = create(:variant_override, variant: variant, hub: hub, count_on_hand: 12, default_stock: 10, resettable: false)
|
||||
vo.reset_stock!
|
||||
vo.reload.count_on_hand.should == 12
|
||||
expect(vo.reload.count_on_hand).to eq(12)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user